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 ?

  • $\begingroup$ Use the bmesh module. $\endgroup$ – Leander Jan 15 '19 at 17:03
  • $\begingroup$ ok but how to write the function ? $\endgroup$ – DayNightler Jan 15 '19 at 17:14
  • $\begingroup$ 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? $\endgroup$ – Leander Jan 15 '19 at 17:16
  • 2
    $\begingroup$ 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. $\endgroup$ – Leander Jan 15 '19 at 17:18
  • 3
    $\begingroup$ Possible duplicate of Python set active face/batch unwrap Follow Active Quads $\endgroup$ – 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):
    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)


To also make the face the active select history

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.