Notifications
Clear all

[Closed] Normalize UV clusters???

Does any one know of a script or a way to select all your uv clusters and normalize there scale. Some of the mapping tools, (flatten mappin) in the unwrap modifier, will normalize they’re scale as it flatten maps it, but is there a way to do this to uv clusters that are already unrwraped?
Thanks,
David

1 Reply
1 Reply
(@prettypixel)
Joined: 11 months ago

Posts: 0

This script does what you request.

macroscript normalizeUVWMacro
	category:"UVWtoolBox"
	buttonText:"normalizeUVW"
	toolTip:"normalizeUVW"
(
global normalizeUVWFloater
try (closerolloutfloater normalizeUVWFloater)catch()
rollout normalizeUVWRollout "normalize UVW 0.1a"
(
group "channel"
	(
	spinner channel_osd "channel:" range:[1,99,1] type:#integer scale:1 fieldwidth:28
	)
group "normalize"
(
	button normalizeUVW_osd "normalize UVW"
)
label lb0 "beware alpha version !!!"
label lb1 "by Olivier Vandecasteele"
label lb2 "email: gray@skynet.be"

fn getSizeUVW obj currentFaces objUnwrap objUnwrap2 =
	(
	TVmode=objUnwrap2.getTVSubObjectMode()
	TVelementMode=objUnwrap2.getTVElementMode()
	objUnwrap2.setTVSubObjectMode 1
	objUnwrap2.setTVElementMode false
	
	objUnwrapGetVertexPosition=objUnwrap.getVertexPosition
	objUnwrapGetVertexIndexFromFace=objUnwrap.getVertexIndexFromFace
	objUnwrapNumberPointsInFace=objUnwrap.numberPointsInFace
	coord=objUnwrapGetVertexPosition 0f (objUnwrapGetVertexIndexFromFace currentFaces[1] 1)
	minU=coord.x ; minV=coord.y ; minW=coord.z
	maxU=coord.x ; maxV=coord.y ; maxW=coord.z
	for f in currentFaces do
		(
		nVerts=objUnwrapNumberPointsInFace f
		for v=1 to nVerts do
			(
			coord=objUnwrapGetVertexPosition 0f (objUnwrapGetVertexIndexFromFace f v)
			if coord.x<minU do minU=coord.x
			if coord.y<minV do minV=coord.y
			if coord.z<minW do minW=coord.z
			if coord.x>maxU do maxU=coord.x
			if coord.y>maxV do maxV=coord.y
			if coord.z>maxW do maxW=coord.z
			)--for v
		)--for f
	objUnwrap2.setTVSubObjectMode TVmode
	objUnwrap2.setTVElementMode TVelementMode
	-- min,max,size,center
	#([minU,minV,minW],[maxU,maxV,maxW],[maxU-minU,maxV-minV,maxW-minW],[(minU+((maxU-minU)/2.0)),(minV+((maxV-minV)/2.0)),(minW+((maxW-minW)/2.0))])
	)--fn

fn normalizeCoord coord size =
	(
	sx=if size[3].x==0 then 1.0 else (1.0/size[3].x)
	sy=if size[3].y==0 then 1.0 else (1.0/size[3].y)
	sz=if size[3].z==0 then 1.0 else (1.0/size[3].z)
	coord=coord*(transMatrix [-size[1].x,-size[1].y,-size[1].z])*(scaleMatrix [sx,sy,sz])
	)

fn normalizeUVW obj =
	(
	addModifier obj (Unwrap_UVW ())
	objUnwrap=obj.modifiers[1].unwrap
	objUnwrap2=obj.modifiers[1].unwrap2
	objUnwrap.setMapChannel channel_osd.value
	objUnwrap.edit()
	objUnwrap.displayMap false
	objUnwrap2.setGridVisible false
	objUnwrap2.setTVSubObjectMode 3
	objUnwrap2.setTVElementMode true
	objUnwrap2.selectFaces #{1..obj.numfaces}
	facesArray=(objUnwrap2.getSelectedFaces()) as array
	size=getSizeUVW obj facesArray objUnwrap objUnwrap2
	objUnwrap2.setTVSubObjectMode 1
	objUnwrap2.setTVElementMode false
	nVerts=objUnwrap.NumberVertices()
	for v=1 to nVerts do
		( objUnwrap.setVertexPosition 0f v (normalizeCoord (objUnwrap.getVertexPosition 0f v) size) )
	)

on normalizeUVW_osd pressed do
	(
	sel=getCurrentSelection()
	max modify mode
	for obj in sel do
		(
		select obj
		normalizeUVW obj
		)
	max create mode
	max views redraw
	)--on

)--rollout
normalizeUVWFloater = newRolloutFloater "normalizeUVW" 162 144
addRollout normalizeUVWRollout normalizeUVWFloater rolledUp:false
)--macro

Installation of the macroscript:

  1. Click the MAXScript item in the Main Menu, select Run Script…
  2. Locate and select the script and press Open
  3. Go to Customize > Customize User Interface and select the desired Tab (Keyboard, Toolbars, Quad or menu)
  4. Locate the category ‘UVWtoolBox’ under group ‘Main UI’
  5. Search in the category for the name ‘normalizeUVW’
  6. Drag the script to the toolbar, Menu, QuadMenu or assign to a Shortcut

Beware. I wrote it with the code of my other scripts without optimisation and without verifications of yours objects. If the objects cannot receive UV the script will not work.

I hope this helps.