you will need 2 text data blocks.
- a script
- the text
some_text_body.txt
Commodi. Do laboriosam. Veniam aliqua modi, aperiam quo so velit. Duis
elit but consequat yet aspernatur. Anim quam incidunt and do. Irure.
Laboris modi and nequeporro aute. Ipsam laboris but minima, and amet
veniam.
Quo eu. Vel. Beatae sed magna yet perspiciatis. Aliquam nesciunt,
quam, and iste, minim, quaerat. Aute natus, or explicabo and odit
eaque but aliquam. Autem. Et dolores eos, laudantium, so ab,
molestiae.
script.py
import bpy
text_object = bpy.data.objects['Text']
text_object.data.body = bpy.data.texts['some_text_body.txt'].as_string()
to get this:
Some gotchas (Texts with sparse line breaks, how to reflow / wrap ? )
With the some_text_body.txt
example above I manually added newlines by hitting enter where I wanted the text to drop to the next line. Hitting Enter puts an invisible character called newline (\n
). Prior to that the text was one line per paragraph. The hidden newline character is often only present at the end of a paragraph or as a single character on a line to denote a line of only white-space.
The difference looks like this:
Same text different reflows:
Unfortunately the reflow / text wrapping isn't done automatically for any of the proposed methods. see:
You can set the width of text box to automatically reflow to the next line. This is found in the Font object data panel.
And that's probably fine for most stuff, but it doesn't appear to do hyphenation on words. In that case you'd look at the Python utility module called textwrap to help reformat text with a given line length measured in characters.
import textwrap
import bpy
text_object = bpy.data.objects['Text']
text_str = bpy.data.texts['some_text_body.txt'].as_string()
new_body = "\n".join(textwrap.wrap(text_str, width=80))
text_object.data.body = new_body
But sadly this doesn't deal with the drop line in the middle..see:
then you use something like this:
import textwrap
import bpy
text_object = bpy.data.objects['Text']
text_str = bpy.data.texts['some_text_body.txt'].as_string()
text_array = text_str.splitlines()
final_list = []
for paragraph in text_array:
final_list.append("\n".join(textwrap.wrap(paragraph, width=80)))
final_list.append('\n')
text_object.data.body = ''.join(final_list)