Reputation: 3
I've been looking around the internet for a solution but to no avail. I am currently trying to add a compound attribute that is index based (i.e. "object.attribute[0], object.attribute[1], object.attribute[2], etc...) similar to how vertex and uv attributes are used. Looking through the documentation it seems that there is no clear way to achieve this.
Attempts:
How I define the parent: cmds.addAttr(nodeType, ln=theParent, nc=x, at='compound')
-Usual use of the compound flag in addAttr.
-Using a for loop with string formatting:
for i in range(x):
cmds.addAttr(ln='object.attribute[%s]' %i, p=theParent)
-Eval:
for i in range(x):
mel.eval("addAttr -ln attribute["+str(i)+"] -p theParent;")
With string formatting I run into the this error.
# Traceback (most recent call last):
# File "<maya console>", line 2, in <module>
# RuntimeError: Error occurred during execution of MEL script
# line 1: Long name 'attribute[0]' contains invalid characters. //
This compound attribute will eventually hold an arbitrary number of Int32Array data types.
I could create my own node and create the necessary attributes through the API but I don't want to create any additional dependencies.
I apologize for any holes in my question or if something is unclear. Kindly ask and I can explain further.
Thank you.
Upvotes: 0
Views: 3679
Reputation: 12208
Set the parent attribute to be a multi attribute using the -m
flag:
cmds.addAttr("polyCube1", ln = "example", at="compound", nc = 2, m=True)
cmds.addAttr("polyCube1", ln = "atx", at="float", p="example")
cmds.addAttr("polyCube1", ln = "aty", at="float", p="example")
This sets polyCube1.compound to be a two-part attribute (the way UV is a two-parter) and also a multi-attribute. You can add pairs by indexing your setAttr
or connections:
cmds.setAttr("polyCube1.example[0].atx", 1)
cmds.setAttr("polyCube1.example[0].aty", 1)
cmds.setAttr("polyCube1.example[1].atx", 2)
cmds.setAttr("polyCube1.example[1].aty", 2)
print cmds.getAttr("polyCube1.example[1].aty")
# 2.0
Upvotes: 0