Basic question about pyMel

40 views
Skip to first unread message

Simon Davies

unread,
Mar 23, 2015, 1:59:00 PM3/23/15
to python_in...@googlegroups.com
Hi all,

I understand that if I import pyMel into Maya like this:

from pymel.core import *

I can then build up pyMel functions like this:

ls(type='camera')[0].getParent().getTranslation().z

However if I import pyMel with a name space instead:

import pymel.core as pm

Would the pyMel methods above all need prefixing with pm like this?

pm.ls(type='camera')[0].pm.getParent().pm.getTranslation().z

The above code doesn't work, so how should it be written if I import pymel as pm?

Thanks a lot.

Robert White

unread,
Mar 24, 2015, 3:14:13 PM3/24/15
to python_in...@googlegroups.com

Nope this wouldn't work:

pm.ls(type='camera')[0].pm.getParent().pm.getTranslation().z

Instead you'd need:

pm.ls(type='camera')[0].getParent()..getTranslation().z

Its only the initial function that gets prefixed with the module namespace.

Jesse Kretschmer

unread,
Mar 25, 2015, 9:02:13 AM3/25/15
to python_in...@googlegroups.com
There have been some great explanations here already, but I felt like chiming in still.

The line of code in your example is a bunch of methods chained together (method chaining). Each returns a new object and then you call a method on that object. It is possible to rewrite the code in an expanded view. This creates more variables, but it should make it more obvious as to why you are only adding one "pm." prefix.

Here is the code expanded:
cameras = pm.ls(type='camera')
cameraTransform = cameras[0].getParent()
transformMatrix = cameraTransform.getTranslation()
print "Value of z: %s" % tranformMatrix.z

Reply all
Reply to author
Forward
0 new messages