Zoo Maya Command¶
Exact same as the standalone version but integrated into maya’s command engine. Standalone commands will still be usable inside maya, if a command is undoable then it will be part of maya internal undostack. Maya commands are a thin wrapper around the MPxCommand so we maintain the undo/redo feature’s but we extended the possibilities with maya by allowing for arbitrary data types to be passed to and from commands eg. om2.MObject. we only support using om2, cmds and pure python, no om1 code as per maya documentation. A few design decision have been made to simplify command creation.
Only the doIt and undoIt methods need to be implemented.
Zoo handles the registry of supported commands and only one plugin is registered to maya which is the undo.py in zoo.
User’s only need to tell zoo executor instance about the command location(Environment variable), no need for the initializePlugin().
Minimal differences between MPxCommand and Zoocommand
maya’s undo/redo stacks and zooCommands stacks are synced via the custom MPx.
ZooCommands support passing api objects and any datatype to and from a command(see below).
ZooCommands are not meant to do atomic operations and query ops. Only for maya state changes and only for large operations.
ZooCommands are not meant to replace c++ commands or for efficient code but for tool development, it’s not meant to be run in loops or something stupid like that. eg. you press a pushbutton then you execute a command that builds a rig which can be undone.
Example¶
from zoo.libs.command import executor
exe = executorExecutor()
nodes = exe.execute("zoo.create.nodetype", name="transform", amount=10, Type="transform")
print(nodes)
# (<OpenMaya.MObjectHandle object at 0x0000024911572E70>, <OpenMaya.MObjectHandle object at 0x0000024911572E30>,
<OpenMaya.MObjectHandle object at 0x0000024911572CB0>, <OpenMaya.MObjectHandle object at 0x0000024911572E90>,
<OpenMaya.MObjectHandle object at 0x0000024911572EB0>, <OpenMaya.MObjectHandle object at 0x0000024911572ED0>,
<OpenMaya.MObjectHandle object at 0x0000024911572EF0>, <OpenMaya.MObjectHandle object at 0x0000024911572F10>,
<OpenMaya.MObjectHandle object at 0x0000024911572F30>, <OpenMaya.MObjectHandle object at 0x0000024911572F50>)
# see below for the command class
from zoo.libs.command import command
class CreateNodeTypeAmount(command.ZooCommand):
id = "zoo.create.nodetype" # id which is used for execution, and any filtering, lookups, GUIs etc
creator = "David Sparrow"
isUndoable = True
_modifier = None
def resolveArguments(self, arguments):
"""Method to Pre check arguments this is run outside of mayas internals and the result cached on to the command instance.
Since the result is store for the life time of the command you need to convert MObjects to MObjectHandles.
:param arguments: dict representing the arguments
:type arguments: dict
"""
name= arguments.get("name")
if not name:
self.cancel("Please provide a name!")
amount = arguments.get("amount")
if amount < 1:
self.cancel("The amount can't be below one")
if not arguments.get("Type"):
arguments["Type"] = "transform"
return arguments
def doIt(self, name=None, amount=1, Type=None):
"""Its expected that the arguments are setup correctly with the correct datatype,
"""
mod = om2.MDagModifier()
nodes = [None] * amount
for i in xrange(amount):
obj = mod.createNode(Type)
mod.renameNode(obj, "name{}".format(i))
nodes[i] = obj
mod.doIt()
nodes = map(om2.MObjectHandle, nodes)
self._modifier = mod
return tuple(nodes)
def undoIt(self):
if self._modifier is not None:
self._modifier.undoIt()
Executor Core¶
- class MayaExecutor¶
Bases:
ExecutorBase
Maya Executor class for safely injecting zoo commands into the maya undo stack via MPXCommands. Always call executor.execute() method when executing commands
- execute(commandName, **kwargs)¶
Function to execute Zoo commands which lightly wrap maya MPXCommands. Deals with prepping the Zoo plugin with the command instance. Safely opens and closes the undo chunks via maya commands (cmds.undoInfo)
- undoLast()¶
- redoLast()¶
- flush()¶
Zoo Command Library¶
Alignselectedcommand¶
- class AlignSelectedCommand(stats=None)¶
Bases:
ZooCommandMaya
This command Creates a meta node from the registry.
- id = 'zoo.maya.alignSelected'¶
- creator = 'David Sparrow'¶
- isUndoable = True¶
- uiData = {'backgroundColor': '', 'color': '', 'icon': '', 'label': 'Align selected nodes', 'tooltip': 'Align rotation of two objects'}¶
- transformations = None¶
- resolveArguments(arguments)¶
Method which allows the developer to pre doIt validate the incoming arguments. This method get executed before any operation on the command.
- doIt(target=None, driven=None, aimVector=MockExt.OpenMaya.MVector, upVector=MockExt.OpenMaya.MVector)¶
Create the meta node based on the type parameter, if the type isn’t specified then the baseMeta class will be used instead
- undoIt()¶
If this command instance is set to undoable then this method needs to be implemented, by design you do the inverse operation of the doIt method
- Returns
- Return type
Connectsrt¶
- class ConnectSRTSelectedCommand(stats=None)¶
Bases:
ZooCommandMaya
- id = 'zoo.maya.connect.selection.localsrt'¶
- creator = 'David Sparrow'¶
- isUndoable = True¶
- uiData = {'backgroundColor': '', 'color': '', 'icon': 'connection', 'label': 'Connect SRT', 'tooltip': 'Connects the translate, rotate, scale from the first selected node to all others'}¶
- resolveArguments(arguments)¶
Method which allows the developer to pre doIt validate the incoming arguments. This method get executed before any operation on the command.
- doIt(driver=None, targets=None, translate=True, rotate=True, scale=True)¶
Create the meta node based on the type parameter, if the type isn’t specified then the baseMeta class will be used instead
- undoIt()¶
If this command instance is set to undoable then this method needs to be implemented, by design you do the inverse operation of the doIt method
- Returns
- Return type
Createmetacommand¶
- class CreateMetaCommand(stats=None)¶
Bases:
ZooCommandMaya
This command Creates a meta node from the registry.
- id = 'zoo.meta.create'¶
- creator = 'David Sparrow'¶
- isUndoable = True¶
- uiData = {'backgroundColor': '', 'color': '', 'icon': '', 'label': 'Create meta Node', 'tooltip': 'Create meta node'}¶
- resolveArguments(arguments)¶
Method which allows the developer to pre doIt validate the incoming arguments. This method get executed before any operation on the command.
- doIt(node=None, name=None, type_=None, initDefaults=True)¶
Create the meta node based on the type parameter, if the type isn’t specified then the baseMeta class will be used instead
- Parameters
node (MObject) – The node to convert to the meta class(optional)
name (str) – The new name for the create meta node(optional)
type (str) – The meta node class name, if not specified then the base meta class is used. This is converted to the class instance during command.resolvearguments method operation.
initDefaults (bool) – If true then the standard meta attributes are added
- Returns
Returns the class instance of the meta class thats created
- Return type
- undoIt()¶
If this command instance is set to undoable then this method needs to be implemented, by design you do the inverse operation of the doIt method
- Returns
- Return type
Nodeeditorcommands¶
- class NodeAlignmentCommand(stats=None)¶
Bases:
ZooCommandMaya
- id = 'zoo.maya.nodeEditor.alignment'¶
- creator = 'David Sparrow'¶
- isUndoable = True¶
- uiData = {'backgroundColor': '', 'color': '', 'icon': 'horizontalAlignLeft', 'label': 'Align Nodes', 'tooltip': "Align's the selected nodes in the node editor to be at the farthest left point"}¶
- resolveArguments(arguments)¶
Method which allows the developer to pre doIt validate the incoming arguments. This method get executed before any operation on the command.
- doIt(nodeEditor=None, nodeItems=None, align=0)¶
Create the meta node based on the type parameter, if the type isn’t specified then the baseMeta class will be used instead
- undoIt()¶
If this command instance is set to undoable then this method needs to be implemented, by design you do the inverse operation of the doIt method
- Returns
- Return type
Setselectednodes¶
- class SetSelectedNodes(stats=None)¶
Bases:
ZooCommandMaya
This command selected maya nodes
- id = 'zoo.maya.setSelectedNodes'¶
- creator = 'David Sparrow'¶
- isUndoable = True¶
- uiData = {'backgroundColor': '', 'color': '', 'icon': '', 'label': 'Select Nodes', 'tooltip': 'This command selected maya nodes\n\n '}¶
- currentSelection = []¶
- resolveArguments(arguments)¶
Method which allows the developer to pre doIt validate the incoming arguments. This method get executed before any operation on the command.
- doIt(nodes=None)¶
- Parameters
nodes (seq or om2.MSelectionList) –
- Returns
- Return type
om2.MObjectArray
- undoIt()¶
If this command instance is set to undoable then this method needs to be implemented, by design you do the inverse operation of the doIt method
- Returns
- Return type
Swapconnections¶
- class SwapConnectionsCommand(stats=None)¶
Bases:
ZooCommandMaya
This command Creates a meta node from the registry.
- id = 'zoo.maya.connections.swap.all'¶
- creator = 'David Sparrow'¶
- isUndoable = True¶
- uiData = {'backgroundColor': '', 'color': '', 'icon': 'transfer', 'label': 'Swap connections', 'tooltip': 'Swaps all connections between two selected nodes'}¶
- resolveArguments(arguments)¶
Method which allows the developer to pre doIt validate the incoming arguments. This method get executed before any operation on the command.
- doIt(source=None, target=None, plugs=None)¶
Create the meta node based on the type parameter, if the type isn’t specified then the baseMeta class will be used instead
- undoIt()¶
If this command instance is set to undoable then this method needs to be implemented, by design you do the inverse operation of the doIt method
- Returns
- Return type