pm.hasAttr(obj,'getShape') not working, even though obj.getShape() works...

157 views
Skip to first unread message

Brian Eyre

unread,
Jun 17, 2014, 6:08:12 PM6/17/14
to python_in...@googlegroups.com
Hi,

I'm trying to add a line that error checks for whether the getShape() method exists for selected objects, by using hasAttr, but it keeps returning false even when getShape() exists for the obj:

import maya.cmds as cmd
import pymel.core as pm
   
list = cmd.ls(sl=True)

for obj in list:
    obj = pm.PyNode(obj)
    print pm.hasAttr(obj,'getShape')


Any ideas??


   



Kurian O.S

unread,
Jun 17, 2014, 6:20:19 PM6/17/14
to python_in...@googlegroups.com
hasAttr will check only for existing attribute.

import maya.cmds as cmd

import pymel.core as pm

list = cmd.ls(sl=True)

for obj in list:

    obj = pm.PyNode(obj)

    print pm.hasAttr(obj,'tx')


This will work and what is getShape ?, is it a function ? or is it attribute which is existed in selected object ?


--
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/0abb0814-ff47-42a9-9464-5ce2f856dbee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--:: Kurian ::--

Kurian O.S

unread,
Jun 17, 2014, 6:22:27 PM6/17/14
to python_in...@googlegroups.com

Brian Eyre

unread,
Jun 17, 2014, 6:24:44 PM6/17/14
to python_in...@googlegroups.com
It's actually a function that returns the shape node, yes.  Do I need a different syntax for that?  obj.getShape() definitely works, which is why i'm puzzled.


On Tuesday, June 17, 2014 3:20:19 PM UTC-7, Kurian wrote:
hasAttr will check only for existing attribute.

import maya.cmds as cmd

import pymel.core as pm

list = cmd.ls(sl=True)

for obj in list:

    obj = pm.PyNode(obj)

    print pm.hasAttr(obj,'tx')


This will work and what is getShape ?, is it a function ? or is it attribute which is existed in selected object ?
On Tue, Jun 17, 2014 at 3:08 PM, Brian Eyre <brian...@googlemail.com> wrote:
Hi,

I'm trying to add a line that error checks for whether the getShape() method exists for selected objects, by using hasAttr, but it keeps returning false even when getShape() exists for the obj:

import maya.cmds as cmd
import pymel.core as pm
   
list = cmd.ls(sl=True)

for obj in list:
    obj = pm.PyNode(obj)
    print pm.hasAttr(obj,'getShape')


Any ideas??


   



--
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_maya+unsub...@googlegroups.com.



--
--:: Kurian ::--

Kurian O.S

unread,
Jun 17, 2014, 6:27:16 PM6/17/14
to python_in...@googlegroups.com

import maya.cmds as cmd

import pymel.core as pm

list = cmd.ls(sl=True)

for obj in list:

    obj = pm.PyNode(obj)

    if obj.getShape():

        print obj.getShape()


Are you looking for this ?


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/1637e11d-8553-4b2d-a79a-43ae0c0cfc27%40googlegroups.com.

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



--
--:: Kurian ::--

Brian Eyre

unread,
Jun 17, 2014, 6:35:33 PM6/17/14
to python_in...@googlegroups.com
No, sorry.  That returns an error if obj happens to have no getShape() method, which is what i'm rying to check for...
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.



--
--:: Kurian ::--

--
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_maya+unsub...@googlegroups.com.



--
--:: Kurian ::--

Kurian O.S

unread,
Jun 17, 2014, 6:41:02 PM6/17/14
to python_in...@googlegroups.com
I didn't use pymel much, but I am not sure you can check a function with hasAttr. Maybe some one will have better idea, I would go for a AttributeError check with try.


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/dca48f87-4fa9-4f61-ab57-0182c9d6f16a%40googlegroups.com.

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



--
--:: Kurian ::--

Kurian O.S

unread,
Jun 17, 2014, 6:43:11 PM6/17/14
to python_in...@googlegroups.com

import maya.cmds as cmd

import pymel.core as pm

list = cmd.ls(sl=True)

for obj in list:

    obj = pm.PyNode(obj)

    print hasattr(obj,'getShape')

--
--:: Kurian ::--

Kurian O.S

unread,
Jun 17, 2014, 6:46:15 PM6/17/14
to python_in...@googlegroups.com
Just a note hasattr is not a pymel function its a python built in method. https://docs.python.org/2/library/functions.html#hasattr
--
--:: Kurian ::--

Brian Eyre

unread,
Jun 17, 2014, 6:54:33 PM6/17/14
to python_in...@googlegroups.com
Oh, that's brilliant, thanks!  Wasn't aware of 'try'.. so this seems to work:



import maya.cmds as cmd
import pymel.core as pm
   
list = pm.ls(sl=1)



for obj in list:
    obj = pm.PyNode(obj)
    try:
        print obj.getShape()
    except:
        pm.warning('no shape!')

If anyone knows why hasattr(obj,'getShape') does't work, please let me know!



--
--:: Kurian ::--

--
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_maya+unsub...@googlegroups.com.



--
--:: Kurian ::--

Kurian O.S

unread,
Jun 17, 2014, 6:58:43 PM6/17/14
to python_in...@googlegroups.com
I replied already I think :)

python built in hasattr will also work for you

import maya.cmds as cmd

import pymel.core as pm

list = cmd.ls(sl=True)

for obj in list:

    obj = pm.PyNode(obj)



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/3096bcdd-5f8e-44af-8f2b-fc6f396c4e91%40googlegroups.com.

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



--
--:: Kurian ::--

Brian Eyre

unread,
Jun 17, 2014, 7:19:06 PM6/17/14
to python_in...@googlegroups.com
Oh, weird,

I could have sworn i just  tried that and it didn't work.. but it works, thanks ;-)



--
--:: Kurian ::--

--
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_maya+unsub...@googlegroups.com.



--
--:: Kurian ::--

Justin Israel

unread,
Jun 17, 2014, 9:13:23 PM6/17/14
to python_in...@googlegroups.com

Sorry if this is late info but, the Maya hasAttr is meant to check for the existence of attributes on an object.  But these are not functions as everything in Maya command API are string references and you use function calls only to perform certain operations on string references 
Python has a similar hasattr which tells you if a python object has a certain member attribute  

In your case,  neither of these applied, as what you were after was just to try and call the PyMel getShape and handle the specific AttributeError

Hope that was clear enough? Let me know if you need more details on that 

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/1627dff8-1c2b-4dd9-974d-ce6512165ce1%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages