0

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.

1 Answers1

0

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
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Ah, thank you so much for the prompt answer! I had a feeling it was right under my nose. I glossed over the flag thinking it would not be relevant. I do have one more question, however. How would you initialize the attribute through the index itself instead of it being a child? Thanks again! – Christopher Magno Mar 10 '15 at 23:19
  • In this example the attributes will initialize to a single instance of the attribute; to add more you setAttr to the index you need. If the attribute you're adding is just a simple numeric grouping, like a float3 or a double3, you don't need to make it a compound - you can just use the m flag. If you do it that way you can't edit or connect to the parts (the x, y or z for example) separately - you have to get and set as a group – theodox Mar 10 '15 at 23:34