rykardo
rykardo

Reputation: 175

Force compute when transform node changes (Maya Plugin)

Using the attributeAffects function two attributes of a Maya node can be linked. For example an input attribute, x can be linked to an output attribute, y. This means that when x is changed, Maya will run a compute() callback function on the given node, to calculate y.

However, as far as I can tell, only attributes on the node itself can connected like this, from inside a plugin.

In my plugin I extend an MPxLocator, and make the output attribute, out. I want to do the following:

# replace <...> with transform node name.
attributeAffects(CustomNode.out, <custom node's tranform node>.translateX)
attributeAffects(CustomNode.out, <custom node's tranform node>.translateZ)

I can't find any docs on how to do this at all! Has anyone done it / know how? There is a way to hack this by doing the following in the script editor (python):

import maya.cmds as cmds

# Creates CustomNode1, which is linked to transform1 in the DG.
cmds.createNode("CustomNode") 
cmds.connectAttr("transform1.translateX", "CustomNode.out")
cmds.connectAttr("transform1.translateZ", "CustomNode.out")

Upvotes: 3

Views: 4305

Answers (1)

joojaa
joojaa

Reputation: 4434

A node by design should not know of other nodes. That is if the node has some data it needs then that data needs to be internal, or connected in when created or by the user. This is that causes Maya to be efficient. Its not that Maya actually enforces this but neglecting this design idea will make you very unhappy, because you fight Maya all the time (plus its more, extremely error prone, code to maintain).

So this leaves you with 2 options:

  • creating 2 input attributes and read those (make 3 while your at it, then you can just connect all of the translate). The bonus of this approach is that now any Maya user can do something you didn't expect with your node. This is how all other Maya nodes work. Most factory nodes supply a command for making the node so they can hook up the expected connections to rest of the nodes. Users wishing to do something else can always use createNode. So no it's not really a hack to do this, if you don't connect to the out attribute.
  • it so happens that the locator node already knows the transformation of its parent, in the form of the DAG node inherited attribute parentMatrix , now this is not the local coordinates of the parent but its in world space coordinates. Now this is also a connection that Maya makes but its a hidden connection type.*

Those are your only sane options. But if you really must you can also do what you ask but then you will be making your own event monitoring on top of the free performance optimizing one Maya supplies. Its a bit of extra code and much more debugging on your part, and it will be in all ways inferior.

PS: the reason why your hack works is that Maya evaluates the connections the opposite way they are made, so if you make the output dirty and Maya is asked to evaluate the output then Maya will fire compute

*Just like time, if you make a attribute named time of time type then Maya will connect it for you without asking and showing, unless you connect it to something else. Shading is mostly based on these connections.

Upvotes: 4

Related Questions