Skip to content

Commit

Permalink
Merge pull request #76 from chelloiaco/list-relatives-cmds-behavior
Browse files Browse the repository at this point in the history
Makes listRelatives behave more like Maya cmds
  • Loading branch information
mottosso authored Feb 15, 2024
2 parents ddf276a + ac80138 commit 344aed0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
16 changes: 9 additions & 7 deletions cmdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import collections
import contextlib
from functools import wraps
from itertools import chain

from maya import cmds
from maya.api import OpenMaya as om, OpenMayaAnim as oma, OpenMayaUI as omui
Expand Down Expand Up @@ -7238,16 +7239,17 @@ def listRelatives(node,
if not isinstance(node, DagNode):
return None

elif allDescendents:
return list(node.descendents(type=type))
elif shapes:
if shapes and not parent:
return list(node.shapes(type=type))

elif parent:
return [node.parent(type=type)]
if parent:
_parent = node.parent(type=type)
return [_parent] if _parent else []

if allDescendents:
return list(node.descendents(type=type))

elif children:
return list(node.children(type=type))
return list(chain(node.shapes(type=type), node.children(type=type)))


def listConnections(attr):
Expand Down
54 changes: 54 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,57 @@ def undo():

def test_modifier_redo():
pass


def _setup_listrelatives_test():
new_scene()
cmds.createNode('transform', name='topGrp')
cmds.polyCube(name='hierarchyCube', constructionHistory=False)
cmds.parent('hierarchyCube', 'topGrp')
cmds.createNode('transform', name='botGrp', parent='hierarchyCube')
cmds.polyCube(name='worldCube', constructionHistory=False)
cmds.blendShape('hierarchyCube', 'worldCube', name='cubeBlend')


@with_setup(_setup_listrelatives_test)
def test_listrelatives_children():

result_cmdx = cmdx.listRelatives('hierarchyCube', children=True)
result_cmdx = [i.name() for i in result_cmdx]
result_cmds = cmds.listRelatives('hierarchyCube', children=True)

assert_equals(result_cmdx, result_cmds)


@with_setup(_setup_listrelatives_test)
def test_listrelatives_alldescendents():

result_cmdx = cmdx.listRelatives('topGrp', allDescendents=True)
result_cmdx = [i.name() for i in result_cmdx]
result_cmds = cmds.listRelatives('topGrp', allDescendents=True)

# cmds has a special result order, so compare sets
result_cmdx = set(result_cmdx)
result_cmds = set(result_cmds)

assert_equals(result_cmdx, result_cmds)


@with_setup(_setup_listrelatives_test)
def test_listrelatives_parent():

result_cmdx = cmdx.listRelatives('hierarchyCubeShape', parent=True)
result_cmdx = [i.name() for i in result_cmdx]
result_cmds = cmds.listRelatives('hierarchyCubeShape', parent=True)

assert_equals(result_cmdx, result_cmds)


@with_setup(_setup_listrelatives_test)
def test_listrelatives_shapes():

result_cmdx = cmdx.listRelatives('worldCube', shapes=True)
result_cmdx = [i.name() for i in result_cmdx]
result_cmds = cmds.listRelatives('worldCube', shapes=True)

assert_equals(result_cmdx, result_cmds)

0 comments on commit 344aed0

Please sign in to comment.