Advertisement
Lucas_3D

3ds Max: Two Point Align

May 20th, 2023 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. (
  2. --updated to handle groups
  3. --check if correct selection exists
  4. if (selection.count == 1) or (classof selection == ObjectSet) then
  5. (
  6. local obj = $
  7. if classof obj == ObjectSet do
  8. (
  9. for i in selection where isGroupHead i do obj = i
  10. )
  11. -- Create the variables
  12. local fromPivot
  13. local fromVector
  14. local toPivot
  15. local toVector
  16.  
  17. -- Turn on snaps
  18. snapMode.active = true
  19.  
  20. -- from object
  21. fromPivot = pickpoint snap:#3d
  22. print("from pivot is " + fromPivot as string)
  23. fromVector = pickpoint snap:#3d
  24. print("from vector is " + fromVector as string)
  25. print("and the unit vector is: " + (normalize(fromVector - fromPivot)) as string)
  26.  
  27. -- To object
  28. toPivot = pickpoint snap:#3d
  29. print("to pivot is " + toPivot as string)
  30. toVector = pickpoint snap:#3d
  31. print("to vector is " + toVector as string)
  32. print("and the unit vector is: " + (normalize(toVector - toPivot)) as string)
  33.  
  34. -- Set pivot for rotation
  35. obj.pivot = fromPivot
  36.  
  37. -- Move object to the correct location
  38. moveAmount = (toPivot - fromPivot)
  39. move obj moveAmount
  40.  
  41. -- The from vector has moved, updated it.
  42. updatedFromVector = (fromVector + moveAmount)
  43. print ("The updated from vector is: " + updatedFromVector as string)
  44. print ("As a unit vector it is: " + (normalize(updatedFromVector)) as string)
  45.  
  46. -- Create the variables for the rotation math
  47. A = normalize(updatedFromVector - toPivot)
  48. B = normalize(toVector - toPivot)
  49.  
  50. -- Calculate the cross product
  51. crossProduct = cross A B
  52.  
  53. -- Calculate the length of the cross product vector
  54. crossProductLength = length crossProduct
  55.  
  56. -- Calculate the dot product
  57. dotProduct = dot A B
  58.  
  59. -- Calculate the angle using atan2
  60. rotationAngle = atan2 crossProductLength dotProduct
  61. print ("rotation angle is: " + rotationAngle as string)
  62.  
  63. -- Find and apply the rotation Axis
  64. rotationAxis = normalize crossProduct
  65. print ("the rotation axis is " + rotationAxis as string)
  66. rotate obj (angleaxis rotationAngle rotationAxis)
  67. ) else
  68. (
  69. messageBox "You need to have the one object that you want to move selected!"
  70. )
  71. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement