Search code examples
pythonmethodsmayapymel

addAttr method on pymel objects


I can't seem to utilize the addAttr method correctly. I'm using the same arguments as when I call from pymel.core but it's not giving me the same results.

I'm trying to add a custom message attribute so I can easily search for certain types of objects later. When I do it from pymel.core and include the same object reference as an argument, it works fine.

#get object reference
test_object = pm.ls(sl=1)[0]

#this one spits out an error
test_object.addAttr(longName = 'custom', attributeType = 'message')

#this one works fine
pm.addAttr(test_object, longName = 'custom', attributeType = 'message')

I keep getting this error Error: TypeError: file line 1: addAttr() takes exactly 2 arguments (1 given) What additional argument is it looking for when I use it this way? I am clearly missing something obvious about how methods work but I can't figure it out.


Solution

  • The addAttr method exposed for DG nodes in Maya PyMel has following signature.

    addAttr(attr, **kwargs)
    

    Here attr is an positional argument representing the attribute name. The kwargs can be supplied with all other relevant flags used in pm.addAttr() method. So you have to pass the attribute name as first argument.

    node.addAttr('custom', attributeType='message')
    

    Hope this will help.