0
$\begingroup$

I want to flip the normals via python. I have a single polygon as an object loaded. I am using blender 3.4.1.

enter image description here

I try to do the following in python:

>>> bpy.data.meshes['Plane'].polygons[0].normal
Vector((0.0, 0.0, 1.0))    
>>> bpy.data.meshes['Plane'].polygons[0].flip()
>>> bpy.data.meshes['Plane'].polygons[0].normal
Vector((-0.0, 0.0, -1.0))

After executing this, the normals in the viewport did not change. Often I see the command normal_flip being used which seems to be not in the scope of the object I work with:

>>> bpy.data.meshes['Plane'].polygons[0].normal_flip()
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'MeshPolygon' object has no attribute 'normal_flip'
$\endgroup$

1 Answer 1

2
$\begingroup$

Flip normals on a selection

If you do a Flip Normals with ⎇ AltN and look at the Info Editor, you will see that the only output done is:

bpy.ops.mesh.flip_normals()

All you have to run is this. Though I would also put this inside a check for selection first, because it works only on a selection yet will run with no error nor feedback of whether it did something or not, even with nothing selected.

import bpy
import bmesh

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

if any (f.select for f in bm.faces):
    print("Flipping Normals")
    bpy.ops.mesh.flip_normals()

    
else:
    print("Nothin selected to flip normals.")

Flip selection on a specific polygon index independently of selection

normal_flip() is part of bmesh, here's hot to use it on a specific face:

import bpy
import bmesh

bm = bmesh.from_edit_mesh(bpy.context.object.data)
me = bpy.context.object.data

bm.faces[0].normal_flip()

bmesh.update_edit_mesh(me)

To learn more:

mesh - Python: find faces with incorrect normals to flip and flip them - Blender Stack Exchange

Scaling/Rezise in Blender 2.8 Python/Script not working on mesh - Blender Stack Exchange

$\endgroup$
5
  • $\begingroup$ My actual case is a pretty complex mesh, with broken normals where I only have to treat only a subset of faces. If I use mesh.flip_normals I cannot differentiate unfortunately. $\endgroup$
    – dba
    Commented Mar 23, 2023 at 16:29
  • $\begingroup$ What about Recalculate Normals (Shift N)? bpy.ops.mesh.normals_make_consistent(inside=False) $\endgroup$
    – Lauloque
    Commented Mar 23, 2023 at 16:44
  • $\begingroup$ Many have flipped by this. But still some are wrong it seems. $\endgroup$
    – dba
    Commented Mar 23, 2023 at 16:50
  • $\begingroup$ You can always select the problematic faces and run the above. But if you really want to write down the indexes of faces and treat them with normal_flip(), I'll edit that into my answer. Just a few minutes. $\endgroup$
    – Lauloque
    Commented Mar 23, 2023 at 16:55
  • $\begingroup$ Yeah, they are quite a lot. $\endgroup$
    – dba
    Commented Mar 23, 2023 at 17:06

You must log in to answer this question.

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