API: MFnAttribute setArray; setting default array size?

API: MFnAttribute setArray; setting default array size?

ptorrevillas
Participant Participant
1,207 Views
1 Reply
Message 1 of 2

API: MFnAttribute setArray; setting default array size?

ptorrevillas
Participant
Participant

Hey Folks,

I am trying to create a plugin with an array of MFnEnumerated Attribute.
I want to be able to set the default array size upon initialization.

For Example:

import maya.OpenMayaMPx as OpenMayaMPx
import maya.OpenMaya as OpenMaya

class TestNode ( OpenMayaMPx.MPxNode 😞
    kPluginNodeId = OpenMaya.MTypeID ( 0x00000001 )
    
    out_color_attr   = OpenMaya.MObject()
    input_mode_attr  = OpenMaya.MObject()
    
    def __init__( self 😞
        OpenMayaMPx.MPxNode.__init__( self 😞
        
    def compute ( self, plug, data 😞
        if plug != TestNode.out_color_attr:
            return OpenMaya.MStatus.kUnknownParameter
        data.setClean( plug )
        return
        
def creator():
    return OpenMayaMPx.asMPxPtr( TestNode() )
    
def initialize():
    n_attr = OpenMaya.MFnNumericAttribute()
    e_attr = OpenMaya.MFnEnumAttribute()
    c_attr = OpenMaya.MfnCompoundAttribute()
    
    TestNode.out_color_attr = n_attr.createColor( "outColorR", "ocr")
    n_attr.setWritable(False)
    n_attr.setStorable(False)
    TestNode.addAttribute( TestNode.out_color_attr )


    TestNode.input_mode_attr = e_attr.create( "mode", "mde", 2 )
    e_attr.addField( "Add", 0 )
    e_attr.addField( "Subtract", 1 )
    e_attr.addField( "Multiply", 2 )
    e_attr.setKeyable(True)
    ### -----------------------------------------------------------------
    ### WHAT IS THE BEST WAY TO SET THE DEFAULT ARRAY SO IT ALREADY
    ### HAS 4 SETS OF ENUMERATED ITEMS ON INIT? IDEALLY THE USER WOULD
    ### END UP ADDING MORE LATER, BUT I WANT TO PROVIDE 4 AT INIT!
    ### -----------------------------------------------------------------    
    e_attr.setArray(True)
    ### -----------------------------------------------------------------
    TestNode.addAttribute( TestNode.input_mode_attr )
    TestNode.attributeAffects( TestNode.input_mode_attr, TestNode.out_color_attr )
    
def initializePlugin( obj 😞
    plugin = OpenMayaMPx.MFnPlugin( obj, "Test Author", "0.1", "Any" )
    
    try:
        plugin.registerNode( PLUGIN_NAME, TestNode.kPluginNodeId, creator, initialize )
    except:
        raise RuntimeError, "Failed to register node"

def uninitializePlugin( obj 😞
    plugin = OpenMayaMPx.MFnPlugin( obj )
    
    try:
        plugin.deregisterNode( TestNode.kPluginNodeId )
    except:
        raise RuntimeError, "Failed to register node"
    




Please Help!

Thanks!

0 Likes
1,208 Views
1 Reply
Reply (1)
Message 2 of 2

ptorrevillas
Participant
Participant

I was able to figure this out a couple hours after my original post using the postConstructor.

I made sure to set the value however since if the value isnt set and the defaults are used, maya will often not save the elements, so the next time the file is opened, the elements wont be there.


def postConstructor():
     plug = OpenMaya.MPlug( self.thisMObject(), TestNode.input_mode_attr)
     for i in range(4):
         element_plug = plug.elementByLogicalIndex( i )
         element_plug.setValue(0);

0 Likes