From 49a638dd0e201dd39c090bcd70ee7f6ce7c91ac1 Mon Sep 17 00:00:00 2001 From: Marcelo Saguas Iacovone Date: Sat, 10 Feb 2024 16:17:49 -0800 Subject: [PATCH 1/3] Makes listRelatives behave more like Maya cmds --- cmdx.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cmdx.py b/cmdx.py index 5ab6361..2803397 100644 --- a/cmdx.py +++ b/cmdx.py @@ -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 @@ -7238,16 +7239,20 @@ def listRelatives(node, if not isinstance(node, DagNode): return None - elif allDescendents: - return list(node.descendents(type=type)) - elif shapes: + if shapes and parent: + return [] + + if shapes: 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): From ee5bef92cab3e48cf6cf697bd2e44aafe75c582c Mon Sep 17 00:00:00 2001 From: Marcelo Saguas Iacovone Date: Sun, 11 Feb 2024 12:03:44 -0800 Subject: [PATCH 2/3] Fixes incorrect assumption that shapes + parent returned nothing --- cmdx.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmdx.py b/cmdx.py index 2803397..d592474 100644 --- a/cmdx.py +++ b/cmdx.py @@ -7239,10 +7239,7 @@ def listRelatives(node, if not isinstance(node, DagNode): return None - if shapes and parent: - return [] - - if shapes: + if shapes and not parent: return list(node.shapes(type=type)) if parent: From ac801383af7f1fbfe54b87d9d38b90141257d2fa Mon Sep 17 00:00:00 2001 From: Marcelo Saguas Iacovone Date: Mon, 12 Feb 2024 22:45:10 -0800 Subject: [PATCH 3/3] Adds basic tests for cmdx.listRelatives --- tests.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests.py b/tests.py index 8bcb8d7..73ccac6 100644 --- a/tests.py +++ b/tests.py @@ -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)