0
$\begingroup$

Hi I'm trying to find a script to use in Blender that would select each mesh one by one (I have 350 to do), go into edit mode, select all the faces, recalculate the normal's and then set from faces all the normal's.

I'm new to Blender scripts so any help would be greatly helpful!

$\endgroup$
  • $\begingroup$ You will need a script to recalculate the normals. What do you mean "set from faces all the normal's"? $\endgroup$ – David May 4 '18 at 13:42
1
$\begingroup$

I figured out how to do this using the following script.

import bpy

if bpy.context.selected_objects != []:
for obj in bpy.context.selected_objects: #loop through all the selected objects
    if obj.type == 'MESH':
        bpy.context.scene.objects.active = obj
        bpy.ops.object.editmode_toggle() #enter edit mode
        bpy.ops.mesh.select_all(action='SELECT') #select all objects elements
        bpy.ops.mesh.normals_make_consistent(inside=False) #recalc normals
        bpy.ops.mesh.set_normals_from_faces() #set from faces
        bpy.ops.object.editmode_toggle() #exit edit mode
$\endgroup$
  • $\begingroup$ I have replaced the code with more common tools (bmesh), which will perform better. $\endgroup$ – Leander May 4 '18 at 9:51
1
$\begingroup$

As batFINGER suggested, I have added this as a separate answer.

This alternate script includes more common python practices.

import bpy
import bmesh

if bpy.context.selected_objects != None:
    for obj in bpy.context.selected_objects: #loop through all the selected objects
        if obj.type == 'MESH':
            bm = bmesh.new()        # create a new bmesh
            bm.from_mesh(obj.data)  # assign the data of the obj (mesh-datablock)
            bmesh.ops.recalc_face_normals(bm, faces = bm.faces) 
            bm.to_mesh(obj.data)    # write the bmesh data back to the object data
            obj.data.update()       # this line isn't necessary,
                                    # but otherwise, the change is only shown 
                                    # at the next scene update and not
                                    # immediately
  1. Compare the selected_objects list with None instead of creating a new list. (Unmeasurably faster, but reads like english.)
  2. Use the bmesh module to manipulate meshes. Avoid the ops commands.
$\endgroup$

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.