PDA

View Full Version : Python API: getting a list of attributes


Buexe
05-08-2007, 08:36 AM
hi, I would like to get a list of a node`s Attributes in a scripted plug-in. I searched the docs and examples, but have not found a nice solution yet. Would I do that with iterating over MFnDependencyNode`s attribute and attributeCount function? Or is there something more elegant. Hopefully this not too profane of a question :sad:
Thanks in advance!
b

EigenPuff
05-08-2007, 06:37 PM
I don't have the python API in front of me right now, but from what I've heard, you should still have access to mel from it, right? If so, listAttr should still work to give you every attribute under the sun.

Barring that, iterating from 0 to attributeCount() -1 is about as elegant as you are going to get. Welcome to plugins (^_^).

Buexe
05-08-2007, 08:01 PM
thanks for your reply, EigenPuff, seems a little bit odd that there is no equivalent in the API but anyway, here is what I came up with. The purpose is to list a node`s keyable attributes and find out which ones are animated and which ones not. Basically I have two arrays and want to substract them. Since I couldn`t get the MEL command stringArrayRemove I had to do it "manually":


import maya.cmds as cmds
import maya.mel as mm

animated = []
static = []

# Find animated Plugs
OpenMayaAnim.MAnimUtil.findAnimatedPlugs ( dagPath, plugs )

# Get animated attributes into a list
for i in range (0, plugs.length()):
animated.append( plugs[i].partialName(False, False, False, False, False, True) )

# list keyable Attributes
fullPath = dagPath.fullPathName()
keyable = cmds.listAttr( fullPath, k=True )

# get the keyable not animated attributes
for i in range (0, len (keyable)):
match = False
for j in range (0, len (animated)):
if animated[j] == keyable[i]:
match = True
if match == False:
static.append( keyable[i] )

# try to use MEL to get the same, but not this way...
unkeyed = mm.stringArrayRemove( animated, keyable )

But the last line does not work. I get the error message :AttributeError: 'module' object has no attribute 'stringArrayRemove'
So if anybody can think of more elegant ways, I`d really appreciate learning something about Python, which I`m a complete noob in. Thanks!

marcinm
05-08-2007, 08:30 PM
If you need difference between two lists you can use sets.


unkeyed = list(set(animated)-set(keyable))


I hope this is what you want.

Buexe
05-08-2007, 08:52 PM
This is what I`ve been searching for almost the whole day! :beer:
I bought a big book about Python, I googled, I even RTFM but this info did not cross my way. What I really find hard so far concerning basic Python stuff is when you enter your search keywords you get tons of pages with more or less useless results.
Anyway, thanks marcinm, this helps me a lot! :bounce:

marcinm
05-08-2007, 09:31 PM
I'm glad to be able to help. Below you have some info about sets in Python

http://www.python.org/doc/2.4.2/tut/node7.html#SECTION007400000000000000000 (http://www.python.org/doc/2.4.2/tut/node7.html#SECTION007400000000000000000)

Some info about sets you can find in Apress Beginning Python From Novice to Professional book. Look also for list comprehensions it's quite handy for manipulating data.

Buexe
05-08-2007, 10:04 PM
Cool stuff there, I`ll check it out, thanks marcinm!

CGTalk Moderation
05-08-2007, 10:04 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.