1

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

Gorgious
  • 30,723
  • 2
  • 44
  • 101
romanzham
  • 11
  • 1
  • 1
    related https://blender.stackexchange.com/questions/15118/how-do-i-override-context-for-bpy-ops-mesh-loopcut – Ratt Aug 25 '23 at 12:03

2 Answers2

1

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()

jos3ph_1205
  • 371
  • 12
1

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()

Ratt
  • 2,126
  • 1
  • 10
  • 17