-------- 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 pmWould 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.
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 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?
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/20150323111411.4b67a6573d13e8dd18091ea81788b1e6.4b8c60eefb.wbe%40email06.secureserver.net.
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
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBtFFtW132_K_RQhYSe69rA%2BfkRhVgUbV_crq0Fho2Jug%40mail.gmail.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.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/360d4fa5-26f7-4b76-9421-3fd098038a80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.