I try to split the VIEW_3D area of my screen (2/3 - 1/3), and to display in the 1/3 a file with the TEXT_EDITOR.

The problems are:

  1. that the activate area seems not follow the area's iteration

  2. the width of the new area created seems not to have been update. (0 is returned)

here is the code:

bpy.context.area.type = 'VIEW_3D'
orig_width = bpy.context.area.width 
print("originale width of View 3D is : " + str(orig_width))
bpy.ops.screen.area_split(direction='VERTICAL', factor=0.3)
final_width = round(bpy.context.area.width * 0.3, 0)
print("Final width of View 3D is : " + str(final_width))    
i = 0

for ecran in bpy.context.screen.areas:
    if ecran.type == 'VIEW_3D':
        print("Space.active = " + str(ecran.spaces.active))
        current_width = bpy.context.area.width
        print(str(current_width) + " - current_width")
        print("i = " + str(i) + " - " + bpy.context.screen.areas[i].type + "Width : " + str(bpy.context.screen.areas[i].width))
        if current_width == final_width:
            bpy.types.spaces[i] == 'TEXT_EDITOR'
i = i + 1 

The script is executing from an addon in the context of 3d_view, called from the spacebar menu. It splits correctly but the Text_Editor is not displayed.


Your sample code is not actually changing any area to a text editor. At the end of your code you are comparing to the string 'TEXT_EDITOR' not assigning. While you go through the areas you won't find a match to final_width as you are setting final_width to 1/3 of the width of the active area after the split, you would need to calculate the width before the split to have a chance of matching it.

When splitting an area we don't actually get a reference to the new area. One solution I can think of is to copy the list of areas, split the current area and then find the area that didn't exist to start with, this leads to the following demo operator to split the view and change the new area to a text editor.

class SplitArea(bpy.types.Operator):
    """Split area to include a text editor"""
    bl_idname = "screen.split_area"
    bl_label = "Split area"

    def execute(self, context):
        start_areas = context.screen.areas[:]
        bpy.ops.screen.area_split(direction='VERTICAL', factor=0.3)
        for area in context.screen.areas:
            if area not in start_areas:
                area.type = 'TEXT_EDITOR'
        return {'FINISHED'}
  • $\begingroup$ Thank you for your answer sambler. You are right for the assignation, i made a mistake when i copied the script to the forum. What it surprised me, it was that the width of the new created area was 0. Despite it was correctly display on the screen. Your solution works well i will continu with it. $\endgroup$ – freecadol Jan 10 '16 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.