1
$\begingroup$

Very often I only want to execute a line of code which can only be executed in the 3D view, for example this:

bpy.ops.view3d.view_selected(use_all_regions=False)

bpy.context.space_data.region_3d.view_location

Is there a solution to still execute in the Python console but simulating the 3D View context?

$\endgroup$
1
  • $\begingroup$ It's possible as in it works. But writing everything out in the console line by line would be hugely inconvenient. It would be way more practical to use text editor. Why do you want it to be the Python console? There can also be multiple viewports open at the same time and that complicates stuff more. I suppose we all do that from time to time when writing scripts and exploring the API in the console... $\endgroup$ Commented Apr 25, 2024 at 20:04

2 Answers 2

0
$\begingroup$

as you know you can give multiple commands in python console. So you can e.g. copy this into your python console, and it works:

def context_override():
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'VIEW_3D':
                for region in area.regions:
                    if region.type == 'WINDOW':
                        return {'window': window, 'screen': screen, 'area': area, 'region': region, 'scene': bpy.context.scene} 


with bpy.context.temp_override(**context_override()):
    
    bpy.ops.view3d.view_selected(use_all_regions=False);print ("works",bpy.context.space_data.region_3d.view_location)
$\endgroup$
0
$\begingroup$

The closest and easiest thing to this I can think of is making yourself an operator that runs a text block from the text editor:

import bpy

class RunFromTextEditor(bpy.types.Operator):
    """Run from text editor"""
    bl_idname = "wm.run_from_tex_editor"
    bl_label = "Run from Text Editor"

    def execute(self, context):
        override = context.copy()
        override["edit_text"] =  bpy.data.texts['run_from_operator.py']
        with context.temp_override(**override):
            bpy.ops.text.run_script()
        return {'FINISHED'}

def register():
    bpy.utils.register_class(RunFromTextEditor)

def unregister():
    bpy.utils.unregister_class(RunFromTextEditor)

if __name__ == "__main__":
    register()

Once you register it, you will have an operator that you can reach from search menu and it will run anything you have in a text block named "run_from_operator.py" so you can run code that is in the text editor from any context in the UI that you call the operator from. Might be useful to print the context for example to help you find out, what you need to override some operator's context, or just simply test some code in another context.

$\endgroup$
1
  • $\begingroup$ If I understand correctly, when this code is called, it gets the context and executes run_from_operator.py with the context. Tell yourself that we can run a script from the search menu. I can not do it. $\endgroup$ Commented Apr 29, 2024 at 6:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.