mayawalk
Collection of traversal algorithms for Autodesk Maya.
Github
Content
Nodes functions
Plugs functions
Enums
Nodes functions
-
mayawalk.
parent
(node, include_world=False)
Return the parent of node
, if it has one, None otherwise.
- Parameters
node (MObject) – Dag node we want the parent of.
include_world (bool) – If parent is the world node, return it if True,
return None if False. Defaults to False.
- Returns
MObject, None
Example
>>> modifier = OpenMaya.MDagModifier()
>>> root = modifier.createNode('transform') # root
>>> child = modifier.createNode('transform', root) # |- child
>>> modifier = modifier.doIt()
>>> parent(child) == root
True
>>> parent(root) == None
True
-
mayawalk.
children
(node, api_type=None)
Make an iterator returning children of node
.
- Parameters
node (MObject) – Dag node we want the children of.
api_type (int, None) – Only yields nodes of specified Mfn constant
type. Defaults to None (same as filtering by MFn.kDagNode).
- Yields
MObject – children of node
.
Example
>>> modifier = OpenMaya.MDagModifier()
>>> root = modifier.createNode('transform') # root
>>> shape = modifier.createNode('nurbsCurve', root) # |- shape
>>> group = modifier.createNode('transform', root) # |- group
>>> modifier = modifier.doIt()
>>> list(children(root)) == [shape, group]
True
>>> list(children(root, api_type=OpenMaya.MFn.kNurbsCurve)) == [shape]
True
-
mayawalk.
siblings
(node, api_type=None)
Make an iterator returning siblings of node
.
- Parameters
node (MObject) – Dag node we want the siblings of.
api_type (int, None) – Only yields nodes of specified Mfn constant
type. Defaults to None (same as filtering by MFn.kDagNode).
- Yields
MObject – Nodes at the same hierarchical level (same parent) than
node
.
Example
>>> modifier = OpenMaya.MDagModifier()
>>> root = modifier.createNode('transform') # root
>>> node = modifier.createNode('transform', root) # |- node
>>> group = modifier.createNode('transform', root) # |- group
>>> joint = modifier.createNode('joint', root) # |- joint
>>> modifier = modifier.doIt()
>>> list(siblings(node, api_type=OpenMaya.MFn.kJoint)) == [joint]
True
>>> list(siblings(node)) == [group, joint]
True
-
mayawalk.
hierarchy
(root, stoppers=None, api_type=None, depth_first=False, upstream=False)
Traverse root
hierarchy.
- Info:
root
is included as the first element in the returned iterator.
- Parameters
root (MObject) – Starting dag node.
stoppers (list[MObject], None) – Don’t iterate past these nodes. They
are still yielded and won’t break the iteration. Defaults to None.
api_type (int, None) – Only yields nodes of specified Mfn constant
type. Defaults to None (same as filtering by MFn.kDagNode).
depth_first (bool) – Traversal algorithm.
Use depth-first search if True, breadth-first search if False.
Defaults to False.
downstream (bool) – Traversal direction.
Go upstream (parents) if True, downstream (children) if False.
The depth_first
param has no effect when going upstream.
Defaults to False.
- Yields
MObject – Nodes in root
(included) hierarchy.
Example
>>> modifier = OpenMaya.MDagModifier()
>>> root = modifier.createNode('transform') # root
>>> node_a = modifier.createNode('transform', root) # |- node_a
>>> shape = modifier.createNode('nurbsCurve', node_a) # |- shape
>>> node_b = modifier.createNode('joint', root) # |- node_b
>>> modifier = modifier.doIt()
>>> list(hierarchy(root)) == [root, node_a, node_b, shape]
True
>>> list(hierarchy(shape, upstream=True)) == [shape, node_a, root]
True
-
mayawalk.
top_nodes
(nodes, sparse=False)
Make an iterator returning the topmost nodes in nodes
.
A top node is a node whose parent(s) is not in the input nodes
list.
- Parameters
nodes (list[MObject]) – A list of dag nodes objects.
sparse (bool) – If you want to find the top nodes in a sparse hierarchy.
If False, only check the first parent of each node against
nodes
.
If True, check all parents in the upstream hierarchy of each
nodes against nodes
.
- Yields
MObject – Members of nodes
list whose parent is not in nodes
.
Example
>>> modifier = OpenMaya.MDagModifier()
>>> node_a = modifier.createNode('transform') # node_a
>>> node_b = modifier.createNode('transform', node_a) # |- node_b
>>> node_c = modifier.createNode('transform', node_b) # |- node_c
>>> modifier = modifier.doIt()
>>> list(top_nodes([node_b, node_c])) == [node_b]
True
>>> list(top_nodes([node_a, node_c], sparse=True)) == [node_a]
True
-
mayawalk.
connections
(root, stoppers=None, api_type=None, depth_first=False, upstream=False)
Traverse root
connections.
- Info:
root
is included as the first element in the returned iterator.
- Parameters
root (MObject) – Starting dependency node.
stoppers (list[MObject], None) – Don’t iterate past these nodes. They
are still yielded and won’t break the iteration. Defaults to None.
api_type (int, None) – Only yields nodes of specified Mfn constant type.
Defaults to None (same as filtering by MFn.kDagNode).
depth_first (bool) – Traversal algorithm.
Use depth-first search if True, breadth-first search if False.
Defaults to False.
downstream (bool) – Traversal direction.
Go upstream (sources, left) if True,
downstream (destinations, right) if False.
Defaults to False.
- Yields
MObject – Nodes connected directly and indirectly to root
(included).
Example
>>> modifier = OpenMaya.MDagModifier()
>>> node_a = modifier.createNode('transform')
>>> node_b = modifier.createNode('transform')
>>> node_c = modifier.createNode('transform')
>>> modifier = modifier.doIt()
>>> plug_a = OpenMaya.MFnDagNode(node_a).findPlug('tx', False)
>>> plug_b = OpenMaya.MFnDagNode(node_b).findPlug('tx', False)
>>> plug_c = OpenMaya.MFnDagNode(node_c).findPlug('tx', False)
>>> modifier = modifier.connect(plug_a, plug_b).doIt()
>>> modifier = modifier.connect(plug_b, plug_c).doIt()
>>> # node_a.tx >> node_b.tx >> node_c.tx
>>> list(connections(node_a)) == [node_a, node_b, node_c]
True
>>> list(connections(node_c, upstream=True)) == [node_c, node_b, node_a]
True
>>> list(connections(node_a, stoppers=[node_b])) == [node_a, node_b]
True
-
mayawalk.
connected
(node, sources=True, destinations=True)
Make an iterator returning nodes connected node
.
- Parameters
node (MObject) – Node to find the connections of.
sources (bool) – Include node
sources. Defaults to True.
destinations (bool) – Include node
destinations. Defaults to True.
- Yields
MObject – Nodes directly connected to node
.
Example
>>> modifier = OpenMaya.MDagModifier()
>>> src = modifier.createNode('transform')
>>> dst = modifier.createNode('transform')
>>> modifier = modifier.doIt()
>>> modifier = modifier.connect(
... OpenMaya.MFnDagNode(src).findPlug('tx', False),
... OpenMaya.MFnDagNode(dst).findPlug('tx', False)).doIt()
>>> # src.tx >> dst.tx
>>> list(connected(src, sources=False, destinations=True)) == [dst]
True
>>> list(connected(dst, sources=True, destinations=False)) == [src]
True
Plugs functions
-
mayawalk.
plugs
(node, connection=None)
Make an iterator returning plugs of node
.
- Parameters
node (MObject) – Node we want the plugs of.
connection (int, None) – If specified, only yields nodes of this
ConnectionStatus
. Defaults to None.
- Yields
MPlug – Plugs of node
.
-
mayawalk.
plug_parent
(plug)
Return plug
parent, if it has one, None otherwise.
- Parameters
plug (MPlug) – Plug we want the parent of.
- Returns
MPlug, None
Example
>>> modifier = OpenMaya.MDagModifier()
>>> node = OpenMaya.MFnDagNode(modifier.createNode('transform'))
>>> modifier = modifier.doIt()
>>> translate = node.findPlug('translate', False)
>>> translate_x = node.findPlug('translateX', False)
>>> plug_parent(translate) == None
True
>>> plug_parent(translate_x) == translate
True
-
mayawalk.
plug_children
(plug, reverse=False, physical_indexes=False)
Make an iterator returning children plugs of plug
.
Iterator might be empty if plug
is neither an array nor a compound, or
is a non-physical plug (for example animCurveUU.keyTanOutX).
- Parameters
plug (MPlug) – The plug we want chilren of.
physical_indexes (bool) – If mplug
is an array, return the physical
indexes if True, the logical indexes if False.
Default to False.
reverse (bool) – Iter children
in the correct order if False (X, Y, Z or 0, 1, 2
)
or in reverse order if True (Z, Y, X or 2, 1, 0
).
Default to False.
- Yields
MPlug – Children mplugs of plug
- Raises
IndexError – Unknown error when trying to get a child plug.
Example
>>> modifier = OpenMaya.MDagModifier()
>>> node = OpenMaya.MFnDagNode(modifier.createNode('transform'))
>>> modifier = modifier.doIt()
>>> translate = node.findPlug('t', False)
>>> children = [node.findPlug('t' + axis, False) for axis in 'xyz']
>>> list(plug_children(translate)) == children
True
>>> children.reverse()
>>> list(plug_children(translate, reverse=True)) == children
True
-
mayawalk.
plug_has_source
(plug, nested=False)
Return True if plug
has any source connection, False otherwise.
- Parameters
nested (bool) – If True, extend the check to all children in plug
hierarchy.
- Returns
bool
-
mayawalk.
plug_has_destinations
(plug, nested=False)
Return True if plug
has any destination connection, False otherwise.
- Parameters
nested (bool) – If True, extend the check to all children in plug
hierarchy.
- Returns
bool
-
mayawalk.
plug_has_connections
(plug, nested=False)
Return True if plug
has any source or destination connection.
Convenience function that run both plug_has_source
and
plug_has_destinations
.
- Parameters
nested (bool) – If True, extend the check to all children in plug
hierarchy.
- Returns
bool
Enums
-
class
mayawalk.
ConnectionStatus
All connections status a plug can have. Enum class.
-
classmethod
has_status
(plug, connection)
- Parameters
-
- Returns
bool – True if plug
has connection
status, False otherwise.
-
kConnected
= 1
Has any connection.
-
kConnectedDestinations
= 3
Has destination(s) / is a source.
-
kConnectedSources
= 2
Has a source / is a destination.
-
kDisconnected
= 4
Has no connection.
-
kDisconnectedDestinations
= 6
Has no destinations / is not a source.
-
kDisconnectedSources
= 5
Has no source / is not a destination.