# set random active face in python without toggle mode

In python how can I set random active Face in edit mode without toggle the Object/Edit Mode ?

For example re code in this answer can you also automatically set an active face again from the script to loop the option ?

• Use the bmesh module. – Leander Jan 15 '19 at 17:03
• ok but how to write the function ? – DayNightler Jan 15 '19 at 17:14
• Well, you know how to write a python function, right? You get the active mesh, get the bmesh from it, generate a random number and select that face from it. At the end you write your bmesh back. Where are you stuck / What have you tried? – Leander Jan 15 '19 at 17:16
• On Blender.SE, it is common to show that you are willing to do the work yourself with guidance. If the question looks like an unpaid job offer, it may even get closed. You can always edit your question to include more details. Writing the function and explaining every bit of it wouldn't be in the scope of a normal answer. – Leander Jan 15 '19 at 17:18
• Possible duplicate of Python set active face/batch unwrap Follow Active Quads – Duarte Farrajota Ramos Jan 15 '19 at 18:07

Random active face

Please heed the advice given in comments above. One of the (IMO) dopier rules on bse is the inability of new users to comment.

Noticed your deleted post here re Deselect Neighbours Faces

Here is a test script using method outlined in https://blender.stackexchange.com/a/81425/15543 Sets a random face active, and selects it.

import bpy
import bmesh
from random import randint

def random_face(bm):
bm.faces.ensure_lookup_table()
return bm.faces[randint(0, len(bm.faces) - 1)]

context = bpy.context
ob = context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
print("before", bm.faces.active)
bm.faces.active = random_face(bm)
bm.faces.active.select = True
print("after", bm.faces.active)

bmesh.update_edit_mesh(me)


To also make the face the active select history

bm.select_history.add(bm.faces.active)
print(bm.select_history.active is bm.faces.active)

• does not work ? if I execute it nothing happens... or i get a report message (wm_operator_invoke: invalid operator call) ... – DayNightler Jan 16 '19 at 7:59
• Thanks. How are you "executing" it? Have no idea how that error message is related??? – batFINGER Jan 16 '19 at 8:09
• oh it works, sorry was my fault, I forgot to go to the edit mode. – DayNightler Jan 16 '19 at 8:42