Pymel and background threads

Hi,

I just want to share a curious behaviour recently discovered.

I’m doing some maya scene dependency processing and im doing it in a background QThread.

I have some time now dealing with Maya Python and Threading and i m aware Maya is everything but thread friendly so you need to be very careful when using maya.cmds in threads.

However, if i use pymel commands instead i get errors at runtime in the first command i try to use. I know Pymel is an abstract object oriented layer above maya.cmds but one would hope at least the same behaviour as maya.cmds and not encounter such problems. The error it reports points to the need of specifying a boolean flag, this is nonsense because i tried the same command in the script editor and it works flawlessly.

I just wonder why if pymel is neither thread safe the error it throws is not more clear and states so or at least demonstrates same behaviour as maya.cmds (which is that maya crashes)

Anyone know better why is this behaviour?

While pymel is a wrapper around maya.cmds, it also wraps the maya.OpenMaya namespaces. Basically the wrapper around maya.cmds is setup to return objects wrapped from maya.OpenMaya, and those wrapped objects might be the elements throwing the error.

I’ve had to many hard crashes from working with threads, so if there is even a chance of some maya related call happening, I just wrap it in maya.utils.executeDeffered.

You should be very careful about trying to run scene operations in anything except the main thread: it’s an invitation to crashes. The limitation isn’t in pymel proper - it’s just Maya. Nothing but the main maya thread can reliably update the scene.

The spurious error message is really common when you’ve crossed a thread boundary: in fact nonsense messages are the number one sign that you’ve cross-threaded by accident.

Running the same code from the interpreter is not a valid test – the interpreter is always in the right thread so it won’t repro the real problem (it’s worth it as a test, of course, just to be sure it’s a thread bug and not a typo).