Hi, I have a question about calling function for outliner from view3d . There are several tricks with context changing but it is not exactly what I need. Because I want evaluate this (boy.ops.outliner.show_active())function for existing outliner. Please help!
You need to supply ‘area’ and ‘region’ context keys to override the operator from a different area.
import bpy
context = bpy.context
def show_active(context):
override = None
for area in context.screen.areas:
if 'OUTLINER' in area.type:
for region in area.regions:
if 'WINDOW' in region.type:
override = {'area': area, 'region': region}
break
break
if override is not None:
bpy.ops.outliner.show_active(override)
if '__main__' in __name__:
show_active(context)
You can turn this into a hotkey by saving to a py file and adding it using the blender operator script.python_file_run
in the hotkey editor.
2 Likes
Hi @iceythe !!! Super cool solution and thank you very much!!! It’s good to learn new things. Thank you very much!
This appears not to work anymore in B4.1
Anyone have a clue please ?
Now you can’t override context within operators. You should use context.temp_override
instead:
import bpy
context = bpy.context
def show_active(context):
override = None
for area in context.screen.areas:
if 'OUTLINER' in area.type:
for region in area.regions:
if 'WINDOW' in region.type:
override = {'area': area, 'region': region}
break
break
if not override:
return
with context.temp_override(area=override['area'], region=override['region']):
bpy.ops.outliner.show_active()
if '__main__' in __name__:
show_active(context)
2 Likes
You can also use the dictionary unpacker in this case
with context.temp_override(**override):
bpy.ops.outliner.show_active()
2 Likes