-
Notifications
You must be signed in to change notification settings - Fork 14
/
textToFile.py
133 lines (113 loc) · 4.64 KB
/
textToFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# Copyright (c) 2015 Shane Ambler
#
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# based on response to --
# http://blender.stackexchange.com/a/26722/935
# by Chebhou
#
# My version splits into two operators to work without a dialog
# this allows use of buttons without a dialog
# in my opinion this is a better user experience.
bl_info = {
"name": "Text conversion",
"author": "sambler",
"version": (1,1),
"blender": (2, 80, 0),
"location": "View3D > Object > Convert text object",
"description": "Create a text file from the contents of a text object, or set the object contents from a file.",
"warning": "",
"wiki_url": "https://github.com/sambler/addonsByMe/blob/master/textToFile.py",
"tracker_url": "https://github.com/sambler/addonsByMe/issues",
"category": "3D View",
}
import bpy
class textToFile(bpy.types.Operator):
"""text file to text object"""
bl_idname = "object.text_to_file"
bl_label = "Convert text object to file"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
obj = bpy.context.active_object
if obj.type == 'FONT':
if None == bpy.data.texts.get(obj.name):
bpy.data.texts.new(name = obj.name)
text_file = bpy.data.texts[obj.name]
text_file.clear()
text = obj.data.body
text_file.write(text)
return {'FINISHED'}
class fileToText(bpy.types.Operator):
"""text object from text file"""
bl_idname = "object.file_to_text"
bl_label = "Convert text file to object"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
obj = bpy.context.active_object
if obj.type == 'FONT':
if None == bpy.data.texts.get(obj.name):
bpy.data.texts.new(name = obj.name)
text_file = bpy.data.texts[obj.name]
obj.data.body = text_file.as_string()
return {'FINISHED'}
class textConversion(bpy.types.Panel):
"""Panel for quick text conversion"""
bl_label = "Convert Text"
bl_idname = "SCENE_PT_conversion"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
@classmethod
def poll(cls, context):
return (context.object is not None and
context.object.type == 'FONT')
def draw(self, context):
layout = self.layout
scene = context.scene
row = layout.row()
row.operator("object.text_to_file")
row = layout.row()
row.operator("object.file_to_text")
class textSubMenu(bpy.types.Menu):
bl_label = "Convert text"
bl_idname = "OBJECT_MT_convert_text"
def draw(self, context):
self.layout.operator(textToFile.bl_idname)
self.layout.operator(fileToText.bl_idname)
def convertMenu(self, context):
self.layout.menu(textSubMenu.bl_idname, icon = 'OUTLINER_DATA_FONT')
def register():
bpy.utils.register_class(textToFile)
bpy.utils.register_class(fileToText)
bpy.utils.register_class(textConversion)
bpy.utils.register_class(textSubMenu)
bpy.types.VIEW3D_MT_object.append(convertMenu)
def unregister():
bpy.types.VIEW3D_MT_object.remove(convertMenu)
bpy.utils.unregister_class(textToFile)
bpy.utils.unregister_class(fileToText)
bpy.utils.unregister_class(textConversion)
bpy.utils.unregister_class(textSubMenu)
if __name__ == "__main__":
register()