# Is it possible to drive the contents of an existing text object with a text file?

Recently I have been trying to use Blender to make some screen animation templates. I am running into one major roadblock though in the form of getting large amounts of data into a pre-existing text object. It seems that the standard Ctrl+V method of pasting does not work for text objects, and I can't migrate the text from a file to my pre-existing text objects.

I know how to convert a text file to a text object, but is there any way to make a text object display what is contained in a text file, manually or dynamically?

• can you elaborate in what way you would need this to be dynamic? – zeffii Oct 7 '15 at 21:30
• @zeffii This will be a template, so I need to be able to easily change the content by updating the text file – VRM Oct 7 '15 at 22:10
• This answer provides a way to automatically update the text object from text editor content and an addon to transfer text between an object and text editor. – sambler Oct 8 '15 at 12:26
• @sambler Wow! That is even better! – VRM Oct 8 '15 at 12:49

you will need 2 text data blocks.

1. a script
2. 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)


• This does exactly what I needed, thank you! – VRM Oct 7 '15 at 23:14

you can CTRL+V into the text editor and then use "text to object"

• As I wrote in my question, I know about this. I want to use a pre-existing object. – VRM Oct 7 '15 at 18:57

On your existing text object just "paste file" the text from the file you want to import.

In edit mode for your text object, from the 3D view header Edit > Paste File. That will add the text from the selected file to the text object. If you want to replace all the current text, you must delete all the text first.

(Two ways to remove all text form the text object.)

1. CtrlA then Delete

or

1. Backspace or Delete then in the operator settings, change the Type to All.

As you noticed the standard CtrlV will not work, that is because the copy and paste is only in side the current text object. Even a different text object in the same blend file will not paste text copied from another text object.

You can paste from the clipboard (3D view header Edit > Paste Clipboard), which uses the system clipboard and is much more useful.

To be able to copy to the system clipboard (something that would make copying and pasting text outside of one text object possible) you would need an addon I wrote:

### CopyToClipboard

It is hardly worthy of the title "addon" (only two lines) but it is the only way to copy text to the system clipboard. It will copy all the text in the active text object.

Usage is simple, from the 3D View Header: Edit > Copy To Clipboard.