[Closed] Object orientation and position-script ptoblems
Step by step:
I have an obj1(plane object), on which poly1(polygon) is selected
I have a Teapot object
How to orient an Teapot so its Z axis to match the normal of a poly1, and its X axis to match the local X axis of the obj1?
Right now I am using this:
(
faceCenter = polyop.GetSafeFaceCenter obj1 f
-- match the object axies to the surface axies
Teapot.transform = obj1.transform
Teapot.pos = faceCenter
-- Teapot.dir = poGetFaceNormal obj1 f
--// match the Z axis of the object with the face normal
-- the X axis of the object should(I hope) match the surface local X axis
faceMatrix = matrixFromNormal (polyop.GetFaceNormal obj1 f)
Teapot.transform *= \
(inverse \
( quat \
(acos (dot Teapot.transform[3] faceMatrix[3])) \
( normalize (cross Teapot.transform[3] faceMatrix[3]) )\
) \
) as matrix3
-- put the object in the proper position
Teapot.pos = faceCenter
)
I have to put that in a position_script controler.
I’ve tried with the code above, but Max tells me that I can’t use the Teapot in the controler’s expression(Illegal self-reference in controller script).
Is the position_script a necessity? Given that you are setting the orientation, transform_script seems to be more appropriate here:
(
local obj = convertToPoly (Plane lengthSegs:20 widthSegs:20)
local pot = Teapot radius:2
local scriptTM = transform_script()
scriptTM.addConstant #face 110
scriptTM.addObject #obj (NodeTransformMonitor node:obj)
scriptTM.script = "x = normalize obj.transform.row1
" +
"z = polyOp.getFaceNormal obj face
" +
"y = normalize (cross x z)
" +
"pos = polyOp.getSafeFaceCenter obj face
" +
"matrix3 (normalize (cross z y)) y z pos"
pot.transform.controller = scriptTM
)
using the snippet above i show the trick how to make this controller be updated on plane deformation:
(
delete objects
local obj = convertToPoly (Plane lengthSegs:20 widthSegs:20)
local pot = Teapot radius:2
local scriptTM = transform_script()
scriptTM.addConstant #face 110
scriptTM.addObject #obj (NodeTransformMonitor node:obj)
[b]scriptTM.addObject #att (Attachment node:obj)[/b] -- ************
scriptTM.script = "x = normalize obj.transform.row1
" +
"z = polyOp.getFaceNormal obj face
" +
"y = normalize (cross x z)
" +
"pos = polyOp.getSafeFaceCenter obj face
" +
"matrix3 (normalize (cross z y)) y z pos"
pot.transform.controller = scriptTM
)
Thank you both of you.
With Denis addition the teapot behaves just like I want.
How to remove the transform_script, so the object can be moved freely in the scene?