PDA

View Full Version : Python node


lab00
02-14-2008, 10:33 AM
Hi all,
i'm trying to create in Python a node with 2 input vectors and 1 output vector.
The compute function does the vector sum between the 2 inputs and store
the result in the output vector. The problem is that it stores totally crazy and random
numbers inside the output vectors.
This is the code:
import math, sys

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

kPluginNodeTypeName = "testNode"
kPluginNodeId = OpenMaya.MTypeId(0x87003)

# Node definition
class testMe(OpenMayaMPx.MPxNode):
# class variables
aInput = OpenMaya.MObject()
bInput = OpenMaya.MObject()
aOutput = OpenMaya.MObject()

def __init__(self):
OpenMayaMPx.MPxNode.__init__(self)

def compute(self, plug, data):
# Check that the requested recompute is one of the output values
if (plug == testMe.aOutput):
# Read the input values
inputA = OpenMaya.MVector()
inputB = OpenMaya.MVector()
result = OpenMaya.MVector()

hValue = data.inputValue(testMe.aInput)
inputA = hValue.asVector()

hValue = data.inputValue(testMe.bInput)
inputB = hValue.asVector()

result = inputA + inputB

# Set the output values
hOutValue = data.outputValue(testMe.aOutput)
hOutValue.setMVector(result)
data.setClean(plug)
else:
return OpenMaya.MStatus.kUnknownParameter

return OpenMaya.MStatus.kSuccess

# creator
def nodeCreator():
return OpenMayaMPx.asMPxPtr( testMe() )


# initializer
def nodeInitializer():
nAttr = OpenMaya.MFnNumericAttribute()
# Setup the input attributes

testMe.aInput = nAttr.create("aInput", "aIn", OpenMaya.MFnNumericData.k3Float, 0.0 )
nAttr.setStorable(True)
nAttr.setKeyable(True)

testMe.bInput = nAttr.create("bInput", "bIn", OpenMaya.MFnNumericData.k3Float, 0.0 )
nAttr.setStorable(True)
nAttr.setKeyable(True)

testMe.aOutput = nAttr.create("output", "out", OpenMaya.MFnNumericData.k3Float, 0.0)
nAttr.setWritable(False)
nAttr.setStorable(False)

# Add the attributes to the node
testMe.addAttribute(testMe.aInput)
testMe.addAttribute(testMe.bInput)
testMe.addAttribute(testMe.aOutput)

# Set the attribute dependencies
testMe.attributeAffects(testMe.aInput, testMe.aOutput)
testMe.attributeAffects(testMe.bInput, testMe.aOutput)

def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.registerNode( kPluginNodeTypeName, kPluginNodeId, nodeCreator, nodeInitializer )
except:
sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
raise

# uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode( kPluginNodeId )
except:
sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
raise

I tried to change this line:
result = inputA + inputB
with this:
result = inputA
and with this too:
result = inputB

And in this two cases the resultant vector is the same as the input one.
So i believe the problem is in the vector sum...
result = inputA + inputB
I don't know...
Could anyone tell me where's the error?
Thanks

lab00
02-18-2008, 05:05 PM
Do anyone have some ideas about it?
Thank you

Sphere♥
02-18-2008, 08:15 PM
Hi lab00, I haven't looked closely, but quickly: "inputA = OpenMaya.MVector()" gets overridden by "inputA = hValue.asVector()". Try a "print type(inputA)" to see what you actually have after that, maybe it's not an OpenMaya.MVector() object?

pascal
02-20-2008, 01:10 PM
Hello,

It is probably because you define your vectors using floats and then use a MVector to retrieve/set the data.

You should either use MFloatVector, in place of MVector, in your compute method, or define your attributes using doubles instead of floats...

Pascal.

lab00
02-28-2008, 11:53 AM
I used doubles instead of floats to define attributes, and it works. Thank you !

CGTalk Moderation
02-28-2008, 11:53 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.