This script aligns and orients objects to vertices
By:
1)Taking a selection of vertexes and storing them as an array
2)asking the user to pick a piece of geometry
3)calculating the "orientation" of a vertex by
--getting the faces which use that vertex
--calculating and combining their local orientation
4)using that info to create a resultant matrix3 transform
5)copying the target object and applying the matrix3 transform to the object
I made it to do things like add studs to fabric, or place oriented objects onto a mesh surface. It could easily be adapted to distribute randomly sized objects over a mesh
There is a problem with it - the simple code I wrote, correctly solves the orientation, but it scales up the target object. I used a workaround to solve this by resetting the scale of the object to -1 -1 -1 for x,y,z respectively, on line 27:
targetMesh.scale = point3 -1 -1 -1
I'm not an expert coder and would like some help making this more elegant. I think I understand the arithmetic I need to do to adjust the matrix at lines 19-21, but I don't know what the relationship is between the original scale of the object and the scale of the transform matrix I'm creating. If I could get some help with this, I'd really appreciate it.
---
fn orientObjectToVertex targetVertVal targetMesh= (
--work out what bits of the target object have the info we need
targetVert = targetVertVal --getVertSelection $ as array
targetFaces = meshop.getFacesUsingVert $ targetVert as array
--variable points
faceNormal = point3 0 0 0
rightVector = point3 0 0 0
upVector = point3 0 0 0
--constants
faceNum = targetFaces.count
worldUpVector = [0,0,1]
centerPos = getVert $ targetVertVal
for i = 1 to faceNum do (
faceNormal += in coordsys $ (getFaceNormal $ targetFaces[i])
rightVector += normalize (cross worldUpVector faceNormal)
upVector += normalize ( cross rightVector faceNormal)
)
theMatrix = matrix3 rightVector upVector faceNormal centerpos
targetMesh.transform = theMatrix
targetMesh.scale = point3 -1 -1 -1
)
fn g_filter o = superclassof o == Geometryclass
targetMesh = pickObject message:"Pick Target Surface:" filter:g_filter
targetVertArray = getVertSelection $ as array
targetObjectArray = #()
for q = 1 to targetVertArray.count do
(
targetObjectArray[q] = instance targetMesh
orientObjectToVertex targetVertArray[q] targetObjectArray[q]
)