Selected Light To view Python

Hello :slight_smile: I’m looking for a solution :
I try to create a script to shortcut this fonction :

  • use the selected light as a local camera
  • lock this camera to view
  • Change the view of the viewport to this active camera

Everything work, excepte that, when i try to switch from one light to another, I have to lunch the script twice…

Any Idea ?

import bpy


class MyOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.my_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
       
        bpy.context.space_data.use_local_camera = True
        bpy.context.space_data.camera = bpy.context.active_object
        bpy.context.space_data.lock_camera = True
        bpy.ops.view3d.view_camera()
        
        return {'FINISHED'}


def menu_func(self, context):
    self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)


# Register and add to the "object" menu (required to also use F3 search "Simple Object Operator" for quick access).
def register():
    bpy.utils.register_class(MyOperator)
    bpy.types.VIEW3D_MT_object.append(menu_func)


def unregister():
    bpy.utils.unregister_class(MyOperator)
    bpy.types.VIEW3D_MT_object.remove(menu_func)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.my_operator()

python.blend (903.3 KB)

Anyone has an idea ?

Take out the test call?

It changes nothing…