In order to quickly “recalculate the normals” – change the direction of the normals so that they are all directed towards the outer surface of the mesh, using the Blender Python API, we can create a script consisting of just a few lines of code.
Let’s recalculate the normals for all meshes selected in the scene.
First, we need to go into editing mode:
1 |
bpy.ops.object.mode_set(mode='EDIT') |
Loop through the selected objects:
1 |
for obj in bpy.context.selected_objects: |
We can recalculate normals using the Blender Python API with the “normals_make_consistent()” operator. This operator works with the currently active object in the scene. Therefore, we need to make each object passing through our loop active and next, use the operator:
1 |
bpy.context.view_layer.objects.active = obj |
Also, before using the operator, we need to select all the mesh polygons so that the normals are recalculated correctly:
1 |
bpy.ops.mesh.select_all(action='SELECT') |
And now simply call the operator itself:
1 |
bpy.ops.mesh.normals_make_consistent(inside=False) |
In the “inside” parameter, we pass the desired direction of the normals. False – the direction “outwards” of the mesh, True – inwards.
Full code:
1 2 3 4 5 6 7 8 |
import bpy bpy.ops.object.mode_set(mode='EDIT') for obj in bpy.context.selected_objects: bpy.context.view_layer.objects.active = obj bpy.ops.mesh.select_all(action='SELECT') bpy.ops.mesh.normals_make_consistent(inside=False) |
After executing this code, the normals for all selected meshes will be recalculated to look “outside” of the mesh.