Calculate rotation of target object based on source

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Lets assume you have 3 identical doors in a max file. Due to Xform, the doors have lost their rotation information.
I want a MaxScript which can figure and rotate the pivots of door B and C using A as reference. In other words, door A should be selected as the master object and the rotation of object B and C should be calculated using face normal of a poly for example.
Think of this as using object A as source for transformation and rotation for object B, C. How much would you have to rotate object A if it was placed exactly the same way object B and C are.
Attaching a file with several doors. I want one of the doors to become the master object and the rest of them should have a pivot rotation in relation to the source object.
I have a semi working max script for this but it has bugs. Especially if there is an X form on the target objects then its rather difficult to even use face normal of poly.
(
local geoToUV = (for o in selection where (isKindOf o GeometryClass and canConvertTo o Editable_Mesh) collect o)
local MasterObject = null
local MasterObjectLargestFaceArea = 0
local MasterObjectLargestFaceIndex = 0
local MasterObjectLargestFace = null
local MasterObjectFaceNormal = null --polyop.getFaceNormal
local poGetFaceNormal = polyop.getFaceNormal
if geoToUV.count == 0 then messagebox "No suitable geometry selected!"
else
(
clearSelection() -- clears selection once original selection has been saved as geoToUV array
for i in 1 to geoToUV.count do -- loops through array of suitable geometry
(
obj = geoToUV[i]
select geoToUV[i]
-- convert selected object to Editable Poly if it not already is
if (classOf obj != Editable_Poly)
then (convertToPoly obj)
if(i == 1) then
(
selectedFaces = polyop.GetFaceSelection selection[1]
if selectedFaces.isEmpty == false then
(
if(selectedFaces.numberSet == 1) then
(
MasterObject = obj
MasterObjectLargestFaceIndex = MasterObject.selectedFaces[1].index
MasterObjectFaceNormal = poGetFaceNormal MasterObject MasterObjectLargestFaceIndex
local selectedIndex = MasterObjectLargestFaceIndex as string
--MasterObjectLargestFaceIndex = selectedFaces[1].index
)else
(
MessageBox "Too many faces slected on the first object"
)
)
else
(
MessageBox "No face selected"
)
) else
(
local currentFaceNormal = poGetFaceNormal Obj MasterObjectLargestFaceIndex
OffsetAngleForPivot = acos (dot MasterObjectFaceNormal currentFaceNormal)
currentMatrix = matrixFromNormal (currentFaceNormal) as eulerAngles
masterMatrix = matrixFromNormal (MasterObjectFaceNormal) as eulerAngles
AngleMatrixMaster = "X:" + ((masterMatrix.x as string) + " Y:" + (masterMatrix.y as string) + " Z:" + (masterMatrix.z as string)) as string
AngleMatrixCurrent = "X:" + ((currentMatrix.x as string) + " Y:" + (currentMatrix.y as string) + " Z:" + (currentMatrix.z as string)) as string
masterMatrixX = masterMatrix.x
masterMatrixY = masterMatrix.y
masterMatrixZ = masterMatrix.z
currentMatrixX = currentMatrix.x
currentMatrixY = currentMatrix.y
currentMatrixZ = currentMatrix.z
Xdiff = amax #(masterMatrixX, currentMatrixX) - amin #(masterMatrixX, currentMatrixX)
if (Xdiff > 180) then
(
Xdiff = 360 - Xdiff
)
Ydiff = amax #(masterMatrixY, currentMatrixY) - amin #(masterMatrixY, currentMatrixY)
if (Ydiff > 180) then
(
Ydiff = 360 - Ydiff
)
Zdiff = amax #(masterMatrixZ, currentMatrixZ) - amin #(masterMatrixZ, currentMatrixZ)
if (Zdiff > 180) then
(
Zdiff = 360 - Zdiff
)
--Negative values......
if(masterMatrix.z > 0 and currentMatrix.z < 0) then
(
Zdiff = Zdiff *-1
)
print ("AngleMatrixMaster:" + (AngleMatrixMaster as string))
print ("AngleMatrixCurrent:" + (AngleMatrixCurrent as string))
print ("Zdiff:" + (Zdiff as string))
print ("Ydiff:" + (Ydiff as string))
print ("Xdiff:" + (Xdiff as string))
--Call to function for sorting the pivot
--RotatePivotOnly obj (EulerAngles 0 0 Zdiff)
)
clearSelection()
)
select geoToUV
Messagebox ("Done")
)
)