CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 04-13-2013, 04:35 PM   #1
lazed
New Member
portfolio
Laszlo Mandi
bournemouth, United Kingdom
 
Join Date: Apr 2013
Posts: 7
Help with Python For loop? (solved)

hey guys currently working on my scripting assignment at ncca, i am allowed to ask for feedback so here is my problem.

im making a script to select verticies on a plane, only selecting every second one in every second row without selecting the ones on the edges. So far i got this far and it is working. now at the end as u can tell i have 5 for loops but im pretty sure i do the same thing with putting one for loop inside the other but i cant get my head around how to do it. any feedback would be great. Thanks

import maya.cmds as cmds
cmds.polyPlane(n="ground", sx=10, sy=10, w=300, h=300)
psw = cmds.getAttr("polyPlane1.subdivisionsWidth")
psh = cmds.getAttr("polyPlane1.subdivisionsHeight")

vertexcount = (psw+1)*(psh+1)-1
print vertexcount



startVert = vertexcount/psw
endVert = ((startFace-2)*2)+1
row = (psw + psh)+2
print row
print startVert

for i in range(startVert, endVert, 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+row, endVert+row, 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+(row*2), endVert+(row*2), 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+(row*3), endVert+(row*3), 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+(row*4), endVert+(row*4), 2):
cmds.select("ground.vtx[%d]" % i, add=True)

Last edited by lazed : 04-13-2013 at 08:11 PM.
 
Old 04-13-2013, 06:34 PM   #2
Joviex
Modern Major General
 
Joviex's Avatar
portfolio
Alexander Morano
Monkey who can talk
Bel Aire, USA
 
Join Date: Sep 2002
Posts: 814
Post

Quote:
Originally Posted by lazed
. So far i got this far and it is working. now at the end as u can tell i have 5 for loops but im pretty sure i do the same thing with putting one for loop inside the other but i cant get my head around how to do it. any feedback would be great. Thanks


for i in range(startVert, endVert, 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+row, endVert+row, 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+(row*2), endVert+(row*2), 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+(row*3), endVert+(row*3), 2):
cmds.select("ground.vtx[%d]" % i, add=True)

for i in range(startVert+(row*4), endVert+(row*4), 2):
cmds.select("ground.vtx[%d]" % i, add=True)


my assumption is you want the above to shrink into a double loop?

If so, you are algorithmicly offsetting the starts and ends, so you can parameterize said offsets in an outer loop and collapse them all down to:

Code:
for j in range(0, 4): for i in range(startVert+(row*j), endVert+(row*j), 2): cmds.select("ground.vtx[%d]" % i, add=True)


You could also do other reduction to make the full list of selections (string) and feed them all to a single cmds.select instead of firing one off for each, etc...

But it depends on if speed is a goal, or the sample size if extremely large, etc...

HTH
__________________
~~ Ordinary people built the ark, specialists built the titanic.
 
Old 04-13-2013, 06:54 PM   #3
lazed
New Member
portfolio
Laszlo Mandi
bournemouth, United Kingdom
 
Join Date: Apr 2013
Posts: 7
Yes your assumption was right. Thanks so much for helping me. that works like a charm. speed isnt really a goal but i would be interested to see how you would go on reducing it more if you didnt mind ?

Last edited by lazed : 04-13-2013 at 08:12 PM.
 
Old 04-13-2013, 11:21 PM   #4
Joviex
Modern Major General
 
Joviex's Avatar
portfolio
Alexander Morano
Monkey who can talk
Bel Aire, USA
 
Join Date: Sep 2002
Posts: 814
ok then =p

So, instead of just a here ya go, a little exposition.

A simple scene with 1000 pSpheres to select.

Code:
import maya.cmds as cmds import time t = time.time() for x in range(1, 1000): cmds.select('pSphere%d' % x, add=1) print time.time()-t t = time.time() s = ['pSphere%d' % x for x in range(1, 1000)] cmds.select(s, add=1) print time.time()-t 16.4590001106 0.0820000171661


That is 2x 2 orders of magnitude (200x) faster if you bundle the selection.

Of course you are always going to fight speed vs. memory usage. If you can spare the memory....

The reason is: the adding flag means maya will push all the current selection, add the new without adding something twice <--- there is the time suck

I'd imagine, I dont know the code in Maya per se, but this is what it feels like it is doing because the more items you add the time seems O(N).

so! the second example is much faster because we just tell Maya to add all the items at the same time, rather than re-loop everything one item at a time.

Thus, to re-write the first stuff the same as the second example:

Code:
s = ["ground.vtx[%d]" % i for i in range(startVert+(row*j), endVert+(row*j), 2) for j in range(0, 4)] cmds.select(s, add=1)

or if you like the original long hand:

Code:
s = [] for j in range(0, 4): for i in range(startVert+(row*j), endVert+(row*j), 2): s.append("ground.vtx[%d]" % i) cmds.select(s, add=1)

OR, if you want some hybrid to ease the amount of obscure code:

Code:
s = [] for j in range(0, 4): s.extend(["ground.vtx[%d]" % i for i in range(startVert+(row*j), endVert+(row*j), 2)] cmds.select(s, add=1)

etc...

HTH
__________________
~~ Ordinary people built the ark, specialists built the titanic.
 
Old 04-14-2013, 01:14 AM   #5
lazed
New Member
portfolio
Laszlo Mandi
bournemouth, United Kingdom
 
Join Date: Apr 2013
Posts: 7
wow amazing reply thanks for your time. It defently tought me alot and thats always helpful because im currently a first year student at the NCCA !

Many thanks again !
 
Old 04-14-2013, 01:14 AM   #6
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,480
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 07:02 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.