1
$\begingroup$

So, I have some arbitrary object referred by the variable target_object. I can directly add the modifier to this object:

mod = target_obj.modifiers.new(type='BOOLEAN', name='Boolean')
mod.operation = 'DIFFERENCE'
mod.object = tool_obj #some tool object

I have the one main question: is it possible to somehow directly apply this newly created modifier? I doesn't find any ways to do this, avoiding to use bpy.ops.object.modifier_apply() which works with the active object only.

I have the secondary question: does some built-in function or method exist to get unique modifier's name? For unclear reasons the name parameter of the new() method is required and I do not wish to write the additional code to generate such name manually...

$\endgroup$

1 Answer 1

1
$\begingroup$
bpy.ops.object.modifier_apply(modifier = mod.name)

using the active object; as you indicated. But I don't understand your concern with the name. Modifiers are created with names, because new requires a name, so you don't have to modify the name for this to work.

As pointed out in the comments, you can use the default name in the new operator by typing name="" and Blender will sort out making a unique name.

$\endgroup$
10
  • $\begingroup$ Also you might want to use an override if target_obj is not the active object otherwise the operator won't work and throw RuntimeError: Operator bpy.ops.object.modifier_apply.poll() failed, context is incorrect. eg : bpy.ops.object.modifier_apply({"active_object": target_obj}, modifier=mod.name) $\endgroup$
    – Gorgious
    Dec 21, 2021 at 16:30
  • 1
    $\begingroup$ @Gorgious Good point, I'll clarify. $\endgroup$ Dec 21, 2021 at 16:32
  • $\begingroup$ Of course I can set my object as active and apply the modifier via the bpy.ops... But I read that using the bpy.ops works slowly and it's recomended to avoid using it. Also my logic tells me if I can directly create the modifier then must be the way to directly apply it without refering to the entire Blender envirement and changing the context. $\endgroup$
    – LShadow77
    Dec 21, 2021 at 16:38
  • 1
    $\begingroup$ Yeah maybe we should stop systematically saying operators are bad to use in scripts. They are bad if you use them hundreds of times in loops but the difference won't even be measurable if you run them once or twice compared to their low-level counterparts. But they do have the problem to absolutely need a correct context to be run (active object, selected objects, 3d viewport, etc.) $\endgroup$
    – Gorgious
    Dec 21, 2021 at 17:39
  • 2
    $\begingroup$ You can use an operator if the object isn't selected but you have to use an override dictionary with bpy.ops.object.modifier_apply({"active_object": target_obj}, modifier=mod.name) You can learn more about operator overrides here : docs.blender.org/api/current/bpy.ops.html#overriding-context $\endgroup$
    – Gorgious
    Dec 21, 2021 at 19:03

You must log in to answer this question.

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