4
$\begingroup$

I wrote s simple script that lets me manage Blender's geometry smoothing a little easier and faster but I'm struggling with something. Since I'm fiddling with the smoothing, I'd like to "Copy" Blenders Auto Smooth checkbox and Smooth Angle slider for a faster and more convenient workflow.

I tried a few things and nothing worked. I created a Custom BoolProperty for the checkbox but I can't get it to control the setting. This is the Custom property:

class AutoSmoothData(bpy.types.PropertyGroup):

auto_smooth_toggle: bpy.props.BoolProperty(
    name="Auto Smooth",
    description="Toggle Auto Smooth",
    default=bpy.context.object.data.use_auto_smooth
    )

After this, I just put it on the UI, but I have no idea where do I have to "tell" it to change the actual built-in Auto Smooth setting.

I also have this function:

def auto_smooth():
    if bpy.context.scene.smooth_prop.auto_smooth_toggle == True:
        bpy.context.object.data.use_auto_smooth = True
    else:
        bpy.context.object.data.use_auto_smooth = False

But as I said, I don't know where to execute it.

$\endgroup$

1 Answer 1

6
$\begingroup$

You don't have to create new properties to mimic existing properties. You just need to find their data path. Make sure you have enabled all the developer extras in the preferences and hover over the field you want to copy.

enter image description here

Based on that you just have to add a property field to your layout, that redirects to this data path : object.data.use_auto_smooth

Using the simple panel template shipped with Blender :

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"
    
    @classmethod
    def poll(cls, context):
        obj = context.object
        return obj and obj.data and hasattr(obj.data, "use_auto_smooth")

    def draw(self, context):
        layout = self.layout
        layout.prop(context.object.data, "use_auto_smooth")


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


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


if __name__ == "__main__":
    register()

This creates a panel with a direct link to this property. No need for a third party property with convoluted callbacks.

enter image description here

Also, if you want to mimic exactly how it is laid out in the origin panel, in most cases you can right click and choose "Edit Source". This opens a Text datablock centered on the line which created the UI element you selected.

Example for the Normals Panel :

enter image description here

$\endgroup$
4
  • $\begingroup$ hmm, I see, I knew I was trying to do it the hard way.. Anyway, thank you for your help! $\endgroup$
    – Aleks K.
    Feb 14, 2021 at 9:37
  • $\begingroup$ I'm not really sure I fully get it though.. Can you explain to me the whole idea behid the two IF statements? $\endgroup$
    – Aleks K.
    Feb 14, 2021 at 10:07
  • 1
    $\begingroup$ Sure ! I've updated the answer so it makes a little more sense hopefully. The poll method is automatically called before updating the UI, it decides wether to draw the panel or not. If we try to access a property if no object is selected (if context.object is None, it evaluates to False in a boolean operation), or if the object doesn't even have the use_auto_smooth property, the script will throw an error. We premptively catch these edge cases and tell the script not to bother if we haven't selected a mesh object. $\endgroup$
    – Gorgious
    Feb 14, 2021 at 11:03
  • $\begingroup$ Yes! Thank you! I'm actually more familiar with what I see now. I just haven't seen before the method you used last time where the actual poll is kind of non-existent but located in the draw call. That's why I got a little confused.. Anyway, thank you a lot for your help!!! I can see that you're not new to this so maybe you could help me with another question I asked for a different script? It's this one: blender.stackexchange.com/questions/211224/… and still haven't gotten an answer and haven't figured it out on my own $\endgroup$
    – Aleks K.
    Feb 14, 2021 at 22:40

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.