userSetup.py fired prior to Main Window initialization?

415 views
Skip to first unread message

Tim Crowson

unread,
Apr 17, 2014, 10:08:27 AM4/17/14
to python_in...@googlegroups.com
Hello all, I'm a Softimage user needing to move over and get familiar with Maya. I apologize in advance if I post any stupid questions. Does this list have its own search function? I looked for one but couldn't find any.

I'm using Maya 2015 on Windows at the moment and I'm having trouble with my userSetup.py file. Basically, I want to create a custom menu in the menu bar at launch. After googling some tutorials on the matter, I have code that works great when fired from the the script editor, but when run at launch, it bombs. Basically my function to build the window is unable to get a handle on the main Maya window... it gets returned as NoneType. The relevant part of my code looks something like this:


def buildMenu():
    ptr
= mui.MQtUtil.mainWindow()
    mayaWindow
= shiboken.wrapInstance(long(ptr), QtGui.QWidget)
 
    mainMenu
= cmds.menu('MyMenu', p=mayaWindow, l='My Menu')
    cmds
.menuItem(p=mainMenu, d=True, dl='Some Divider')
    cmds
.menuItem(p=mainMenu, l='Some Tool')


The error comes from wrapInstance(long(ptr)), because ptr is being returned as None. I'm unsure how to proceed. It basically seems as though my code is getting run before the main window has initialized, so it doesn't exist yet?

Thank you for any assistance...

-Tim Crowson
CG Lead, Magnetic Dreams

Tony Barbieri

unread,
Apr 17, 2014, 10:18:22 AM4/17/14
to python_in...@googlegroups.com
Hey Tim,

You'll want to use evalDeferred.  So for your example above, instead of calling buildMenu directly in the userSetup.py, call it like:


# Defer our startup commands until after Maya has finished loading
# completely.
cmds.evalDeferred("buildMenu()")

notice that "buildMenu()" is a string.



--
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/cf9a9f08-7a78-44c4-9a6e-c225f50424e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-tony

Tim Crowson

unread,
Apr 17, 2014, 10:19:39 AM4/17/14
to python_in...@googlegroups.com
Gotcha! Thanks Tony! Nice to see a familiar face too. I'm sure I'll have plenty more questions as I progress...


Marcus Ottosson

unread,
Apr 17, 2014, 10:36:17 AM4/17/14
to python_in...@googlegroups.com

Tony Barbieri

unread,
Apr 17, 2014, 10:37:45 AM4/17/14
to python_in...@googlegroups.com
Np!  I'll do my best to help out with additional questions :).  This list is pretty active so I'm sure someone will be able to help.



For more options, visit https://groups.google.com/d/optout.



--
-tony

Tim Crowson

unread,
Apr 17, 2014, 10:46:06 AM4/17/14
to python_in...@googlegroups.com
I'm seeing another example that uses utils.executeDeferred() rather than cmds.evalDeferred(). What's the difference between these two?


Tony Barbieri

unread,
Apr 17, 2014, 11:07:25 AM4/17/14
to python_in...@googlegroups.com
As far as I can tell the difference is executeDeferred will accept a pointer to a callable whereas evalDeferred is the old mel call which accepts a string.  Reading the pymel wrapper information, the default utils.executeDeferred will not execute when in batch mode:




For more options, visit https://groups.google.com/d/optout.



--
-tony

Tony Barbieri

unread,
Apr 17, 2014, 11:10:04 AM4/17/14
to python_in...@googlegroups.com
Btw, if you are not familiar with pymel it's a 3rd party developed alternative to maya.cmds.  It's much more pythonic and ships with Maya by default now.  It basically wraps up the default maya python implementation plus the maya python API and defers to the faster implementation automatically.  I'm sure it's much more elegant than that, but that's what it does in a nut shell :).
--
-tony

Brad Friedman

unread,
Apr 17, 2014, 11:08:42 AM4/17/14
to python_in...@googlegroups.com
That's a python thing. There's a lot of discussion on eval vs exec as they're a pair of low level calls that end up exposed together. You also run into it a lot when embedding python. The biggest difference is return value. There are also potential differences in scope.

Marcus Ottosson

unread,
Apr 17, 2014, 11:21:43 AM4/17/14
to python_in...@googlegroups.com
Interesting! Where can I find these discussions?


On Thursday, April 17, 2014, Brad Friedman <br...@fie.us> wrote:
That's a python thing. There's a lot of discussion on eval vs exec as they're a pair of low level calls that end up exposed together. You also run into it a lot when embedding python. The biggest difference is return value. There are also potential differences in scope.

On Apr 17, 2014, at 10:46 AM, Tim Crowson <tcro...@gmail.com> wrote:

I'm seeing another example that uses utils.executeDeferred() rather than cmds.evalDeferred(). What's the difference between these two?


On Thu, Apr 17, 2014 at 9:37 AM, Tony Barbieri <grea...@gmail.com> wrote:
Np!  I'll do my best to help out with additional questions :).  This list is pretty active so I'm sure someone will be able to help.


On Thu, Apr 17, 2014 at 10:19 AM, Tim Crowson <tcro...@gmail.com> wrote:
Gotcha! Thanks Tony! Nice to see a familiar face too. I'm sure I'll have plenty more questions as I progress...


On Thu, Apr 17, 2014 at 9:18 AM, Tony Barbieri <grea...@gmail.com> wrote:
Hey Tim,

You'll want to use evalDeferred.  So for your example above, instead of calling buildMenu directly in the userSetup.py, call it like:


# Defer our startup commands until after Maya has finished loading
# completely.
cmds.evalDeferred("buildMenu()")

notice that "buildMenu()" is a string.



On Thu, Apr 17, 2014 at 10:08 AM, Tim Crowson <tcro...@gmail.com> wrote:
Hello all, I'm a Softimage user needing to move over and get familiar with Maya. I apologize in advance if I post any stupid questions. Does this list have its own search function? I looked for one but couldn't find any.

I'm using Maya 2015 on Windows at the moment and I'm having trouble with my userSetup.py file. Basically, I want to create a custom menu in the menu bar at launch. After googling some tutorials on the matter, I have code that works great when fired from the the script editor, but when run at launch, it bombs. Basically my function to build the window is unable to get a handle on the main Maya window... it gets returned as NoneType. The relevant part of my code looks something like this:


def buildMenu():
    ptr
= mui.MQtUtil.mainWindow()
    mayaWindow
= shiboken.wrapInstance(long(ptr), QtGui.QWidget)
 
    mainMenu
= cmds.menu('MyMenu', p=mayaWindow, l='My Menu')
    cmds
.menuItem(p=mainMenu

--
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/6E9179D0-F7BF-497B-9B99-9B8AD96FDF1D%40fie.us.

For more options, visit https://groups.google.com/d/optout.


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


Brad Friedman

unread,
Apr 17, 2014, 11:31:52 AM4/17/14
to python_in...@googlegroups.com
By googling "python eval exec". Or even looking at the python docs on the eval and exec calls themselves which explain the low level calls that are being exposed in the first place. 

Marcus Ottosson

unread,
Apr 17, 2014, 11:35:41 AM4/17/14
to python_in...@googlegroups.com
Ah, I see. I thought you were referring to executeDeferred and evalDeferred. Thanks though.

For more options, visit https://groups.google.com/d/optout.

--
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/A349A738-19C0-457C-858D-AC7ACB693041%40fie.us.

For more options, visit https://groups.google.com/d/optout.


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


Tim Crowson

unread,
Apr 17, 2014, 11:35:51 AM4/17/14
to python_in...@googlegroups.com
So as far as the practical difference between evalDeferred() and executeDeferred() in Maya goes... it sounds like they differ in what they return, and that the latter doesn't work in batch mode...?


Tony Barbieri

unread,
Apr 17, 2014, 11:41:19 AM4/17/14
to python_in...@googlegroups.com
That and the arguments they accept are different.  evalDeferred takes a string, executeDeferred accepts a callable and any arguments you would like to pass to the callable.  executeDeferred works in batch mode only if you use the pymel implementation of it.



For more options, visit https://groups.google.com/d/optout.



--
-tony

Eduardo Grana

unread,
Apr 17, 2014, 1:29:57 PM4/17/14
to python_in...@googlegroups.com
Hi Guys,
One thing silly coment that maybe is usefull to mention, is that userSetup.mel is executed after the ui is initialized, so you can execute thing
at that moment that will modify the existing ui. As you probably know, it's mel code, so you will have to do things like 'python ("pyhton code here");'
Cheers!
Eduardo



For more options, visit https://groups.google.com/d/optout.



--
Eduardo Graña
www.eduardograna.com.ar

Chad Dombrova

unread,
Apr 17, 2014, 1:37:16 PM4/17/14
to Maya Python Group

That and the arguments they accept are different.  evalDeferred takes a string, executeDeferred accepts a callable and any arguments you would like to pass to the callable.  executeDeferred works in batch mode only if you use the pymel implementation of it.

vs.

That's a python thing. There's a lot of discussion on eval vs exec as they're a pair of low level calls that end up exposed together. You also run into it a lot when embedding python. The biggest difference is return value.

As Tony points out in the first quoted section above, the difference between evalDeferred and executeDeferred has to do with the old mel command vs the new python function.  logically, since the former is a mel command, and the latter a python function that takes a python object and not a string, the difference is not related to the difference between eval and exec in python. sorry Brad, it was a good guess, though :)

on the topic of eval vs exec: in brief, eval evaluates a python "expression", which is anything that can be executed on a single line and stored in a variable:

    "1 + 1"
    "'me' if name == 'chad' else 'you'"
    "answer1 or answer2 and answer3"

of course, the variables referenced need to exist in the current scope, or you can pass your own globals and locals.

exec can execute many lines of complete python code and is not limited to just expressions. it can even execute the contents of an entire module, though there is a shortcut for the latter called execfile.  this is actually how python plugins in maya are loaded: they are not imported.

exec should not be used as a crutch to avoid learning how to do things pythonically.  e.g. don't do this:

    exec "import %s" % module_name

do this:

    __import__(module_name, globals(), locals(), [], -1)

or use the imp module.

hope that helps.

chad.



Reply all
Reply to author
Forward
0 new messages