Custom Node (moved from site feedback forum)

I am trying to create a custom node for stretchy setup in maya

after writing the plugin in Python I see some attribue function incorrectly

Here goes the code

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

class MeasureNode(OpenMayaMPx.MPxNode):
kPluginNodeId = OpenMaya.MTypeId(0x00000120)

pos1 = OpenMaya.MObject()
pos2 = OpenMaya.MObject()
midpoint = OpenMaya.MObject()
distance = OpenMaya.MObject()
distance2 = OpenMaya.MObject()
auto = OpenMaya.MObject()
outval = OpenMaya.MObject()

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

def compute(self, plug, data):
    p1 = data.inputValue(MeasureNode.pos1).asDouble3()
    p2 = data.inputValue(MeasureNode.pos2).asDouble3()
    p3 = data.inputValue(MeasureNode.auto).asFloat()
    
    mid = ((p1[0]+p2[0])/2, (p1[1]+p2[1])/2, (p1[2]+p2[2])/2)
    d = ((p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2)**.5
    d1 = ((p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2)**.5
    o = (d/2)*p3
                  
    aOutput = data.outputValue(MeasureNode.midpoint)
    aOutput.set3Double(mid[0], mid[1], mid[2])

    bOutput = data.outputValue(MeasureNode.distance)
    bOutput.setFloat(d)
    cOutput = data.outputValue(MeasureNode.distance2)
    cOutput.setFloat(d1)
            
    dOutput = data.outputValue(MeasureNode.outval)
    dOutput.setFloat(o)

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

def initialize():
nAttr = OpenMaya.MFnNumericAttribute()
MeasureNode.midpoint = nAttr.create(“midpoint”,“mid”, OpenMaya.MFnNumericData.k3Double,0.0)
nAttr.setWritable(True)
nAttr.setStorable(True)
MeasureNode.addAttribute(MeasureNode.midpoint)

MeasureNode.distance = nAttr.create("distance","dist", OpenMaya.MFnNumericData.kFloat,0.0)
nAttr.setWritable(True)

MeasureNode.addAttribute(MeasureNode.distance)

    
MeasureNode.distance2 = nAttr.create("distance2","dist2", OpenMaya.MFnNumericData.kFloat,1.0)
nAttr.setWritable(True)

MeasureNode.addAttribute(MeasureNode.distance2)
    
MeasureNode.outval = nAttr.create("outval","outv", OpenMaya.MFnNumericData.kFloat,0.0)
nAttr.setWritable(True)

MeasureNode.addAttribute(MeasureNode.outval)

    

MeasureNode.pos1 = nAttr.create("pos1","in1", OpenMaya.MFnNumericData.k3Double,0.0)
MeasureNode.addAttribute(MeasureNode.pos1)
MeasureNode.attributeAffects(MeasureNode.pos1, MeasureNode.midpoint)
MeasureNode.attributeAffects(MeasureNode.pos1, MeasureNode.distance)

MeasureNode.pos2 = nAttr.create("pos2","in2", OpenMaya.MFnNumericData.k3Double,0.0)
MeasureNode.addAttribute(MeasureNode.pos2)
MeasureNode.attributeAffects(MeasureNode.pos2, MeasureNode.midpoint)
MeasureNode.attributeAffects(MeasureNode.pos2, MeasureNode.distance)

MeasureNode.auto = nAttr.create("auto","in3", OpenMaya.MFnNumericData.kFloat,0.0)
MeasureNode.addAttribute(MeasureNode.auto)
MeasureNode.attributeAffects(MeasureNode.auto, MeasureNode.outval)

MeasureNode.attributeAffects(MeasureNode.distance, MeasureNode.outval)
MeasureNode.attributeAffects(MeasureNode.distance2, MeasureNode.outval)

def initializePlugin(obj):
plugin = OpenMayaMPx.MFnPlugin(obj, ‘measureMe’, ‘0.1’, ‘macka’)
try:
plugin.registerNode(‘measureMe’, MeasureNode.kPluginNodeId, creator, initialize)
except:
raise RuntimeError, ‘uh oh, initialization fail’

def uninitializePlugin(obj):
plugin = OpenMayaMPx.MFnPlugin(obj)
try:
plugin.deregisterNode(MeasureNode.kPluginNodeId)
except:
raise RuntimeError, ‘oh dear, this node does not want to unload’

o = (d/2)*p3…is the calculation for the output value
MeasureNode.attributeAffects(MeasureNode.auto, MeasureNode.outval)…auto value affects the outval…this works fine
MeasureNode.attributeAffects(MeasureNode.distance, MeasureNode.outval)…but distance does not affect the outval

My question is how to make auto and distance affect the output value?
Can u please help me out…

It looks like you lost all the formatting on your code.
Try placing it in some CODE blocks.

[QUOTE=R.White;31110]It looks like you lost all the formatting on your code.
Try placing it in some CODE blocks.[/QUOTE]

Hi,

thanks for replying

Please find the python file edited with Pycharm. Any help will be appreciated.

This file can be loaded in Maya plugins…

thanx,
Pla

Hey,

You are computing the distance(s), they aren’t ‘inputs’. And as such, they don’t affect the outputs (though, they are calculated during compute of the ‘final’ .outVal).

I think you want to just have them as additional outputs? Same goes for midPoint.

You probably also want to gate the compute for all those output plug names (test plug == outVal/distanceX/midPoint…, else pass back up to MPxNode).

Otherwise, you may want to split your node up in to a couple of extra ones (or do some internal caching of the pre-compute/shared data, but you need to be careful).

[QUOTE=campbell;31115]Hey,

You are computing the distance(s), they aren’t ‘inputs’. And as such, they don’t affect the outputs (though, they are calculated during compute of the ‘final’ .outVal).

I think you want to just have them as additional outputs? Same goes for midPoint.

You probably also want to gate the compute for all those output plug names (test plug == outVal/distanceX/midPoint…, else pass back up to MPxNode).

Otherwise, you may want to split your node up in to a couple of extra ones (or do some internal caching of the pre-compute/shared data, but you need to be careful).[/QUOTE]

Hi Campbell,

Thank you for your reply and making things clear. I am proceeding with the node and things seems to work fine. Can u tell me how to give a condition in the plugin. Mean to say to include something that will behave as a condition node. Shall I have to include it in compute function as well?

regards,
Plabon.