Saturday, August 1, 2015

Custom MPxCommand

Wow this API stuff is really cool. As I'm picking it up it is like learning a language. I'm getting more fluent and understanding the power of it more and more. So I have written a few dependencyNodes now, so I thought I'd make a custom MPxCommand.

I built a command that searches the dependencyGraph for a given node type. You specify the node type to search for and the direction to search in. YOu then call it just as though it is another maya command. So rad as this can now be built into my code as if it were just part of maya.

So my command is: findConnectedNodeType. You call it like:

node = cmds.findConnectedNodeType('blendColors','up')

The return value is either False or the found node name. The command looks through the upstream dependencyGraph until the first instance of that node is found. It then stops the iteration and returns the node name.

Cool stuff I learnt.

To return from a MPxCommand you must use the public function of MPxCommand setResult.

self.setResult(return value goes here)

This comes at the end of the doIt() function.

Enumerators are also making a bit more sense now that I am using them more. For example I had to use MItDependencyGraph. You can specify the direction to search, up or down stream. The help docs show this for the direction.

MItDependencyGraph     (     MObject &      rootNode,
        MPlug &      rootPlug,
        MIteratorType &      infoObject,
        Direction      direction = kDownstream,
        Traversal      traversal = kDepthFirst,
        Level      level = kNodeLevel,
        MStatus *      ReturnStatus = NULL

and this:

enum Direction
Direction within the DG relative to root Node or Plug.
kDownstream  From source to destination.
kUpstream  From destination to source.
A bit cryptic at first if you arent familiar with how to read this. So To insert this correctly into the MItDependencyGraph class call you need to do this.


So because this enumerator is a child of the class MItDependencyGraph , it must be specified in context of this class hierarchy. (OpenMaya.MItDependencyGraph). You can then set any number of the enum options that the help displays, in this case kDownstream or kUpstream and the class call will know to look for that enum if you set it (you don't have to specify anything and maya will use the default)

Ok. Hope that is a bit clearer than mud. Till next time.


No comments:

Post a Comment