Python Noob question

56 views
Skip to first unread message

Johnathan Scoon

unread,
Aug 19, 2017, 9:31:51 AM8/19/17
to Python Programming for Autodesk Maya
Hey guys, I'm just playing around with simple for loops trying to get my head around it. I wrote a script that creates a series of cubes and places them a certain distance apart on the translate X axis. 

from maya import cmds
BlockList = ("BLOCK", "BLOCK1", "BLOCK2", "BLOCK3", "BLOCK4", "BLOCK5")
cmds.polyCube(n="BLOCK")
for s in BlockList:
     cmds.duplicate()
     Name = cmds.ls (selection = True)
     movevalue = 2
     postvalue = cmds.getAttr(Name[0]+".tx")
     shift = movevalue + postvalue
     cmds.move(shift,0,0)


Now this works fine but what if I wanted to increase the number of cubes to say 100 or more? It would be inefficient for me to have to type all those names out into Blocklist. I know you can represent a long list of numbers in a range, but I can't seem to find any information on how to make a range what would include object names with the numbers. Any help here is greatly appreciated! 

haggi

unread,
Aug 19, 2017, 9:43:26 AM8/19/17
to python_in...@googlegroups.com

At a first glance, you do not need the BlockList at all because you so not use the contents. You only use it to get a number of elements to iterate over. You would get the same result if you simply do a:

for i in range(10):
  cmds.duplicate()
  ....

Just in case you want to give the new objects a name, you can do a:

newName = "BLOCK{0}".format(i)

haggi

--
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/6509bf81-3b30-4099-a1c8-de3358da91cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Johnathan Scoon

unread,
Aug 19, 2017, 9:51:56 AM8/19/17
to Python Programming for Autodesk Maya, ha...@haggi.de
Hey thanks haggi, that definitely works. I'm still curious if there is a way to make a list of ascending objects into a range? Or do you think this would not ever be necessary?


On Saturday, 19 August 2017 09:43:26 UTC-4, haggi wrote:

At a first glance, you do not need the BlockList at all because you so not use the contents. You only use it to get a number of elements to iterate over. You would get the same result if you simply do a:

for i in range(10):
  cmds.duplicate()
  ....

Just in case you want to give the new objects a name, you can do a:

newName = "BLOCK{0}".format(i)

haggi


Am 19.08.2017 um 15:31 schrieb Johnathan Scoon:
Hey guys, I'm just playing around with simple for loops trying to get my head around it. I wrote a script that creates a series of cubes and places them a certain distance apart on the translate X axis. 

from maya import cmds
BlockList = ("BLOCK", "BLOCK1", "BLOCK2", "BLOCK3", "BLOCK4", "BLOCK5")
cmds.polyCube(n="BLOCK")
for s in BlockList:
     cmds.duplicate()
     Name = cmds.ls (selection = True)
     movevalue = 2
     postvalue = cmds.getAttr(Name[0]+".tx")
     shift = movevalue + postvalue
     cmds.move(shift,0,0)


Now this works fine but what if I wanted to increase the number of cubes to say 100 or more? It would be inefficient for me to have to type all those names out into Blocklist. I know you can represent a long list of numbers in a range, but I can't seem to find any information on how to make a range what would include object names with the numbers. Any help here is greatly appreciated! 
--
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.

Justin Israel

unread,
Aug 19, 2017, 6:10:40 PM8/19/17
to python_in...@googlegroups.com, ha...@haggi.de
On Sun, Aug 20, 2017 at 1:51 AM Johnathan Scoon <johnath...@gmail.com> wrote:
Hey thanks haggi, that definitely works. I'm still curious if there is a way to make a list of ascending objects into a range? Or do you think this would not ever be necessary?

It could be neccessary given the circumstances. The following creates an empty list and then performs a for-loop to append formatted strings:

objs = []
for i in xrange(1, 10):
    objs.append('BLOCK{}'.format(i))

Python provides a feature called list comprehension, which can perform the equivalent:

objs = ['BLOCK{}'.format(i) for i in xrange(1, 10)]




On Saturday, 19 August 2017 09:43:26 UTC-4, haggi wrote:

At a first glance, you do not need the BlockList at all because you so not use the contents. You only use it to get a number of elements to iterate over. You would get the same result if you simply do a:

for i in range(10):
  cmds.duplicate()
  ....

Just in case you want to give the new objects a name, you can do a:

newName = "BLOCK{0}".format(i)

haggi


Am 19.08.2017 um 15:31 schrieb Johnathan Scoon:
Hey guys, I'm just playing around with simple for loops trying to get my head around it. I wrote a script that creates a series of cubes and places them a certain distance apart on the translate X axis. 

from maya import cmds
BlockList = ("BLOCK", "BLOCK1", "BLOCK2", "BLOCK3", "BLOCK4", "BLOCK5")
cmds.polyCube(n="BLOCK")
for s in BlockList:
     cmds.duplicate()
     Name = cmds.ls (selection = True)
     movevalue = 2
     postvalue = cmds.getAttr(Name[0]+".tx")
     shift = movevalue + postvalue
     cmds.move(shift,0,0)


Now this works fine but what if I wanted to increase the number of cubes to say 100 or more? It would be inefficient for me to have to type all those names out into Blocklist. I know you can represent a long list of numbers in a range, but I can't seem to find any information on how to make a range what would include object names with the numbers. Any help here is greatly appreciated! 
--
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.

--
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/b253ca55-244f-4018-859e-93de87637bb5%40googlegroups.com.

Johnathan Scoon

unread,
Aug 19, 2017, 6:27:32 PM8/19/17
to Python Programming for Autodesk Maya, ha...@haggi.de
Thanks Justin, you're a rock star!
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
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.

Simon Anderson

unread,
Aug 20, 2017, 8:55:46 PM8/20/17
to Python Programming for Autodesk Maya, ha...@haggi.de
As Justin mentioned above, it is also better to use xrange then range, as it is more efficient in memory. As it doesn't actually create a list list in memory like range() does, xrange is a generator.

Johnathan Scoon

unread,
Aug 20, 2017, 10:08:21 PM8/20/17
to Python Programming for Autodesk Maya, ha...@haggi.de
Thanks Simon. I appreciate that explanation

Marcus Ottosson

unread,
Aug 21, 2017, 2:07:27 AM8/21/17
to python_in...@googlegroups.com, ha...@haggi.de

Small nitpick, one reason not to use xrange in favour of range is Python 3 compatibility, something that is about to enter into our lives very shortly, where range has been deprecated and xrange renamed to range.

For cases where performance either isn’t important, such as when showing a window, or where it is negligible, such as when using ranges smaller than thousands or millions, I favour range and would otherwise resort to six.moves.range which uses the right iterator for the Python you currently use.


Reply all
Reply to author
Forward
0 new messages