0
$\begingroup$

I use this command for running (open) Blender

blender --factory-startup --python path_to_script script.py

In script.py i tried to call bpy.ops.view3d.view_all(center=False) But its get wrong context So I tried common way to override the context using:

import bpy

def get_view3d():
    if bpy.context.area.type != 'VIEW_3D':
        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                return area

area = get_view3d()
region = next(region for region in area.regions if region.type == "WINDOW")

with bpy.context.temp_override(area=area, region=region):
    # ops that need 3Dview context should put here
    bpy.ops.view3d.view_all(center=False)

The problem is it's return None when executing bpy.context.area

How to get 3D viewport context using script when running Blender using Command Line (Windows)?,

The goal is I can use another bpy.ops that needs a specific active context.

$\endgroup$

1 Answer 1

1
$\begingroup$
import bpy

def get_view3d():
    for window_manager in bpy.data.window_managers:
        for window in window_manager.windows:
            for area in window.screen.areas:
                if area.type == 'VIEW_3D':
                    for region in area.regions:
                        if region.type == 'WINDOW':
                            return dict(window=window, workspace=window.workspace, screen=window.screen, area=area, region=region)

    raise Exception('View3D context not found.')


with bpy.context.temp_override(**get_view3d()):
    bpy.ops.view3d.view_all(center=False)

$\endgroup$

You must log in to answer this question.

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