Hi,
it’s been a while since i didnt have a look at the forum!!
I’m back with a question regarding the behaviour of maya commands executed in QThreads.
I’ve read the documentation but it’s not clear enough for me: https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/Maya/files/GUID-9B5AECBB-B212-4C92-959A-22599760E91A-htm.html
Where it clearly says:“Maya API and Maya Command architectures are not thread-safe. Maya commands throw an exception if they are called outside the main thread, and use of the OpenMaya API from threads other than the main one has unforeseen side effects.”
But then it also says that executeDeferred() and executeInMainThreadWithResult() are meant to execute the functions you provide when maya is idle, the latter blocking the secondary thread where the call was made until it returns a result.
I’m trying to figure out the best way to execute a list of maya commands in a secondary thread that updates a progress bar in the tool GUI.
After some testing and several Maya crashes i’ve managed to make it work this way:
executeDeferred("stringWithMayaCommandsFunctionCallandArgumentsA")
time.sleep(self.SLEEP_INTERVAL)
executeDeferred("stringWithMayaCommandsFunctionCallandArgumentsB")
time.sleep(self.SLEEP_INTERVAL)
<and so on....>
Needless to say that the SLEEP_INTERVAL has to be big enough so that Maya can enter the idle() state, otherwise Maya will crash because it attempts to execute two tasks at the same time.
Now i dont like this approach that much and my next thought was to try to do it with executeInMainThreadWithResult which would seemingly avoid the need to call “time.sleep()” since the secondary thread is blocked until it returns a result.
Well, this approach crashes Maya… despite my belief according to the documentation… this proves i havent quite understood how those functions work.
is there no “stable” way to call maya commands in a secondary thread? If there is, which is the best way to do this?