Cheers fellow artists!
I am trying to create custom add-on which will align Normals directions of selected Verticies to the square grid (make it parallel to the grid’s axis), but only in one axis at the time and the others should stay intact (so pretty much the same like if I would rotate it in one axis).
For this I need to somehow find the original normal direction before I will move one of the axis.
What command/function should I use so I can print vector’s split normal’s quaternion or some other unit that describes the direction?
There is absolutely missing information about this on internet.
This is my full (lame) code:
import bpy, bmesh
class NormalsX_OT_Operator(bpy.types.Operator):
bl_idname = "objx.alignx"
bl_label = "Align Normals"
bl_description = "Align Normals to Grid XYZ axis"
def execute(self, context):
obj = context.active_object
current_obj = bpy.context.active_object
# Getting index of currently selected vertices
def get_vertex_data(current_obj):
bm = bmesh.from_edit_mesh(current_obj.data)
selected_verts = [vert for vert in bm.verts if vert.select]
print_vert_details(selected_verts)
def print_vert_details(selected_verts):
num_verts = len(selected_verts) # how many verts are selected?
print("number of verts: {}".format(num_verts))
vert_indices = [id.index for id in selected_verts] # list of indices for every selected vertex
print("vert indices: {}".format(vert_indices))
# for every selected vertex, execute this
for item in vert_indices:
######## This is where I need help #########
# Finding vertex's coord Vector
# n = bpy.ops.transform.rotation_normal()
# bpy.ops.mesh.normals_tools(mode='COPY')
# print(bpy.ops.mesh.normals_tools())
######################################
print("Vector Normal: ")
print(n)
v = obj.data.vertices[item] # which vertex? Using index nr
co_final = obj.matrix_world @ v.co # co_final is the global location of the vertex
print("Vector: ")
print(co_final)
x_offset = co_final.x # taking only X value from vector
print(x_offset)
# AIM THE NORMAL TO PROPER DIRECTION ON THE AXIS
# bpy.ops.mesh.point_normals(target_location=(X, Yoffset, Zoffset)) #input previous values
# Finding vertex's coord Vector
#v = obj.data.vertices[vert_indices] # which vertex? Using index nr
# co_final is the global location of the vertex
#co_final = obj.matrix_world @ v.co
#print(co_final)
# checking if user is in the edit mode
if obj.mode == 'EDIT':
print("EditMode!")
bpy.ops.object.editmode_toggle() # reloading edit mode to update location
bpy.ops.object.editmode_toggle() # in case vertices were moved
get_vertex_data(current_obj)
else:
print("This only works in Edit Mode")
return{'FINISHED'}
My code is supposed to take selected vertex, get it’s vector’s normal’s direction and then change just X axis so it is parallel to grid.
Any help is appreciated. I am having a hard time with this. I can’t even find where to look for python commands that blender uses.
Thanks, Cheers