1
$\begingroup$

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()```
$\endgroup$
3
  • $\begingroup$ good question, i have tried and couldn't figure it out, there is a solution using the deprecated context object but that does not work for blender 3.2 and above blender.stackexchange.com/questions/95793/… $\endgroup$ Apr 20 at 15:51
  • $\begingroup$ it seems there is currently no solution for blender 3.5 $\endgroup$ Apr 20 at 15:52
  • $\begingroup$ just switched the order of commands, It worked. This means that the command has to be placed before the specific camera is called,A little strange $\endgroup$
    – lee blues
    Apr 20 at 23:05

1 Answer 1

1
$\begingroup$

just switched the order of commands, It worked. This means that the command has to be placed before the specific camera is called

[1]: https://i.stack.imgur.com/yG6RJ.png

$\endgroup$
2
  • $\begingroup$ cool you resolved it, so i guess it does not need to be in the VIEW_3D context? $\endgroup$ Apr 21 at 1:44
  • $\begingroup$ Strangely, I think it needs to be in the VIEW_3D context and needs to be in camera perspective, so I put the code for camera perspective at the top c.space_data.region_3d.view_perspective = 'CAMERA' ,then call the specific camera. if this order is wrong, the code will not run. Thank you again for your code. $\endgroup$
    – lee blues
    Apr 21 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .