I have a python script that creates a sphere, then randomly selects a face and extrudes it.
This creates new geometry and deletes the old geometry, so I need to call bm.faces.ensure_lookup_table.
When I set the random.seed to a fixed number, I expect the same mesh to be created every time. However, the resulting new indexing does not seem to be consistent, so the first few extrusions are the same every time, but later extrusions happen at different faces every run.
Note that if the number of extrusions is low enough (20, for example), the mesh is always the same. I believe this is because in this case it always finds original faces where the indices are consistent.
import bpy import random import bmesh random.seed(3) scene = bpy.context.scene # Create an empty mesh and the object. mesh = bpy.data.meshes.new('RandomMesh') object = bpy.data.objects.new("RandomMesh", mesh) # Add the object into the scene. scene.objects.link(object) scene.objects.active = object object.select = True # Construct the bmesh sphere and assign it to the blender mesh. bm = bmesh.new() diameter=random.uniform(0.05,0.15) print("diameter", diameter) bmesh.ops.create_icosphere(bm, subdivisions=2, diameter=diameter) bm.to_mesh(mesh) # Extrude the mesh a certain number of times: extrusions = 40 print("extrusions",extrusions) bm.faces.ensure_lookup_table() bpy.ops.object.mode_set(mode='EDIT') for i in range( 0, extrusions ): # Deselect everything for v in bm.verts: v.select = False bm.select_flush(False) # Choose a random face id = random.randint(0, len(bm.faces)-1) print("id",id) face = bm.faces[id] face.select_set(True) # Extrude the selection selection = [f for f in bm.faces if f.select] extruded = bmesh.ops.extrude_face_region(bm, geom=selection) vec = -selection.normal*0.05 # Delete previous faces bmesh.ops.delete(bm, geom=selection, context=5) # Move the new faces outpaaaawards: verts = [v for v in extruded['geom'] if isinstance(v, bmesh.types.BMVert)] bmesh.ops.translate(bm, vec=vec, verts=verts) bm.faces.ensure_lookup_table() bpy.ops.object.mode_set(mode='OBJECT') bm.to_mesh(object.data) bm.free()
I'm using Blender 2.79.