How to get the active script name?

Is there a way to know which is the displayed script in the list “bpy.data.texts”?

Thanks,

Paolo

Well, that assumes you only have one text window in your layout. What if I have four text windows? Which one should the imaginary code return? Focus is determined by the mouse position. So you would need a timer to scan the mouse, then examine the window layouts then determine which on the mouse was over, then if it was a text window you could return the filename of the text window.

If you just want to know the name of the script that is currently running, you can use.

print(__file__)

Follow that up with some path parsing.

you can use something like this.


import bpy

text = bpy.context.area.spaces[0].text
print(text)

Nice to know. According to the docs, spaces[0] is always the space with focus.


import bpy

text = bpy.context.area.spaces[0].text.name
print(text)

Hi this is more accurate.


import bpy
space = bpy.context.area.spaces.active
text = space.text
print(text.name)

The area with the focus is bpy.context.area. Since an area can have multiple spaces, like splitted 3d views, spaces[0] may not be the correct one, so using active we make sure that it is.

You cant, since you are in the console, the text editor hasnt the focus so you cant know what its the active one. You can supply the name of the script that you want to run or try a workaround like saving the last active text editor text into some kind of variable.

Hello,
Inspired by that, I made a UI in the Text Property panel (to have the context) and I would like to return the name of the current file of the Text Editor. Problem is when there is no file, bpy.context.area.spaces.active.text returns “None” which has no method “name”, and I didn’t find any way to pass a string “None” or whatever in the field of row.prop …

Thanks for any idea!


import bpy
from bpy.props import *
 
class TextPanel ( bpy.types.Panel ) :
 
    bl_label = "File :"
    bl_space_type = "TEXT_EDITOR"
    bl_region_type = "UI"
    def draw(self, context):
 
        layout = self.layout
        layout.label("File:")
 
        row = layout.row ( align = True )
 
 
        if bpy.context.area.spaces.active.text == None :
            print ("then how should I use row.prop? I d like to return the string None")
 
        else :
            row.prop ( bpy.context.area.spaces.active.text , "name" )
 
 
def register():
 
    bpy.utils.register_class( TextPanel )
def unregister():
 
    bpy.utils.unregister_class ( TextPanel )
 
if __name__ == "__main__":
    register()

Why do you need to return anything if theres no text? Its ok like that. Besides, anywhere in your script you have to check if theres an active text. If not return nothing.

You can also use poll functions to prevent execution before the draw function.
http://www.blender.org/documentation/blender_python_api_2_58_release/bpy.types.Panel.html

or a more cheese solution:

if context.area.spaces.active.text==None:
row.label(“theres no text”)
else:
row.prop(context.area.spaces.active.text, “name”)

Thanks for answer!
In case there s no text, I d like to have the info : whatever “None” writtten in the field or “There is no file opened”.
I thought about the row.label that you propose, but it does not look nice to have once a label and then a real field created by row.prop.

I am new in python so I don t know how to pass any other argument or variable to row.prop. I tried indeed to reassign the “name” with context.area.spaces.active.text.name = “None” but of course it stays by “NoneType object has no attribute name”.

Finally I found this workaround:
I go on bpy.data.texts and create a dummy file :


        if bpy.context.area.spaces.active.text == None :
            bpy.data.texts.new("")
            row.prop ( bpy.data.texts[0] , "name" )