Maxscript function not working in a for loop (repro example),

Hello,

I’m working on a rather large script, but one problem I have so far is that flattening UVs for an object doesn’t seem to be working inside of a loop (this is just an example script, but it displays the same behavior).

In the example below, the script just creates several spheres, applies a standard material w/ a checker texture, and attempt to flatten them all in a loop, and then it manually selects two of them, and then performs the same function, but it only works on those selected ones, and not those in the loop.


(
	resetMaxFile() 
--resetMaxFile #noPrompt
fn flattentheUVs whatObject angleDeg =
	(
		max modify mode
		addModifier whatObject (Unwrap_UVW ()) ui:on
		whatObject.modifiers[#unwrap_uvw].setMapChannel 1
		whatObject.modifiers[#unwrap_uvw].flattenMap angleDeg #() 0.01 true 0 true true
	)
	
checkMap = checker Color1:black Color2: white --checker texture for the example
checkMap.coordinates.uTiling = checkMap.coordinates.vTiling = 4	
sphArray = #() --empty array to eventually contain all the spheres
offset = 100
for i = 1 to 100 by 20 do
	(
		aSphere = convertToPoly(Sphere pos:[0,i * 2, offset*sin(i)] radius: 5 material: (standard diffusemap:checkMap))
		showTextureMap aSphere.material true 
		append sphArray aSphere 
	)

for o in geometry where canconvertto o Editable_Poly do
	(
		convertToPoly o
		--max modify mode --doesn't appear to fix the problem
		flattentheUVs o 55
	)
	
--iterating through the array below also doesn't work
/* for o = 1 to sphArray.count do
* 	(
* 		convertTopoly sphArray[o]
* 		flattentheUVs sphArray[o] 55
* 	)
*/
	
--works outside of a loop though
	select $'Sphere01' 
	flattentheUVs $ 55

select $'Sphere03' 
flattentheUVs $ 55
	
select $'Sph*'
actionMan.executeAction 0 "311"  -- Tools: Zoom Extents All Selected
actionMan.executeAction 0 "50026"  -- Tools: Maximize Viewport Toggle

)

I’ve done several things from restructuring the for loop structure, etc., as from what I understand, it could be something to do with a possible Maxscript optimization of for loops (i.e.-not returning any results until completion), but I’m not sure just yet.

When you select objects, they become the ‘current object’ the modifier panel is focused on. Objects within the ‘for o in geometry’ loop never become selected or active in the modifier panel, so the flattening fn fails.

Here’s your function, adjusted to make the current ‘whatObject’ set as the current modifier panel object.


fn flattentheUVs whatObject angleDeg =
	(
		max modify mode
                
                -- This makes your object focused within the modifier panel. 
		modPanel.setCurrentObject whatObject 

		addModifier whatObject (Unwrap_UVW ()) ui:on
		whatObject.modifiers[#unwrap_uvw].setMapChannel 1
		whatObject.modifiers[#unwrap_uvw].flattenMap angleDeg #() 0.01 true 0 true true
	)

that worked for me, at least.

Very nice. Works as intended now. Thank you very much for the help. :slight_smile:

A couple of times I’ve had to resort to using “execute” to work around some issues with the Unwrap and Skin modifiers… They seem to have some weird edge cases where trying to do certain things the right way just fails.

SamiV.

Yes indeed.

At times it can be frustrating, but then when everything finally comes together, you breath a sigh of relief. :):