Gist: Why bpy.ops.view3d.camera_to_view_selected() doesn’t work from a Class method or even a global function, when it’s called from any regular method within a registered Class?
My scenario: I have a scene with multiple cameras “binded” to different Markers (in the Timeline) at different frame intervals. I am trying to re-align all the active cameras at their respective frame to a selected object. For example, lets the selected object be our default Cube here.
The picture above shows like 8 cameras, but the codes below iterates through only the first 5.
def cam_realign():
for i in range(1, 5):
bpy.context.scene.frame_set(i)
bpy.ops.object.select_all(action='DESELECT')
active_obj = bpy.context.scene.objects['Cube']
active_obj.select = True
bpy.context.scene.objects.active = active_obj
bpy.ops.view3d.camera_to_view_selected()
cam_realign()
The above code snippet works absolutely fine if I use as it is, i.e. just as a normal function. However, if I try to execute those very set of instructions from within a Class method it doesn’t realign all the cameras except one.
class CamRePosition(bpy.types.Operator):
bl_idname = "object.cam_reposition"
bl_label = "Repositioning of Cameras"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
for i in range(1, 5):
bpy.context.scene.frame_set(i)
bpy.ops.object.select_all(action='DESELECT')
active_obj = bpy.context.scene.objects['Cube']
active_obj.select = True
bpy.context.scene.objects.active = active_obj
bpy.ops.view3d.camera_to_view_selected()
return {'FINISHED'}
Any idea where I could fix this?
PS: I have asked this very same question over Blender Stack Exchange. As I wait for any answer that could be provided, I thought it could be possibly a good idea to post it here as well.