I need a script to extract some info from selected faces, and I need to output faces as if they are triangles, i. e. as if I used Triangulate tool on these faces, or used something else, I just need my ngons to be a bunch of triangles. Also, it would be great if the original mesh is left unchanged. The faces' info includes vertices positions, uv coordinates, and normals. So far I have managed to get this script:
import bpy
import bmesh
# Copy mesh so that the original is intact
mesh = bmesh.from_edit_mesh(bpy.context.edit_object.data).copy()
# Delete unselected faces
# This is the only way I managed to limit the faces the script works on
bmesh.ops.delete(mesh, geom = [f for f in mesh.faces if not f.select], context = 'FACES')
# Triangulate mesh so that faces are triangles
bmesh.ops.triangulate(mesh, faces = mesh.faces, quad_method = 'BEAUTY', ngon_method = 'BEAUTY')
# Iterate through faces and get info
for face in mesh.faces:
for vert in (0, 1, 2):
print('coordinates:', face.verts[vert].co)
print('uv:', face.loops[vert][mesh.loops.layers.uv[0]].uv)
print('normal:', face.normal) # oops
You already might see some issues with this script. I probably could have used loops
instead of faces, but I am not sure if loops
always consist of three vertices. I probably could have used calc_loop_triangles
but I gave up on it since I found bmesh.ops.triangulate
. I also don't understand the difference between Mesh
and BMesh
and other types of primitives. They all seem the same yet the scripts I saw on the internet use them differently. Please let me know if there are better ways to code this.
Script works fine, but only when I have flat shading. I added oops
comment where I use face normal, instead of vertex split normal. I would like this script to use split normal instead of face normal, since split normal is somewhat universal (changes accordingly in flat/smooth shading). I was not able to find a solution to this problem, because all scripts out there use loops
or don't separate selected/unselected faces, or there was some other issue when I tried integrating it in my code.
I am a complete beginner to Blender python.