MotionBuilder: Recursion Error on System event

Hi all,

I have a simple script that creates a Path3D and align evenly models on it, from the start point to the end point. I’m trying to add better functionality, by using the system events so if the user edits the path points, the models update their new position automatically.

I don’t fully understand why, but OnConnectionDataNotify, raises a RecursionError if I try to update the models translation values and is the only callback registered. The other callbacks update the models once the path is unselected.

def align_models_on_path(control, event):
    number_of_bones = 5  # added for testing. It's the value of a spinbox in the ui in the script.
    origin = FBVector3d(dist.PathKeyGet(0)[0], dist.PathKeyGet(0)[1], dist.PathKeyGet(0)[2] )
    end = FBVector3d(dist.PathKeyGet(1)[0], dist.PathKeyGet(1)[1], dist.PathKeyGet(1)[2])
    spacing = (end - origin) / number_of_bones

    print(f"origin: {origin}, end {end}")

    for bone in dist.bones:
        bone.Translation = origin

def register():
    # Used alone 'OnConnectionDataNotify' gives a Recursion Error if try to use the vector values on objects. 
    # Printing values works fine. Any other call back will update the models positions once the Path is unselected.


Thanks in advance for the help!

If you are updating anything from an event handler you can protect against recursion by having a flag that you check against to make sure you aren’t already within an event before updating again.

_in_event = False

def custom_event_function():
    global _in_event
    if _in_event:
        # exit as we are already calling custom_event_function
    _in_event = True
    # code here
    _in_event = False
1 Like

Hi @Munkybutt ,
Thanks a lot ! It worked like a charm and learnt something new :slight_smile:

now worries - glad to help!