-
Notifications
You must be signed in to change notification settings - Fork 9
bpy_data_texts
Dealga McArdle edited this page Oct 26, 2015
·
19 revisions
disambiguation : Font object
Text data-blocks are used for all kinds of things. Storing
- notes
- scripts
- data (coordinates, data structures, relationships, etc.,)
- Any string.
some_str = "Hello\nMy name is\ngobbledigook"
f = bpy.data.texts.new('new_text.txt')
f.from_string(some_str)
Both methods below only work on dicts that can be stringified, so they can't contain Objects.
A general Python solution to this involving JSON writing and reading. 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'])
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π