-
Notifications
You must be signed in to change notification settings - Fork 9
bpy_data_texts
disambiguation: If you're looking for Font / Text Object
go here
Text data-blocks are used for all kinds of things. Storing
- notes
- scripts
- data (coordinates, data structures, relationships, etc.,)
- Any string.
Creating a new text block with content is as simple as this:
some_str = "Hello\nMy name is\ngobbledigook"
f = bpy.data.texts.new('new_text.txt')
f.from_string(some_str)
You might find that it can be useful to indicate what kind of data the text block stores. Just like variables, it's useful to name the text blocks in a meaningful way. The addition of the file-type extension is not mandatory, but often it can describe quickly how the data is arranged.
Both methods below work only on dicts that can be stringified, so they can't contain Objects.
json
JSON can easily be constructed from a dictionary and written to bpy.data.texts
as a string. Imagine you have some dict of information, call it my_dict
. To write that dict as a JSON to the .blend file you do:
import bpy
import json
my_dict = {
'key1': 'some string storage',
'key2': ['some', 'list', 'storage'],
'key3': 5
}
# dump string
m = json.dumps(my_dict, sort_keys=True, indent=2)
text_block = bpy.data.texts.new('my_storage.json')
text_block.from_string(m)
bpy.data.texts['my_storage.json']
would then contain:
{
"key1": "some string storage",
"key2": [
"some",
"list",
"storage"
],
"key3": 5
}
to read this back in at a later stage:
import bpy
import json
text_obj = bpy.data.texts['my_storage.json']
text_str = text_obj.as_string()
my_json = json.loads(text_str)
print(my_json['key1'])
dict and ast.literal_eval
import bpy
import ast
mydict = dict(deb=["two", "three"], far="booo", foo=34)
# write the string representation of the dict to a text datablock
# this assumes you know how to create text datablocks, it is shown in the other method anyway.
d = bpy.data.texts['dict_storage']
d.from_string(str(mydict))
# read from the datablock
d = bpy.data.texts['dict_storage']
stringified_dict = d.as_string()
my_read_dict = ast.literal_eval(stringified_dict)
for k, v in my_read_dict.items():
print(k, v)
Introduction
Objects / Mesh / BMesh
- Empty - null object
- Mesh
- Bmesh
- bmesh.ops - primitives
- bmesh.ops - mesh opsπ§
- Curves (2d, 3d)
- Text (Font Objects)
- Duplication (instancing)
- Metaballs
Time and Motion
- scripted keyframesπ
- Event Handlersπ
- Drivers
Miscellaneous bpy.data.*
Order / Organization
- Groupingπ
- Parentingπ
- Layers
- UI / Layoutπ
Miscellaneous
- Mathutilsπ
- Modifiersπ
- Particle Systemsπ
- vertex colorsπ
- Textures UV DPI
- Propertiesπ§
- Operators (and callbacks)π§
- bgl / blfπ
- Grease Pencil
- Themes
- Areas
Add-ons
- introductionπ
- import / exportπ
- Text Editorπ
- Custom Nodesπ