-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
271 lines (233 loc) · 11.4 KB
/
generator.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
bl_info = {
"name": "Export to p5.js",
"description": "Exports a scene to a hardcoded file using the p5.js library",
"author": "EmuMan",
"version": (0, 0, 1),
"blender": (2, 90, 0),
"location": "3D View > p5.js",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"
}
import json
import bpy
from bpy.props import (StringProperty,
PointerProperty,
)
from bpy.types import (Panel,
Operator,
PropertyGroup,
)
from bpy.utils import register_class, unregister_class
from mathutils import *
class DirectProperties(PropertyGroup):
template_filepath: StringProperty(
name = "Template File",
description = "The template text file to be used",
default = "",
maxlen = 1024,
subtype = 'FILE_PATH'
)
output_dirpath: StringProperty(
name = "Output Directory",
description = "The directory to save the file in",
default = "",
maxlen = 1024,
subtype = 'DIR_PATH'
)
output_filename: StringProperty(
name = "Filename",
description = "The name of the output file",
default = "",
maxlen = 1024,
subtype = 'FILE_NAME'
)
class JSONProperties(PropertyGroup):
output_dirpath: StringProperty(
name = "Output Directory",
description = "The directory to save the file in",
default = "",
maxlen = 1024,
subtype = 'DIR_PATH'
)
output_filename: StringProperty(
name = "Filename",
description = "The name of the output file",
default = "",
maxlen = 1024,
subtype = 'FILE_NAME'
)
class JSExport(Operator):
bl_label = "Export Direct to JS"
bl_idname = "wm.export_direct"
def execute(self, context):
scene = context.scene
tool = scene.p5js_direct_tool
final_lines = []
with open(tool.template_filepath, "rt") as f:
template_lines = f.readlines()
for line in template_lines:
for collection in bpy.data.collections:
if f"%%{collection.name}%%" in line:
indentation = ""
for char in line:
if char == " " or char == "\t": indentation += char
else: break
for object in collection.objects:
if "camera" in object.name and not object.name.startswith("obj_"):
camera_up = object.matrix_world.to_quaternion() @ Vector((0.0, -1.0, 0.0))
camera_forward = object.matrix_world.to_quaternion() @ Vector((0.0, 0.0, -1.0))
camera_pointing = object.location + camera_forward
final_lines.append(indentation +
f"camera({-object.location.x}, {object.location.y}, {object.location.z}, {-camera_pointing.x}, {camera_pointing.y}, {camera_pointing.z}, {-camera_up.x}, {camera_up.y}, {camera_up.z});")
final_lines.append("\n")
for object in collection.objects:
if object.name.startswith("obj_"):
object_type = object.name[4:]
try:
color = [v * 255 for v in object.active_material.node_tree.nodes[1].inputs[0].default_value[:3]]
except AttributeError:
color = [205, 205, 205]
final_lines.append(indentation + "push();\n")
final_lines.append(indentation + f"translate({-object.location.x}, {object.location.y}, {object.location.z});\n")
final_lines.append(indentation + f"rotateZ({-object.rotation_euler.z});\n")
final_lines.append(indentation + f"rotateY({-object.rotation_euler.y});\n")
x_rotation = object.rotation_euler.x + (3.14159265 / 2 if "box" in object_type or "cylinder" in object_type or "cone" in object_type else 0)
final_lines.append(indentation + f"rotateX({x_rotation});\n")
final_lines.append(indentation + f"fill({color[0]}, {color[1]}, {color[2]});\n")
if "box" in object_type:
final_lines.append(indentation + f"box({object.dimensions.x}, {object.dimensions.z}, {object.dimensions.y});\n")
elif "plane" in object_type:
final_lines.append(indentation + f"plane({object.dimensions.x}, {object.dimensions.y});\n")
elif "sphere" in object_type:
final_lines.append(indentation + f"sphere({object.dimensions.x / 2}, 24, 24);\n")
elif "cylinder" in object_type:
final_lines.append(indentation + f"cylinder({object.dimensions.x / 2}, {object.dimensions.z}, 24, 1, true, true);\n")
elif "cone" in object_type:
final_lines.append(indentation + f"cone({object.dimensions.x / 2}, {object.dimensions.z}, 24, 1, true);\n")
elif "ellipsoid" in object_type:
final_lines.append(indentation + f"ellipsoid({object.dimensions.x / 2}, {object.dimensions.y / 2}, {object.dimensions.z / 2}, 24, 24);\n")
elif "torus" in object_type:
final_lines.append(indentation + f"torus({(object.dimensions.x - object.dimensions.z) / 2}, {object.dimensions.z / 2});\n")
final_lines.append(indentation + "pop();\n")
final_lines.append("\n")
elif "light" in object.name:
final_lines.append(indentation + f"pointLight(255, 255, 255, {-object.location.x}, {object.location.y}, {object.location.z});\n")
final_lines.append("\n")
continue
final_lines.append(line)
with open(str(tool.output_dirpath) + str(tool.output_filename), "wt+") as f:
f.writelines(final_lines)
return {"FINISHED"}\
class JSONExport(Operator):
bl_label = "Export to JSON"
bl_idname = "wm.export_json"
def execute(self, context):
scene = context.scene
tool = scene.p5js_json_tool
final = {}
for collection in bpy.data.collections:
final[collection.name] = []
for object in collection.objects:
name_components = object.name.split("_")
obj = {}
if name_components[0] == 'camera':
obj['type'] = 'camera'
obj['name'] = object.name
obj['location'] = [-object.location.x, object.location.y, object.location.z]
camera_up = object.matrix_world.to_quaternion() @ Vector((0.0, -1.0, 0.0))
obj['up'] = [-camera_up.x, camera_up.y, camera_up.z]
camera_forward = object.matrix_world.to_quaternion() @ Vector((0.0, 0.0, -1.0))
camera_target = object.location + camera_forward
obj['target'] = [-camera_target.x, camera_target.y, camera_target.z]
elif name_components[0] == "obj" and len(name_components) > 1:
obj_type = name_components[1]
obj['type'] = obj_type
obj['name'] = "_".join(name_components[2:])
try:
obj['color'] = [v * 255 for v in object.active_material.node_tree.nodes[1].inputs[0].default_value[:3]]
except AttributeError:
obj['color'] = [205, 205, 205]
obj['color'].append(255) # alpha value
obj['location'] = [-object.location.x, object.location.y, object.location.z]
x_rotation = object.rotation_euler.x + (3.14159265 / 2 if obj_type == 'box' or obj_type == 'cylinder' or obj_type == 'cone' else 0)
obj['rotation'] = [x_rotation, -object.rotation_euler.y, -object.rotation_euler.z]
obj['scale'] = [1, 1, 1]
if obj_type == 'box':
obj['dimensions'] = [object.dimensions.x, object.dimensions.z, object.dimensions.y]
elif obj_type == 'plane':
obj['dimensions'] = [object.dimensions.x, object.dimensions.y]
elif obj_type == 'sphere':
obj['dimensions'] = [object.dimensions.x / 2]
elif obj_type == 'cylinder' or obj_type == 'cone':
obj['dimensions'] = [object.dimensions.x / 2, object.dimensions.z]
elif obj_type == 'ellipsoid':
obj['dimensions'] = [object.dimensions.x / 2, object.dimensions.y / 2, object.dimensions.z / 2]
elif obj_type == 'torus':
obj['dimensions'] = [(object.dimensions.x - object.dimensions.z) / 2, object.dimensions.z / 2]
elif name_components[0] == 'light':
obj['type'] = 'light'
obj['name'] = object.name
obj['color'] = [255, 255, 255] # TODO: Make support for different colors
obj['location'] = [-object.location.x, object.location.y, object.location.z]
else:
continue
final[collection.name].append(obj)
continue
with open(str(tool.output_dirpath) + str(tool.output_filename), "wt+") as f:
json.dump(final, f, indent=4)
return {"FINISHED"}
class DirectPanel(Panel):
bl_label = "Direct to JS"
bl_idname = "OBJECT_PT_p5js_direct"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "p5.js"
bl_context = "objectmode"
@classmethod
def poll(cls, context):
return True
def draw(self, context):
layout = self.layout
scene = context.scene
tool = scene.p5js_direct_tool
layout.prop(tool, "template_filepath")
layout.prop(tool, "output_dirpath")
layout.prop(tool, "output_filename")
layout.operator("wm.export_direct")
class JSONPanel(Panel):
bl_label = "JSON Intermediary"
bl_idname = "OBJECT_PT_p5js_json"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "p5.js"
bl_context = "objectmode"
@classmethod
def poll(cls, context):
return True
def draw(self, context):
layout = self.layout
scene = context.scene
tool = scene.p5js_json_tool
layout.prop(tool, "output_dirpath")
layout.prop(tool, "output_filename")
layout.operator("wm.export_json")
classes = (
DirectProperties,
JSONProperties,
JSExport,
JSONExport,
DirectPanel,
JSONPanel
)
def register():
for cls in classes:
register_class(cls)
bpy.types.Scene.p5js_direct_tool = PointerProperty(type=DirectProperties)
bpy.types.Scene.p5js_json_tool = PointerProperty(type=JSONProperties)
def unregister():
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.p5js_direct_tool
del bpy.types.Scene.p5js_json_tool