Post #4692: I’ve solved it, but thanks for the reply. and When I call the plug-in with PME,

📋 Metadata

🏷️ Tags

macro python-scripting intermediate unsolved

  • Macro Editor
  • Python Scripting

💬 Content

I’ve solved it, but thanks for the reply. and When I call the plug-in with PME, all are normal, but only one line of code is wrong, please tell me what is going on, I am sure it is used in 3D_view context.
Problem Code:bpy.ops.view3d.view_center_camera()
image

import bpy

class Cam_Base(bpy.types.Operator):
    bl_idname = "cam.base"
    bl_label = "Cam Base"
    cam_name = "C_"
    bp_options = {'REGISTER', "UNDO"}

    def execute(self, context):
        area_type = 'VIEW_3D'
        areas = [area for area in bpy.context.window.screen.areas if area.type == area_type]

        if len(areas) <= 0:
            raise Exception(f"Make sure an Area of type {area_type} is open or visible in your screen!")

        with bpy.context.temp_override(area=areas[0]):
            c = bpy.context
            c.object.lock_scale = (True, True, True)
            c.object.lock_rotation = (True, True, True)
            c.object.lock_location = (True, True, True)
            c.space_data.camera = bpy.data.objects[self.cam_name]
            c.scene.camera = bpy.data.objects[self.cam_name]
            c.space_data.shading.color_type = 'OBJECT'
            c.space_data.region_3d.view_perspective = 'CAMERA'

            #Modify camera attributes
            bpy.data.objects[self.cam_name].scale = (1, 1, 1)
            bpy.data.cameras[self.cam_name].display_size = 0.05
            bpy.data.cameras[self.cam_name].background_images[0].alpha = 0
            bpy.data.cameras[self.cam_name].dof.use_dof = False
            bpy.data.cameras[self.cam_name].clip_start = 25
            bpy.data.cameras[self.cam_name].clip_end = 100000
            bpy.data.cameras[self.cam_name].background_images[0].scale = 1
            bpy.data.cameras[self.cam_name].background_images[1].scale = 1
            bpy.data.cameras[self.cam_name].background_images[2].scale = 1

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

             #select camera as active objcet in object mode
            if bpy.context.object.mode == 'OBJECT':
                bpy.ops.object.select_all(action='DESELECT')
                objectToSelect = bpy.data.objects[self.cam_name]
                objectToSelect.select_set(True)
                bpy.context.view_layer.objects.active = objectToSelect
                
        return {'FINISHED'}

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

addon_keymaps = []

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

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

if __name__ == "__main__":
    register()

🔗 View on Blender Artists