1

I'm currently working on an Addon that handles polyhedra and I need to change the faces' indices in order to keep my structure coherent when the user does operations like extrusion, etc...

So far I have this Operator that basically modifies the index of every face that is selected by the user.

class Extrude(Operator):
    def execute(self, context):
        context = bpy.context
        obj = context.edit_object
        me = obj.data
        bm = bmesh.from_edit_mesh(me)
    # list of selected faces
    selfaces = [f for f in bm.faces if f.select]
    if selfaces:
        for f in selfaces :
                f.index = something

    return {'FINISHED'}

During the method's execution the indices are modified, but at the end the indices are back to their original values. I tried using bm.faces.index_update() but it still does not work. Is their a way to make these index changes permanent ?

Thank you in advance.

Wass
  • 13
  • 2

1 Answers1

1

Call sort, not index update.

Exactly as explained here, https://blender.stackexchange.com/a/36619/15543 can set all of a BMVert, BMEdge, BMFace indices and then call collections sort to have new order.

Here is an example that simply shuffles all face indices

import bpy
import bmesh
from random import shuffle
from bpy import context

ob = context.edit_object me = ob.data bm = bmesh.from_edit_mesh(me)

indices = list(range(len(bm.faces))) shuffle(indices) for i, f in zip(indices, bm.faces): f.index = i

bm.faces.sort() bmesh.update_edit_mesh(me)

  • Suggest index update method is designed to "re-validate" or ensure the indices of the bmesh sequence, eg n faces have indices f.index in range(n) It's partner in crime is ensure_lookup_table which is required before indexing a sequence bm.faces[index].

  • Update edit mesh to apply the changes.

  • Question "code" offers up some questions? What is the expected result for a 100 face mesh with only 10 faces selected. How are the 90 other "somethings" ordered? Maybe by swapping values, eg

     v[i].index, v[j].index = j, i
    
batFINGER
  • 84,216
  • 10
  • 108
  • 233