5
$\begingroup$

.

Hi all,

Here is the issue. I've a mesh with some faces that need to be flipped in other to have something consistent:

enter image description here

These faces are selected: enter image description here

So I would like to find the faces that must be flipped and flip them using a Python script. I tried making faces constistent by it didn't work.

My idea was the following:

  1. Loop on the bmesh vertices (I can do it)
  2. Check that the vertice is selected (I can do it)
  3. Check that the normal has to be flipped (I can't do it. Should I check the normal??
  4. Flip normal (I can do it)

I hope I explained the issue clearly. Thanks, Maxime

PS: here is my blend file enter link description here

$\endgroup$
3
$\begingroup$

enter image description here

This script finds the average normal from the selected faces, then finds normals that are very different by calculating the dot product between the average and each face normal.

Any face that has a negative dot product will be flipped. This fixes the normals on the faces in your blendfile, although your selection doesn't include all the flipped faces (if selected, they will be fixed).

The script assumes you are in edit mode with all the faces of interest selected.

import bpy, bmesh
from mathutils import Vector

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

# Reference selected face indices
bm.faces.ensure_lookup_table()
selFaces = [ f.index for f in bm.faces if f.select ]

# Calculate the average normal vector
avgNormal = Vector()
for i in selFaces: avgNormal += bm.faces[i].normal
avgNormal = avgNormal / len( selFaces )

# Calculate the dot products between the average an each face normal
dots = [ avgNormal.dot( bm.faces[i].normal ) for i in selFaces ]

# Reversed faces have a negative dot product value
reversedFaces = [ i for i, dot in zip( selFaces, dots ) if dot < 0 ]

# Deselect all faces and (later) only select flipped faces as indication of change
for f in bm.faces: f.select = False
bm.select_flush( False )

for i in reversedFaces:
    bm.faces[i].select = True
    bm.faces[i].normal_flip()  # Flip normal

bm.select_flush( True )
$\endgroup$
  • 1
    $\begingroup$ Was considering an answer using face.normal.angle(up) > degrees(90) to select faces, which I suppose is what the dot product does anyhow. $\endgroup$ – batFINGER Aug 3 '17 at 9:54
  • $\begingroup$ @batFINGER I guess using the dot could be a bit more general, and flip faces that are different than whatever other normals you want to compare them with. $\endgroup$ – TLousky Aug 3 '17 at 9:57
2
$\begingroup$

Did you try to go to edit mode, select all faces then recalculate normals? (Ctrl+N)

$\endgroup$
  • $\begingroup$ Thank you for the tip but it doesn't change anything $\endgroup$ – Maxime Aug 3 '17 at 7:14
  • 2
    $\begingroup$ @Maxime problem with your mesh is that if you rotate mesh to see under the place where normals are bad you will see sufficient faces. Delete them, then follow my advice (select faces + Ctrl N) and you will se consistent mesh. $\endgroup$ – Izzy Aug 3 '17 at 7:25
  • $\begingroup$ @Izzy What does [sufficient faces] mean? $\endgroup$ – atomicbezierslinger Aug 3 '17 at 14:20
  • 1
    $\begingroup$ @Maxime For example. Recalculate normals seems not working (although it does, just not in a way you would like) because your vertex is shared by several faces. Deleting those faces will cause problematic faces to have proper normals. Although TLousky script will work, it will not fix your original problem. That is your geometry is wrong at some points, and that is why you have incorrect normals. Fixing geometry will fix your normals as well. $\endgroup$ – Izzy Aug 3 '17 at 14:46
0
$\begingroup$

. well i had same as your issue, i was trying to resolve it, i get in your question to see the answers to resolve mine! , i found the solution, is Make Normals Consistent the shortcut Ctrl + N while you are still in the edit mode :)

thanks

$\endgroup$
  • 1
    $\begingroup$ yes but the original question was related to python (script) usage... $\endgroup$ – m.ardito Mar 12 '18 at 21:49
  • $\begingroup$ Actually i am new to blender till now i am just training, but i had this issue, so wondered to post how i found the solution. i am sorry if that doesn't benefits you as well :) $\endgroup$ – Jamil Mar 14 '18 at 10:53

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.