bpy.ops.text.open - Use and Access data of the read Text file

Hello Developpers,
Blender 2.58. I m trying to read an ASCII file into the Text Editor using bpy.ops.text.open (filepath=“c: emp”). I didn’t manage so far to bring it running. Further I am wondering how could I access the data read by that operation (I guess there must be a list stored internally) since I need to perform some operations on the read text. I found bpy.types.bpy_struct subclass BlendDataTexts. But how to use it (in 2.58) ?

Thanks for any info, I am starting learning the API…

will create a text file called MyTextName (it will appear in the dropdown)


give give you a reference to that object, once created.

textobject = bpy.data.texts[“dothistext”]

then you read in

readfile = open("yourtexttoread.txt", r)
for line in readfile:

you can probably load straight from a file too… dig around. dir(textobject)

Thanks, that is a start for me.

Digging a bit I found the following, coming further but still want to understand …

>>>type (bpy.data.texts[0].lines)
<class 'bpy_prop_collection'>
>>> bpy.data.texts[0].lines[2]

But trying then:

>>> bpy.data.texts[0].lines[2] [5] or (5)

brings nothing or ‘Textline’ object is not callable.
So I understood that lines is not a python list ?!
I tried then:

>>> for line in bpy.data.text[0].lines :
 ...       print (line)
<bpy_struct, TextLine at 0x07EB8D70>
<bpy_struct, TextLine at 0x07EB8CC8>
<bpy_struct, TextLine at 0x07EB8C18>
<bpy_struct, TextLine at 0x07DE5CA0>
<bpy_struct, TextLine at 0x07EA6450>

So what is to take from that :
Each line is stored somewhere in bpy_struct ?! What’s that? Is there a command to access it ?

You probably have a good reason for wanting to first import to a textobject then reading that textobject and parsing the data, but i can’t imagine that it is a very efficient way of going about parsing a text file.

with a textobject with text do

...this is the first line of your text object

so if you want the lines between some selection

>>> [line.body for line in bpy.data.texts[0].lines[4:8]]
['line 5', 'line 6', 'line 7', 'line 8']

>>> selection = [line.body for line in bpy.data.texts[0].lines[4:8]]
>>> for line in selection: print(line)
line 5
line 6
line 7
line 8

for operations like bpy.ops.text you’ll probably want to make a GUI so you can run code from the Text Editor toolshelf.

Thanks, that helped a lot, I can now access the lines. I am too young in programing to say what is the best way. What I want is the following:
I have a text file representing points coordinates X= Y=, I want to import it in the Text Editor and to display the corresponding points mesh in the Viewer. The difficulty will start: I d like the Viewer and the Editor to interact in the way that if I select a point in the viewer the cursor of the Text Editor goes to the corresponding line, showing X= Y= of the point. So there should be a link (correspondance) between the lines X= Y= and the displayed point.
The lines should be an ordered list since the points are driven in the same order.
I thought about numbering the lines and the corresponding points while parsing … seems basic.

I 'll drive everythg through an own GUI indeed.

@LittleNeo: Thx for the link! “Magnifique” that Chess game!

with one vertex selected, go back to ‘OBJECT’ mode, run this in console
>>> [i.index for i in bpy.context.active_object.data.vertices if i.select == True]

also blender.org/documentation/…bpy.ops.text is a good resource, but you are better off making a panel in the TextEditor so that you have context to run bpy.ops.text commands (only available if the script is running from TextEditor view)

I’m interested too in such kind of feature.
bpy.data.texts won’t help I think for selection.

bpy.ops.text. ... 


but it needs a bpy.context.edit_text context to work. I can make it work from inside the current text block. but since your context won’t be the text block…
I don’t know how to … ‘pass the right context’ to the operator (still don’t get it for such case) it seems feasible :

TypeError: Calling operator "bpy.ops.text.select_all" error,<b> custom context expected a dict</b> or None

Thanks to both for your answers!

@littelneo :
I am not sure I understand what you mean. Since I don’t have much knowledge in programing I thought about doing it the hard way, meaning once I have selected a point in the 3d View I will return its coordinates X Y Z and then look in the coordinates dictionary I created when reading the file into the Text Editor (the dictionary’s keys are ‘X’ ‘Y’ ‘Z’). Searching for the match values I will return the line in the Text Editor.
Hope my explaination is clear.

But for now I have a crash when editing the mesh (here)


I was talking about the corresponding line selection in the blender interface.
I answered for your crash.
yes the dict way is a good idea to know the line index.
be careful with floats as key : blender returns 6e-06 notation for 0.000006. or 1e-06 for a vert set located at ‘zero’. you’ll have to find a way with things like :
round(x,6) or int(x*1000000) / 1000000 or convert it to strings :

f = '%1.6f'%f

also if you don’t need 3 dimensions for your dict (do you need to retrieve list of lines or just one?). you could :

def key(v) :
    return '%1.6f-%1.6f-%1.6f'%(v[0],v[1],v[2])

# dict creation
v = [0.00002,0.126375,123,354654]
linedict = {}
linedict[key(v)] = 3 # line number

# dict lookup
v = [0.00002,0.126375,123,354654]
if key(v) in linedict :
    print('line is %s'%linedict[key])