# Modifiers: add and set “live” parameters then apply via python

Here is my question: I have a single object and 5 modifiers to be applied to it. I would like to:

1. Add all of them together
2. Allow the user to change a set of parameters with some GUI, maybe a bar scrolling from a minimum to a maximum value
3. Allow the user to apply the modifiers (all together) when he is satisfied with the result.

Let's keep it simple for now and let's say I consider

• DECIMATE MODIFIER with the ratio parameter to be set
• WIREFRAME MODIFIER with the thickness parameter to be set

Point 1.

I was thinking to something simple

Code:

class AddModifiers(bpy.types.Operator):
bl_options = {"UNDO"}

def execute(self,context):
obj = bpy.context.active_object
bpy.context.object.modifiers["Decimate"].ratio=0.5 # parameter inizialization
bpy.context.object.modifiers["Wireframe"].thickness=2 # parameter initialization

return {'FINISHED'}

class Panel(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_context = "objectmode"
bl_category = "Modifiers"

def draw(self, context):
layout = self.layout
# ALLINEAMENTO
row = layout.row(align=True)
row.alignment = 'EXPAND'

def register():

register()


Point 2.

Really don't know how to do that... class and gui...

Point 3.

Adding a class like that (with its own button in the gui of course)

Code:

class ApplyModifiers(bpy.types.Operator):
bl_idname = "modifiers.apply"
bl_label = "Applying Modifiers"
bl_options = {"UNDO"}

bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Decimate")
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Wireframe")
return {'FINISHED'}


In point 1 for the add modifiers operator you set obj to the active_object but don't use it. Rather than using other operators it would be better to perform the task as -

obj = context.active_object
dm = obj.modifiers.new('Decimate','DECIMATE')
dm.ratio = 0.5


And similar for other modifiers.

For point 2 Inside draw() for your panel class you want to add

obj = context.active_object
row.prop(obj.modifiers['Decimate'], "ratio")


That will show the decimate modifiers ratio property right there in your panel. Adjust for other modifier properties you want.

For point 3 the apply modifiers needs to be inside the execute function.

• hi sambler thanks for your question, very helpful. – piepolitb Nov 13 '14 at 15:00
• thanks again sambler, I have 2 more questions 1. is it possible to constraint the parameter modifiers in a pre-set range? 2. I always get in the python console "keyError: 'bpy_prop_collection[key]: key "Descimate" not found'", although it works. The error is referred to the line in draw() row.prop(obj.modifiers['Decimate'], "ratio") – piepolitb Nov 14 '14 at 17:39
• 1. no, the limits are set in the source code. 2. The panel can't display the value before the modifier is created, add a test if the modifier exists to show the ratio. – sambler Nov 26 '14 at 11:35