[Closed] Align Rotation
Ok, didn’t know that before. I also tried like you did with the inverse technique, but I didn’t manage to assign the result correctly. So the trick here is to use “*=” to add the quat difference to an existing quat, right? Thanks a bunch!
In exchange to your help (though I don’t think you’ll need it, but anyway), here is the script I was working on and now is complete. It automatically attaches a number of objects to a surface while keeping the initial offset.
(
try
(
source_array = #()
for s = 1 to selection.count - 1 do append source_array selection[s]
for c = 1 to source_array.count do
(
target_obj = selection[selection.count]
source_obj = source_array[c]
face_count = meshop.getnumfaces target_obj
source_position = source_obj.position
source_rotation = source_obj.transform.rotation
distance_array = #()
for v = 1 to face_count do
(
append distance_array (distance (meshop.getfacecenter target_obj v) source_position)
)
min_distance = amin distance_array
min_distance_v = finditem distance_array min_distance
source_obj.position.controller = attachment()
source_obj.position.controller.node = target_obj
attachctrl.addnewkey source_obj.position.controller 0
attachctrl.update source_obj.position.controller
(attachctrl.getkey source_obj.position.controller 1).face = min_distance_v
source_obj.position.controller = position_list()
source_obj.position.controller[2].controller = position_xyz()
source_obj.position.controller.active = 2
source_new_rotation = source_obj.transform.rotation
source_obj.rotation *= source_new_rotation * (inverse source_rotation)
source_obj.position = source_position
)
)
catch (messagebox "Please make sure to select all source objects first, then add the target object to your selection. Also make sure your target object is an Editable Mesh." title:"AdvancedAttachment")
)
Right.
“a *= b” is the same as “a = a * b”. But when you multiply matrices you have to know that the order or multipliers is important. So for matrices:
m1 = m2m3 not equals m1 = m1 * m2 * m3, it’s m1 = m1 * (m2 * m3)
Interesting. I have to dig deaper into this matrices stuff and find out why that is.