4
$\begingroup$

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

$\endgroup$
2
  • 3
    $\begingroup$ how does it fail ? $\endgroup$
    – Chebhou
    Commented Aug 22, 2015 at 19:22
  • 1
    $\begingroup$ " AttributeError: 'NoneType' object has no attribute 'data' " $\endgroup$
    – rucativava
    Commented Aug 23, 2015 at 10:41

1 Answer 1

3
$\begingroup$

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()
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – rucativava
    Commented Aug 24, 2015 at 18:06
  • $\begingroup$ is it possible to do this without bpy.ops and/or without having to select the object? $\endgroup$
    – splic
    Commented Nov 29, 2023 at 23:25

You must log in to answer this question.

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