Stack Exchange
How to simplify this class of plugins
All classes are basically the same, just one line of code is different
bpy.context.space_data.camera = bpy.data.objects["C_0V0"]
bl_info = {
    "name": "CamS",
    "description": "",
    "author": "Jim lee",
    "version": (1, 0, 0),
    "blender": (3, 3, 0),
    "location": "",
    "warning": "",
    "wiki_url": "",
    "category": "My"
}

import bpy


class C_0V0(bpy.types.Operator):
    bl_idname = "cam.c0v0"
    bl_label = "C_0V0"
    cam_name = "C_0V0"

    def execute(self, context):
        C = bpy.context
        C.object.lock_scale = (True, True, True)
        C.object.lock_rotation = (True, True, True)
        C.object.lock_location = (True, True, True)

        # select cam
        C.space_data.camera = bpy.data.objects["C_0V0"]

        # Select Active Cam
        for area in C.screen.areas:
            if area.type == 'VIEW_3D':
                A = C.area.spaces.active.camera

        C.scene.camera = bpy.data.objects[A.name]

        C.space_data.shading.color_type = 'OBJECT'
        C.space_data.region_3d.view_perspective = 'CAMERA'

        bpy.data.objects[A.name].lock_scale = (True, True, True)
        bpy.data.objects[A.name].lock_location = (True, True, True)
        bpy.data.objects[A.name].lock_rotation = (True, True, True)
        bpy.data.objects[A.name].scale = (1, 1, 1)
        bpy.data.cameras[A.name].display_size = 0.05
        bpy.data.cameras[A.name].background_images[0].alpha = 0
        bpy.data.cameras[A.name].dof.use_dof = False
        bpy.data.cameras[A.name].clip_start = 25
        bpy.data.cameras[A.name].clip_end = 100000
        bpy.data.cameras[A.name].background_images[0].scale = 1
        bpy.data.cameras[A.name].background_images[1].scale = 1
        bpy.data.cameras[A.name].background_images[2].scale = 1

        bpy.ops.view3d.view_center_camera()
        bpy.ops.image.reload()

        if C.object.mode == 'OBJECT':
            bpy.ops.object.select_all(action='DESELECT')
            objectToSelect = bpy.data.objects[A.name]
            objectToSelect.select_set(True)
            C.view_layer.objects.active = objectToSelect

            C.view_layer.objects.active = objectToSelect

        return {'FINISHED'}


class C_0V1(bpy.types.Operator):
    bl_idname = "cam.c0v1"
    bl_label = "C_0V1"
    cam_name = "C_0V1"

    def execute(self, context):
        C = bpy.context
        C.object.lock_scale = (True, True, True)
        C.object.lock_rotation = (True, True, True)
        C.object.lock_location = (True, True, True)

        # select cam
        C.space_data.camera = bpy.data.objects["C_0V1"]

        # Select Active Cam
        for area in C.screen.areas:
            if area.type == 'VIEW_3D':
                A = C.area.spaces.active.camera

        C.scene.camera = bpy.data.objects[A.name]

        C.space_data.shading.color_type = 'OBJECT'
        C.space_data.region_3d.view_perspective = 'CAMERA'
        # bpy.data.cameras[A.name].photographer.longedge = 4096

        bpy.data.objects[A.name].lock_scale = (True, True, True)
        bpy.data.objects[A.name].lock_location = (True, True, True)
        bpy.data.objects[A.name].lock_rotation = (True, True, True)
        bpy.data.objects[A.name].scale = (1, 1, 1)
        bpy.data.cameras[A.name].display_size = 0.05
        bpy.data.cameras[A.name].background_images[0].alpha = 0
        bpy.data.cameras[A.name].dof.use_dof = False
        bpy.data.cameras[A.name].clip_start = 25
        bpy.data.cameras[A.name].clip_end = 100000
        bpy.data.cameras[A.name].background_images[0].scale = 1
        bpy.data.cameras[A.name].background_images[1].scale = 1
        bpy.data.cameras[A.name].background_images[2].scale = 1

        bpy.ops.view3d.view_center_camera()
        bpy.ops.image.reload()

        if C.object.mode == 'OBJECT':
            bpy.ops.object.select_all(action='DESELECT')
            objectToSelect = bpy.data.objects[A.name]
            objectToSelect.select_set(True)
            C.view_layer.objects.active = objectToSelect

            C.view_layer.objects.active = objectToSelect

        return {'FINISHED'}

addon_keymaps = []

def register():
    bpy.utils.register_class(Cam_0V0)
    bpy.utils.register_class(Cam_0V1)

def unregister():
    bpy.utils.unregister_class(Cam_0V0)
    bpy.utils.unregister_class(Cam_0V1)

if __name__ == "__main__":
    register()



Is your goal to register 3 classes or 1 class?

register 3 classes and  can call it with a button

You can define a base class to eliminate the redundancy
import bpy

class Cam_Base(bpy.types.Operator):
    bl_idname = "cam.base"
    bl_label = "Cam Base"
    cam_name = "C_"
    
    def execute(self, context):
        C = bpy.context

        # lock cam
        bpy.context.object.lock_scale = (True, True, True)
        bpy.context.object.lock_rotation = (True, True, True)
        bpy.context.object.lock_location = (True, True, True)

        # select cam
        bpy.context.space_data.camera = bpy.data.objects[self.cam_name]

        # Select Active Cam
        for area in C.screen.areas:
            if area.type == 'VIEW_3D':
                A = C.area.spaces.active.camera

        bpy.context.scene.camera = bpy.data.objects[A.name]
        return {'FINISHED'}

class Cam_0V0(Cam_Base):
    bl_idname = "cam.0v0"
    bl_label = "Cam_0V0"
    cam_name = "C_0V0"

class Cam_0V1(Cam_Base):
    bl_idname = "cam.0v1"
    bl_label = "Cam_0V1"
    cam_name = "C_0V1"

class Cam_0V2(Cam_Base):
    bl_idname = "cam.0v2"
    bl_label = "Cam_0V2"
    cam_name = "C_0V2"

addon_keymaps = []

def register():
    bpy.utils.register_class(Cam_Base)
    bpy.utils.register_class(Cam_0V0)
    bpy.utils.register_class(Cam_0V1)
    bpy.utils.register_class(Cam_0V2)

def unregister():
    bpy.utils.unregister_class(Cam_Base)
    bpy.utils.unregister_class(Cam_0V0)
    bpy.utils.unregister_class(Cam_0V1)
    bpy.utils.unregister_class(Cam_0V2)

if __name__ == "__main__":
    register()


thank you very much

Please see my additional questions that according to the answer I added the code to make the command into a button but there is an error, how to make a button to call this class?

Thanks for the reply, if I want these classes to be called by other plugins, like Pie Menu Editor addon, do I have to add some other code? For example “def invoke”，How do I write it

Thanks for the reminder, I have re-asked a question with the following link https://blender.stackexchange.com/questions/291104/how-do-get-the-classes-to-be-called-by-other-addons

hi，You rewrite the code there is a little problem, before my code ca switch the area‘s camera in the current area where the mouse is located,it is this paragraph code： “bpy.context.space_data.camera = bpy.data.objects["C_0V0"]” change to “c.space_data.camera = bpy.data.objects[self.cam_name]” It doesn't work anymore.，But the new code can only switch the camera in one area now when there ara many areas in windows, how can I modify it?
