Maya Python API - Custom node with string as input/output

python
maya

#1

Hi all,

I have been delving into the Maya API and have come into a bit of a block on how to move forward, hoping that someone could help me out.

What I am trying to accomplish is to make a custom node that takes a string as the input and outputs that string data. In practicality, a file path as an input and I would like to output that file path to another node.

This is the code that I have currently. Any and all help will be appreciated!

I look forward hearing what everyone has to say.
Thanks in advance!

> import sys
> import maya.api.OpenMaya as om
> import maya.OpenMayaMPx as omMPx
> 
> nodeName = "Custom_Node"
> nodeID = om.MTypeId(0x100fff)
> 
> class CustomNode(omMPx.MPxNode):
> 
>     input = om.MObject()
>     output = om.MObject()
> 
>     def __init__(self):
>         omMPx.MPxNode.__init__(self)
> 
>     def compute(self, plug, dataBlock):
>         print "Computing"
> 
>         #dataBlock.asString()
>         #dataBlock.setString()
> 
> def nodeCreator():
>     return omMPx.asMPxPtr(CustomNode())
> 
> def nodeInitializer():
> 
>     # Create function set for string attribute
>     mFnStringAttr = om.MFnTypedAttribute()
> 
>     # Create Attributes - Input and Output
>     CustomNode.input = mFnStringAttr.create("input", "i", om.MFnData.kString)
>     CustomNode.output = mFnStringAttr.create("output", "o", om.MFnData.kString)
> 
>     # Attaching the Attributes
>     CustomNode.addAttribute(CustomNode.input)
>     CustomNode.addAttribute(CustomNode.output)
> 
>     # Node Relationships
>     CustomNode.attributeAffects(CustomNode.input, CustomNode.output)
> 
> # Initialize the script plugin
> def initializePlugin(mobject):
> 
>     mPlugin = omMPx.MFnPlugin(mobject)
>     try:
>         mPlugin.registerNode(nodeName, nodeID, nodeCreator, nodeInitializer)
>     except:
>         sys.stderr.write("Failed to register command: {0}".format(nodeName))
> 
> # Uninitialize the script plugin
> def uninitializePlugin(mobject):
> 
>     mPlugin = omMPx.MFnPlugin(mobject)
>     try:
>         mPlugin.deregisterCommand(nodeName)
>     except:
>         sys.stderr.write("Failed to unregister command: {0}".format(nodeName))

#2

So I have answered my own question, after playing around a little bit I have a version of this custom node working. For some reason the code doesnt like the Maya API 2.0? (maya.api.OpenMaya)

For anyone who is interested here’s the code that I managed to get working:

# -------------------------------------------------------------------------------------------------------------------- #

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

# ==================================================================================================================== #
# Custom Node
# ==================================================================================================================== #

# Basic Parameters for node creation
nodeName = "jwCustomNode"
nodeID = OpenMaya.MTypeId(0x100fff)

# -------------------------------------------------------------------------------------------------------------------- #

# Class to create Custom Node
class CustomNode(OpenMayaMPx.MPxNode):
    def __init__(self):
        OpenMayaMPx.MPxNode.__init__(self)

# -------------------------------------------------------------------------------------------------------------------- #

def nodeCreator():
    return OpenMayaMPx.asMPxPtr(CustomNode())

# -------------------------------------------------------------------------------------------------------------------- #

def nodeInitializer():

    # Get Input Path
    # Create function set for input string attribute
    input_path_string = OpenMaya.MFnStringData().create("<FilePathIn>")
    input_directory_attribute = OpenMaya.MFnTypedAttribute()
    CustomNode.input_directory = input_directory_attribute.create("File_Input", "fileIn", OpenMaya.MFnData.kString,
                                                                  input_path_string)
    # Attaching input Attributes
    CustomNode.addAttribute(CustomNode.input_directory)

    # Get Output Path
    # Create function set for output string attribute
    output_path_string = OpenMaya.MFnStringData().create("<FilePathOut>")
    output_directory_attribute = OpenMaya.MFnTypedAttribute()
    CustomNode.output_directory = output_directory_attribute.create("File_Output", "fileOut", OpenMaya.MFnData.kString,
                                                                    output_path_string)
    # Attaching output Attributes
    CustomNode.addAttribute(CustomNode.output_directory)

# -------------------------------------------------------------------------------------------------------------------- #

# Initialize the script plugin
def initializePlugin(mobject):
    mPlugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mPlugin.registerNode(nodeName, nodeID, nodeCreator, nodeInitializer)
    except:
        sys.stderr.write("Failed to register node: {}".format(nodeName))

# -------------------------------------------------------------------------------------------------------------------- #

# Uninitialize the script plugin
def uninitializePlugin(mobject):
    mPlugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mPlugin.deregisterNode(nodeID)
    except:
        sys.stderr.write("Failed to deregister node: {}".format(nodeName))

# -------------------------------------------------------------------------------------------------------------------- #