Notifications
Clear all

[Closed] align uv-vertex of multiple objects

Hi, i am looking for a way how i can align selected vertex or edges of several selected objects in the uv editor.

The problem is that aperently i can only acess the uv modifier of one object at a time, so i figured out i would have to loop through all my selected objects, get the position of the selected vertex and than move them to the average of the position. So this is the script i came up with (i am not rly good at scripting – still learning)

sel_obj = selection as array
   sel_vert = #()
   vert_pos = #()
   pos_x_array = #()
   pos_y_array = #()
   pos_x_all = 0
   pos_y_all = 0
   pos_x_avg = 0
   pos_y_avg = 0
   
   	try
   	(	
   		--suspendEditing()
   		
   		-- loop all selected objects and find selected vertices in uv editor
   		for i in sel_obj do
   		(
   			select i
   			sel_vert = i.modifiers[#unwrap_uvw].getSelectedVertices() as array
   			-- if object got selected vertices loop trough them and append position to array
   			if sel_vert[1] != undefined 
   			then
   			(
   				for f = 1 to sel_vert.count do 
   				(
   					vert_pos = i.modifiers[#unwrap_uvw].getVertexPosition 0 sel_vert[f]
   					append pos_x_array vert_pos[1]
   					append pos_y_array vert_pos[2]
   				)
   			)
   		)
   		-- loop position array and calculate average 
   		for f = 1 to pos_x_array.count do
   		(
   			pos_x_all = pos_x_all + pos_x_array[f]
   			pos_y_all = pos_y_all + pos_y_array[f]
   		)
   		pos_x_avg = (pos_x_all / pos_x_array.count)
   		pos_y_avg = (pos_y_all / pos_y_array.count)
   		
   		-- loop all objects aggain and move vertices to average position
   		for i in sel_obj do
   		(
   			select i
   			sel_vert = i.modifiers[#unwrap_uvw].getSelectedVertices() as array
   			if sel_vert[1] != undefined 
   			then
   			(
   				vert_pos = i.modifiers[#unwrap_uvw].moveX pos_x_avg
   				--vert_pos = i.modifiers[#unwrap_uvw].moveY pos_y_avg
   			)
   		)		
   		select sel_obj
   		sel_obj[1].modifiers[#unwrap_uvw].unwrap.edit ()
   		--resumeEditing()
   	)
   	catch
   	(
   		print "failed"
   		select sel_obj
   		--resumeEditing()
   	)
   

and well the script does work so thats great already but it is way to slow (takes 4-5sec when i have ~30 objects selected) i guess it is because max keeps updating the ui while i loop through the objects, so i tried to use “suspendEditing()” but than i am not able to access the uv – modifier anylonger ?

So it would be great if someone could help me optimize my script, or tell me an alternative way of doing this – maybe access the uv coordinates directly without the modifier ? (dunno if thats possible)

2 Replies

This one finds the selected Verts and aligns them all to the right i believe. This is stripped out of one of my tools…Basically you use modpanel.getcurrentobject() to get the Uv Modifier, and then i think the rest should be self explainatory.

 My formating got screwed up when pasted.  I hope its clear.

      --//////////////////////////// Get Selected Verts
        fn Get_VertSelection objMod =
         (
        		local selectedVerts = #()
        		local count = 0;
        				
        	   for obj in $selection do
        	   (
        			 vertList = (objMod.unwrap6.getSelectedVerticesByNode obj) as array
        					
        			if vertList.count != 0 then
        			(	count +=1;
        				 selectedVerts[count] = vertList
        				 selectedVerts[count][vertList.count+1] = obj	-- store the object in the array as well.
        			)
        		)	   
        		 return selectedVerts
         );
        --//////////////////////////// Get Position by Node
          fn GetUVPosition_Multi _mod obj index = 
           (
        	   return (	_mod.unwrap6.GetVertexPositionByNode 0 index obj )
           );
        --////////////////////////////  Main Code
        local unWrapMod = modpanel.getcurrentobject()		-- gets the current modifier on the stack
        selectedVerts = Get_VertSelection(unWrapMod)
        
        -- find highest point, then move verts to location
        for vertList in selectedVerts do
        (
        	  if vertList.count != 0  then
        	  (
        		   local obj = vertList[vertList.count] -- object reference was stored at the end of the array,	   hacky, but so is this exposed max code
        								
        		   value = (GetUVPosition_Multi(unWrapMod)(obj)(vertList[1])).x
        								
        		 for n = 2 to vertList.count-1 do	-- -1 remember because the last instance is an object pointer.
        		 (
        				  tempValue =  (GetUVPosition_Multi(unWrapMod)(obj)(vertList[n])).x
        				  if tempValue > value then value = tempValue
        		 )
        		 with undo on	unWrapMod.moveX value
        		  )
        	  )
        

thank you it works great