Python Specific

Why does DLL loading fail when loading an external Python Package?

Only Python DLL’s (or .pyd files) build with MS Visual Studio 2010 are compatible with Modo 801/901.

ImportError: DLL load failed: %1 is not a valid Win32 application.

Most often the source code can be found on GitHub to be compiled with MSVC 2010.

How do I remove a listener object in Python?

A listener is an object you export to be called when changes occur. You create the object and add it, and then remove it when you are done listening. This is a bit tricky in Python, however, because the automatic type conversion in the API works against you. For example, let’s say you do the obvious thing and try this:

Python

class MyListener(lxifc.SceneItemListener):
        pass

lSrv = lx.service.Listener()
listener = MyListener()

lSrv.AddListener(listener)
lSrv.RemoveListener(listener) # This won't do what you expect.

Your listener object will be added, but trying to remove it will result in a ‘not found’ exception. (In older versions of MODO this did not generate an error but left your listener object installed.) The problem is that passing a Python object to a method that expects a COM object will do the conversion automatically, but this means that the two functions are called with different COM objects. Because MODO doesn’t recognize the second object as being the same as the first, the listener remains installed.

The solution is to pre-allocate the COM version of your object. You can then pass this to both functions and it will be seen as the same object.

As part of modifier evaluation however, time is not universal. Modifiers can be evaluated at arbitrary times and should not rely on the time provided by the selection system. Instead, they should allocate time as an input to the modifier, allowing the evaluation time to be determined.

Python

class MyListener(lxifc.SceneItemListener):
        pass

lSrv = lx.service.Listener()
listener = MyListener()
com_listener = lx.object.Unknown(listener)

lSrv.AddListener(com_listener)
lSrv.RemoveListener(com_listener) # This will correctly remove the listener that was previously spawned.