New to PyMEL. Couple question please?

73 views
Skip to first unread message

Panupat Chongstitwattana

unread,
Mar 11, 2015, 12:44:20 AM3/11/15
to python_in...@googlegroups.com
Trying my hands on PyMEL for the first time. Would appreciate some pointers.

First, in the documentaion I see there's another level of hierarchy.
pymel.core.general
pymel.core.modeling
etc

However in all examples I see, none of them are using the general/modeling/etc when they call methods. Are those above just a category for documentation and not actual Python hierarchy?

Second  if I know the my object's name and store the name as string, how can I get/set attributes through PyMEL without selecting it? Or convert it into PyMEL objects? for example

import pymel.core as pmc

myname = "loop_anim_v001:master_ctrl"

pmc.objExists(myname)
# True

myname.getPosition() # AttributeError
pmc.getPosition(myname) # AttributeError
pmc.general.getPosition(myname) # AtributeError

I'm a bit stuck because examples from docs and Vimeo/Youtube that I found are creating objects from scratch and go from there, like,

myobj = polySphere()

In which case all the get/set method are already avaibale under myobj.

Thank you!

AK Eric

unread,
Mar 11, 2015, 1:02:20 AM3/11/15
to python_in...@googlegroups.com
Nearly everything you need can be accessed by importing pymel.core
When running this:

myname = "loop_anim_v001:master_ctrl"
pmc.objExists(myname)
# True

You're passing the string name to the PyMel command.  Note most of their commands take either strings, or PyNodes.
The reason your getPosition method is failing is that myname is a string, not a PyNode.
Do this to convert to a PyNode from string:

mynode = pmc.PyNode(myname)

However, I'm not sure what the "getPosition" function\method your calling lives.  You could do something like:

restPos = mynode.getRestPosition()

Since that's a method of a PyNode Transform (presuming myname was a transform).

The reason this works: (and I added the pmc, I presume you left that off)

myobj = pmc.polySphere()

Is that PyMel returns PyNodes when nodes are created, rather than strings, so all the methods are available.



Panupat Chongstitwattana

unread,
Mar 11, 2015, 1:43:05 AM3/11/15
to python_in...@googlegroups.com
Thank you Eric!

Sorry about getPosition, my bad, I actually mean getTranslation.

Panupat Chongstitwattana

unread,
Mar 11, 2015, 2:02:03 AM3/11/15
to python_in...@googlegroups.com
Hi.

I ran into another problem. objExists is returning different result when I put it in a for loop.

selects = pmc.selected()

for sel in selects:
    tmpname = "%smaster_crtl" % sel.namespace()
    print tmpname
    print pmc.objExists(tmpname)
# SC05A_0110_ECh_LoopAnim_v002:SC05A_0020_Extra_Cheer_LoopAnim_v001:exMaleHair001:master_ctrl
# False

tmp = "%smaster_ctrl" % selects[0].namespace()
print tmp
print pmc.objExists(tmp)
# SC05A_0110_ECh_LoopAnim_v002:SC05A_0020_Extra_Cheer_LoopAnim_v001:exMaleHair001:master_ctrl
# True

What could be the cause of this?

Justin Israel

unread,
Mar 11, 2015, 3:02:06 AM3/11/15
to python_in...@googlegroups.com

You have a typo between the two ways you format the string :

tmpname = "%smaster_crtl" % sel.namespace()

tmp = "%smaster_ctrl" % selects[0].namespace()

crtl vs ctrl


--
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/53b82fa7-91de-4eee-b8f1-03d5c6c668e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Panupat Chongstitwattana

unread,
Mar 11, 2015, 3:32:36 AM3/11/15
to python_in...@googlegroups.com
Ah thank you. Such a silly mistake :(

Is there any draw back if I don't check objExists but instead go for this?

try:
    PyNode(string)
except:
    print "something"

Paul Molodowitch

unread,
Mar 11, 2015, 9:22:06 AM3/11/15
to python_inside_maya
From a functional standpoint, there isn't a difference. There may be a performance difference, but unless you're making thousands, I wouldn't worry about it.

Generally speaking, if I usually expect it to exist, and want to use it as a PyNode if it does, I'll do the try/except thing.  One note though - you should probably do:

try:
    pmc.PyNode(string)
except pmc.MayaNodeError:
    print "something"

It's bad practice to have blanket except statements** - particularly if you're "expecting" a certain type of error. Get in the habit of having targeted try excepts, and you'll save yourself from headaches down the road.

- Paul

**There are always exceptions, of course - for instance, if you're running an app and don't want an unknown error to bring everything to a halt, it can make sense to have some generic high-level try/except statements.  And in those cases, you'll probably want to at least print some sort of warning or traceback, or maybe log it to an error log, or email a bug report, etc.  Also, even then, it's better to do "except Exception as e:" or "except Exception:" instead of "except:", because the latter will catch strange things you almost certainly don't want to - ie, if the user presses ctrl-c, or even if you've explicitly tried to call sys.exit!


--
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.

Geordie Martinez

unread,
Mar 11, 2015, 1:44:54 PM3/11/15
to python_inside_maya

Panupat Chongstitwattana

unread,
Mar 11, 2015, 11:08:12 PM3/11/15
to python_in...@googlegroups.com
Thank you every one.

The current script I trying this is about retrieving hundreds or even thousands of names from Json/SQL and adjust maybe 1 or 2 attributes of each. Looping through strings creating PyNode over and over only to perform 1 operation... would this bother you or do you think it shouldn't matter? I get a feeling PyMel maybe more suitable for scripts that manipulate fewer number of objects where you're adjusting many attributes.

Justin Israel

unread,
Mar 12, 2015, 2:23:56 AM3/12/15
to python_in...@googlegroups.com

You should just test both approaches and find out if the performance difference is acceptable. In a recent thread on this group, there was an example of creating many pynodes vs using cmds. At lower numbers you may not perceive a difference. At higher numbers it could be seconds vs subseconds


--
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.

AK Eric

unread,
Mar 12, 2015, 12:38:46 PM3/12/15
to python_in...@googlegroups.com
I tend to prototype in PyMel, just since doing stuff is easy, less typing.  But like Justin said, can be slow depending on what you're doing.  If I found it this is a performance critical application I move it into cmds, and if not suitable, the API.  Over time you sort of learn at which point you should start.

Paul Molodowitch

unread,
Mar 12, 2015, 1:53:39 PM3/12/15
to python_inside_maya
I'd say yeah, if you're talking about only adjusting an attribute or two on thousands of nodes, then making a PyNode for each will probably be significant.  I'd suggest just calling setAttr directly - using either pymel or cmds.

- Paul

On Thu, Mar 12, 2015 at 9:38 AM, AK Eric <war...@sbcglobal.net> wrote:
I tend to prototype in PyMel, just since doing stuff is easy, less typing.  But like Justin said, can be slow depending on what you're doing.  If I found it this is a performance critical application I move it into cmds, and if not suitable, the API.  Over time you sort of learn at which point you should start.

--
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.

Panupat Chongstitwattana

unread,
Mar 13, 2015, 12:55:11 AM3/13/15
to python_in...@googlegroups.com
Is there a list of available except type? For example if I'm doing

try:
    os.makedir(dir)
except .....:
    pass

Where can I find out what error to catch apart from trying it out?
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Geordie Martinez

unread,
Mar 13, 2015, 1:08:29 AM3/13/15
to python_inside_maya
you can use the pymel path method that wraps os and glob into one useful set of utilities.

https://docs.python.org/2/library/glob.html

# make a path object
thePath =pm.Path(<YOURPATH>)

if not thePath.exists():
    thePath.mkdir()

tests:
thePath.exists()
thePath.isFolder()
thePath.mkdir()
etc. see link above

# RETURN ALL FILES IN A FOLDER. returns path objects instead of strings. 
filesInFolder = thePath.glob("*.*")

super useful


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/7d8ad41f-3f98-43ca-946a-1d5f14ab4b50%40googlegroups.com.

Justin Israel

unread,
Mar 13, 2015, 1:22:31 AM3/13/15
to python_in...@googlegroups.com


On Fri, 13 Mar 2015 5:55 PM Panupat Chongstitwattana <panu...@gmail.com> wrote:

Is there a list of available except type? For example if I'm doing

try:
    os.makedir(dir)
except .....:
    pass

Where can I find out what error to catch apart from trying it out?


docs are your friend:
https://docs.python.org/2/library/os.html

"Note All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system."

Usually either the module or specific function docstring should explain the exceptions that can be raised

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

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/7d8ad41f-3f98-43ca-946a-1d5f14ab4b50%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages