Storing custom data on a face
Example adds an int layer, named "Selection" to the context object mesh (in edit mode) and assigns each faces corresponding layer value to 1 if selected else 0.
This data is stored "on" the face, it will be lost if the face is deleted, but will remain the same if the face indices are re-sorted.
import bpy
import bmesh
context = bpy.context
obj = context.edit_object
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)
# custom int property on face
selectionlayer = bm.faces.layers.int.get("Selection")
if not selectionlayer: # create one
selectionlayer = bm.faces.layers.int.new("Selection")
# assign 1 if face selected else 0
for f in bm.faces:
f[selectionlayer] = int(f.select)
bmesh.update_edit_mesh(mesh)
mesh.update()
Save and close, reopen and run, and there will still be an int prop assoc with each face that will match the selection when above was run.
import bpy
import bmesh
context = bpy.context
obj = context.object
mesh = obj.data
bm = bmesh.new()
bm.from_mesh(mesh)
# get the custom layer
customlayer = bm.faces.layers.int.get("Selection")
if customlayer: # create one
# print face index, layer value
for f in bm.faces:
print("face %d " % f.index, f[customlayer])