Pregunta

I've been making a tool for Maya with PyQt. So, it have to execute a Maya Command. It means that the program import Maya Libraries. When I test my program on IDLE (I use Eclipse), it bothers me. I want to block the Maya Command on IDLE, and just enable on Maya. Is there any way to find out it's running on Maya or IDLE?

¿Fue útil?

Solución

What I do is simply wrap my import in a try..except block:

try:
    from maya import cmds
except ImportError:
    print 'Not Maya'

My expectation is there is value to this code outside Maya, but not for functions that require a Maya api.

Otros consejos

The import check in mhlester's answer will not work for all possible setups - if you are using the MayaPy.exe interpreter in Eclipse, importing maya.cmds will give you an empty module rather than raising an import error (the empty modules get replaced if you start a maya.standalone but otherwise they are still there and won't trigger an ImportError)

You can catch that by looking for the actual commands inside the module:

try:
    import maya.cmds as cmds
    cmds.about()
except  AttributeError:
    print "not Maya"  # cmds.about doesn't exist
except ImportError:
    print "not Maya"  # Maya modules not on the path
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top