Python API 2.0: Custom Node no input plug ...

Python API 2.0: Custom Node no input plug ...

g2m.agent
Collaborator Collaborator
2,470 Views
2 Replies
Message 1 of 3

Python API 2.0: Custom Node no input plug ...

g2m.agent
Collaborator
Collaborator

I tried to modify this example to input a Float and output a String.

https://help.autodesk.com/view/MAYAUL/2020/CHS/?guid=__developer_Maya_SDK_MERGED_Maya_Python_API_May...

 

the core code is only one line long:

v = "%0.2f" % sampleInValue

 

but my node  has no input slot, don't know why

maya_krJPaXaNpE.png

 

All my modifications have been marked: (<------------------------------)

# pySampleDGNode.py

import sys

import maya.api.OpenMaya as OpenMaya
# ... additional imports here ...

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

# Plug-in information:
kPluginNodeName = 'myNodeName'              # The name of the node.
kPluginNodeClassify = 'utility/general'     # Where this node will be found in the Maya UI.
kPluginNodeId = OpenMaya.MTypeId( 0x87EFE ) # A unique ID associated to this node type.

# Default attribute values
sampleDefaultValue = 1

##########################################################
# Plug-in 
##########################################################
class myNode(OpenMaya.MPxNode):
    # Static variables which will later be replaced by the node's attributes.
    sampleInAttribute = OpenMaya.MObject()
    sampleOutAttribute = OpenMaya.MObject()

    def __init__(self):
        ''' Constructor. '''
        OpenMaya.MPxNode.__init__(self)

    def compute(self, pPlug, pDataBlock):
        '''
        Node computation method.
            - pPlug: A connection point related to one of our node attributes (could be an input or an output)
            - pDataBlock: Contains the data on which we will base our computations.
        '''

        #if( pPlug == myNode.sampleOutAttribute ):	<------------------------------------------------------------ cancel if/else

            # Obtain the data handles for each attribute
        sampleInDataHandle = pDataBlock.inputValue( myNode.sampleInAttribute )
        sampleOutDataHandle = pDataBlock.outputValue( myNode.sampleOutAttribute )

            # Extract the actual value associated to our sample input attribute (we have defined it as a float)
        sampleInValue = sampleInDataHandle.asFloat()

            # ... perform the desired computation ...             
        v = "%0.2f" % sampleInValue #v = sampleOutDataHandle.asString()	<----------------------------------------format a float to string ----------------

            # Set the output value.
            #sampleOutDataHandle.setFloat( sampleInValue ) #  As an example, we just set the output value to be equal to the input value.
        sampleOutDataHandle.setString( v )	#<------------------------------------------------------------------ set output value

            # Mark the output data handle as being clean; it need not be computed given its input.
        sampleOutDataHandle.setClean()

        #else:
         #   return OpenMaya.kUnknownParameter			<------------------------------------------------------------ cancel if/else

##########################################################
# Plug-in initialization.
##########################################################
def nodeCreator():
    ''' Creates an instance of our node class and delivers it to Maya as a pointer. '''
    return  myNode() 

def nodeInitializer():
    ''' Defines the input and output attributes as static variables in our plug-in class. '''

    # The following MFnNumericAttribute function set will allow us to create our attributes.
    numericAttributeFn = OpenMaya.MFnNumericAttribute()
    stringAttributeFn = OpenMaya.MFnTypedAttribute()	# <-------------------------------------------------------- i want to output port is String

    #==================================
    # INPUT NODE ATTRIBUTE(S)
    #==================================
    global sampleDefaultValue
    myNode.sampleInAttribute = numericAttributeFn.create( 'myInputAttribute', 'i', 
                                                          OpenMaya.MFnNumericData.kFloat,
                                                          sampleDefaultValue )
    numericAttributeFn.writable = True 
    numericAttributeFn.storable = True 
    numericAttributeFn.hidden = False 
    myNode.addAttribute( myNode.sampleInAttribute ) # Calls the MPxNode.addAttribute function.

    #==================================
    # OUTPUT NODE ATTRIBUTE(S)
    #==================================
    #myNode.sampleOutAttribute = numericAttributeFn.create( 'myOutputAttribute', 'o', OpenMaya.MFnNumericData.kFloat )
    myNode.sampleOutAttribute = stringAttributeFn.create('myOutputAttribute', 'o', OpenMaya.MFnData.kString) # <------------ create output port
    
    numericAttributeFn.storable = False 
    numericAttributeFn.writable = False
    numericAttributeFn.readable = True 
    numericAttributeFn.hidden = False 
    myNode.addAttribute( myNode.sampleOutAttribute )

    #==================================
    # NODE ATTRIBUTE DEPENDENCIES
    #==================================
    # If sampleInAttribute changes, the sampleOutAttribute data must be recomputed.
    myNode.attributeAffects( myNode.sampleInAttribute, myNode.sampleOutAttribute )


def initializePlugin( mobject ):
    ''' Initialize the plug-in '''
    mplugin = OpenMaya.MFnPlugin( mobject )
    try:
        mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator,
                              nodeInitializer, OpenMaya.MPxNode.kDependNode, kPluginNodeClassify )
    except:
        sys.stderr.write( 'Failed to register node: ' + kPluginNodeName )
        raise

def uninitializePlugin( mobject ):
    ''' Uninitializes the plug-in '''
    mplugin = OpenMaya.MFnPlugin( mobject )
    try:
        mplugin.deregisterNode( kPluginNodeId )
    except:
        sys.stderr.write( 'Failed to deregister node: ' + kPluginNodeName )
        raise

 

Accepted solutions (1)
2,471 Views
2 Replies
Replies (2)
Message 2 of 3

stuzzz
Collaborator
Collaborator

I guess that the attribute has failed silently. I Believe the python API has no MStatus (which check the function validation). It might worth raise exception while creating the attributes (try/execption) to see where is the problem.

Sometimes an attribute is not created because its name is in conflict with another one. What if you replace the shortname "i" with "foo" (or whatever)?

Message 3 of 3

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

Here are some tips for attributes not displayed in node editor.

 

https://forums.autodesk.com/t5/maya-programming/custom-node-s-plug-not-displayed-in-node-editor/m-p/...

 

Hope it helps.

 

Yours,

Li