2
$\begingroup$

I had a small autosmooth script based on this post by user @zeffii

And recently updated to blender 3.4 alpha and somehow the checkbox is not being checked when running the script. Here is the script:

import bpy
import math

selected_objects = bpy.context.selected_objects

for ob in selected_objects:
    ob.select_set(True)
    ob.data.use_auto_smooth = True 
#        ob.data.use_auto_smooth = 1
    ob.data.auto_smooth_angle = math.radians(60)  # 40 degrees as radians

bpy.ops.object.shade_smooth()

"use_auto_smooth" part is not working. The checkbox is not being check but the angle is updated. Not sure if its a bug or a mistake in the code?

Also, I used to use 1 but saw that True is now the correct way. Or are both correct to use?

$\endgroup$
3
  • 1
    $\begingroup$ Hello ! As a heads-up we don't really encourage questions and answers about alpha or beta versions. Usually most of the problems are solved at the official release date either by being fixed if they come from a bug, or by being included in the release notes if they come from a breaking change in the API. In this case it's the call to bpy.ops.object.shade_smooth() that unticks the checkbox. Call it before your for loop. (It was the same in realier versions) $\endgroup$
    – Gorgious
    Commented Oct 31, 2022 at 10:45
  • $\begingroup$ And using True instead of 1 is more semantically correct because the property is representing a boolean value, although I don't think there is any impact code-wise, 1 is cast to a boolean value automatically. It might have been the only way to set this property in the past. $\endgroup$
    – Gorgious
    Commented Oct 31, 2022 at 10:47
  • $\begingroup$ Sorry I was wrong, the change was introduced in 3.3 which is a released version. :) Writing an answer right now $\endgroup$
    – Gorgious
    Commented Oct 31, 2022 at 10:51

1 Answer 1

3
$\begingroup$

Since Blender 3.3 bpy.ops.object.shade_smooth takes use_auto_smooth as an argument. By default it is set to False, which means if you don't explicitely add the argument as True it will set data.use_auto_smooth to False.

The good thing is that it renders the for loop useless.

import bpy
import math


bpy.ops.object.shade_smooth(use_auto_smooth=True, auto_smooth_angle=math.radians(40))

Or if you don't want to rely on having to select the objects in the interface, use an Operator Override :

import bpy
import math

objects_to_smooth = [
    bpy.data.objects["Cube"],
    bpy.data.objects["Cube.001"],
    bpy.data.objects["Cube.002"],
    # etc.
]

with bpy.context.temp_override(selected_editable_objects=objects_to_smooth):
    bpy.ops.object.shade_smooth(use_auto_smooth=True, auto_smooth_angle=math.radians(40))

By the way there is now an operator to do exactly what this script does in the interface. Search for "Shade Auto Smooth".

enter image description here

$\endgroup$
1
  • $\begingroup$ Sorry you are right about the alpha version, ill keep that in mind next time. Thanks for the quick and detailed answer, it works perfectly. :) $\endgroup$ Commented Nov 1, 2022 at 1:09

You must log in to answer this question.

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