PySide QThread error on cmds.ls(l=True) where True isn't bool

509 views
Skip to first unread message

Asi Sudai

unread,
Apr 3, 2014, 11:51:48 PM4/3/14
to python_in...@googlegroups.com
Hi gang,

I'm getting a strange error in a very simple query of cmds.ls( type="transform", long=True ) running inside a PySide QThread.

I extracted the issue into a basic example that reproduce the issue:
https://gist.github.com/asisudai/9967431

The error is ( it actually prints out upside down like this, might not be related   ):

Flag 'long' must be passed a boolean argument: # TypeErrorreturn maya.cmds.ls( type="transform", long=True ) # Will give TypeError on long=True where True not a bool?!
#     #   File "c:/temp\threadTest.py", line 52, in funcA
result
= self.function( *self.args, **self.kwargs )
#     #   File "c:/temp\threadTest.py", line 80, in run
# Traceback (most recent call last):



def funcA( data ):
   print "%s inside funcA" %threading.current_thread().name
   
   ## Here's the issue:
   # Running cmds.ls command will result in the TypeError where long=True isn't a Bool.
   return maya.cmds.ls( type="transform", long=True ) # Will give TypeError on long=True where True not a bool?!

    ## While running this ( or any other module/code will work?! why?
   # return [1,2,3] # Works

Any idea why?

Marcus Ottosson

unread,
Apr 4, 2014, 1:37:54 AM4/4/14
to python_in...@googlegroups.com
Hey Asi, good to see you back in these threads. :)

Maya, like Qt, isn't a big fan of having things run outside of their main threads. You can circumvent the issue by calling upon the overly descriptive command maya.utils.executeInMainThreadWithResult()which will queue the event in the main thread, rather than calling it directly.

More info here

In code-land, you could wrap up your calls with something like this

from maya import cmds
from maya import utils

class Wrapper(object):
    """Make thread-safe calls to maya.cmds

    Description
        Maya can't deal with commands coming in from threads other
        than main. This wrapper takes whatever we call and wrap it up
        using maya.utils.executeInMainThreadWithResult()

    How
        maya.utils.execu... has an argument signature that
        looks like this: (command, *args, **kwargs)

        Wrapper then intercepts any attribute-queries and wraps
        them up in a lambda that forwards the call to this method.

    """

    def __init__(self, module):
        self._module = module

    def __getattr__(self, attr):
        wrapper = utils.executeInMainThreadWithResult
        command = getattr(self._module, attr)

        return lambda *args, **kwargs: wrapper(
            command, *args, **kwargs)

cmds = Wrapper(cmds)

That way, you could pretend like cmds is thread-safe, even though it would wrap things up for you in the background.

Another alternative, since you're using QThreads rather than regular Python threads, is to set up a signal in the main thread. The QThread may call upon a signal in the main thread and Qt will do what Maya does with its descriptive method and wrap it up in a thread-safe manner for you.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/210f2ebd-077f-42bf-af20-fbbdb7e57163%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Asi Sudai

unread,
Apr 4, 2014, 4:53:10 PM4/4/14
to python_in...@googlegroups.com
Thanks Marcus, it did the trick plus I learned something new :)

and yes, I'm back ;) baby vacation time is over :(
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages