I would like to achieve this with python script in blender:
- select a face in edit mode, the script will get and store the orientation of the normal of that face.
2.create a cone or import an object at location, and set its rotation so its Z axis is aligned with normal of the previously face.
Following code is what i pieced together so far, it's not working as expected. Not good at math and not really knowing how the orientation is calculated, so i can only logically guess hows things works from here and there.
Hope someone here could be nice enough to walk me through and correct it for me, this is the best i can get as a beginner in coding. Thx~
import bpy
import mathutils
bpy.context.area.type = 'VIEW_3D'
bpy.context.space_data.transform_orientation = 'NORMAL'
# object to align i.e. the selected face(in edit mode)
obj = bpy.context.active_object
# normal vector of the selected face
norm = bpy.context.active_object.data.polygons[0].normal
diff = norm.rotation_difference(mathutils.Vector((0, 0, 1)))
# set quat orientation
obj.rotation_quaternion = diff
# move cursor to selected face
bpy.ops.view3d.snap_cursor_to_selected()
# save location of cursor
my_location=bpy.context.scene.cursor_location
# quit edit mode
bpy.ops.object.mode_set(mode='OBJECT')
# to simplify for this example i create a cone here
#(the original idea was to import an stl object here)
bpy.ops.mesh.primitive_cone_add(location=my_location)
# apply the quat orientation/rotation to the cone
bpy.context.active_object.rotation_mode='QUATERNION'
bpy.context.active_object.rotation_quaternion= obj.rotation_quaternion
bpy.context.area.type = 'TEXT_EDITOR'
Tried look into this thread but cannot make sense of it: Rotation by normal