# How to open a new editor with python

I currently have

class OpenNodeEditor(bpy.types.Operator):
"""Open the node editor"""
bl_idname = "scene.open_node_editor"
bl_label = "Open Node Editor"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
bpy.ops.wm.context_set_string(data_path="area.type", value="NODE_EDITOR")

return {'FINISHED'}


This works, but when I press the button, it switches the space that the button is in (active editor), in this case, the properties editor, to the node editor. How can I make it open up a new section in the layout or open the node editor in the largest open editor?

• a better and more general title (useful to more people) would be 'Find the largest open editor window, split it and set its type' – zeffii Jun 30 '16 at 7:33

Your code is working as expected as it is setting the type of the context area, which is where the button is, or the text editor if run from there, or the py console if run from there etc.

You could fix by overriding the wm.context_set_string operator.

However it's prob much easier to find the other area with maximimum area.

import bpy
from bpy.types import Operator
from operator import itemgetter

class OpenNodeEditor(Operator):
"""Open the node editor"""
bl_idname = "screen.open_node_editor"
bl_label = "Open Node Editor"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
screen = context.screen
areas = [(area, area.width * area.height) for area in screen.areas
if area != context.area]
if len(areas):
maxarea, a = max(areas, key=itemgetter(1))
maxarea.type = 'NODE_EDITOR'
else:
#split areas
direction = 'HORIZONTAL' if context.area.width < context.area.height else 'VERTICAL'
bpy.ops.screen.area_split(direction=direction)
screen.areas[-1].type = 'NODE_EDITOR'

return {'FINISHED'}


Also note: your operator above is the equiv of

row = layout.row() # some layout
op = row.operator("wm.context_set_string")
op.data_path = "area.type"
op.value = 'NODE_EDITOR'