Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements a different approach for __repr__ #78

Merged
merged 8 commits into from
Feb 27, 2024
130 changes: 108 additions & 22 deletions cmdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ class Node(object):
>>> transform["tx"] = 5
>>> transform["worldMatrix"][0] >> decompose["inputMatrix"]
>>> decompose["outputTranslate"]
<cmdx.Plug : (5.0, 0.0, 0.0)>
>>> decompose["outputTranslate"].read()
chelloiaco marked this conversation as resolved.
Show resolved Hide resolved
(5.0, 0.0, 0.0)

"""
Expand Down Expand Up @@ -548,7 +550,8 @@ def __str__(self):
return self.name(namespace=True)

def __repr__(self):
return self.name(namespace=True)
cls_name = '{}.{}'.format(__name__, self.__class__.__name__)
return "<{} : '{}'>".format(cls_name, self.name(namespace=True))

def __add__(self, other):
"""Support legacy + '.attr' behavior
Expand Down Expand Up @@ -579,6 +582,8 @@ def __getitem__(self, key):
>>> node = createNode("transform")
>>> node["translate"] = (1, 1, 1)
>>> node["translate", Meters]
<cmdx.Plug : (0.01, 0.01, 0.01)>
>>> node["translate", Meters].read()
(0.01, 0.01, 0.01)

"""
Expand Down Expand Up @@ -625,6 +630,8 @@ def __setitem__(self, key, value):
>>> node["rotateX", Degrees] = 1.0
>>> node["rotateX"] = Degrees(1)
>>> node["rotateX", Degrees]
<cmdx.Plug : 1.0>
>>> node["rotateX", Degrees].read()
1.0
>>> node["myDist"] = Distance()
>>> node["myDist"] = node["translateX"]
Expand Down Expand Up @@ -981,6 +988,8 @@ def update(self, attrs):
>>> node = createNode("transform")
>>> node.update({"tx": 5.0, ("ry", Degrees): 30.0})
>>> node["tx"]
<cmdx.Plug : 5.0>
>>> node["tx"].read()
5.0

"""
Expand All @@ -999,13 +1008,19 @@ def clear(self):
>>> node = createNode("transform")
>>> node["translateX"] = 5
>>> node["translateX"]
<cmdx.Plug : 5.0>
>>> node["translateX"].read()
5.0
>>> # Plug was reused
>>> node["translateX"]
<cmdx.Plug : 5.0>
>>> node["translateX"].read()
5.0
>>> # Value was reused
>>> node.clear()
>>> node["translateX"]
<cmdx.Plug : 5.0>
>>> node["translateX"].read()
5.0
>>> # Plug and value was recomputed

Expand Down Expand Up @@ -1513,7 +1528,8 @@ def __str__(self):
return self.path()

def __repr__(self):
return self.path()
cls_name = '{}.{}'.format(__name__, self.__class__.__name__)
return "<{} : '{}'>".format(cls_name, self.path())

def __or__(self, other):
"""Syntax sugar for finding a child
Expand All @@ -1523,12 +1539,22 @@ def __or__(self, other):
>>> parent = createNode("transform", "parent")
>>> child = createNode("transform", "child", parent)
>>> parent | "child"
|parent|child
<cmdx.DagNode : '|parent|child'>
>>> (parent | "child").path() in (
... '|parent|child',
... u'|parent|child'
... )
True

# Stackable too
>>> grand = createNode("transform", "grand", child)
>>> parent | "child" | "grand"
|parent|child|grand
<cmdx.DagNode : '|parent|child|grand'>
>>> (parent | "child" | "grand").path() in (
... '|parent|child|grand',
... u'|parent|child|grand'
... )
True

"""

Expand Down Expand Up @@ -2291,18 +2317,24 @@ def flatten(self, type=None):
"""Return members, converting nested object sets into its members

Example:
>>> from maya import cmds
>>> _ = cmds.file(new=True, force=True)
>>> a = cmds.createNode("transform", name="a")
>>> b = cmds.createNode("transform", name="b")
>>> c = cmds.createNode("transform", name="c")
>>> cmds.select(a)
>>> gc = cmds.sets([a], name="grandchild")
>>> cc = cmds.sets([gc, b], name="child")
>>> parent = cmds.sets([cc, c], name="parent")
>>> mainset = encode(parent)
>>> sorted(mainset.flatten(), key=lambda n: n.name())
[|a, |b, |c]
>>> from maya import cmds
>>> _ = cmds.file(new=True, force=True)
>>> a = cmds.createNode("transform", name="a")
>>> b = cmds.createNode("transform", name="b")
>>> c = cmds.createNode("transform", name="c")
>>> cmds.select(a)
>>> gc = cmds.sets([a], name="grandchild")
>>> cc = cmds.sets([gc, b], name="child")
>>> parent = cmds.sets([cc, c], name="parent")
>>> mainset = encode(parent)
>>> sorted(mainset.flatten(), key=lambda n: n.name())
[<cmdx.DagNode : '|a'>, <cmdx.DagNode : '|b'>, <cmdx.DagNode : '|c'>]
chelloiaco marked this conversation as resolved.
Show resolved Hide resolved
>>> result = sorted([n.name() for n in mainset.flatten()])
>>> result in (
... ['a', 'b', 'c'],
... [u'a', u'b', u'c']
... )
True

"""

Expand Down Expand Up @@ -2561,8 +2593,12 @@ def __add__(self, other):
>>> node = createNode("transform")
>>> node["tx"] = 5
>>> node["translate"] + "X"
<cmdx.Plug : 5.0>
>>> (node["translate"] + "X").read()
5.0
>>> node["t"] + "x"
<cmdx.Plug : 5.0>
>>> (node["t"] + "x").read()
5.0
>>> try:
... node["t"] + node["r"]
Expand Down Expand Up @@ -2598,10 +2634,16 @@ def __iadd__(self, other):
>>> node["myArray"] += 5.1
>>> node["myArray"] += [1.1, 2.3, 999.0]
>>> node["myArray"][0]
<cmdx.Plug : 1.0>
>>> node["myArray"][0].read()
1.0
>>> node["myArray"][6]
<cmdx.Plug : 999.0>
>>> node["myArray"][6].read()
999.0
>>> node["myArray"][-1]
<cmdx.Plug : 999.0>
>>> node["myArray"][-1].read()
999.0

"""
Expand All @@ -2627,7 +2669,13 @@ def __str__(self):
return str(self.read())

def __repr__(self):
return str(self.read())
cls_name = '{}.{}'.format(__name__, self.__class__.__name__)
read_val = self.read()
if isinstance(read_val, string_types):
# Add surrounding single quotes, indicating the value is a string
read_val = "'{}'".format(read_val)

return "<{} : {}>".format(cls_name, read_val)

def __rshift__(self, other):
"""Support connecting attributes via A >> B"""
Expand Down Expand Up @@ -2799,6 +2847,8 @@ def __setitem__(self, index, value):
>>> node = createNode("transform")
>>> node["translate"][0] = 5
>>> node["tx"]
<cmdx.Plug : 5.0>
>>> node["tx"].read()
5.0

"""
Expand Down Expand Up @@ -3174,8 +3224,12 @@ def extend(self, values):
>>> node["myArray"] = Double(array=True)
>>> node["myArray"].extend([1.0, 2.0, 3.0])
>>> node["myArray"][0]
<cmdx.Plug : 1.0>
>>> node["myArray"][0].read()
1.0
>>> node["myArray"][-1]
<cmdx.Plug : 3.0>
>>> node["myArray"][-1].read()
3.0

"""
Expand Down Expand Up @@ -4102,6 +4156,8 @@ def connections(self,
>>> b["ihi"].connection() == a
True
>>> a["ihi"]
<cmdx.Plug : 2>
>>> a["ihi"].read()
2
>>> b["arrayAttr"] = Long(array=True)
>>> b["arrayAttr"][0] >> a["ihi"]
Expand Down Expand Up @@ -6351,15 +6407,26 @@ def connect(self, src, dst, force=True):
... mod.connect(tm["sx"], tm["tx"])
...
>>> tm["tx"].connection()
|myTransform
<cmdx.DagNode : '|myTransform'>
>>> tm["tx"].connection().path() in (
... '|myTransform',
... u'|myTransform'
... )
True

>>> cmds.undo()
>>> tm["tx"].connection() is None
True

# Connect without undo
>>> tm["tx"] << tx["output"]
>>> tm["tx"].connection()
myAnimCurve
<cmdx.AnimCurve : 'myAnimCurve'>
>>> tm["tx"].connection().name() in (
... 'myAnimCurve',
... u'myAnimCurve'
... )
True

# Disconnect without undo
>>> tm["tx"] // tx["output"]
Expand Down Expand Up @@ -6445,11 +6512,21 @@ def connectAttr(self, srcPlug, dstNode, dstAttr):
... mod.connectAttr(newNode["newAttr"], otherNode, otherAttr)
...
>>> newNode["newAttr"].connection()
|otherNode
<cmdx.DagNode : '|otherNode'>
>>> newNode["newAttr"].connection().path() in (
... '|otherNode',
... u'|otherNode'
... )
True

>>> cmds.undo()
>>> newNode["newAttr"].connection()
|newNode
<cmdx.DagNode : '|newNode'>
>>> newNode["newAttr"].connection().path() in (
... '|newNode',
... u'|newNode'
... )
True

"""

Expand Down Expand Up @@ -6769,8 +6846,12 @@ class DagModifier(_BaseModifier):
>>> getAttr(node1 + ".translateX")
1.0
>>> node2["translate"][0]
<cmdx.Plug : 1.0>
>>> node2["translate"][0].read()
1.0
>>> node2["translate"][1]
<cmdx.Plug : 2.0>
>>> node2["translate"][1].read()
2.0
>>> with DagModifier() as mod:
... node1 = mod.createNode("transform")
Expand All @@ -6779,8 +6860,12 @@ class DagModifier(_BaseModifier):
... node1["translate"] >> node2["translate"]
...
>>> node2["translate"][0]
<cmdx.Plug : 5.0>
>>> node2["translate"][0].read()
5.0
>>> node2["translate"][1]
<cmdx.Plug : 6.0>
>>> node2["translate"][1].read()
6.0

Example, without context manager:
Expand Down Expand Up @@ -7721,7 +7806,8 @@ def __hash__(self):

def __repr__(self):
"""Avoid repr depicting the full contents of this dict"""
return self["name"]
cls_name = '{}.{}'.format(__name__, self.__class__.__name__)
return "<{} : '{}'>".format(cls_name, self["name"])

def __new__(cls, *args, **kwargs):
"""Support for using name of assignment
Expand Down
3 changes: 2 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ def test_nodeoperators():

node = cmdx.createNode(cmdx.tTransform, name="myNode")
assert_equals(node, "|myNode")
assert_equals(repr(node), "<cmdx.DagNode : '|myNode'>")
assert_not_equals(node, "|NotEquals")
assert_equals(str(node), repr(node))
assert_not_equals(str(node), repr(node))


@with_setup(new_scene)
Expand Down
Loading