When writing an MPxNode which stores data internally using setInternalValueInContext that is connect to another node. Everything will work well until you save the scene and re-load the scene. It seems you have to force an update of the attribute of your node with a refresh, such as displaying the Attribute Editor.
The reason for this is the value that is maintain for an internal attribute is only used when the attribute does not have an incoming connection. If the attribute has an incoming connection then the connection determines the value of the attribute and your node has no say in the matter. To handle this, use MPxNode::connectionMade() and MPxNode::connectionBroke() to maintain a flag telling you whether the attribute is currently connected.
In Python, that would look something like this:
class myNode(maya.OpenMayaMPx.MPxNode):
myNode.myAttr = maya.OpenMaya.MObject()
def __init__(self):
self.myValue = 0.0
self.myAttrIsConnected = False
def connectionMade(self, myPlug, theirPlug, iAmSrc):
if not iAmSrc and myPlug == myNode.myAttr:
self.myAttrIsConnected = True
return ompx.MPxNode.connectionMade(self, myPlug, theirPlug, iAmSrc)
def connectionBroken(self, myPlug, theirPlug, iAmSrc):
if not iAmSrc and myPlug == myNode.myAttr:
self.myAttrIsConnected = False
return ompx.MPxNode.connectionBroken(self, myPlug, theirPlug, iAmSrc)
In the compute(), check the flag. If it's true then update the internal value from the datablock before using it:
def compute(self, plug, block):
if plug == myNode.someOutputAttr:
if self.myAttrIsConnected:
self.myValue = block.inputValue(myNode.myAttr).asFloat()
If you need the internal value outside the compute() then write yourself a helper method which gets the value from a plug when the flag is set and call that method as needed:
def get_myValue(self):
if self.myAttrIsConnected:
plug = maya.OpenMaya.MPlug(thisMObject(), myNode.myAttr)
self.myValue = plug.asFloat()
return self.myValue
Recent Comments