1
$\begingroup$

I made a scene with a cube and a sphere. The sphere has a "displace" modifier on it, which I want to apply while having the cube (or nothing at all) selected.

Executing the following lines one by one in the console works well (don't forget to select the cube before the last line):

ctx = bpy.context.copy()
ctx["active_object"] = bpy.data.objects["Sphere"]
bpy.ops.object.modifier_apply(ctx, modifier="Displace", apply_as='DATA')

But when trying to call those exact same three lines from an operator, it fails without any error message:

import bpy

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        ctx = bpy.context.copy()
        ctx["active_object"] = bpy.data.objects["Sphere"]
        bpy.ops.object.modifier_apply(ctx, modifier="Displace", apply_as='DATA')
        return {'FINISHED'}

register, unregister = bpy.utils.register_classes_factory((SimpleOperator,))

So where is the catch? Download the scene with everything set up here

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.