Anvesh Chary
Anvesh Chary

Reputation: 65

how can i create vector type of attributes in maya python api?

i am very new to api, recently i learned how to create a node then i want to create 3 attributes which should be parent to one attribute like when we create vector attribute via "add attribute" in maya then we get 3 attributes(x,y,x) and they will be parent to one attribute. so, then how can i create them? am working with this code but i cant get what am expecting.

nAttr = OpenMaya.MFnNumericAttribute()
cAttr = OpenMaya.MFnCompoundAttribute()

    node.old = cAttr.create('oldValue', 'old')
    node.oldX =nAttr.create('oldValueX', 'oldX', OpenMaya.MFnNumericData.kFloat)
    node.oldY =nAttr.create('oldValueY', 'oldY', OpenMaya.MFnNumericData.kFloat)
    node.oldZ =nAttr.create('oldValueZ', 'oldZ', OpenMaya.MFnNumericData.kFloat)
    cAttr.setArray(True)
    cAttr.addChild(node.oldX)
    cAttr.addChild(node.oldY)
    cAttr.addChild(node.oldZ)
    cAttr.setKeyable(True)
    node.addAttribute(node.old)

thank you...

Upvotes: 0

Views: 1940

Answers (1)

theodox
theodox

Reputation: 12208

You're on the right track, it's just a little wonky. You create a compound attribute, then the child attributes, add all of these to the node class and then add the child attributes to the compound.

compound = OpenMaya.MFnCompoundAttribute()
node.target = compound.create("target", "t")

xv = OpenMaya.MFnUnitAttribute()
node.inTargetX = xv.create("targetTranslateX", "ttx", OpenMaya.MFnUnitAttribute.kDistance)
xv.setStorable(1)
xv.setWritable(1)
compound.addChild(node.inTargetX)

yv = OpenMaya.MFnUnitAttribute()
node.inTargetY = xv.create("targetTranslateY", "tty", OpenMaya.MFnUnitAttribute.kDistance)
yv.setConnectable(1)
yv.setStorable(1)
yv.setWritable(1)
compound.addChild(node.inTargetY)

zv = OpenMaya.MFnUnitAttribute()
node.inTargetZ =  xv.create("targetTranslateZ", "ttz", OpenMaya.MFnUnitAttribute.kDistance)
zv.setConnectable(1)
zv.setStorable(1)
zv.setWritable(1)
compound.addChild(node.inTargetZ)

# add to the node
node.addAttribute(node.target)

Are you remembering to use attributeAffects() to set up the dependencies between attributes? Also, you probably want to use a MFnUnitAttribute.kDistance rather than a kFloat value for a vector attribute that represents a spatial position.

Other than that, what exactly do you mean by "not getting what you expect?"

Upvotes: 1

Related Questions