Maxscript: Scale UVs

Written by Chris

3Ds Max 2018

This little code changes the size of the UVs. It can adjust it based on the element selected or the faces selected. Also it allows you to skip the multiple steps to add modifiers and so on.

If you used it, xform your object first and of course save your file, I haven’t had any issues, but each machine is different.

--Christian Pulla
--Funciton: Validates the input
fn InputValidator inputTxt = (
	--This could be done as a single line, but decided to break it so i can reuse the code somewhere else
	-- is there any input?
	if inputTxt != "" do
	(	
		--is there a number in there?
		if (trimLeft inputTxt "0123456789.").count == 0 do
		(
			--is the number 0, for this script we don't want a zero as it would break the uvs and won't be able to rescale them.
			if (inputTxt as string) != "0" do
				--there is a valid input
				return true;
		)
	)
	--if it fails any statement above, it's a invalid input.
	return false;
)

rollout boxCreator "UVWScaler"
(
	--setting up multipliers to be used
	local scaleMultiplierX = 1
	local scaleMultiplierY = 1
	
	--define UI
	edittext ux_txt1 "UX:" fieldWidth:50 labelOnTop:false
	edittext vy_txt2 "VY:" fieldWidth:50 labelOnTop:false
	button b1 "Scale All" height:25 width:80 pos:[5,50]
	button b2 "Scale Faces" height:25 width:80 pos:[5,80]
	
	--when information is entered..
	on ux_txt1 entered txt do
	(
		--to avoid multiple if's and ensure it's initialized to 1, which keeps the scale
		scaleMultiplierX = 1
		--test input and if valid assign it to be used
		if (InputValidator txt) do
			scaleMultiplierX = txt
	)
	
	--see above prefix_txt1
	on vy_txt2 entered txt2 do
	(
		scaleMultiplierY = 1
		if (InputValidator txt2) do
			scaleMultiplierY = txt2
	)	
	
	--Scale UVs of the entire mesh
	on b1 pressed do
	(
		--store selected mesh
		myPolyName = $
		
		--set subfunction variables 
		ScaleMeXU = scaleMultiplierX as float
		ScaleMeYV = scaleMultiplierY as float     

		--sometimes scaling the UVs of a mesh with History can break the model.
		macros.run "PolyTools" "ResetXForm"
		
		--select face levels
		subobjectLevel = 4
		--select all the faces of the mesh
		myPolyName.selectedfaces = #{1..myPolyName.faces.count}
		
		--scale the uvs as they are.
		macros.run "Modifiers" "Unwrap_UVW"
		myPolyName.modifiers[#unwrap_uvw].unwrap2.setFreeFormMode on
		myPolyName.modifiers[#unwrap_uvw].unwrap2.ScaleSelectedXY ScaleMeXU ScaleMeYV [0,0,0]
		
		--to make sure the mesh stays clean and won't break.
		macros.run "PolyTools" "ResetXForm"
	)
	
	--Scale the selected faces' UVs only.
	on b2 pressed do
	(
		--store selected mesh
		myPolyName = $
		
		--set subfunction variables
		ScaleMeXU = scaleMultiplierX as float
		ScaleMeYV = scaleMultiplierY as float     
	
		--scale the UVs as they are
		macros.run "Modifiers" "Unwrap_UVW"
		myPolyName.modifiers[#unwrap_uvw].unwrap2.setFreeFormMode on
		myPolyName.modifiers[#unwrap_uvw].unwrap2.ScaleSelectedXY ScaleMeXU ScaleMeYV [0,0,0]
		
		--collapse instead, as xForm can mess with the other elments if the mesh hasn't been xFormed before.
			maxOps.CollapseNode myPolyName off	
	)
)

 --create a Dialog from the rollout 
createDialog boxCreator 120 120