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 On Jun 17, 2014 6:19 PM, "Brian Eyre" <brianeyr...@googlemail.com> wrote: > Oh, weird, > > I could have sworn i just tried that and it didn't work.. but it works, > thanks ;-) > > On Tuesday, June 17, 2014 3:58:43 PM UTC-7, Kurian wrote: >> >> 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) >> >> print hasattr(obj,'getShape') >> >> https://docs.python.org/2/library/functions.html#hasattr >> >> >> On Tue, Jun 17, 2014 at 3:54 PM, Brian Eyre <brian...@googlemail.com> >> wrote: >> >>> 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! >>> >>> >>> >>> On Tuesday, June 17, 2014 3:41:02 PM UTC-7, Kurian wrote: >>> >>>> 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. >>>> >>>> >>>> On Tue, Jun 17, 2014 at 3:35 PM, Brian Eyre <brian...@googlemail.com> >>>> wrote: >>>> >>>>> No, sorry. That returns an error if *obj* happens to have no >>>>> getShape() method, which is what i'm rying to check for... >>>>> >>>>> >>>>> On Tuesday, June 17, 2014 3:27:16 PM UTC-7, Kurian wrote: >>>>> >>>>>> 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 ? >>>>>> >>>>>> >>>>>> On Tue, Jun 17, 2014 at 3:24 PM, Brian Eyre <brian...@googlemail.com> >>>>>> wrote: >>>>>> >>>>>>> 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+unsubscr...@googlegroups.com. >>>>>>>>> To view this discussion on the web visit >>>>>>>>> https://groups.google.com/d/msgid/python_inside_maya/0abb081 >>>>>>>>> 4-ff47-42a9-9464-5ce2f856dbee%40googlegroups.com >>>>>>>>> <https://groups.google.com/d/msgid/python_inside_maya/0abb0814-ff47-42a9-9464-5ce2f856dbee%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>>>>>> . >>>>>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> -- >>>>>>>> --:: 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+unsubscr...@googlegroups.com. >>>>>>> To view this discussion on the web visit >>>>>>> https://groups.google.com/d/msgid/python_inside_maya/1637e11 >>>>>>> d-8553-4b2d-a79a-43ae0c0cfc27%40googlegroups.com >>>>>>> <https://groups.google.com/d/msgid/python_inside_maya/1637e11d-8553-4b2d-a79a-43ae0c0cfc27%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>>>> . >>>>>>> >>>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>>> >>>>>> >>>>>> >>>>>> >>>>>> -- >>>>>> --:: 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+unsubscr...@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 >>>>> <https://groups.google.com/d/msgid/python_inside_maya/dca48f87-4fa9-4f61-ab57-0182c9d6f16a%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>> . >>>>> >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> >>>> >>>> -- >>>> --:: 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+unsubscr...@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 >>> <https://groups.google.com/d/msgid/python_inside_maya/3096bcdd-5f8e-44af-8f2b-fc6f396c4e91%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> --:: 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+unsubscr...@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 > <https://groups.google.com/d/msgid/python_inside_maya/1627dff8-1c2b-4dd9-974d-ce6512165ce1%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 python_inside_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3Vih_9BYm7CHEt6S%3Di-NxJLKR%3DDGwF0Y6vhfMUXAUc7g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.