Notifications
Clear all

[Closed] For Loop Breaks my Script

I’ve made a little script to change the Map Channel while preserving the UVW coordinates. It works just fine when I run it outside a for loop like so:


      polynum = $.modifiers[#Automatic_Flatten_UVs].unwrap.numberPolygons ()
      $.modifiers[#Automatic_Flatten_UVs].unwrap2.selectFaces #{1..polynum}
      $.modifiers[#Automatic_Flatten_UVs].unwrap2.copy ()	
      $.modifiers[#Automatic_Flatten_UVs].unwrap.setMapChannel 1
      polynum2 = $.modifiers[#Automatic_Flatten_UVs].unwrap.numberPolygons ()
      $.modifiers[#Automatic_Flatten_UVs].unwrap.selectPolygons #{1..polynum2}
      $.modifiers[#Automatic_Flatten_UVs].unwrap2.paste false
      
  [i][b]BUT[/b][/i] When I put it into a loop to have it happen to all my selected objects, it just doesn't work.

    for s in 1 to selection.count do 
    (
    	polynum = selection[s].modifiers[#Automatic_Flatten_UVs].unwrap.numberPolygons ()
    	selection[s].modifiers[#Automatic_Flatten_UVs].unwrap2.selectFaces #{1..polynum}
    	selection[s].modifiers[#Automatic_Flatten_UVs].unwrap2.copy ()	
    	selection[s].modifiers[#Automatic_Flatten_UVs].unwrap.setMapChannel 1
    	polynum2 = selection[s].modifiers[#Automatic_Flatten_UVs].unwrap.numberPolygons ()
    	selection[s].modifiers[#Automatic_Flatten_UVs].unwrap.selectPolygons #{1..polynum2}
    	selection[s].modifiers[#Automatic_Flatten_UVs].unwrap2.paste false
    )
    

Any Ideas what I’m doing wrong here?

Here’s a note: If I enclose the above script in any kind of do statement, it stops working. For example, if I tell it:
if 1 > 0 then (
unlooped script from above
)
It exibits the same behavior.

Am I missing something in the way I’m referring to the objects that is different than doin it at the script root?

HELP!!!

4 Replies

hey, selection.count means that you’re applying the operation the same number of times you have objects selected. I think this should work:

for s in selection do
(
)

CML

That does help trim the code a bit, but i’m still not getting the right results.

What seems to be happening is that on the last few lines of the script in a loop are not selecting the faces then pasting the vertex information.

This behavior occurs if there is only one object selected, or many.

When it’s out of the loop, it works just fine.

I found the problem. The setMapChannels function is broken. But I have found a workaround. By putting a print statement referring to the object immediately after the function, it ‘revives’ the rest of the script.


 newmapch = 1
 objs = selection
 modname = Unwrap_UVW
 
 for s in 1 to selection.count do  --loop through selected objects 
 ( 
 	for m in 1 to selection[s].modifiers.count do --loop through modifiers on object  
 	(  
 
 		--if modifier is "modname" then set its map channel to 1  
 		if ((classof selection[s].modifiers[m]) == modname) do
 		(
 			print ("GOT MOD")
 			local vertnum = selection[s].modifiers[m].unwrap.numberVertices ()
 			selection[s].modifiers[m].unwrap.selectVertices #{1..vertnum}
 			
 			selection[s].modifiers[m].unwrap2.copy()
 			
 			selection[s].modifiers[m].unwrap.setMapChannel (newmapch)
 			print selection[s] -- THIS LINE MUST STAY or nothing will work
 
 			local vertnum2 = selection[s].modifiers[m].unwrap.numberVertices ()
 			selection[s].modifiers[m].unwrap.selectVertices #{1..vertnum2}
 	  
 			selection[s].modifiers[m].unwrap2.paste false
 		)	
 	) 
 )
 
 

After 2 days of breaking my back over this. I finally have it!!!
The secret was to only work on a single selection at a time. I accomplished this by looping through objects one by one, so the copy-paste would work. And work it did. Here’s my nice little script in unfinished form for you all to gawk at! (Okay… I’m a bit over-proud… but I should be able to keep my job now …)

newmapch = 1
 objs = selection
 modname = Unwrap_UVW
 selnum = selection.count 
 objnames = selection as array --Collect the names of the selected objects in an array
 clearSelection () --clear the selection set
 
 for o in 1 to selnum do --begin the loopies	
 (
 	select geometry[o]
 	modnum = geometry[o].modifiers.count
 	for m in 1 to modnum do --loop through modifiers on object  
 		(  
 		if ((classof $.modifiers[m]) == modname) do
 		(
 			print ("GOT MOD")
 			local vertnum = $.modifiers[m].unwrap.numberVertices ()
 			$.modifiers[m].unwrap.selectVertices #{1..vertnum}
 			
 			$.modifiers[m].unwrap2.copy()
 			
 			$.modifiers[m].unwrap.setMapChannel (newmapch)
 			print $ -- THIS LINE MUST STAY or nothing will work
 			local vertnum2 = $.modifiers[m].unwrap.numberVertices ()
 			$.modifiers[m].unwrap.selectVertices #{1..vertnum2}
 			
 			$.modifiers[m].unwrap2.paste false
 		)
 	)
 )
 clearSelection ()

I hope somebody can find this usefull. I’m going to get this into the GUI so it’s a bit more user-friendly then I will post it here for everybody to use =)