Code Snippet

Maya - UV Shell Aignment


NOTE: Select two UV points you want to use as reference, and run the script.


# Paul Ambrosiussen - Technical Artist
# Free Code Snippet

import maya.cmds as cmds
from math import atan, degrees

def RotationDirection(a_Positions):
	if a_Positions[0] > a_Positions[2]:
		if a_Positions[1] > a_Positions[3]:
			return 1
	else:
		if a_Positions[3] > a_Positions[1]:
			return 1
	return -1

def RotateShellByAngleAndPivot(a_Angle, a_Pivot):
	cmds.polyEditUV( pivotU=a_Pivot[0], pivotV=a_Pivot[1], angle=a_Angle)

def PointsAligned(a_Positions):
	return [a_Positions[0] == a_Positions[2], a_Positions[1] == a_Positions[3]]

def GetPointPositions(a_UVPoints):
	cmds.select(a_UVPoints)
	return cmds.polyEditUV( query=True )

def GetAngleBetweenPoints(a_Positions):
	Radians = atan( abs( a_Positions[0] - a_Positions[2] ) / abs( a_Positions[1] - a_Positions[3] ) )
	return degrees(Radians)

def MainFunction():

	## Got a selection of max two UV points
	items = cmds.filterExpand(sm=35)[:2]
	if len(items) < 2:
		return

	##calculate center between two UV points
	t_UVPos = GetPointPositions(items)
	t_center = [0,0]
	t_center[0] = ( t_UVPos[0] + t_UVPos[2] ) / 2.0
	t_center[1] = ( t_UVPos[1] + t_UVPos[3] ) / 2.0

	#Calculate Angle between UV points
	Angle = 0.0
	if not (PointsAligned(t_UVPos)[0] or PointsAligned(t_UVPos)[1]):
		Angle = GetAngleBetweenPoints(t_UVPos) * RotationDirection(t_UVPos)

		#Got entire UV shell selected
		mel.eval("polySelectBorderShell 0")

		#Rotate around Pivot
		RotateShellByAngleAndPivot(Angle, t_center)

	cmds.select(cl=1)

MainFunction()