def get_stkLevel():
sel = cmds.ls(selection=True, l=True)
if len(sel) == 0:
cmds.warning("Please select something")
stkLevel_node = cmds.ls(sel, type = "stkLevel")
if len(sel) > 1:
if stkLevel_node:
cmds.warning("Please select a valid stkLevel node")
else:
root_list = []
for item in sel:
root_node = item.split('|')[1]
if root_node not in root_list:
root_list.append(root_node)
return cmds.ls(root_list, l=True)[0]
else:
return stkLevel_node[0]
I got the issue, where if I select only 1 pCube#, and it returns nothing, though it seems to be working if I only select 1 Root NodeWhile that is one of the issue, can someone kindly guide me if I am writing my code correctly?
I asked this because first I am still not that great of a scripter and I feel that I am forcing my way through while following those conditions
def get_all_parents(s_node):
"""
!@Brief Get all parents of given node.
@type s_node: string
@param s_node: Node name for start surch.
@rtype: list(string)
@return: List of all parents
"""
if not isinstance(s_node, basestring):
s_node = s_node.name()
l_s_parent_nodes = list()
current_node = s_node
while cmds.listRelatives(current_node, parent=True, fullPath=True):
parent_node = cmds.listRelatives(current_node, parent=True, fullPath=True)[0]
l_s_parent_nodes.append(parent_node)
current_node = parent_node
return l_s_parent_nodes
def get_parent_by_type(s_node, node_type=None):
"""
!@brief Find first parent from given node type
@type s_node: string
@param s_node: Node name for start surch.
@type node_type: string
@param node_type: Type you want to surch
@rtype: string
@return: Parent node found. None if don't found node.
"""
if not isinstance(s_node, basestring):
s_node = s_node.name()
l_s_parent_nodes = get_all_parents(s_node)
for s_parent_node in l_s_parent_nodes:
if cmds.nodeType(s_parent_node) == "transform" and node_type != "transform":
l_s_shape_nodes = cmds.listRelatives(s_parent_node, shapes=True, fullPath=True, type=node_type)
if l_s_shape_nodes:
return s_parent_node
else:
return s_parent_node
return None
def get_selection():
sel = cmds.ls(selection=True, long=True)
stkLevel_node = cmds.ls(sel, type="stkLevel")
if stkLevel_node:
if len(stkLevel_node) > 1:
cmds.warning("Please select 1 stkLevel node only")
else:
return stkLevel_node
else:
root_node = []
for sel_path in sel:
split_sel_path = sel_path.split('|')[1]
if split_sel_path not in root_node:
root_stack.append(split_sel_path)
return cmds.ls(root_node, long=True)[0]
return cmds.ls(root_node, long=True)[0]
You return the first item of the list that you build up. If you want all of them, why don't you just return all of them?
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/15fb02f2-7cbf-4b2c-a3ef-58ccb8ebc86f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Maybe an an iterative loop could work?
def find_special(start):
parent = start
while parent:
if cmds.nodeType(parent[0]) == "stkLevel":
return parent[0]
parent = cmds.listRelatives(parent, parent=True)
return None # not found
Example
cmds.file(new=True, force=True)
# Generate hierarchy
cmds.spaceLocator(name="selectMe")
for group in range(5):
cmds.group()
cmds.group(name="special")
cmds.group()
# Select node
cmds.select("selectMe")
# Find "special"
def find_special(start):
parent = start
while parent:
if parent[0] == "special":
return parent[0]
parent = cmds.listRelatives(parent, parent=True)
return None # not found
start = cmds.ls(selection=True)
special = find_special(start)
print(special)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/15fb02f2-7cbf-4b2c-a3ef-58ccb8ebc86f%40googlegroups.com.