Writing into a text datablock can be done with the script
text = bpy.data.texts[name]
text.write(something)
However, when I used the script text.read
, an error appeared.
Question: How do I read the content in a text datablock with a script?
TextBlock.as_string()
Will give the complete contents of the text block.
Test Text block "FooBarBar"
Foo
Bar
Bar
Once again off to the python console to test.
>>> D.texts['FooBarBar'].as_string()
'Foo\nBar\nBar'
>>> print(D.texts['FooBarBar'].as_string())
Foo
Bar
Bar
>>>
same result as joining lines with
>>> '\n'.join(l.body for l in D.texts['FooBarBar'].lines)
'Foo\nBar\nBar'
On the error...
>>> D.texts['FooBarBar'].read
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
AttributeError: 'Text' object has no attribute 'read'
The error appears in RED that the text block has no read method. This is confirmed by consulting the documentation
My 2c worth of advice, try autocomplete in the console "@MartinZ style", consult the docs, and then....
I have never needed to work with text blocks so I have no idea yet. Let's find out.
If you press Tab after writing something in the Python Console, you can see all the available options from auto complete like this:
We can see that it is possible to access lines text.lines
so if we want to read the 4th line for example we carry on with auto complete:
and see that it has body
, that's probably the thing we need here then:
It's a string. That's it.