1

Instead of clicking shade smooth, then auto smooth separately, I want to turn this into a single operation via python. I want this to work on all meshes I add in. How would I do this? Thanks in advance

MGM-51
  • 13
  • 3

3 Answers3

1

You can use the Info editor to find out python commands which you run through the UI. Switch to the Scripting workspace, and do right click > shade smooth ....

Here's the output

bpy.ops.object.shade_smooth()
bpy.context.object.data.use_auto_smooth = True
bpy.context.object.data.auto_smooth_angle = 0.523598 # in radians (~ 30 deg)

blender info editor output

https://docs.blender.org/manual/en/latest/editors/info_editor.html

Shantanu Aryan
  • 1,014
  • 7
  • 14
1

shade smooth and auto smooth for selected objects

bl_info = {
    "name": "My Addon",
    "author": "X Y",
    "version": (0, 1),
    "blender": (2, 80, 0),
    "location": "View3D",
    "description": "my operator",
    "category": "Object",
}

import bpy

class MY_OP(bpy.types.Operator): bl_idname = "view3d.my_operator" bl_label = "some label" bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
    if context.object:
        if context.object.mode != 'OBJECT':
            self.report({'WARNING'}, "Object mode only!")
            return {'CANCELLED'}

    for obj in bpy.context.selected_objects:
        if obj.type == 'MESH':
            data = obj.data
            data.polygons.foreach_set('use_smooth',  [True] * len(data.polygons))
            data.use_auto_smooth = True

    return {'FINISHED'}


def register(): bpy.utils.register_class(MY_OP)

def unregister(): bpy.utils.unregister_class(MY_OP)

if name == "main": register()

Install and Assign shortcut

How to duplicate parented objects as one object

X Y
  • 5,234
  • 1
  • 6
  • 20
-1

I notices potential for a few improvements in @X Y's script.

bl_info = {
    "name": "Set Shade Auto Smooth",
    "author": "X Y",
    "version": (0, 1),
    "blender": (2, 80, 0),
    "location": "View3D",
    "description": "Set Shade Auto Smooth with one Click",
    "category": "Object",
}

import bpy

class OBJECT_OT_shade_auto_smooth(bpy.types.Operator): bl_idname = "object.shade_auto_smooth" bl_label = "Shade Auto Smooth" bl_options = {'REGISTER', 'UNDO'}

# put checking for object type and correct context 
# in the poll method rather than execute.
@classmethod
def poll(cls, context):
    return context.object is not None and context.object.type in {"MESH", "CURVE"}

def execute(self, context):
    # allows for use in edit mode and object mode alike.
    in_edit = 'EDIT' in context.mode
    if in_edit:
        bpy.ops.object.mode_set(mode="OBJECT")
    # using one operator call to act on all selected objects 
    # rather than looping over every polygon
    bpy.ops.object.shade_smooth()
    for obj in context.selected_objects:
        if obj.type == "MESH":
            obj.data.use_auto_smooth = True
    if in_edit:
        bpy.ops.object.mode_set(mode="EDIT")   
    return {'FINISHED'}


def register(): bpy.utils.register_class(OBJECT_OT_shade_auto_smooth)

def unregister(): bpy.utils.unregister_class(OBJECT_OT_shade_auto_smooth)

if name == "main": register() ```

Jakemoyo
  • 4,375
  • 10
  • 20