0
$\begingroup$

I would like to rearrange the order of vertices for each face, so that I can use the new order for the "FIXED" --- quad_method of the Triangulate Modifier. However, I am unsure whether it is correct to assign the new order to obj.data.polygons[i].vertices.

Here are my codes:

import bpy

def unify_triangulate(input_path, output_path):


    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()


    bpy.ops.import_scene.gltf(filepath=input_path)


    obj = bpy.context.active_object
    obj.modifiers.new(name='Decimate', type='DECIMATE')
    obj.modifiers['Decimate'].decimate_type = 'DISSOLVE'
    obj.modifiers['Decimate'].angle_limit = 0.0872665  # 5 degrees in radians
    bpy.ops.object.modifier_apply(modifier='Decimate')


    obj = bpy.context.active_object


    bpy.context.view_layer.objects.active = obj
    bpy.ops.object.mode_set(mode='EDIT')


    bpy.ops.mesh.select_mode(type="FACE")
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.mesh.select_linked()



    for face in obj.data.polygons:
        for vertex_index in face.vertices:
            print(obj.data.vertices[vertex_index].co)
        print()    

    print("11111111111")


    selected_faces = [f for f in obj.data.polygons]
    i = -1
    for face in selected_faces:
        i = i+1

        vertex_indices = face.vertices


        vertices = [obj.data.vertices[i].co for i in vertex_indices]

  
        new_vertices = sorted(vertices, key=lambda x: (x[0], x[1], x[2]))
        new_vertex_indices = [vertex_indices[vertices.index(vertex)] for vertex in new_vertices]

        obj.data.polygons[i].vertices = new_vertex_indices

 
    bpy.ops.mesh.normals_make_consistent(inside=False)



    for face in obj.data.polygons:
        for vertex_index in face.vertices:
            print(obj.data.vertices[vertex_index].co)
        print()   



    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.quads_convert_to_tris(quad_method='FIXED_ALTERNATE', ngon_method='BEAUTY')


    bpy.ops.export_scene.gltf(filepath=output_path, export_format="GLTF_SEPARATE")
$\endgroup$

1 Answer 1

0
$\begingroup$

The poly with vertices 1234 can have its vertices ordered in four ways.

1234
2341
3412
4123

So only re-orderings that are rotations of the original order will not change the poly. So to sort it, you would rotate the smallest vertex to the beginning. Sorry if I was unclear.

Example to sort poly verts based on the lexicographic order of vertex coordinates: (note that it's not particularly fast)

import bpy
import random

mesh = bpy.context.active_object.data

for poly in mesh.polygons:
    if len(poly.vertices) == 3:
        continue

    min_co = mesh.vertices[poly.vertices[0]].co[:]
    min_i = 0
    for i in range(1, len(poly.vertices)):
        co = mesh.vertices[poly.vertices[i]].co[:]
        if co < min_co:
            min_co = co
            min_i = i

    # Uncomment this to randomize the order instead (for testing)
    #min_i = random.randrange(len(poly.vertices))

    poly.vertices = poly.vertices[min_i:] + poly.vertices[:min_i]

# Needed to see changes in viewport
mesh.update()

Before, order randomized:

After:

Note that in this example, the bottom-left direction is -X,-Y, so the lexicographically first vertex in each poly should be at the bottom-left of the poly. If you're wondering why the diagonals don't all run from bottom-left to top-right then, it's because of tiny noise in the vertex coordinates. This demonstrates that although this method removes the dependence on the internal vertex order of each poly, it can depend very precisely on the vertex coordinates.

(Also it obviously won't work for degenerate quads.)

$\endgroup$
1
  • $\begingroup$ Many thanks for your answer. But your codes do not work well on my project. I just reorganized my confusion, if you are interested in that, please access the new post: blender.stackexchange.com/questions/313820/… $\endgroup$
    – Bill XU
    Commented Feb 29 at 3:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .