Hi all,
I’ve been studying PyMEL for Maya and have come up against a situation that’s a bit puzzling to me. I was trying to add duplicated objects to a list but when I tried to access the list items the results were more lists rather than the items themselves.
Below are two quick examples, the first working and the second not working.
Specifically, in the second example it appears that I am populating the list with three lists rather than three spheres and that doesn’t make sense to me, since the ‘obj’ is a transform.
Thanks for any thoughts/suggestions!
from pymel.core import *
list = []
for i in range(3):
list.append(polySphere()[0])
print list
# Result: [nt.Transform(u'pSphere1'), nt.Transform(u'pSphere2'), nt.Transform(u'pSphere3')]
type(list[1])
# Result: <class 'pymel.core.nodetypes.Transform'> #
obj = polySphere()[0]
list = []
for i in range(3):
list.append(duplicate(obj,rr=1))
print list
# Result: [[nt.Transform(u'pSphere2')], [nt.Transform(u'pSphere3')], [nt.Transform(u'pSphere4')]]
type(list[1])
# Result: <type 'list'> #
What type of value does “duplicate()” return? Based on above, it looks like it returns a list of objects, not just a single object? I’m not a PyMel expert, but that would explain the behavior.
[QUOTE=Adam Pletcher;15239]What type of value does “duplicate()” return? Based on above, it looks like it returns a list of objects, not just a single object? I’m not a PyMel expert, but that would explain the behavior.[/QUOTE]
Thanks for the response, Adam.
Maybe the better way to ask is why would duplicating an object return a list type rather than a Transform type, since I’m duplicating a Transform type to begin with - obj = polySphere()[0] in the second example?
[QUOTE=etudenc;15242]
Maybe the better way to ask is why would duplicating an object return a list type rather than a Transform type, since I’m duplicating a Transform type to begin with - obj = polySphere()[0] in the second example?[/QUOTE]
If you look at the return types for duplicate, even the MEL version returns an array. duplicate assumes you may have more than one object being acted upon, so it returns a list.
[QUOTE=djTomServo;15251]If you look at the return types for duplicate, even the MEL version returns an array. duplicate assumes you may have more than one object being acted upon, so it returns a list.[/QUOTE]
Ah that makes sense, thank you for clearing that up.
In some circumstances, you may also want to try using extend() rather than append(). It adds the items of a list rather than the list itself.
A simple example to illustrate what it does:
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
a.extend(b)
b.append(c)
print a
print b
Result:
[1, 2, 3, 4, 5, 6]
[4, 5, 6, [7, 8, 9]]
Also as a rule- try to use map
or list comprehensions, rather than appending to a list like the original example.
@ clesage and Rob, Thank you for the useful tips, will definitely put into practice!