Merging curves causes entity to be invalid

I am using curves to create boundary conditions. After imprinting the curves onto volumes, and doing a merge all curve, my python object containing one of my curves becomes invalid:

  File "/Users/jsanchez/Desktop/build3.py", line 111, in <module>
    sid = bd['body'].id()
  File "/Applications/Coreform-Cubit-2021.5.app/Contents/MacOS/cubit3.py", line 6794, in id
    return _cubit3.Entity_id(self)
RuntimeError: Invalid entity

In order to fix the issue, I have to search the volume containing the curve, and match up the name again:

if rectangle_data:
    cubit.cmd('merge all curve')
    for k, v in body_data.items():
        for c in v['volume'].curves():
            if k + "_sb" in c.entity_names():
                v['body'] = cubit.curve(c.id())

Is it expected behavior that a Python object representing a curve becomes invalid when it merged?

The entity with the higher id is hidden during a merge operation and is no longer accessible. It is not destroyed to allow for unmerge. Holding an object that is merged will give you an invalid object.

If you are creating the curves first and then imprinting, the original curves should continue to exist.

As a trivial example consider,

cubit.cmd('reset')
cubit.cmd('bri x 10')
cubit.cmd('create curve arc radius 2 center location 0 0 5 normal 0 0 1 start angle 0 stop angle 360')
cubit.cmd('imprint curve 13 on surf 1 ')
c = cubit.curve(13)
cubit.cmd('merge curve all')
c.id()

Verify that Cubit is not giving any other errors earlier in the process.

Can you replicate your issue in a simply contained subset of your code?

Since you know the id will be the id of the curve before the merge, you could keep the ids around and recreate the object based on the id rather than searching the entity names.

Thanks for the explanation. For right now I will just keep my code that matches up curves by their given name.