1

This statment is from the book practical maya programming. The author later goes on to use xform and shape as arguments in type() and dir() functions which I understand.

>>> xform, shape = pmc.polySphere()

Why / how are both xform and shape equalling pmc.polysphere?.. aren't they redundant as transform and shape nodes are created anyway when instancing a sphere? Would this cause complications later on when creating other shapes?

xform is blue in the script editor, what does that mean and how can it be used as the name of a variable?

shfury
  • 389
  • 1
  • 5
  • 15
  • 1
    Almost all maya commands that create objects return two values: the first is the transform node, the second the shape node. This example just uses regular python's ability to assign parts of a multi-part return in one go. – theodox Nov 25 '15 at 17:58
  • Ok, thanks.. So is there anything notable about assigning the values to xform and shape as opposed to value1 and value2 – shfury Nov 25 '15 at 21:55
  • no, just variable names. Good variable names = sanity. – theodox Nov 25 '15 at 22:04

2 Answers2

1

pmc.polySphere() returns a sequence with two elements. The first is assigned to xform, and the second to shape.

>>> a, b = [1, 2]
>>> a
1
>>> b
2
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Expanding the answer a little more.

You would expect executing pmc.polySphere() would give you just its transform, but it actually returns a list of its transform and shape node: [nt.Transform(u'pSphere1'), nt.PolySphere(u'polySphere1')]

You could assigned the variables like this:

sphereObj = pmc.polySphere()
xform = sphereObj[0]
shape = sphereObj[1]

But it's much more readable and Pythonic to unpack & assign the list to your variables at one go:

xform, shape = pmc.polySphere()

Just as long as you know the length of the list, you can make it a one-liner:

a, b, c, d = [1, 2, 3, 4]

Most of the time though you'll probably just need the transform, so you can always do this too:

xform = pmc.polySphere()[0]
Green Cell
  • 4,677
  • 2
  • 18
  • 49