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_variable5
>>> 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?
​

On 23 March 2015 at 18:14, <[email protected]> wrote:

> Methods do no need to reference the module.
>
> pm.ls(type='camera')[0].getParent().getTranslation().z
>
> -brian
> www.meljunky.com
>
>  -------- Original Message --------
> Subject: [Maya-Python] Basic question about pyMel
> From: Simon Davies <[email protected]>
> Date: Mon, March 23, 2015 12:59 pm
> To: [email protected]
>
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/33fcd70f-b4c2-496f-b05d-a41196a428ca%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/33fcd70f-b4c2-496f-b05d-a41196a428ca%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/20150323111411.4b67a6573d13e8dd18091ea81788b1e6.4b8c60eefb.wbe%40email06.secureserver.net
> <https://groups.google.com/d/msgid/python_inside_maya/20150323111411.4b67a6573d13e8dd18091ea81788b1e6.4b8c60eefb.wbe%40email06.secureserver.net?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Marcus Ottosson*
[email protected]

-- 
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 [email protected].
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to