Faces selection works in command and not in script

Hi everybody,

I’m writing a python script for blender in order to generate characters procedurally. Still at the premices, though.

I wish to select the highest face of a cube, and while the command line works when I write it in the command, it doesn’t when I put it in my script.

I do not understand why, can anyone share his lights with a poor soul ?

Thanks y’all !

Procedural generation of a character

import bpy
import bmesh
from bpy import context

mode = bpy.context.mode

if(mode != ‘OBJECT’):
bpy.ops.object.editmode_toggle()
*# make sure nothing is selected

bpy.ops.object.select_all(action =‘TOGGLE’)
bpy.ops.object.delete(use_global=True)
bpy.ops.object.select_all(action =‘TOGGLE’)
bpy.ops.object.delete(use_global=True)
bpy.ops.object.select_all(action =‘TOGGLE’)
bpy.ops.object.delete(use_global=True)

Everything starts with a cube

bpy.ops.mesh.primitive_cube_add(location = (0,0,0))

ob = bpy.data.objects[‘Cube’]

valX = ob.dimensions.x/2

bpy.ops.object.editmode_toggle()

bpy.ops.object.modifier_add(type=‘MIRROR’)

Création du torse

bpy.ops.transform.translate(value=(valX,0,0))

bpy.context.object.modifiers[“Mirror”].use_clip = True

mesh=bmesh.from_edit_mesh(bpy.context.object.data)

bpy.ops.mesh.select_all(action =‘TOGGLE’)

#selection of the highest face

Ini = 0
compteur = 0
i = 0
for f in mesh.faces:
i = i+1
Boucle = f.calc_center_median()
if(Boucle[2] > Ini):
Ini = Boucle[2]
compteur = i

mesh.faces[compteur-1].select = True

Création d’une jambe

Creation d’un bras