1
$\begingroup$

I'm writing a plugin for Blender to apply the decimate modifier automatically given an angle limit. I'm working on this little project just for fun and also to meet some of my recent needs.

I'm trying to define a function with an argument that goes as follows:

deg5 = radians(5)
deg30 = radians(30)
deg45 = radians(45)
deg60 = radians(60)
deg90 = radians(90)   

def apply_decimate_modifier(degrees):
        
        bpy.ops.object.modifier_add(type='DECIMATE')
        bpy.context.object.modifiers["Decimate"].decimate_type = 'DISSOLVE'
        bpy.context.object.modifiers["Decimate"].angle_limit = {degrees}
        #bpy.ops.object.modifier_apply(modifier="Decimate", report=True)

then I'm trying to put this function on the Operator:

#defines class for the Angle Limit of 5 degrees.
class decimate_5degrees(bpy.types.Operator):
    """
    Set Planar Mode Angle Limit to 5°
    """

    bl_idname = "decimate.5degrees"
    bl_label = "Angle Limit: 5°"
    
    #TODO:
    @classmethod
    def poll(cls, context):
        return context.active_object is not None
    
    def execute(self, context):
        apply_decimate_modifier(deg5)
        return {'FINISHED'}

However, Blender gives me the following error message:

Python: Traceback (most recent call last):
  File "\Text", line 67, in execute
  File "\Text", line 49, in apply_decimate_modifier
TypeError: bpy_struct: item.attr = val: DecimateModifier.angle_limit expected a float type, not set

I don't understand why is this happening because the "deg5 (...) deg90" variables are already float numbers. Am I missing something?

Also, I don't know if the following piece of code is sufficient for Blender to only apply the modifier if a mesh object is selected. How could I translate it to Blender so the Decimate Modifier is applied ONLY to an object if it's selected, and if nothing is selected, Blender shouldn't apply any modifiers to any random objects.

@classmethod
def poll(cls, context):
    return context.active_object is not None

Thanks a lot!

$\endgroup$

1 Answer 1

1
$\begingroup$

The braces are not necessary here;

bpy.context.object.modifiers["Decimate"].angle_limit = {degrees}

Instead assign angle_limit like so;

bpy.context.object.modifiers["Decimate"].angle_limit = degrees

The error message is telling you that angle_limit has been assigned the incorrect type.

DecimateModifier.angle_limit expected a float type, not set

Perhaps the wording of the error is confusing. After all the word 'set' holds the record for being the word with the most definitions. Anyway, just remove the braces and you're all set!

As for the second part of your question;

You could add to the conditional in your poll method as follows;

    def poll(cls, context):
        return context.active_object is not None and context.active_object.select_get() is True and context.active_object.type == 'MESH'
$\endgroup$
8
  • $\begingroup$ Oops! It works! Thank you so much! I was getting very confused because I didn't know exactly how to declare arguments to a function in Python, because I'm still used to C. Also, do you know how I could solve the 2nd part of my problem? $\endgroup$
    – thevoyager
    Dec 28, 2022 at 20:55
  • $\begingroup$ Edited to add a suggestion for how to ensure active object is selected to the poll method. $\endgroup$ Dec 29, 2022 at 14:17
  • $\begingroup$ I tried doing this but so far what I got was this error message when I select the right option: Python: Traceback (most recent call last): File "\ui_pie_menu.py", line 68, in execute File "\ui_pie_menu.py", line 48, in apply_decimate_modifier File "C:\Program Files\WindowsApps\BlenderFoundation.Blender_3.4.1.0_x64__ppwjx1n5r4v9t\Blender\3.4\scripts\modules\bpy\ops.py", line 113, in call ret = _op_call(self.idname_py(), None, kw) TypeError: Converting py args to operator properties: enum "DECIMATE" not found in () $\endgroup$
    – thevoyager
    Dec 29, 2022 at 19:07
  • $\begingroup$ Updated the poll() method test to include testing that the selected object type is MESH. This should address the error. $\endgroup$ Dec 30, 2022 at 17:16
  • $\begingroup$ Hello, and thanks a lot! This worked like a charm. I have only one more question about this: When I run the script now and select to decimate an object that's not a mesh, Blender displays an error message. Is it possible to choose not to display it? I don't know if an error message would be a good thing for the end user, rather than Blender just ignoring an element that's not a Mesh for example. $\endgroup$
    – thevoyager
    Jan 2, 2023 at 13:14

You must log in to answer this question.

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