Custom node stringArray attribute

Custom node stringArray attribute

jan
Enthusiast Enthusiast
1,090 Views
3 Replies
Message 1 of 4

Custom node stringArray attribute

jan
Enthusiast
Enthusiast

Hi all! I've been banging my head against the desk all day over this one. I just cannot figure out how to output a string array from a python custom node.

 

I can initialize the plug:

 

tAttr = OpenMaya.MFnTypedAttribute()
myPlugin.outPath = tAttr .create('outPath', 'outPath', OpenMaya.MFnData.kStringArray)
tAttr .setWritable(False)
myPlugin.addAttribute(myPlugin.outPath)

 

 
But in my compute function I cannot figure out how to parse a string array as an output value. Eg for a string I would do this:

 

data.outputValue(self.outPath).setString("stringToOutput")

 

 

I thought I'd cracked it with this:

 

strArrayDataFn = OpenMaya.MFnStringArrayData()
strArrayDataFn.create(["STRING"])
strArray = strArrayDataFn.array()
data.outputArrayValue(self.outPath).setStringArray(strArray)

 

 

But I still get a "(kInvalidParameter): Data is not an array" result.

 

If someone could please point out what I'm missing here I would be eternally grateful!

 

Thanks,

Jan.

 

0 Likes
1,091 Views
3 Replies
Replies (3)
Message 2 of 4

stuzzz
Collaborator
Collaborator

I'm not sure about python but you would find a way to turn this from C++

You may a try the following instead:

 

MFnTypedAttribute typedFn;
myAttr = typedFn.create("longname", "shortname", MFnData::kString);
typedFn.setArray(true); 
typedFn.setUsesArrayDataBuilder(true);

 

0 Likes
Message 3 of 4

jan
Enthusiast
Enthusiast

Thanks for the response Stuzzzz....

 

Now, you'll have to forgive me if this question lays bare my fingernail grip of programming but the code you've shown me, doesn't that create the attribute on the custom node ready for connection to other nodes in the DG?

 

This bit I have managed. I can create a custom node with an output port of type stringArray, I can connect it to other ports of the same type. My problem is that I am incapable of writing output values to it within the plugin.

 

I am deeply confused. It appears at one point in history there was an object in openmaya called MStringArray which would appear to be what I need to pass to the output command. Something I imagine that is similar to:

data.outputArrayValue(self.outPath).setStringArray(MStringArrayObject)

 

I'm lost.

 

0 Likes
Message 4 of 4

jan
Enthusiast
Enthusiast

I'm certain I'm missing something blindingly obvious here as it seems so basic. So I thought I would post the whole plugin code as an example:

 

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

nodeId = OpenMaya.MTypeId(xxxxxxxxxx)
nodeType = 'amPath' 
 
class amPath(OpenMayaMPx.MPxNode):     
     
    def __init__(self): OpenMayaMPx.MPxNode.__init__(self)   
    
    def compute(self, plug, data):   
        
        #get string
        inString = data.inputValue(self.inString).asString()

        #output string
        data.outputValue(self.outString).setString(inString)

        #output string in an array
        data.outputArrayValue(self.outStringArray).setStringArray([inString])

def initialize():
    
    tAttr = OpenMaya.MFnTypedAttribute()
    
    '''INPUT'''
    amPath.inString = tAttr.create('inString', 'inString', OpenMaya.MFnData.kString)
    tAttr.setReadable(False)
    amPath.addAttribute(amPath.inString)    
    
    '''OUTPUT''' 
    amPath.outString = tAttr.create('outString', 'outString', OpenMaya.MFnData.kString)
    tAttr.setWritable(False)
    amPath.addAttribute(amPath.outString)

    amPath.outStringArray = tAttr.create('outStringArray', 'outStringArray', OpenMaya.MFnData.kStringArray)
    tAttr.setWritable(False)
    amPath.addAttribute(amPath.outStringArray)

    '''TRIGGER'''
    amPath.attributeAffects(amPath.inString, amPath.outString)
    amPath.attributeAffects(amPath.inString, amPath.outStringArray)

def creator(): return OpenMayaMPx.asMPxPtr(amPath()) 

def initializePlugin(obj):
    try: OpenMayaMPx.MFnPlugin(obj).registerNode(nodeType, nodeId, creator, initialize)
    except: pass
 
def uninitializePlugin(obj):
    try: OpenMayaMPx.MFnPlugin(obj).deregisterNode(nodeId)
    except: pass

 

Capture.JPG

 

setAttr -type "string" amPath1.inString "THIS IS A STRING";

print `getAttr amPath1.inString`;
THIS IS A STRING

print `getAttr amPath1.outString`;
THIS IS A STRING

print `getAttr amPath1.outStringArray`;
// Error: line 1: RuntimeError: file amPath.py line 24: (kInvalidParameter): Data is not an array //

 

0 Likes