I would like to run a python script, which adds every frame a new Mask-modifier to an object (and deleting the old). While following script works if the code runs in interactive blender window, during rendering it leads to an error message when it calls modifier_move_up().
import bpy
object_name = "Cube"
bpy.app.handlers.frame_change_post.clear()
def my_handler(scene):
print("Frame Change", bpy.context.scene.frame_current )
obj = bpy.data.objects[object_name]
# delete mask-modifier and add new to last but one position
if obj.modifiers.find("Mask")+1:
obj.modifiers.remove(obj.modifiers.get("Mask"))
obj.modifiers.new("Mask", type="MASK")
#################################################
bpy.ops.object.modifier_move_up(modifier="Mask")
#################################################
bpy.app.handlers.frame_change_post.append(my_handler)
Error message:
RuntimeError: Operator bpy.ops.object.modifier_move_up.poll() failed, context is incorrect
As far as I understand, I should override the context, but I don't know how exactly and with which parameters.
According to this question (How do I override context for bpy.ops.mesh.loopcut?) I added an override, but with no effect.
import bpy
object_name = "Cube"
bpy.app.handlers.frame_change_post.clear()
win = bpy.context.window
scr = win.screen
areas3d = [area for area in scr.areas if area.type == 'VIEW_3D']
region = [region for region in areas3d[0].regions if region.type == 'WINDOW']
override = {'window':win,
'screen':scr,
'area' :areas3d[0],
'region':region[0],
'scene' :bpy.context.scene,
}
def my_handler(scene):
print("Frame Change", bpy.context.scene.frame_current )
obj = bpy.data.objects[object_name]
# delete mask-modifier and add new to last but one position
if obj.modifiers.find("Mask")+1:
obj.modifiers.remove(obj.modifiers.get("Mask"))
obj.modifiers.new("Mask", type="MASK")
#################################################
bpy.ops.object.modifier_move_up(override, modifier="Mask")
#################################################
bpy.app.handlers.frame_change_post.append(my_handler)
While running this code in blender I get after frame change following messages (the code itself works):
PyContext 'modifier' not found PyContext 'object' not found PyContext 'modifier' not found PyContext 'object' not found PyContext 'object' not found
While I'm try to render this part of the messages changes to...
PyContext 'modifier' not found PyContext 'object' not found PyContext 'active_object' not found
... followed by the mentioned "context is incorrect" error message.
What am I doing wrong?