4

I am modifying an Import OBJ script. I want it to make all objects smooth, with Auto Smooth turned on, and with an auto_smooth_angle of 30º.

My edit is as follows: after

for mesh in new_objects: I have added

bpy.ops.object.shade_smooth() and it works as expected, but

bpy.context.object.data.use_auto_smooth=1 fails me. Any thoughts?

Just do clarify: I want this:

enter image description here

to become this:

enter image description here

rucativava
  • 143
  • 1
  • 4

1 Answers1

3

Maybe use something like this which first selects all the objects that you have listed in 'new_objects', and sets autosmooth to 1 on them, then once they're all selected it sets smooth shading on all of them

import bpy
import math

for obj in bpy.data.objects:
    obj.select = False  # safe to un-select first, all objects.

    ''' if you have a 'new_objects' list , then skip objects not on the list: '''
    # if not (obj in new_objects): continue

    if obj.type == 'MESH':
        obj.select = True 
        obj.data.use_auto_smooth = 1
        obj.data.auto_smooth_angle = math.pi/4  # 45 degrees
        # ob.data.auto_smooth_angle = math.radians(40)  # 40 degrees as radians

bpy.ops.object.shade_smooth()
zeffii
  • 39,634
  • 9
  • 103
  • 186
  • That worked [virtually] out of the box. Perfect, thanks, @zeffii. [virtually] explained: I had to comment out the import bpy and import math lines, because it was giving me an error, and I already had those imports in he beginning of the code. – rucativava Aug 24 '15 at 18:06
  • is it possible to do this without bpy.ops and/or without having to select the object? – splic Nov 29 '23 at 23:25