Text Editor. Close current documents and start "text.open"

Hi developers !

I’d like a single document Text Editor.

So MyButton should first close all opened text blocks and then starts the “text.open”.


import ...

class PT_Visualize ( Panel ):
    bl_space_type = 'TEXT_EDITOR'
    bl_region_type = 'UI'
    bl_label = ""

    def draw ( self, context ):
        layout = self.layout
        layout.operator ( "object.button", "text.open" )

class MyButton ( bpy.types.Operator ) :
    bl_idname = "object.button"
    bl_label = ""

    def execute ( self, context ) :
        if bpy.ops.text :
            bpy.ops.text.unlink ( )

That code close any opened document but doesn’t anything else.

  1. If I put the unlink method away, then the “text.open” works, but how to close the current documents then ?
  2. Other problem is that if I want to close all opened docs I have to use a while loop:

        while bpy.ops.text :
            bpy.ops.text.unlink ( )

But that brings an error after closing the first document !

Thank you for any idea !
SF

Try:

import bpy


def main(context):
    for ob in context.scene.objects:
        print(ob)


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        for t in bpy.data.texts:
            bpy.data.texts.remove(t)
        bpy.ops.text.open('INVOKE_SCREEN')
        return {'FINISHED'}


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


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


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_operator()


If you don’t load another file, make sure to set the reference to the text in text editor spaces to None:

        for area in context.screen.areas:
            if area.type == 'TEXT_EDITOR':
                area.spaces.active.text = None

Thanks CoDEman !
It works fine !
(only changed ‘INVOKE_SCREEN’ to ‘INVOKE_DEFAULT’ to have the opened document active, otherwise the empty screen is active).

Do you have any idea about how to modify the original Blender “text.open” - … layout.operator (“text.open”) - I found it in the original Blender code (…/startup/bl_ui/space_text.py) ?
What is it exactely (module, class …) where is it ? Is it written in C, Py ?

Thx

Right… forgot to change that (got previous version of the script on clipboard).

The file selector is written in C.

Thx for the help !