RE: [Maya-Python] Basic question about pyMel

47 views
Skip to first unread message

br...@meljunky.com

unread,
Mar 23, 2015, 2:14:26 PM3/23/15
to python_in...@googlegroups.com
Methods do no need to reference the module.

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

-brian

-------- Original Message --------
Subject: [Maya-Python] Basic question about pyMel
From: Simon Davies <simonj...@hotmail.co.uk>
Date: Mon, March 23, 2015 12:59 pm
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.

--
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/33fcd70f-b4c2-496f-b05d-a41196a428ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Mar 23, 2015, 2:31:08 PM3/23/15
to python_in...@googlegroups.com

Though Brian is right, it doesn’t quite explain why you are getting the behaviour you’re seeing.

It’s a very good question, and I can completely understand why you would expect to prefix each PyMEL related call with pm as that is what you imported it as. The reason you don’t is a tad complicated, but has to do with two things.

  • Scope
  • Object Oriented Programming

Scope has to do with what variable names you can call without referencing anything else.

>>> my_variable = 5
>>> print my_variable
5

>>> import os
>>> print os
<module 'os' from 'C:\Python27\lib\os.py'>

The object-oriented part on the other hand is responsible for allowing you to call upon members of a variable; which in this case would instead be called an object.

>>> my_variable = "hello"
.>> print my_variable.upper()
HELLO

And that has to do with the dot following a variable. The dot is essentially saying “run the following command within the scope of the previous variable”. In this case, the variable is a string-object, and the string is an instance of a class with a method called upper() inside of it.

# Looks something like this.
class String:
    def upper(self):
        ...

If you were to make your own class, you could do the same.

class MyClass(object):
    def __init__(self, value):
        self.value = value

    def plus_one(self):
        return self.value + 1

myobject = MyClass(5)
print myobject.plus_one()
# Which would print 6

Objects returned by PyMEL works just like this, which is why you can call upon methods of those objects, without prefixing them with pm.

Does that help?



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



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

Justin Israel

unread,
Mar 23, 2015, 2:36:28 PM3/23/15
to python_in...@googlegroups.com

Just think of it like a directory structure.
If you import *, you are putting everything in your "current directory". If you import with a namespace, everything is under a "new directory". The only thing that changes is that leading namespace. Prefer the later and "keep your directory clean" with a namespaced import.

Justin


Simon Davies

unread,
Mar 23, 2015, 5:50:33 PM3/23/15
to python_in...@googlegroups.com
Thanks I think its becoming clearer:)

So in my example this part of the code:

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

is an instance of a class and that class contains the "getParent" and "getTranslation" methods, which means that the instance has access to those methods.

Marcus Ottosson

unread,
Mar 24, 2015, 3:36:42 AM3/24/15
to python_in...@googlegroups.com

You’ve got it. :)

In this case, you’re getting instances of a class called Camera, which has a number of parent classes, each adding additional methods to the Camera class.

>>> camera = pm.ls(type='camera')[0]
>>> camera_class = type(camera)
>>> camera_superclasses = camera_class.__mro__

>>> print camera_superclasses
(<class 'pymel.core.nodetypes.Camera'>,
 <class 'pymel.core.nodetypes.Shape'>,
 <class 'pymel.core.nodetypes.DagNode'>,
 <class 'pymel.core.nodetypes.Entity'>,
 <class 'pymel.core.nodetypes.ContainerBase'>,
 <class 'pymel.core.nodetypes.DependNode'>,
 <class 'pymel.core.general.PyNode'>,
 <class 'pymel.util.utilitytypes.ProxyUnicode'>,
 <type 'object'>) #

That is why this particular instance has so many members for you to call.

>>> dir(camera)
['activeColor',
 'addAttr',
 'addBookmark',
 'addChild',
 'addPrefix',
 'applyBookmark',
 'attr',
 'attrDefaults',
 'attrInfo',
 ...

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

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



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

Reply all
Reply to author
Forward
0 new messages