Query the type of an attribute, consistently?

1,416 views
Skip to first unread message

Paul Molodowitch

unread,
Jul 1, 2010, 3:24:02 PM7/1/10
to python_inside_maya
Hey all - I was wondering if anyone here knew of a way to query the
'type' (ie, similar to what getAttr('foo.bar', type=True) would
return) of an attribute, for ANY attribute. Specifically, I can't
seem to find ANY way to query the type of multi-compound attribute's
children, when the multi does not have any indices created yet.

To see what I mean:

import maya.cmds as cmds

def getType(node):
print "%-50s :" % node,
try:
print cmds.getAttr(node, type=True)
except Exception:
print "<ERROR>"

getType(u'persp.publishedNodeInfo')
getType(u'persp.publishedNodeInfo.publishedNode')
getType(u'persp.publishedNode')
getType(u'persp.publishedNodeInfo[-1].publishedNode')

getType(u'persp.instObjGroups.objectGroups')
getType(u'persp.instObjGroups.objectGroups.objectGroupId')
getType(u'persp.objectGroupId')

Is there another way to query the type of an attribute other than
getAttr? attributeQuery doesn't seem to have any relevant flags, and I
couldn't find any way to access this information from the API.

Any ideas?

- Paul

Judah Baron

unread,
Jul 1, 2010, 4:55:05 PM7/1/10
to python_in...@googlegroups.com
I think the safest thing to do is to use the attribute's mobject.apiType() func to get the id. It can be pretty complex though because for the non-standard types, such as string you have to do multiple lookups. For instance, a string type will return you a type id of 562, for kTypedAttribute, not exactly a string type. Then you need to create an MFnTypedAttribute so you can ask it what its actual type is. You can create a base type attribute function set, MFnAttribute, to query any attribute about any of the shared settings, such as isArray, etc.

I'll stop there because I'm not sure if this is of use to you, Paul.

-Judah



Paul Molodowitch

unread,
Jul 1, 2010, 7:16:43 PM7/1/10
to python_in...@googlegroups.com
Unfortunately, what I ultimately need to do is get results similar to
those returned by getAttr(type=1)... this is for the implementation of
PyNode.type(), which has been using getAttr... but I've found it
errors out on the sort of attributes I mentioned.

I may ultimately have to do some sort of branched logic similar to
what you suggest, and try to find out all the possible types returned
by getAttr, and implement my own mapping to them, etc... but I'd like
to avoid that if at all possible...

- Paul

> --
> http://groups.google.com/group/python_inside_maya

philippe leprince

unread,
Jul 2, 2010, 4:33:15 AM7/2/10
to python_in...@googlegroups.com
the maya commands are not fully implemented and I always use the API
to reliably find an attribute's type :


attrObj = attrPlug.attribute()
if attrObj.hasFn( om.MFn.kNumericAttribute ):
fnAttr = om.MFnNumericAttribute(attrObj)
realtype = fnAttr.unitType()
if realtype == om.MFnNumericData.kBoolean:
...
elif realtype == om.MFnNumericData.kByte:
...
elif realtype == om.MFnNumericData.kChar:
...
elif realtype == om.MFnNumericData.kShort:
...
elif realtype == om.MFnNumericData.kLong:
...
elif realtype == om.MFnNumericData.kFloat:
...
elif realtype == om.MFnNumericData.kDouble:
...
elif realtype == om.MFnNumericData.k3Float:
...
elif realtype == om.MFnNumericData.k3Double:
...
else:
raise drh.Error( '[ MayaAttr ] Unknown
kNumericAttribute : %s.%s.%s' % (node,attr,subattr) )
elif attrObj.hasFn( om.MFn.kTypedAttribute ):
fnAttr = om.MFnTypedAttribute(attrObj)
realtype = fnAttr.attrType()
if realtype == om.MFnData.kString:
...
if realtype == om.MFnData.kStringArray:
...
if realtype == om.MFnData.kDoubleArray:
...
if realtype == om.MFnData.kIntArray:
...
else:
m = '[ MayaAttr ] Unknown kTypedAttribute :
%s.%s.%s' % (node,attr,subattr)
raise drh.Error( m )
elif ...

Further to that, you would have to make sure you are not looking at a
compound attr. If so, get the children plugs and run that sort of code...

philippe

Paul Molodowitch

unread,
Jul 2, 2010, 12:53:13 PM7/2/10
to python_in...@googlegroups.com
Hmm... well, seems like this is the way I'll have to go. Thanks for
the help, the template code will provide a place to start, at least!

- Paul

> --
> http://groups.google.com/group/python_inside_maya

Reply all
Reply to author
Forward
0 new messages