View Full Version : Pymel - python and mel working together
Chadrik 08-02-2007, 08:49 PM hey all,
i've created a new module aimed at making python a viable alternative to mel within maya. you could think of it as object-oriented mel on steroids. it comes with a translator for converting mel scripts into python scripts.
check this link (http://www.highend3d.com/maya/downloads/tools/syntax_scripting/Pymel-4844.html) for more info and some example code.
it's definitely still a beta project, so i'd love any feedback you have on how to make this an even better module.
-chad
|
|
thematt
08-03-2007, 09:08 PM
that's way cool, I might use that sometime.
thanks and great work
wrend
08-04-2007, 07:00 AM
brilliant. thankyou.
hibigibees
08-04-2007, 03:20 PM
thats good work man, I have not tested it, but it should be certainly useful for production !
Correct me if I m wrong, its basically a string processing right - i mean u look for stuff line by line or ";" - which ever is first - tokenize stuff - then reason stuff and replace stings right ?
Chadrik
08-06-2007, 10:42 PM
the string processing is only part of it, and is primarily in the class that allows mel to be called as if it were python.
much of the rest is organizational stuff, like creating class methods that redirect to mel commands. for example, the Attribute class has the methods 'isKeyable', 'setKeyable', and 'isConnectable', which delegate to getAttr, setAttr, and attributeQuery commands respectively.
another good example is that both the Attribute class and the Transform class have a 'getChildren' method, but the former calls attributeQuery, while the latter calls listRelatives. the end result is a more uniform and predictable way to script.
and since all these things can be strung together, you can do something like:
ls(type='transform')[0].getChildren()[0].tx.isKeyable()
not that i'd recommend it.
anyway, i'd like to stress again that if there are things about pymel that you think should work a different way, be sure to let me know.
Sphere♥
08-15-2007, 04:17 PM
Wow, nice work, thank you. Old habits are hard to break and this breaks more of 'em, but definitely for the better.
Chadrik
10-14-2007, 08:55 PM
i just released a new version of pymel which you can find here (http://www.highend3d.com/maya/downloads/tools/syntax_scripting/Pymel-Python-Module-4844.html). there are a slew of new classes and methods, bug fixes, and much better documentation.
as always, feel free to hit me up with any suggestions, comments, or bugs.
-chad
BenBarker
10-24-2007, 06:09 PM
This is very nice.
Have you decided on a license? I browsed the source and didn't see any license info, aside from a few GPL'd snippets. I ask only because it is a factor in getting it in use here.
Chadrik
10-25-2007, 08:47 PM
i was planning on releasing it under the LGPL. i've read up on the different open-source licenses, but the practical differences still elude me. at the time of doing the research LGPL struck me as a good one, but i honestly can't remember why.
which do you recommend and why?
also, out of curiousity, why do you require that it is open-source?
-chad
BenBarker
10-26-2007, 05:37 PM
I don't know what your situation is regarding your company and your work, so I can't really give a recommendation. It's a personal decision really, no need to justify your choice, whatever it may be :) The subtleties elude me too. I've never made it even halfway through a license agreement.
I don't require that it is open source by any means. I would never request that people give away their hard work for free if they don't want to. I only bring up the license issue at all because unknowns make deciders scared. I can just imagine the meeting now.
Me: "Hey I recommend pyMel, a lot of the work has been done already, and done very well to boot."
Them: "Do we have to buy it? Can they turn around and sue us? IS THE SKY FALLING?"
Me: "I don't know."
Them: *Head Explodes*
Chadrik
10-26-2007, 06:52 PM
haha. no, we're not planning on selling it. the only way i would let that happen is if autodesk decided to pick it up, and although improbable, in that case you would basically be getting it for free along with maya anyway. so as long as i'm working somewhere like luma that has an open policy i'll be releasing updates to pymel.
right now i'm looking for some good scripters to start using it full time so i can get some quality feedback and make it even better. if you have a library of mel scripts and you think some of them should be included as methods to pymel classes, send them over and if they fit the bill, i'll encorporate them.
-chad
Droolz
12-07-2007, 09:56 AM
Just wanted to add that this really is a wonderfully slick integration - I have very little python knowledge but just checking your docs allready gives me extreme urges to convert. Brilliant. Thanks so much for making this freely available.
Jules
Chadrik
01-05-2008, 05:36 AM
a new version of pymel has been released. many new features, improvements, and bug fixes.
http://www.highend3d.com/maya/downloads/tools/syntax_scripting/Pymel-4844.html
also, there is now a google code page if you're interested in contributing keeping up-to-date with the development of the project.
http://code.google.com/p/pymel/
T1ckL35
01-07-2008, 12:09 PM
Good news on the update :)
I've been learning python scripting in Maya and quite easily get bogged down in the non encapsulated object style method you end up having to use (especially in UI coding).
After having a read of your project Chadrik I may well give it a go today and see how it goes.
Cheers
Simon
Chadrik
01-08-2008, 08:54 PM
if you are having this error when trying to setup pymel:
# Error: No module named pymel
download the latest version from highend. when i first released the new version (0.7) one of my "cleanup" scripts cleaned up a little too much and removed some important files. i got a corrected version uploaded as quickly as i could, but at least 40 downloads went by before i could get it fixed.
by the way, for those with concerns over licensing like BenBarker, pymel is now open source under the GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
also, i thought i'd mention that the next release will feature an enhanced script editor which can report mel output in python, so all you script kiddies can learn python the same way you learned mel -- by performing an action in maya and watching the script editor output.
-chad
twilightened
01-18-2008, 10:53 AM
Hi,
How is it possible to have pymel running in python 2.4. Correct me if I am wrong. I think all maya commands can be run on a python shell using pymel. If I am correct. Then I try install pymel. No problem - it loads. Now I try the command
from pymel import * and boom I get the following error.
BRgds,
kNish
IDLE 1.1.2
>>> from pymel import *
Unable to determine which verson of Maya should be initialized, trying for Maya.env in C:\Documents and Settings\knish\maya
PYTHONPATH set to value C:\Program Files (x86)\Autodesk\Python\Lib\site-packages
Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
from pymel import *
File "C:\Program Files (x86)\Autodesk\Python\Lib\site-packages\pymel\__init__.py", line 506, in -toplevel-
assert util.mayaInit()
AssertionError
>>>
Chadrik
01-18-2008, 04:27 PM
hi kNish,
are you using mayapy to launch the interpreter? this little script is found in the maya bin directory and ensures that certain environment variables are set which are necessary for maya's modules to import correctly.
it's a little different on each os. here's what it looks like on osx:
#path=`dirname $0`
path=/Applications/Autodesk/maya8.5/Maya.app/Contents/bin/
pythonhome=$path/../Frameworks/Python.framework/Versions/Current
export PYTHONHOME=$pythonhome
export DYLD_LIBRARY_PATH=$path/../MacOS:$DYLD_LIBRARY_PATH
export DYLD_FRAMEWORK_PATH=$path/../Frameworks:$DYLD_FRAMEWORK_PATH
export MAYA_LOCATION=$path/..
export MAYA_NO_BUNDLE_RESOURCES=1
exec $pythonhome/Resources/Python.app/Contents/MacOS/Python "$@"
essentially, what going on is maya is ensuring that you're using its own copy of python and that the core maya libraries can be found.
a quick test to see if the problem is on the pymel side or on the interpreter setup side is to execute this code
import maya.standalone
maya.standalone.initialize(name="python")
import maya.cmds as cmds
cmds.about(version=True)
if that works, then pymel should work. pymel does this setup for you when you import it and it also attempts to find your Maya.env and parse that as well. however, it cannot do this unless the environment variables mentioned in mayapy (os specific) are set.
-chad
mlykke
09-05-2008, 08:54 AM
I'm new to all this scripting but slowly working my way thou all the setup stuff. I would love to see a step by step list on how to install, and test that it works correctly. Even the maya manual is very limited on this.
Its hard to get into scripting when there are so many bumps on the road. Thats why having check like this helps:
import maya.standalone
maya.standalone.initialize(name="python")
import maya.cmds as cmds
cmds.about(version=True)
Running that gives me and error from the second line. maya.standalone.initialize(name="python")
The error I get is:
# Error: initialize() takes no keyword arguments
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# TypeError: initialize() takes no keyword arguments #
Funny thing is that I can run some python examples. Have had a pSphere created. :)
mlykke
09-05-2008, 09:21 AM
http://groups.google.com/group/python_inside_maya/web/first-steps-to-start-python-inside-maya
Seams usefull.
mlykke
09-05-2008, 02:03 PM
I looked around the net for examples and found:
http://linil.wordpress.com/2008/01/24/python-maya-part-1 (http://forums.cgsociety.org/python-maya-part-1)
Ofcause i soon found out that it wasn't pymel but python, but I learn one or two things in converting it to pymel:
python version:
import maya.cmds as cmds
cmds.polySphere( name = ‘bouncing’, radius = 2 )
g = -0.04
v0y = 0.8
v0x = 0.1
pos0y = 2
pos0x = 0
for itr in xrange(0,6):
for tx in xrange(0,42):
posy = pos0y + v0y*(tx-1) + g*(tx-1)*(tx-1)/2
posx = pos0x + v0x*((itr*42) + tx-1)
cmds.setKeyframe( ‘bouncing’, attribute=’translateY’, value=posy, t=(itr*42) + tx )
cmds.setKeyframe( ‘bouncing’, attribute=’translateX’, value=posx, t=(itr*42) + tx )
pymel version:
sphere( name = 'bouncing', radius = 2 )
g = -0.04
v0y = 0.8
v0x = 0.1
pos0y = 2
pos0x = 0
for itr in xrange(0,6):
for tx in xrange(0,42):
posy = pos0y + v0y*(tx-1) + g*(tx-1)*(tx-1)/2
posx = pos0x + v0x*((itr*42) + tx-1)
setKeyframe( 'bouncing', attribute='translateY', value=posy, t=(itr*42) + tx )
setKeyframe( 'bouncing', attribute='translateX', value=posx, t=(itr*42) + tx )
paradigm3d
02-16-2009, 03:40 AM
hi there, i have recently installed the latest pymel, and it is successful in my maya 2008 64bit. unfortunately, not in my maya 8.5 64bit. everytime i run "import pymel" maya 8.5 gives me an error of:
import pymel
# Error: ('invalid syntax', ('C:\\Documents and Settings\\xxxxxxx\\My Documents\\maya\\8.5-x64\\scripts\\python\\pymel\\ui.py', 501, 17, ' return ret if moreButtons else (ret==yes)\n'))
# Traceback (most recent call last):
# File "<maya console>", line 1, in ?
# File "C:\Documents and Settings\oliverm\My Documents\maya\8.5-x64\scripts\python\pymel\__init__.py", line 621, in ?
# from node import *
# File "C:\Documents and Settings\oliverm\My Documents\maya\8.5-x64\scripts\python\pymel\node.py", line 17, in ?
# from core import *
# File "C:\Documents and Settings\oliverm\My Documents\maya\8.5-x64\scripts\python\pymel\core.py", line 22, in ?
# import util, factories, node, ui, io
# File "C:\Documents and Settings\oliverm\My Documents\maya\8.5-x64\scripts\python\pymel\ui.py", line 501
# return ret if moreButtons else (ret==yes)
# ^
# SyntaxError: invalid syntax #
anyone encountered this kind of problem and was able to solve it?
thanks for the help. :)
alienpioneer
03-02-2009, 05:28 PM
Yes, me too..
Chadrik
03-03-2009, 11:51 PM
hi all, a brand-spankin' new version of pymel is almost ready to release. needless to say, this problem will be fixed.
-chad
pjrich
04-03-2009, 11:01 PM
Pymel rocks! I'm working on a number of examples converting some beginner-level poly-modeling MEL scripts to Python as a way to learn it. So far almost all the surprises have been good ones, which is amazing.
http://zoomy.net/2009/04/03/aligned-faces-pyramid-worm-in-python/
http://zoomy.net/2009/04/02/random-walk-pyramids-in-python/
http://zoomy.net/2009/04/02/random-walk-keyframed-blocks-in-python/
Nice work Chad et al, it's been a long time since I was excited about coding.
neonbulbs
04-30-2009, 11:12 AM
Hi, I've already did all the steps shown from the manual. But when I try to test it on maya using this command :
from pymel import *
I've got this error :
from pymel import *
# Traceback (most recent call last):
# File "C:\Program Files\Autodesk\Maya8.5\Python\lib\site-packages\maya\app\python.py", line 16, in formatException
# 32, stringBuffer )
# File "C:\Python25\Lib\traceback.py", line 126, in print_exception
# lines = format_exception_only(etype, value)
# File "C:\Python25\Lib\traceback.py", line 171, in format_exception_only
# if (isinstance(etype, BaseException) or
# NameError: global name 'BaseException' is not defined
Can you help me out what's wrong with that? Is there any steps that I missed from the installation step?
Thanks a bunch...
Chadrik
05-05-2009, 02:03 PM
this is not a pymel problem, just a general python setup issue (i'd be surprised if you can import anything at all)
don't put your system python `C:\Python25\Lib` on your PYTHONPATH when using maya, as there is a very good chance that the version is not the same as maya's internal python. in your case, maya 8.5 uses python 2.4.3 whereas your system is 2.5.x. if you have modules you want installed in maya, install them to maya's Python\lib\site-packages directory or to a shared location.
-chad
Chadrik
05-28-2009, 10:36 PM
we just released lots of new bugfixes and features to the 0.9.x line.
among other things, it's got some slick new (searchable!) documentation and instructions on how to setup autocompletion with eclipse.
get em while they're fresh:
http://www.highend3d.com/f/4844.html
http://code.google.com/p/pymel/downloads/list
-chad
Ironhalo
08-20-2009, 06:09 PM
so, i decided its time i learned python. im trying to install pymel, and im getting stuck during the ipymel install, specifically the 5th step. i run the command 'ipymel', and the following error occurs:
ImportError: No module named _ctypes
im running python 3.1.1, which was released a few days ago. should i be using an older version of python? i attached the .txt error report it generated.
also, the files for the replacement script editor aren't there either. is this feature being pulled? sorry if these are beginner questions.
Joviex
08-23-2009, 02:32 AM
im running python 3.1.1, which was released a few days ago. should i be using an older version of python? i attached the .txt error report it generated.
.
I would recommend going back to python 2.6.
As of 2009/2010 I think they are using a custom version of 2/5/2.6. I know pymel works 100% perfectly in the maya version of python for 2009/2010 so.... ymmv but I would guess that is the underlying source of the problem.
Jacobborsting
08-27-2009, 03:02 PM
I can't get Pymel running in 2010. When I try to import pymel I get this error:
Error: AttributeError: import pymel.util as util: 'module' object has no attribute 'util'
I copied my prefs from 2009.
Pymel is still working correctly in 2009.
Chadrik
08-31-2009, 08:46 PM
0.9.2 is out with support for 2010
http://code.google.com/p/pymel/downloads/list
CHANGES AND ADDITIONS
added support for 2010 and python 2.6
added basic support for all component types
added a 'removeNamespace' flag to FileReference.importContents()
added support for open-ended time ranges for command like keyframes (Issue 82)
enhanced keyframe function: if both valueChange and timeChange are queried, the result will be a list of (time,value) pairs
added ability to pass a list of types to ls 'type' argument, as you can with listRelatives
added checkLocalArray and checkOtherArray arguments to Attribute.isConnectedTo which will cause the function to also test mulit/array elements
improved core.language.pythonToMel reliability on lists
improved custom virtual class workflow
added functionality to pymel.tools.py2mel for dynamically creating MEL commands based on python functions/classes
added a new module pymel.api.plugins for working with api plugins in a more reasonable and automated fashion
updated eclipse integration documentation
BUGFIXES
fixed importFile, createReference, loadReference, and openFile now return PyNodes when passed returnNewNodes flag (Issue 85)
fixed rare bug with Vista where platform.system was failing during startup (Issue 87)
fixed a bug with plugin loading to intelligently handle when callback does not get a name
fixed optionMenu and optionMenuGrp to return empty lists instead of None
restored core.other.AttributeName.exists method
fixed a bug in 0.7_compatibility_mode
fixed minor bug in listRelatives
fixed a bug where curve command was returning a string instead of a PyNode (Issue 96)
EASY INSTALL IMPROVEMENTS
setup now copies over a readline library for 2010 OSX using
readline.so from toxik which is more compatible
changed ipymel to be part of the default install instead of an extra package
fixed interpreter path of ipymel and other executable scripts on OSX
setup now detects and fixes invalid python installations on Linux
which previously caused distutils and thus setup.py to fail
Chadrik
08-31-2009, 10:01 PM
so, i decided its time i learned python. im trying to install pymel, and im getting stuck during the ipymel install, specifically the 5th step. i run the command 'ipymel', and the following error occurs:
ImportError: No module named _ctypes
im running python 3.1.1, which was released a few days ago. should i be using an older version of python? i attached the .txt error report it generated.
don't use python 3.x. each version of maya has a specific version of python that it will work with. if you can, you should use "mayapy", which is the launcher for maya's included copy of python. if you want to use a system version of python with maya modules, you should install only exact versions that come with maya.
maya 8.5 = python 2.4.3
maya 2008/2009 = python 2.5.1
maya 2010 = python 2.6.1
also, keep in mind that ipymel is just an additional feature of pymel. you can use pymel without it.
-chad
supertom44
09-01-2009, 09:37 AM
Im really excited about this tool however, I just installed it using the easy install instructions and now I cant start maya 2010, it simply pops up a dialogue box with a red X in it and no message.
I click ok and maya doesnt start. Is there anyway I can either fix this, or uninstall pymel so I can try again?
I'd really appreciate your help.
Thanks.
Chadrik
09-01-2009, 04:23 PM
I just installed it using the easy install instructions and now I cant start maya 2010, it simply pops up a dialogue box with a red X in it and no message.
what OS are you on (be as specific as possible)? any error messages in the console / shell / output window?
also, did you install the script editor?
Chadrik
09-01-2009, 07:12 PM
btw, the best place to post questions and bugs related to pymel is at python_inside_maya (http://groups.google.com/group/python_inside_maya)
supertom44
09-02-2009, 08:40 AM
I am using Vista 64 bit Ultimate, the problem is with Maya 2010 64bit, there are no errors, maya doesn't get chance to pop up the ouput window. I have attached the error that I get.
Would uninstalling maya uninstall pymel, as it worked fine before I installed it, if I could uninstall pymel I could at least try and reinstall it see if it happens again.
I didnt install the script editor, tried to install ipymel but couldnt get that to work. Id like to remove it all and try again but bar a system restore not sure how.
thanks
Chadrik
09-02-2009, 01:35 PM
wow,
that is pretty messed up. i've never seen that before. to uninstall pymel:
if you installed the script editor, first find and delete those files: scriptEditorPanel.mel and pymelScrollFieldReporter.py
restart maya and see if that fixed it.
it not, navigate to this directory in your explorer:
C:\Program Files\Autodesk\Maya2010\Python\Lib\site-packages
if you followed the directions to install ipymel, you should first remove the pyreadline files that you copied over here. try removing those and restarting maya to see if that fixes it. if it doesn't, then delete everything in this folder except the maya folder and its contents.
don't forget to remove any changes you made to userSetup.mel or userSetup.py.
if maya still doesn't work, then uninstall maya and reinstall it. if that still doesn't work, then the problem is most likely not pymel.
i hope that works for you. sorry about the trouble, i do not have vista installed to test this out on. i know that pymel 0.9.1 is compatible with 2009 on vista. it's still too early to tell if others are having your same problem with vista+2010.
-chad
supertom44
09-03-2009, 09:07 AM
Turns out it was the system variables that I created when installing that were causing maya to crash. Im going to uninstall pymel and try again, see if I can get it all working.
You were right the problem wasnt to do with pymel, sorry for any trouble.
Thanks.
proxyPrimate
09-03-2009, 10:27 PM
Hi, so first let me just say that I'm totally blown away by even the few small things I've done with pymel. I've been trying to get into pymel properly for a few months now and have recently put together my new machine at home so I'm busy setting things up again (pymel works fine at work, where I set it up). After going throug the easy setup which went without a hitch (except for refusing to install ipymel but I'm not all that concerned). If I try to import pymel in maya I get this error:
from pymel import *
# Error: No module named ply.lex
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\__init__.py", line 1468, in <module>
# import mayahook
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\mayahook\__init__.py", line 5, in <module>
# from mayautils import *
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\mayahook\mayautils.py", line 7, in <module>
# from pwarnings import *
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\mayahook\pwarnings.py", line 12, in <module>
# from pymel.util.decoration import decorator
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\util\__init__.py", line 19, in <module>
# import nameparse
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\util\nameparse.py", line 1, in <module>
# from objectParser import *
# File "c:\program files\autodesk\maya2009\python\lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\util\objectParser.py", line 23, in <module>
# import ply.lex as lex
# ImportError: No module named ply.lex #
Far as I can see things are working fine except that it's lost this ply.lex module which it seems is used in a lot of other modules. I'm a bit of a noob with this but I'd really appreciate some help. Could it be an environment variable issue? I created the env vars as detailed in the docs. Only difference I could see was PATH was path.
I'm running windows 7 64bit incase thats important.
Thanks so much. And thanks for making pymel available having looked at the differences between standard python and pymel I'm definitely sold on pymel (now if I could just get it to work first time everytime)
Benjamin.
Morganism
09-17-2009, 05:52 PM
First of all, big fan of pymel. Full disclosure, I'm still learning python, so apologies for any misused vernacular.
But here's my question: when you get or set an object's attributes in pymel, it attempts to get or set an attribute on the corresponding node in maya. Which is cool, except when you want to add an attribute to the python object without affecting the maya node. Is there a standard or straighforward workaround for adding an attribute to a PyNode object in python?
Morganism
09-17-2009, 10:19 PM
Another quick question, somewhat similar. I'm planning on writing a lot of code, so I'm trying to nail the best practices early.
This function gets the attribute passed in as a variable, so we don't know what the attribute will be called in order to invoke it:
def myFunction(attrName,node):
node.addAttr(attrName)
node.attrName.set(5) #<--this doesn't work
So, as far as I can tell, this is how you do that:
def myFunction(attrName,node):
node.addAttr(attrName)
attribute = PyNode(node.name()+'.'+attrName)
attribute.set(5)
Which might be fine, but I'm curious if there's any different ways of dealing with this. Thanks!
KarlErlandsen
09-20-2009, 11:22 AM
Hey Morgan,
Here's another approach that also does what you're after:
def myFunction(attrName, node):
node.addAttr(attrName)
node.setAttr(attrName, 5)
I reckon your approach of making a new PyNode will be much more useful to you in the long run though, since once you've got that PyNode object you can also start doing other cool things like setting its max/min values, whether it's keyable, and so on...
Hope this helps!
Karl
Morganism
09-22-2009, 10:47 PM
thanks for the reply. The way you mentioned is probably cleaner for simple stuff where you don't need access to PyNode Attribute methods.
Does anyone have any insight into my previous question?
Chadrik
09-23-2009, 11:37 AM
hi guys,
first of all, it's really cool to see folks from Framestore and Tippett helping each other out with pymel. both of your studios do really great work.
when you get or set an object's attributes in pymel, it attempts to get or set an attribute on the corresponding node in maya. Which is cool, except when you want to add an attribute to the python object without affecting the maya node. Is there a standard or straighforward workaround for adding an attribute to a PyNode object in python?
the PyNodes are directly married to the objects they represent, which is what you want 99% of the time, but if you just want to work with hypothetical node and attribute names, there are PyNode-like objects in core.other called NameParser, AttibuteName, DependNodeName, and DagNodeName.
after i reread your question i realized you're asking for something different. if you asking how to add a *python* attribute to a PyNode object, then to put it simply, this is something you should not do. i'm curious, though, what you're trying to do.
-chad
Morganism
09-23-2009, 04:52 PM
Hey Chad, thanks for your reply. I basically want to add temporary data to a node, without adding any attributes in the maya scene.
The example is: I want to unparent a node but have it remember it's old parent. In this particular case, because of the way it's looping, I can't rely on a separate variable or index numbers or any of that. In mel this had been accomplished by adding attributes and making message connections so that the node could be retrieved later. With python this seems like it'd be really easy to do without cluttering up the maya scene or having much extra code, and it seems like it would be useful in several situations where you want data to follow an object around between scripts. Are there downsides to this? Thanks!
Chadrik
09-23-2009, 05:06 PM
a python dictionary is definitely the way to go here. use the PyNode as the keys, and put whatever data you want in there. also check out defaultdict, part of the collections module, for an even more powerful dictionary solution. once you master dictionaries, which is not hard, you'll really wonder how you ever got along without them.
-chad
Morganism
09-23-2009, 07:15 PM
Cool, that sounds like a good solution, I'll take a look. Thanks!
woadiestyol
12-08-2009, 11:33 PM
Hey all!
First off, I'm loving PyMel - I just started diving into it a couple months back and it's making my coding tasks soooo much nicer. Can't thank you enough for putting it together and releasing it for free(!) and with so much help and documentation(!)
Thus far I've used it for small tasks - 100-200 line scripts that do some fairly simple stuff. Recently though I started writing a little more invoved autorigging toolkit with it, and have started noticing some behavior that might be related to Python, PyMel, or poor coding style on my part, though I'm not sure which (I'd say the latter is most likely).
Basically, I can execute small chunks of code at a time, and they work fine if I do it this way, but if I try to execute, say, the whole arm-setup procedure at once, Maya often crashes. The weird thing is that sometimes it does, sometimes it doesn't, which leads me to believe that there's some sort of memory management stuff happening under the hood that I'm not aware of.
I ran into a similar problem earlier where I was calling these functions through lambdas in the UI function (I wasn't able to get the PyMel Callback objects to work), and that lead to quite a few crashes...I'm not sure how the lambda operates behind the scene, but it was another case where sometimes it would work and sometimes it wouldn't. Either way I've found myself holding my breath every time I execute any chunk of code over 30 or 40 lines.
How I tend to work is that I define the signature of my function, then comment out the "def proc():" line and create a bunch of temporary variables to hold the values that I'll later turn into arguments. I keep the code unindented so that I can step through it easily while I work. Only when I feel like I've gotten everything working properly do I indent the whole thing and uncomment out my "def" line. So while I'm working on my arm-rig script, I might test the whole shebang by executing 500 lines outside of any function. Does this make a difference? I'm not sure why it would, and from my tests it doesn't seem to matter whether my code is inside of a function or not, but I figured it was worth mentioning.
Anyone else run into similar problems, or have any thoughts on the culprit? I'm very likely misusing PyMel somehow, but I'm hoping someone else has some thoughts on my misuse. Thanks in advance!
Chadrik
12-09-2009, 01:55 AM
hi,
can you post an example of a script that tends to fail on pastebin (http://pastebin.com/). we'll take a look at it.
-chad
woadiestyol
12-09-2009, 05:30 AM
Hey Chad,
Wow, thanks for the fast reply!
Here is my utilities module (http://pastebin.com/m63ef6a8e). I have it saved on the path as gsCharacterSetup_utils.py
and here is my spine setup module (http://pastebin.com/d11e59729). This imports PyMel and the gsCharacterSetup_utils module above.
I'm including both as I use a lot of the utilities throughout modules like the spine module, so maybe there's something in here that's the source of trouble. I have to imagine there might be something about the way I'm coding that's causing some sort of trouble I'm not aware of since Python is so high-level. Sorry to be so vauge - I'm a bit in the wilderness as Python and MEL are my first and only languages and I started teaching myself about a year ago. I'm sure learning something like C or C++ would be a benefit in helping me understand how memory is managed, and I hope to learn more about them some day.
In a nutshell, sometimes (90% of the time probably) these run just fine - in fact, I've never had the setupWorld function crash. Every now and then though, the rigSpine function will crash Maya. When Maya crashes, sometimes it pops open the fatal error window, and sometimes it just disappears. It's the inconsistency of the crashing that has me head-scratching and thinking it has something to do with memory issues.
So feel free to pick apart anything you see here that looks out of sorts. My implementation is far from slick, so anything anyone feels like commenting on - I'm all ears!
Again, thanks so much for all the work you've done with PyMel and the community here. Unfortunately nobody around the school knows much about Python (and certainly not PyMel), so it's really awesome to have folks out there who will take time to help out us noobs.:bounce:
Chadrik
12-10-2009, 08:49 PM
there may be something funky going on behind the scenes with the MScriptUtil that makes Transform.setScale work. we're looking into it. in the meantime, try using this style instead:
myTransform.scale.set(values)
by the way, your code looks very clean. nice work.
woadiestyol
12-10-2009, 10:23 PM
Hmm...my pastebin post of the spine setup seems to have been deleted. Here it is again:
http://jspatrick.pastebin.com/m7102fbc0
In other news, I've been testing these modules on a couple different machines and it seems to only affect the computers at and around Gnomon - I haven't been able to get my home machine to crash yet when running any of the functions. It also affects my machine at Gnomon Studios (just started last term), where I'm interning and writing our character setup and pipeline tools.
On computers at school and at Gnomon Studios, I've put the pymel directory on a network path. I'm wondering now if this could somehow be the issue, as it seems to be the common link. In fact, I had written another script a while back that mysteriously was semi-frequently crashing Maya, which was a simple error window written with PyMel that had an iconTextButton inside it. I've since deleted the source or I'd post it, but the icon was also being pulled over the network, and Maya would crash about 1/3 times that the window popped up.
Well, that's about all the information I can think of right now that might provide a clue to the problem. Thanks again to anyone who can offer help!
Best,
-John
woadiestyol
12-10-2009, 10:24 PM
Looks like I was writing my post while you were replying. Thanks again Chad, I'll give it a go!
Chadrik
12-11-2009, 01:19 AM
we store pymel on a network here at luma along with all of our python scripts, which is fairly common practice, but perhaps on a slow network it would cause problems? are the OS's the same between home and Gnomon?
if you ever get another simple script that crashes frequently definitely let us know. you can post here or the python inside maya (http://groups.google.com/group/python_inside_maya) group. if it's simple enough you can try rewriting the same code using maya.cmds to determine if it's a pymel-specific problem.
whalerider
12-19-2009, 12:49 PM
I am having trouble starting ipython and ipymel on Windows 7, Maya 2008 64-bit:
C:\Program Files\Autodesk\Maya2008\bin>ipython
Cannot find Python executable C:\Program Files\Autodesk\Maya2008\bin\mayapy.exe
C:\Program Files\Autodesk\Maya2008\bin>ipymel
Cannot find Python executable C:\Program Files\Autodesk\Maya2008\bin\mayapy.exe
C:\Program Files\Autodesk\Maya2008\Python\Lib\site-packages\pymel-0.9.2-py2.5.egg\pymel\tools>ipymel
Cannot find Python executable C:\Program Files\Autodesk\Maya2008\bin\mayapy.exe
I installed PyMel using the easyInstall, also added MAYA_LOCATION and PYTHONPATH environmental variables and updated the PATH variable to include %MAYA_LOCATION%\bin
whalerider
12-19-2009, 04:54 PM
I am going over the commands of PyMel 0.9.2 and it seems to me that the examples are from Maya's Python help (and some are not formatted properly and hard to read). I would like to see PyMel examples instead.
Chadrik
12-19-2009, 07:27 PM
I'd like more pymel examples too, but there's over 3000 commands and i really don't have the time or the inclination to write that many examples. We did write something tha attempts to convert Maya doc examples to use pymel, but in the process we discovered that many of their examples don't actually work, and so neither did our converter. The pymel class mehods have a number of Hand written examples as does the tutorial. Also, keep in mind that when working with commands in a procedural way, pymel often behaves exactly the same as maya.cmds except that it returns pynodes instead of strings, so for many examples the differences between the Maya docs and what you'd find in the pymel docs would be trivial.
whalerider
12-19-2009, 08:48 PM
Fair enough, Chad.
Actually,this afternoon I converted a couple of modules for my camera transfer tools with just a few small changes. The search functionality in the documentation was really useful.
Thanks.
edit: I already made available the Python for Maya code. Maybe if I upload the Pymel version as well people can compare the two (there are free utilities w/ UI that do that easily) and use that as an example. If anyone has suggestions how to improve the code to capture the essence of Pymel better that would be great.
Chadrik
12-20-2009, 11:41 PM
I already made available the Python for Maya code. Maybe if I upload the Pymel version as well people can compare the two (there are free utilities w/ UI that do that easily) and use that as an example. If anyone has suggestions how to improve the code to capture the essence of Pymel better that would be great.
post a link when you put them both up.
whalerider
12-21-2009, 02:18 PM
http://www.orcavfx.com/downloads/mt_tools/
post a link when you put them both up.
Chadrik
12-21-2009, 03:18 PM
i just took a quick gander. here's some feedback on how to do things in a more pymel-friendly way.
def getCameraName():
"""
Validates the selection and
derives the name of the selected camera.
"""
error_msg = None
camera_name = None
camera_shape = None
selection = selectedNodes(dagObjects=True)
# Checks how many objects have been selected.
if len(selection) != 1:
error_msg = 'Error: You must select a single camera.'
else:
shape = listRelatives(selection, shapes=True)
# Checks if the selected object is a camera.
if nodeType(shape[0]) != 'camera':
error_msg = 'Error: The selected object is not a camera.'
else:
# selectedNodes() retrieves a list that looks like this: [u'|camera1']
# We only need 'camera1'.
camera_name = selection[0][1:]
# In pymel we need the shape reference to extract the properties.
camera_shape = shape[0]
first of all, in python there is no need to declare variables that you intend to use. second, selectedNodes() is obsolete (check the docs), use pymel's selected() instead. it returns PyNodes. lastly, there is no need to do string formatting on the names, because now we have PyNodes which are better than names. lastly, use python's exceptions as they were meant to be used: don't pass around error message strings. use try/except statements when calling getCamera ( i renamed from getCameraName, because we are no longer getting a name, we're getting a PyNode, which is a fundamental building block of pymel code). i created a new exception type, SelectionError, so that you can differentiate exceptions you raise in this code from ones that occur unintentionally in your try/except statement.
class SelectionError(Exception): pass
def getCamera():
"""
Validates the selection and
returns a PyNode for the selected camera.
"""
selection = selected(dag=True)
# Checks how many objects have been selected.
if len(selection) != 1:
raise SelectionError, 'Error: You must select a single camera.'
else:
camera = selection[0]
camera_shape = camera.getShape()
# Checks if the selected object is a camera.
if camera_shape.type() != 'camera':
raise SelectionError, 'Error: The selected object is not a camera.'
return camera, camera_shape
here's a simpler one:
connections = listConnections(camera_name+'.'+channel))
if connections:
for frame in range(export_start_frame, export_end_frame + 1):
frame_value = getAttr(camera_name+'.'+channel, time=frame)
becomes:
attr = camera_name.attr(channel)
connections = attr.connections()
if connections:
for frame in range(export_start_frame, export_end_frame + 1):
frame_value = attr.get(time=frame)
disclaimer: i haven't tested this code.
-chad
whalerider
12-21-2009, 03:55 PM
Thanks, Chad.
I'll go over the rest and try to incorporate your suggestions.
whalerider
12-23-2009, 09:10 PM
The only things I had to change were:
....
# selection should return 2 objects - a Transform and a Camera
if len(selection) != 2:
raise SelectionError, 'Error: You must select a single camera.'
else:
camera = selection[0]
# no need for camera.getShape(), because the shape is returned by selection
camera_shape = selection[1]
....
A lot more intuitive than Autodesk's version, no doubt.
i just took a quick gander. here's some feedback on how to do things in a more pymel-friendly way.
class SelectionError(Exception): pass
def getCamera():
"""
Validates the selection and
returns a PyNode for the selected camera.
"""
selection = selected(dag=True)
# Checks how many objects have been selected.
if len(selection) != 1:
raise SelectionError, 'Error: You must select a single camera.'
else:
camera = selection[0]
camera_shape = camera.getShape()
# Checks if the selected object is a camera.
if camera_shape.type() != 'camera':
raise SelectionError, 'Error: The selected object is not a camera.'
return camera, camera_shape
the RC1 is available for downlaod...
http://code.google.com/p/pymel/downloads/list
and here you can find the help...
http://www.luma-pictures.com/tools/pymel/docs/0.9/index.html
CGTalk Moderation
01-27-2010, 12:28 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.