When running the addon, This error has occurred, how do I fix it? I am running it in 3dview, I don't know why the context is incorrect.
Python:Traceback (most recent call last):
File "C:\Users\blues\AppData\RoamingBlender Foundation\Blender3.3\scripts\addons\CamS3.py",line 49,in execute bpy.ops.view3d.view_center_camera()
File "C:\Programs\blender-3.3.0-windows-x6413.3\scripts\modules\bpyops.py".line 113,in call ret op call(self.idname_py(),None,kw)
RuntimeError:Operator bpy.ops.view3d.view_center_camera.poll()failed,context is incorrect
import bpy
class Cam_Base():
bl_idname = "cam.base"
bl_label = "Cam Base"
cam_name = "C_"
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[1].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(bpy.types.Operator, Cam_Base):
bl_idname = "cam.cam0"
bl_label = "Cam_0V0"
cam_name = "C_0V0"
class Cam_0V1(bpy.types.Operator, Cam_Base):
bl_idname = "cam.cam1"
bl_label = "Cam_0V1"
cam_name = "C_0V1"
class Cam_0V2(bpy.types.Operator, Cam_Base):
bl_idname = "cam.cam2"
bl_label = "Cam_0V2"
cam_name = "C_0V2"
addon_keymaps = []
def register():
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_0V0)
bpy.utils.unregister_class(Cam_0V1)
bpy.utils.unregister_class(Cam_0V2)
if __name__ == "__main__":
register()```