# Operator camera_to_view_selected() doesn't work as expected

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.

def cam_realign():
for i in range(1, 9):
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, 9):
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?

Edit: I did check in the console that the camera is being updated as it iterates through different frame (as also suggested by batFINGER).

• Can you provide a test file? As you are not changing the active object would suggest selecting cube (making it context object) and then run the operator. Printing print(context.scene.camera) to console to check it is being updated (via binding) in script. I wonder too whether a frame change handler would be an easier option. – batFINGER Dec 26 '17 at 5:35
• Sure, here is the test file: [link] (drive.google.com/open?id=1xFX6XO_U7K_G3OUW2c_WCBjEIggHMPhQ) – Arindam Dec 26 '17 at 7:01
• @batFINGER How would you make use of a frame change handler for a workaround in this scenario? – Arindam Dec 27 '17 at 6:54