After years I’m jumping back into Maya, been trying pymel lately.
A frustration I have is that some pymel functions seem to return PyNodes,
while others return a list of [PyNode, string].
so depending on how I acquired a node, and what I pass it to, I reference it as
my_node
or
my_node[0]
this, combined with python’s loose typing is giving me a bit of a headache.
is there a general rule to apply about when I can expect a PyNode vs a List?
or do I just have to watch each pymel fucntion for what it spits out?
You could pass it through a function that always returns the result as a list, example:
#GENERAL FUNCTION: ADD AND RETURN PYNODES TO EACH OBJECT PASSED IN
def addPyNode(objects=[]):
#--DESCRIPTION-----------------------------------------------------------------------------------
# Tries to add a PyNode to each object passed in, raise error if it doesn't work.
#--EXAMPLES--------------------------------------------------------------------------------------
# addPyNode(objects=['char_l_arm', 'char_r_arm'])
#------------------------------------------------------------------------------------------------
#Create a variable for the result
result = []
#Flatten the input-list
objects = pm.ls(objects, flatten=True)
#If the input has data
if objects:
#For each object
for x in range(0, len(objects)):
#Try to add a PyNode to the current object
try:
tmpString = str(objects[x])
result.append(pm.PyNode(tmpString))
#If it didn't work, raise an error message
except:
raise Exception, "function: 'addPyNode' - Could not add a PyNode to the input: " + objects[x]
#Return the objects as a list
return list(result)