mtuttle
How does one force a computation for the output on a MPXNode when one of its attributes have changed, but not directly affected any of its other attributes?
Reply 0 0
JeremyR
It needs to "affect" the output attribute in question using "attributeAffects" in the initilize function.
Reply 0 0
mtuttle
Is using that a bit trickier when you have array attributes? That's what my node has and it doesn't want to compute even with "affributeAffects"...
Reply 0 0
JeremyR
ha, already out of my depth. I guess you have to make sure when you create the attribute that it is the right type. Its been a while since I looked at my API book so I dont know the exact type for an array.
Reply 0 0
mtuttle
Would anyone have the time to look at the code? It's a python plugin though....
Reply 0 0
pshipkov
you can copy/paste it here
Reply 0 0
mtuttle
So far, it looks like this:
import math,sys
import maya.OpenMaya as OM
import maya.OpenMayaMPx as MPx
import maya.OpenMayaAnim as Anim
nodeName = "circusDPNode"
nodeId = OM.MTypeId(0x00032331)
class circusDPNode(MPx.MPxNode):
aInTargetMatrix = OM.MObject()
aTargetSelector = OM.MObject()
aInConstraintMatrix = OM.MObject()
aOutOffsetTranslate = OM.MObject()
aOutOffsetTranslateX = OM.MObject()
aOutOffsetTranslateY = OM.MObject()
aOutOffsetTranslateZ = OM.MObject()
aOutOffsetRotate = OM.MObject()
aOutOffsetRotateX = OM.MObject()
aOutOffsetRotateY = OM.MObject()
aOutOffsetRotateZ = OM.MObject()
def compute(self,plug,data):
if (plug == self.aOutOffsetTranslate or plug == self.aOutOffsetRotate):
selector = data.inputValue(self.aTargetSelector)
parentIndex = selector.asInt()
if parentIndex == 1:
parent = data.inputValue(self.aInTargetMatrix)
parentMatrix = parent.asMatrix()
constraint = data.inputValue(self.aInConstraintMatrix)
constraintMatrix = constraint.asMatrix()
mOutMatrix = OM.MTransformationMatrix(parentMatrix.inverse() * constraintMatrix)
vOutPos = OM.MVector(mOutMatrix.getTranslation(OM.MSpace.kTransform))
vOutRot = OM.MEulerRotation(mOutMatrix.eulerRotation())
vTargetAxis *= parentMatrix
vConstraintAxiz *= constraintMatrix
vToTarget = OM.MVector(
parentMatrix(3,0) - constraintMatrix(3,0),
parentMatrix(3,1) - constraintMatrix(3,1),
parentMatrix(3,2) - constraintMatrix(3,2))
outputHandle = data.outputValue(self.aOutOffsetTranslate)
outputHandle.set3Double(vOutPos.x,vOutPos.y,vOutPos.z)
outputHandle = data.outputValue(self.aOutOffsetRotate)
outputHandle.set3Double(math.degrees(vOutRot.x),math.degrees(vOutRot.y),math.degrees(vOutRot.z))
data.setClean(self.aOutOffsetRotate)
data.setClean(self.aOutOffsetTranslate)
else:
return OM.MStatus()
else:
return OM.kUnknownParameter
return OM.MStatus.kSuccess
@classmethod
def createNode(cls):
return MPx.asMPxPtr(cls())
@classmethod
def initializeNode(cls):
mAttr = OM.MFnMatrixAttribute()
nAttr = OM.MFnNumericAttribute()
pAttr = OM.MFnMatrixAttribute()
oAttr = OM.MFnNumericAttribute()
uAttr = OM.MFnUnitAttribute()
cls.aInConstraintMatrix = mAttr.create("inConstraintMatrix","icm")
cls.aInTargetMatrix = pAttr.create("inTargetMatrix","itm")
cls.aTargetSelector = nAttr.create("parentSelector","ps",OM.MFnNumericData.kInt,0)
nAttr.setMin(0)
nAttr.setMax(1)
nAttr.setDefault(0)
cls.aOutOffsetTranslateX = oAttr.create("outOffsetTranslateX","ootx",OM.MFnNumericData.kDouble,0.0)
cls.aOutOffsetTranslateY = oAttr.create("outOffsetTranslateY","ooty",OM.MFnNumericData.kDouble,0.0)
cls.aOutOffsetTranslateZ = oAttr.create("outOffsetTranslateZ","ootz",OM.MFnNumericData.kDouble,0.0)
cls.aOutOffsetTranslate = oAttr.create("outOffsetTranslate","oot",cls.aOutOffsetTranslateX,cls.aOutOffsetTranslateY,cls.aOutOffsetTranslateZ)
cls.aOutOffsetRotateX = uAttr.create("outOffsetRotateX","oorx",OM.MFnUnitAttribute.kAngle,0.0)
cls.aOutOffsetRotateY = uAttr.create("outOffsetRotateY","oory",OM.MFnUnitAttribute.kAngle,0.0)
cls.aOutOffsetRotateZ = uAttr.create("outOffsetRotateZ","oorz",OM.MFnUnitAttribute.kAngle,0.0)
cls.aOutOffsetRotate = oAttr.create("outOffsetRotate","oor",cls.aOutOffsetRotateX,cls.aOutOffsetRotateY,cls.aOutOffsetRotateZ)
cls.addAttribute(cls.aInConstraintMatrix)
cls.addAttribute(cls.aInTargetMatrix)
cls.addAttribute(cls.aTargetSelector)
cls.addAttribute(cls.aOutOffsetTranslate)
cls.addAttribute(cls.aOutOffsetRotate)
cls.attributeAffects(cls.aInTargetMatrix,cls.aOutOffsetRotate)
cls.attributeAffects(cls.aInTargetMatrix,cls.aOutOffsetTranslate)
cls.attributeAffects(cls.aTargetSelector,cls.aOutOffsetTranslate)
cls.attributeAffects(cls.aTargetSelector,cls.aOutOffsetRotate)
def initializePlugin(mobject):
mplugin = MPx.MFnPlugin(mobject)
try:
mplugin.registerNode(nodeName,nodeId,circusDPNode.createNode,circusDPNode.initializeNode)
except:
sys.stderr.write("registerNode failed")
raise
def uninitializePlugin(mobject):
mplugin = MPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode(nodeId)
except:
sys.stderr.write("deregisterNode failed")
raise
Reply 0 0
mtuttle
To clarify, this is still an attempt at a "xform" node that would allow a user to control a constraint's offset with the position of the object that's being constrained, without cycling.
It would achieve it by only doing the computation once, when the node's "weight" attribute (currently a TargetSelector, because I was trying to control an array of offsets, which wasn't working) changes from 0 to 1...
Reply 0 0
pshipkov
Just to clarify - is not this already built-in the parentConstraint ? I am talking about the "update" buttons in the AE.
What i do is - turn-off the constraint - press the "update" button and then turn the constraint back on.

Is that the behavior you want to achieve ?

Reply 0 0
mtuttle
Yes, but you can't tell the animators in a studio that in order to seamlessly parent the "ikarm" to the "hips" when the arm was previously parented to some other component of a rig, they have to: select ikarm (that's being animated)-> find it's parent transform(that's been constrained to multiple objects) ->then find that transform's parentConstriantNode -> then switch from the channel editor (where they were animating) to the attribute editor->click the "update" button->THEN go back to the channel editor and change the targetWeight value on the parentConstraint via the ikarm's custom enum attribute (which was added to streamline the whole process in the first place)!
Reply 0 0
mtuttle
However, looking through the API docs, I found something interesting about the MPxNode class. It has for attributes a setInternal(True or False) value and a get/setInternalValueInContext function which appears to allow for calling functions when an attribute which has setInternal(True) on that node has been set...Does anyone know how to use this method? My plugin currently now is this (it currently also crashes maya when you attempt to set the weight attribute):
import math,sys
import maya.OpenMaya as OM
import maya.OpenMayaMPx as MPx
nodeName = "circusDPNode"
nodeId = OM.MTypeId(0x00032331)
class circusDPNode(MPx.MPxNode):
aInTargetMatrix = OM.MObject()
aTargetWeight = OM.MObject()
aInConstraintMatrix = OM.MObject()
aOutOffsetTranslate = OM.MObject()
aOutOffsetTranslateX = OM.MObject()
aOutOffsetTranslateY = OM.MObject()
aOutOffsetTranslateZ = OM.MObject()
aOutOffsetRotate = OM.MObject()
aOutOffsetRotateX = OM.MObject()
aOutOffsetRotateY = OM.MObject()
aOutOffsetRotateZ = OM.MObject()
def compute(self,plug,dataBlock):
#if (plug == self.aOutOffsetTranslate or plug == self.aOutOffsetRotate):
#weight = dataBlock.inputValue(self.aTargetWeight)
#wtVal = weight.asInt()
#if wtVal == 0 :
parent = dataBlock.inputValue(self.aInTargetMatrix)
parentMatrix = parent.asMatrix()
constraint = dataBlock.inputValue(self.aInConstraintMatrix)
constraintMatrix = constraint.asMatrix()
mOutMatrix = OM.MTransformationMatrix(parentMatrix.inverse() * constraintMatrix)
vOutPos = OM.MVector(mOutMatrix.getTranslation(OM.MSpace.kTransform()))
vOutRot = OM.MEulerRotation(mOutMatrix.eulerRotation())
vTargetAxis *= parentMatrix
vConstraintAxiz *= constraintMatrix
vToTarget = OM.MVector(
parentMatrix(3,0) - constraintMatrix(3,0),
parentMatrix(3,1) - constraintMatrix(3,1),
parentMatrix(3,2) - constraintMatrix(3,2))
outputHandle = dataBlock.outputValue(self.aOutOffsetTranslate)
outputHandle.set3Double(vOutPos.x,vOutPos.y,vOutPos.z)
outputHandle = dataBlock.outputValue(self.aOutOffsetRotate)
outputHandle.set3Double(math.degrees(vOutRot.x),math.degrees(vOutRot.y),math.degrees(vOutRot.z))
dataBlock.setClean(self.aOutOffsetRotate)
dataBlock.setClean(self.aOutOffsetTranslate)
#else: pass
#else:
#return OM.kUnknownParameter()
return OM.MStatus.kSuccess()
def setInternalValueInContext(self,plug,handle,ctx):
if plug == self.aTargetWeight:
weight = plug.inputValue(self.aTargetWeight)
if weight == 0:
return self.compute()
else:
return plug.inputValue()
@classmethod
def createNode(cls):
return MPx.asMPxPtr(cls())
@classmethod
def initializeNode(cls):
mAttr = OM.MFnMatrixAttribute()
nAttr = OM.MFnNumericAttribute()
pAttr = OM.MFnMatrixAttribute()
oAttr = OM.MFnNumericAttribute()
uAttr = OM.MFnUnitAttribute()
cls.aInConstraintMatrix = mAttr.create("inConstraintMatrix","icm")
mAttr.setStorable(True)
cls.aInTargetMatrix = pAttr.create("inTargetMatrix","itm")
pAttr.setStorable(True)
cls.aTargetWeight = nAttr.create("parentweight","pw",OM.MFnNumericData.kInt,0)
nAttr.setInternal(True)
nAttr.setChannelBox(True)
nAttr.setStorable(True)
nAttr.setMin(0)
nAttr.setMax(1)
nAttr.setDefault(1)
cls.aOutOffsetTranslateX = oAttr.create("outOffsetTranslateX","ootx",OM.MFnNumericData.kDouble,0.0)
cls.aOutOffsetTranslateY = oAttr.create("outOffsetTranslateY","ooty",OM.MFnNumericData.kDouble,0.0)
cls.aOutOffsetTranslateZ = oAttr.create("outOffsetTranslateZ","ootz",OM.MFnNumericData.kDouble,0.0)
cls.aOutOffsetTranslate = oAttr.create("outOffsetTranslate","oot",cls.aOutOffsetTranslateX,cls.aOutOffsetTranslateY,cls.aOutOffsetTranslateZ)
oAttr.setWritable(False)
oAttr.setStorable(False)
cls.aOutOffsetRotateX = uAttr.create("outOffsetRotateX","oorx",OM.MFnUnitAttribute.kAngle,0.0)
cls.aOutOffsetRotateY = uAttr.create("outOffsetRotateY","oory",OM.MFnUnitAttribute.kAngle,0.0)
cls.aOutOffsetRotateZ = uAttr.create("outOffsetRotateZ","oorz",OM.MFnUnitAttribute.kAngle,0.0)
cls.aOutOffsetRotate = oAttr.create("outOffsetRotate","oor",cls.aOutOffsetRotateX,cls.aOutOffsetRotateY,cls.aOutOffsetRotateZ)
oAttr.setWritable(False)
oAttr.setStorable(False)
cls.addAttribute(cls.aInConstraintMatrix)
cls.addAttribute(cls.aInTargetMatrix)
cls.addAttribute(cls.aTargetWeight)
cls.addAttribute(cls.aOutOffsetTranslate)
cls.addAttribute(cls.aOutOffsetRotate)
def initializePlugin(mobject):
mplugin = MPx.MFnPlugin(mobject)
try:
mplugin.registerNode(nodeName,nodeId,circusDPNode.createNode,circusDPNode.initializeNode)
except:
sys.stderr.write("registerNode failed")
raise
def uninitializePlugin(mobject):
mplugin = MPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode(nodeId)
except:
sys.stderr.write("deregisterNode failed")
raise
Reply 0 0
pshipkov
I think you can solve this without going to API level.
The described effect is called animation spaces.
Probably the simplest solution is to prepare the rig in an "anim spaces" friendly way and then give 2 little scripts to animators to put on their shelf.

So here is how the animControl hierarchy should look like:
grpArmCtrlZERO
   grpArmCtrlAUTO1 -> constrained to COG and GrpZERO above
      grpArmCtrlAUTO2 -> constrained to ChestCtrl and GrpZERO above
         grpArmCtrlAUTO3 -> constrained to ShoulderCtrl and GrpZERO above
            etc -> constrained to whatever else you want
               ArmCtrl

On the ArmCtrl node you have control attribute called animSpace of type enum that has the next item: None, COG, Chest, Shoulder, etc, etc
with a set driven keys or expression you drive the weight attributes of the grpAUTO constraints. So if i switch to anim space "COG" it will turn on the related weight and will turn off all other constraints.
This of course will make the arm pop out of position but that's ok, because usually animators use separate animLayer for the animation spaces swaps that they can keyframe by its value, or you can give them a little scriptie that will automatically calculate the compensations and put keyframes on the current and next frames with the proper values.
Most studios i know of use similar approach and it is fine.

Another way is to overload MPxTransform class and add to it inputWorldMatrix that you can connect to whatever other transform you want. This way you can directly "link" transform nodes and avoid the constraints mess. Also you can add "edit" mode when in it you can change connections and preserve the current TRS, this way you mimic parenting in place. This is simple but very useful approach to solve all sorts of problems including yours.

Reply 0 0
mtuttle
I don't exactly know how to do the last solution you have, but I like it a lot. I'm trying to write this plugin for entry-students at the college I go to, who may not know how to, or not be comfortable with using mel scripts or animation layers. It also sounds like it gets rid of any string troubles with mel scripts. Ironically, I've been trying to get the position via message attributes on a transform and attaining the matrix from that string value...All because the math behind having a array of matrix inputs seemed too intimidating..
Reply 0 0
mtuttle
So would it be a transform node plugin, or something of a "solver" node plugin whose outputs affect other transforms that are connected to it?
Reply 0 0
pshipkov
I think there is an example in the devkit about custom transform node.
If you start from there you will have to do only two things:
- add input matrix attribute
- locate in the ::compute method the right place to overwrite the final transformation with the input of your matrix attribute. this should be somewhere at the very bottom of the method.
- for "edit" mode you will need one more boolean type attribute that triggers it on/off T R S buffer attributes to store offsets. then you do one simple "if" statement like this:
if not in "edit" mode - get the matrix data from your inputWorldMatrix attribute and add to it the offset data from the T R S buffer attributes, overwrite the transformation data coming from the code above yours.
if in "edit" mode - only compare the inputWorldMatrix with the current transformation and write out the offsets in the buffer T R S attributes, later when you turn-off the edit mode these offsets will be added to the inputWorldMatrix and by this you get parenting in place.

I don't have time to give you practical examples, but ping me if you decide to explore this approach and hit a problem. I didn't look at your code you posted here, but will try to find some time for it soon.

Reply 0 0
Reply