1
$\begingroup$

Apply modifier button will write this into the console:

bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Box Right ")

Adding this line after "bool_mod.object = obj" does not work as expected stating that object has not attribute 'modifier'

ERROR:

  File "C:\Users\info\Desktop\Boolean.blend\ui_panel_simple.py", line 38, in execute
AttributeError: 'Object' object has no attribute 'modifier_apply'

CODE:

def execute(self, context):   

    target_obj = bpy.context.active_object
    tool_objs = [obj for obj in bpy.context.selected_objects if obj != target_obj]

    for obj in tool_objs:
        bool_mod = target_obj.modifiers.new(obj.name+' ', 'BOOLEAN')
        bool_mod.operation = 'DIFFERENCE'
        bool_mod.object = obj
        target_obj.modifier_apply(modifier=bool_mod.name)

I went to blender python website to read about the modifier command and values and to my surprise found nothing that explains what options there are for operation for example.

https://www.blender.org/api/blender_python_api_2_70_5/bpy.types.Modifier.html

$\endgroup$
  • $\begingroup$ This will be applied to the active object , so make sure your target object is active before applying the modifier ( set it active through script each iteration) $\endgroup$ – Chebhou Jan 7 '16 at 20:12
  • $\begingroup$ You should use the first line bpy.ops.object.apl...... $\endgroup$ – Chebhou Jan 7 '16 at 20:16
  • $\begingroup$ figured it out in my answer $\endgroup$ – Claas Kuhnen Jan 7 '16 at 20:34
4
$\begingroup$

Objects don't have a .modifier_apply(), you can't do active_object.modifier_apply(). That's what the main Error message is saying.

modifier_apply is an operator of bpy.ops.object, which works if there's an active object. Confused? :)

# pick any object
obj = bpy.data.objects['Suzanne']

# set the object to active
bpy.context.scene.objects.active = obj

# this operator will 'work' or 'operate' on the active object we just set
bpy.ops.object.modifier_apply(modifier="my_modifier_name")

In the event that you want to apply a subset of modifiers, you have to start applying from the bottom up. You could put checks in place to skip certain modifier by type, but in your case you have only Boolean_Modifiers.

import bpy

def execute(self, context):   

    target_obj = context.active_object
    tool_objs = [o for o in context.selected_objects if o != target_obj]

    BOOL = 'BOOLEAN'
    # first add them.
    for obj in tool_objs:
        bool_mod = target_obj.modifiers.new(name='diff_' + obj.name, type=BOOL)
        bool_mod.operation = 'DIFFERENCE'
        bool_mod.object = obj
        obj.hide = True

    # then apply them individually.
    for modifier in target_obj.modifiers:
        bpy.ops.object.modifier_apply(modifier=modifier.name)


execute(None, bpy.context)

This is one way to apply the modifier, there is also a direct way to apply all modifiers at once to the Mesh without such a loop. See this answer for details

$\endgroup$
  • $\begingroup$ bpy.context.scene.objects.active = obj doesn't work; fails with AttributeError: bpy_prop_collection: attribute "active" not found. -- Maybe it changed in 2.80? $\endgroup$ – BrainSlugs83 Oct 15 at 2:03
  • $\begingroup$ yes, answer is correct for blender version <= 2.79 $\endgroup$ – zeffii Oct 15 at 8:02
1
$\begingroup$

The trick to make this run is that the () after the modifier_apply() cannot be empty. The moment you define the modifier name it works.

bpy.ops.object.modifier_apply(modifier=bool_mod.name)

I assume the modifier_apply command is just in this case working like a generic apply command because we are working of a list. But you still have to call out the modifier with the name I assume.

$\endgroup$
  • $\begingroup$ it's not about that it is because the object doesn't have a method called modifier_apply() so you can't do this target_obj.modifier_apply() instead you have to make your object active and call the operator bpy.ops.object.modifier_apply() $\endgroup$ – Chebhou Jan 7 '16 at 20:37
  • $\begingroup$ bpy.ops.object.modifier_apply() only works when you call out the modifier to also apply! $\endgroup$ – Claas Kuhnen Jan 7 '16 at 20:46
  • $\begingroup$ yes but he has it like this target_obj.modifier_apply() which is not the same as this bpy.ops.object.modifier_apply() , is it ? $\endgroup$ – Chebhou Jan 7 '16 at 20:47
  • $\begingroup$ True - initially I tried "bpy.ops.object.modifier_apply()" first and then exchanged parts with target_obj but still kept the () empty. $\endgroup$ – Claas Kuhnen Jan 7 '16 at 20:55
  • $\begingroup$ yes it shouldn't be empty $\endgroup$ – Chebhou Jan 7 '16 at 21:10

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.