Creating Triggers¶
Zoo provides a framework to build you’re own marking menus and node based triggers.
This Page is going to walk you through the process of creating a custom marking menus and a selection trigger.
What is a Trigger¶
A trigger is a plugin which gets executed for a given node when the user either selects the node(s) or right clicks a node. When selection occurs for multiple nodes ie. cmds.select([node1, node2]) If there’s multiple nodes with a trigger attached then each trigger found will be executed one at a time so it’s very important to have as optimised code as possible and to avoid using selection triggers to run performance intensive code.
When a trigger is executed for a node, the type of trigger depends on the meta data applied to the node. There are 2 commonly used types.
A marking menu.
Selection trigger which can change the selection or can run some python code.
What you will learn¶
Attach a Trigger to any node for display a marking menu on rmb click.
Create a static marking menu via json.
Create a dynamic marking menu via python.
Create a selection trigger.
To access the code examples in this tutorials to can find them in the below folder inside the zoo_maya package.:
zoo_maya/examples/markingmenus/
Create a Selection Trigger¶
Since you now know how to create a marking menu lets move onto create a Selection Trigger.
What is a Selection trigger¶
A selection trigger is a plugin which gets executed for a given node when the user selects the node(s) When a trigger is executed for a node the type of trigger depends on the meta data applied to the node. There are 2 commonly used types.
A marking menu, What we just built above.
Selection trigger which can change the selection or can run some python code.
Lets cut to the end and show the code to attach a selection trigger which prints helloworld and selects a transform as a connectable.
from zoo.libs.maya import triggers
from zoo.libs.maya import zapi
from zoo.libs.maya.markingmenu import menu
triggerNode = zapi.selected(filterTypes=((zapi.kNodeTypes.kTransform, )))
selectableNode = zapi.createDag("selectable", "transform")
triggers.createSelectionTrigger(triggerNode,
commandStr='print("helloworld")',
connectables=[selectableNode],
triggerCommandId=None
)
To run the above test a new node in the scene, lets make it another sphere and select it then run the code.
If you now try selecting the new sphere you created the transform will be selected instead and “helloworld” will be printed as well.
It’s as simple as that when it comes to selection triggers.
Now you’re probably asking can you create you’re own selection trigger plugins and the answer is absolutely!. While it’s possible to just feed the code as a string into the commandStr argument but without a UI(which we’ve yet to build) to help with the string formatting it’s very error prone.
So lets create our own which simply prints all connectable nodes.
First create a new python file or just reuse our example under the examples/triggers example folder.
This is what the code looks like.
from zoo.libs.maya import triggers
class CustomSelectionTrigger(triggers.TriggerSelectionBase):
id = "customExampleTrigger"
def execute(self):
# at this point before or after super we can run any code.
print([i for i in self.connectedNodes()])
# by calling super we ensure the custom python command on the node gets executed
super(CustomSelectionTrigger, self).execute()
Once again we have the id class variable which we will add to the node on trigger creation. Then we simply re-implement the execute() function. Calling super here will use execute any python code passed through on creation via the commandStr argument.
Connectables is a list of nodes which are attached to the trigger node.
Now update the zoo_package.json file with the below.
"ZOO_TRIGGER_COMMAND_PATH": [
"{self}/examples/triggers"
]
Now do the following.
Reload zoo
Create a new node which will be a selection trigger.
Run the below, passing our command id to the triggerCommandId argument
from zoo.libs.maya import triggers from zoo.libs.maya import zapi from zoo.libs.maya.markingmenu import menu triggerNode = zapi.selected(filterTypes=((zapi.kNodeTypes.kTransform, ))) selectableNode = zapi.createDag("selectable", "transform") triggers.createSelectionTrigger(triggerNode, commandStr='print("helloworld")', connectables=[selectableNode], triggerCommandId="customExampleTrigger" )
Select the new node you created on step 2 and now check the script editor. You find that the below was printed out.:
[<DagNode>selectable] helloworld
We now have extended trigger commands with our own plugin which can react anyway you wish.
That’s it for this tutorial.
Happy coding.