1
$\begingroup$

I want to activate the biggest face by a script. It's no problem to locate and get the data, but how can I activate it in Editor?

# Activate biggest face exclusivly:
import bpy
import operator

# List of Polygons of (first) selected object: 
pgList = bpy.context.selected_objects[0].data.polygons

# sort and get biggest face:
pgMax = sorted(pgList, key=operator.attrgetter('area'), reverse=True)[0]
print("{0:} Polygons, largest = {1:.2f},   selected = {2:}".format(len(pgList),pgMax.area,pgMax.select))

# How do I activate this Polygon?
# The results are very random. Sometimes selected, sometimes not, sometimes Blender chrashes.
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
pgMax.select=True
bpy.ops.object.mode_set(mode = 'EDIT')

#Later i'ld like to create a TransformOrientationSystem. But this will be next problem.
#pgMax ....  .transform.create_orientation(use=True)
# erzeuge dazu ein lokales Bezugssystem

```
$\endgroup$

2 Answers 2

2
$\begingroup$

Set the active face

To set the active face

me.polygons.active = <index of face>

or via bmesh

bm.faces.active = <BMFace>

Bmesh

Can use bmesh in edit mode, bound to the object being edited. Takes away the need to toggle modes

Notice I set the mode to face. Otherwise would need to flush the selection.

import bpy
import bmesh
context = bpy.context

context.tool_settings.mesh_select_mode = (False, False, True) # faces
ob = context.edit_object
me = ob.data

bm = bmesh.from_edit_mesh(me)

faces = sorted(bm.faces, key=lambda f:f.calc_area())
bf = faces.pop()
bf.select = True
for f in faces:
    f.select = False
bm.faces.active = bf
bmesh.update_edit_mesh(me)

In object mode can set all to false by looping or via

import bpy
context = bpy.context
context.tool_settings.mesh_select_mode = (False, False, True) # faces
# deselect all polygons

ob = context.object
me = ob.data
all_false  = [False] * len(me.polygons)
mp = sorted(me.polygons, key=lambda p: p.area).pop()
me.polygons.foreach_set("select", all_false)
mp.select = True
me.polygons.active = mp.index

set random active face in python without toggle mode

Is the active object and zeroth selected object always the same?

$\endgroup$
-1
$\begingroup$

So there was just missing 1 Line:

     pgList.active=pgMax.index

But changing the object.mode after sorting the list chrashed my index.

It works in this order:

import bpy

# List of Polygons of (first) selected object: 
pgList = bpy.context.selected_objects[0].data.polygons

bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
   # didnt work for me: ... foreach_set("select", [False] * len(me.polygons))

bpy.ops.object.mode_set(mode = 'OBJECT')
# sort and get biggest face:
pgMax = sorted(pgList, key=lambda p: p.area, reverse=True)[0]
    # !Dont change object.mode now, or index will crash!
print("{0:} Polygons, largest = {1:.2f},   selected = {2:}".format(len(pgList)
    ,pgMax.area,pgMax.select))

pgMax.select=True
pgList.active=pgMax.index  # <<<== this was missing

bpy.ops.object.mode_set(mode = 'EDIT')

#Later i'ld like to create a TransformOrientationSystem. But this will be next problem.
#pgMax ....  .transform.create_orientation(use=True)
# erzeuge dazu ein lokales Bezugssystem
```
$\endgroup$
1
  • $\begingroup$ Hi! This is not a regular forum. Please help us making this site a real knowledgebase and explain in detail (if necessary) what's actually required to understand when it comes to "activating a face by script" in order to make this post useful for future visitors too. Please read: blender.stackexchange.com/help/how-to-answer Thanks $\endgroup$
    – brockmann
    May 4, 2020 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .