Array attributes in Maya Python API 2 MPxNode

Hello,

I’m trying to use array attributes for the first time and I’m sure I’m making some mistakes

The code below creates an array attribute with two children, the multiplier attribute multiplies the translation X of an output matrix attribute. It seems to work but there is no evaluation when set keys on the multiplier value and playback the animation. I’ve tried looking at the C++ documentation but I couldn’t find a useful example.

import sys
from maya.api import OpenMaya as om


def maya_useNewAPI():
    """
    The presence of this function tells Maya that the plugin produces, and
    expects to be passed, objects created using the Maya Python API 2.0.
    """
    pass


class ArrayTestNode(om.MPxNode):

    NODE_NAME = 'arrayTestNode'
    NODE_ID = om.MTypeId(0x00000001)

    array_test = None
    multiplier = None
    output_matrix = None

    @classmethod
    def creator(cls):

        return cls()

    @classmethod
    def initialize(cls):

        matrix_attr = om.MFnMatrixAttribute()
        numeric_attr = om.MFnNumericAttribute()
        compound_attr = om.MFnCompoundAttribute()

        cls.array_test = compound_attr.create('arrayTest', 'at')
        compound_attr.array = True
        compound_attr.disconnectBehavior = om.MFnAttribute.kDelete

        cls.multiplier = numeric_attr.create('multiplier', 'm', om.MFnNumericData.kDouble, 0.0)
        numeric_attr.keyable = True
        compound_attr.addChild(cls.multiplier)

        cls.output_matrix = matrix_attr.create('outputMatrix', 'om')
        matrix_attr.storable = False
        matrix_attr.writable = False
        compound_attr.addChild(cls.output_matrix)

        cls.addAttribute(cls.array_test)

        cls.attributeAffects(cls.multiplier, cls.output_matrix)

    def compute(self, plug, data_block):

        if plug != self.output_matrix:
            return

        array_test_data_handle_array = data_block.inputArrayValue(self.array_test)
        multiplers = []
        while not array_test_data_handle_array.isDone():
            multiplers.append(array_test_data_handle_array.inputValue().child(self.multiplier).asDouble())
            array_test_data_handle_array.next()

        matrix_values = [om.MMatrix(((1,0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (m, 0, 0, 1))) for m in multiplers]

        array_test_data_handle_array = data_block.outputArrayValue(self.array_test)
        counter = 0
        while not array_test_data_handle_array.isDone():
            array_test_data_handle_array.outputValue().child(self.output_matrix).setMMatrix(matrix_values[counter])

            counter += 1
            array_test_data_handle_array.next()

        data_block.setClean(plug)


def initializePlugin(plugin):

    plugin_fn = om.MFnPlugin(plugin)
    try:
        plugin_fn.registerNode(
        ArrayTestNode.NODE_NAME, ArrayTestNode.NODE_ID, ArrayTestNode.creator,
        ArrayTestNode.initialize)
    except:
        sys.stderr.write(f'Failed to register node: {ArrayTestNode.NODE_NAME}')


def uninitializePlugin(plugin):

    plugin_fn = om.MFnPlugin(plugin)
    try:
        plugin_fn.deregisterNode(ArrayTestNode.NODE_ID)
    except:
        sys.stderr.write(f'Failed to unregister node: {ArrayTestNode.NODE_NAME}')

Thanks,
-Harry

I randomly started browsing this site today so sorry for the exactly one month late reply :slightly_smiling_face:

I’m also somewhat new to the Maya API ( mostly working with C++ currently ). My personal favourite debugging method when I’m completely out of idea’s is to spam a bunch of print statements in order to start understanding what my code is actually doing.

adding print(plug.name()) at the start of the compute function gives us this:

currentTime 0 ;

arrayTestNode1.arrayTest

arrayTestNode1.arrayTest[0]

currentTime 1 ;

So we have proof that compute does run.

Now if we add print("Data set... (plug, value): ({0}, {1})".format(array_test_data_handle_array.outputValue().child(self.output_matrix).name(), matrix_values[counter])) right above where we setMMatrix, we can notice two things.

When we key the multiplier attribute we get:
# Error: AttributeError: file C:/…./Documents/maya/2026/plug-ins/weird_online_node.py line 68: ‘OpenMaya.MDataHandle’ object has no attribute ‘name’ #
*
and when we scrub the timeline we get:

currentTime 0 ;*

arrayTestNode1.arrayTest

arrayTestNode1.arrayTest[0]

currentTime 1 ;

So we’re not actually getting the plug using .child(self.output_matrix) and the while not array_test_data_handle_array.isDone(): is never reached when scrubbing through the timeline.

I don’t have enough time today to figure out how to fix it but I hope it helps! ( if you’re somehow still working on this after a month )

1 Like

Hi, thanks for replying :slight_smile: in the end I decided to created an output array attribute, rather than trying to have a compound array attribute that contains the inputs and output for each index (although I think the UI in the Attribute Editor looks nicer this way). For the output attribute, I set usesArrayDataBuilder to True