MPlug - Accessing plug from plug array

Hey All,

Hopefully this will be a simple one - it feels like it should be so its most likely me missing something obvious.

If I have a node wihch has an array attribute on it, how (through the API) do I then initialise a plug within that array? Lets say my attribute has zero incoming connections but I want to connect something into element 10 for instance. I am struggling to find a way to initialise an MPlug.

I have added this repro code. It will create a locator, add an array attribute and for the sake of illustration I end by trying to get as plug by physical index - which fails because no ‘sub’ plugs are initialised.


from maya.api import OpenMaya

# -- Create a node...
new_node = OpenMaya.MFnDagNode()
new_node.create("locator")

# -- Add an array attribute
message_fn = OpenMaya.MFnMessageAttribute()
new_msg_attr = message_fn.create("anExample", "ane")
message_fn.array = True
new_node.addAttribute(new_msg_attr)

# -- How do i get the plug (or create the plug) of element 10?
plug = new_node.findPlug("anExample", True)
sub_plug = plug.elementByPhysicalIndex(0)  # -- This line fails because there is no element at that index... how do i make one?

# -- How do i get a plug representing locator1.anExample[10] etc?

The equivilent mel/python code would look like this:

connectAttr -f locatorShape1.message locator1.anExample[10];

And that works, initialising the plug at index 10. I could get around it by using ‘commandToExecute’ to run the mel command, but I would rather not. Given than the way mel exposes it works, i am certain there must be a way to do this through the API!

Thanks

Mike.

Try changing “elementByPhysicalIndex” to “elementByLogicalIndex”.

sub_plug = plug.elementByLogicalIndex(10)
1 Like

Thanks for that - worked like a charm!