Skip to content

Commit

Permalink
Minor additions
Browse files Browse the repository at this point in the history
- Add `dump(ignore_error=True)` to fetch attributes even though they aren't supported, e.g. `.geometry`
- Add `DagNode.dagPath`
- Add `DagNode.mapFrom` for getting one transformation matrix relative another
- Add `DagNode.mapTo`
- Enable PEP8 alternative syntax per default
  • Loading branch information
mottosso committed Aug 25, 2018
1 parent 79913aa commit d619303
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
91 changes: 86 additions & 5 deletions cmdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
# Increase performance by not bothering to free up unused memory
MEMORY_HOG_MODE = not SAFE_MODE and bool(os.getenv("CMDX_MEMORY_HOG_MODE"))

ENABLE_PEP8 = bool(os.getenv("CMDX_PEP8"))
ENABLE_PEP8 = True

# Support undo/redo
ENABLE_UNDO = not SAFE_MODE
Expand Down Expand Up @@ -823,7 +823,7 @@ def pop(self, key):

del self[key]

def dump(self, detail=0):
def dump(self, ignore_error=True):
"""Return dictionary of all attributes
Example:
Expand All @@ -846,11 +846,14 @@ def dump(self, detail=0):

try:
value = Plug(self, plug).read()
except RuntimeError:
except (RuntimeError, TypeError):
# TODO: Support more types of attributes,
# such that this doesn't need to happen.
value = None

if not ignore_error:
raise

attrs[plug.name()] = value

return attrs
Expand Down Expand Up @@ -1043,6 +1046,24 @@ def path(self):

return self._fn.fullPathName()

@protected
def dagPath(self):
"""Return a om.MDagPath for this node
Example:
>>> _ = cmds.file(new=True, force=True)
>>> parent = createNode("transform", name="Parent")
>>> child = createNode("transform", name="Child", parent=parent)
>>> path = child.dagPath()
>>> str(path)
'Child'
>>> str(path.pop())
'Parent'
"""

return om.MDagPath.getAPathTo(self._mobject)

@protected
def shortestPath(self):
"""Return shortest unique path to node
Expand Down Expand Up @@ -1128,10 +1149,45 @@ def assembly(self):
return self.__class__(root.node()) if root else self

def transform(self, space=Object, time=None):
"""Return MTransformationMatrix"""
"""Return TransformationMatrix"""
plug = self["worldMatrix"][0] if space == World else self["matrix"]
return TransformationMatrix(plug.asMatrix(time))

def mapFrom(self, other, time=None):
"""Return TransformationMatrix of `other` relative self
Example:
>>> a = createNode("transform")
>>> b = createNode("transform")
>>> a["translate"] = (0, 5, 0)
>>> b["translate"] = (0, -5, 0)
>>> delta = a.mapFrom(b)
>>> delta.translation()[1]
10.0
>>> a = createNode("transform")
>>> b = createNode("transform")
>>> a["translate"] = (0, 5, 0)
>>> b["translate"] = (0, -15, 0)
>>> delta = a.mapFrom(b)
>>> delta.translation()[1]
20.0
"""

a = self["worldMatrix"][0].asMatrix(time)
b = other["worldInverseMatrix"][0].asMatrix(time)
delta = a * b
return TransformationMatrix(delta)

def mapTo(self, other, time=None):
"""Return TransformationMatrix of self relative `other`
See :func:`mapFrom` for examples.
"""

return other.mapFrom(self, time)

# Alias
root = assembly

Expand Down Expand Up @@ -1363,6 +1419,9 @@ def duplicate(self):
if ENABLE_PEP8:
shortest_path = shortestPath
add_child = addChild
dag_path = dagPath
map_from = mapFrom
map_to = mapTo


class ObjectSet(Node):
Expand Down Expand Up @@ -1648,6 +1707,8 @@ def __iadd__(self, other):
else:
self.append(other)

return self

def __str__(self):
"""Return value as str
Expand Down Expand Up @@ -2223,7 +2284,7 @@ def setScale(self, seq, space=None):

if ENABLE_PEP8:
set_translation = setTranslation
set_Scale = setScale
set_scale = setScale


class Vector(om.MVector):
Expand Down Expand Up @@ -2693,6 +2754,15 @@ def asHex(mobj):
return "%x" % asHash(mobj)


if ENABLE_PEP8:
from_hash = fromHash
from_hex = fromHex
to_hash = toHash
to_hex = toHex
as_hash = asHash
as_hex = asHex


def clear():
"""Remove all reused nodes"""
Singleton._instances.clear()
Expand Down Expand Up @@ -2832,6 +2902,14 @@ def connect(self, plug1, plug2):
def disconnect(self, plug1, plug2):
plug1 // plug2

if ENABLE_PEP8:
do_it = doIt
undo_it = undoIt
create_node = createNode
delete_node = deleteNode
rename_node = renameNode
set_attr = setAttr


class DGModifier(_BaseModifier):
"""Modifier for DG nodes"""
Expand Down Expand Up @@ -2913,6 +2991,9 @@ def parent(self, node, parent=None):
parent = parent._mobject if parent is not None else None
self._modifier.reparentNode(node._mobject, parent)

if ENABLE_PEP8:
create_node = createNode


def ls(*args, **kwargs):
return map(encode, cmds.ls(*args, **kwargs))
Expand Down
6 changes: 2 additions & 4 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import os
import sys
import nose
import flaky.flaky_nose_plugin as flaky

if __name__ == "__main__":
print("Initialising Maya..")
from maya import standalone, cmds
standalone.initialize()
cmds.loadPlugin("matrixNodes", quiet=True)

os.environ["CMDX_ENABLE_NODE_REUSE"] = "1"
os.environ["CMDX_ENABLE_PLUG_REUSE"] = "1"

argv = sys.argv[:]
argv.extend([
"--verbose",
Expand All @@ -30,7 +28,7 @@
"cmdx.py",
])

nose.main(argv=argv)
nose.main(argv=argv, addplugins=[flaky.FlakyPlugin()])

if os.getenv("TRAVIS_JOB_ID"):
import coveralls
Expand Down

0 comments on commit d619303

Please sign in to comment.