I decided to convert the weightListNode.cpp example in the devkit into python, to get the elements to add to each parent element to work. Here is the code I used for anyone else is having the same problem or will have in the future.

def compute(self,plug,dataBlock):
        thisNode = self.thisMObject()
        wPlug = OpenMaya.MPlug( thisNode, weightListNode.aWeights )

        #Write into aWeightList
        for i in range(3):
            wPlug.selectAncestorLogicalIndex( i, weightListNode.aWeightsList )
            wHandle = wPlug.constructHandle(dataBlock)
            arrayHandle = OpenMaya.MArrayDataHandle( wHandle )
            arrayBuilder = arrayHandle.builder()
            for j in range( 5 ): #They used a formula to increment the element count, not needed
                handle = arrayBuilder.addElement( j )
                val = i+j
                handle.setFloat(val) #was changed from set due to compatibilty issues
                #end j loop <-- prevent formating proplems
            arrayHandle.set(arrayBuilder)
            wPlug.setMDataHandle(wHandle) #was changed from setValue due to compatibilty issues
            wPlug.destructHandle(wHandle)
            #end i loop <-- prevent formating proplems

In the example it used (line 187):
MDataHandle eHandle = arrayHandle.inputValue(&status).child(aWeights);

When I converted it, into python, it gave me an error. I changed aWeights to wPlug and it read the values with no problems.

Had to also add a another attribute, for the attribute affects to trigger.
attributeAffects(aBias, aWeightsList);
Was not enough to run the compute function.

If anyone else has anything to add, please respond.

-brian
www.meljunky.com


-------- Original Message --------
Subject: [Maya-Python] array plug array
From: meljunky <[email protected]>
Date: Fri, November 13, 2009 2:55 pm
To: python_inside_maya <[email protected]>


I am creating a array plug array and stuck at adding elements to the
array plug ("'sub' elements to each 'parent' element).


#When an gradient element is created, create a cooresponding weight
index
if plug.isElement() and plug.array() == averageWeightsNode.gradient:
weightBuilder = weightHandle.builder( )
weightElement = weightBuilder.addElementArray( plug.logicalIndex
() )
weightHandle.set(weightBuilder)

The code adds the new array element correctly but I can't seem to add
elements to the new added array. I tried to use before and after the
addElementArray, including after the 'set' which did not work.:

weightBuilder.setGrowSize(10)
weightBuilder.growArray(10)

I tried to create another builder which crashed Maya every time.

weightElementBuilder = weightElement.builder()

Which was mention at:
http://groups.google.com/group/python_inside_maya/browse_thread/thread/a55f2d3cbc92e902/5085c6781162243e?hl=en&lnk=gst&q=MArrayDataBuilder#5085c6781162243e

"Unfortunately this won't actually work because there is currently a
bug in the Python interface for MArrayDataBuilder such that the code
... will crash as soon as you try to retrieve the handle's builder. "

I tried after the weightHandle was set and got the following error:

weightHandle.jumpToElement( plug.logicalIndex() )
weightElementBuilder = weightHandle.outputArrayValue()

# weightElementBuilder = weightHandle.outputArrayValue()
# RuntimeError: (kInvalidParameter): Data is not an array #

Also tried:
weightElementBuilder = weightElement.outputArrayValue()

# weightElementBuilder = weightElement.outputArrayValue()
# RuntimeError: (kFailure): Object does not exist #

Both the array and its array are set:
cAttr.setArray(1)
cAttr.setReadable(1)
cAttr.setUsesArrayDataBuilder(1)



Thanks for any insight on getting an array plug array to added "sub"
elements correcty.




On a side note I found a strang bug when adding attributes
a...Node.gradient_FloatValue = nAttr.create( "gradient_FloatValue",
"gv", OpenMaya.MFnNumericData.kFloat, 0.0 )

Will automatically add the first element Gradient[0], now if I change
the line to gradient_floatValue it will not add the Gradient[0], when
its created. Well maybe its because the attribute name is the same as
the MObject? If I change gradient_FloatValue to gradient_FloatValues,
it will create the Gradient[0] again. If I keep it at
"gradient_FloatValue" and change the order when the attributes are
added as children to gradient, it will not add Gradient[0].

Not a true issue, I can deal with the attribute being named
gradient_floatValue since it does not compromise the plug-in.


-brian
www.meljunky.com


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to