1
$\begingroup$

I have a script that creates new camera and make it active but i cant override context for 'align camera to view'.

How to do it in blender 3.6?

import bpy

# Create a new camera
bpy.ops.object.camera_add()

# Get the newly created camera object
new_camera = bpy.context.object

# Set the new camera as the active camera
bpy.context.scene.camera = new_camera
$\endgroup$
1

2 Answers 2

1
$\begingroup$

I don't know too much about python

Maybe this:

import bpy

# Create a new camera
bpy.ops.object.camera_add()

# Get the newly created camera object
new_camera = bpy.context.object

# Set the new camera as the active camera
bpy.context.scene.camera = new_camera

# Align the camera to the current view
bpy.ops.view3d.camera_to_view()
$\endgroup$
2
  • $\begingroup$ Thank you for your variant $\endgroup$
    – romanzham
    Commented Aug 30, 2023 at 11:13
  • $\begingroup$ Im happy to help! :) $\endgroup$
    – Hazrd
    Commented Aug 30, 2023 at 12:48
1
$\begingroup$

For your temporary override you will still need to locate a 3d viewport window to use the command.

# Blender 3.6.1

import bpy

# Create a new camera
bpy.ops.object.camera_add()

# Get the newly created camera object
new_camera = bpy.context.object

# Set the new camera as the active camera
bpy.context.scene.camera = new_camera

# overrides
win = bpy.context.window
scr = win.screen
areas3d = [area for area in scr.areas if area.type == 'VIEW_3D']
region = [region for region in areas3d[0].regions if region.type == 'WINDOW']

with bpy.context.temp_override(window=win, area=areas3d[0], region=region[0]):
        bpy.ops.view3d.camera_to_view()
$\endgroup$
1
  • $\begingroup$ Ratt, thank you a lot! $\endgroup$
    – romanzham
    Commented Aug 30, 2023 at 11:12

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