Avoiding the UnbindWrapperError
Functionality is exposed to Python via a wrapper around the actual MotionBuilder object.
For example b = FBButton() creates:
- A button in the UI.
- A Python wrapper you reference with b.
Once the two objects are created, the following states can arise:
- Normally, both the object and the wrapper are bound together, enabling you to access
the MotionBuilder object via the Python wrapper.
- If the MotionBuilder object is destroyed (deleted from the scene) and the Python wrapper
still exists in the global namespace, then if you try to access it, the Python exception
unbind.UnbindWrapperError is thrown. See PropertyDrop.py for an example showing how the Tool.OnUnbindSDK event
can register a callback that unregisters the tool from the scene as soon as the tool
becomes unbound.
- If the Python wrapper has been destroyed (by executing another script, or resetting
all names in the global namespace), but the MotionBuilder object still exists, then
nothing happens. The MotionBuilder object still exists, and you can get another wrapper
for it, for example by using FBGetModelByName.
You can register a callback on an object to be notified if it ever becomes unbound.
For an example of unbinding, see Unbind_example.py. It creates a tool and adds a label
to it. It then calls DestroyTool, which deletes all the UI controls in the tool. But the Python wrappers (for the
tool and label) are still accessible, and if you access the label caption property
an exception is thrown.
You can also see the unbind error as follows:
- Add a cube to a scene and select it. Use the UI, or type in the work area: myCube = FBModelCube("Cube0")
- Delete the cube.
- If you now access the cube in the work area, for example: myCube.Name, this works because the cube persists in the undo system..
- Now do undoable operations in the scene which will replace the cube, for example by
adding objects to the scene. After adding a number of objects, myCube.Name will throw an UnboundWrapperError.