source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/utils/exporters/blender/2.65/scripts/addons/io_mesh_threejs/__init__.py@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 17.0 KB
Line 
1# ##### BEGIN GPL LICENSE BLOCK #####
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software Foundation,
15# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16#
17# ##### END GPL LICENSE BLOCK #####
18
19# ################################################################
20# Init
21# ################################################################
22
23
24bl_info = {
25 "name": "three.js format",
26 "author": "mrdoob, kikko, alteredq, remoe, pxf, n3tfr34k",
27 "version": (1, 4, 0),
28 "blender": (2, 65, 0),
29 "location": "File > Import-Export",
30 "description": "Import-Export three.js meshes",
31 "warning": "",
32 "wiki_url": "https://github.com/mrdoob/three.js/tree/master/utils/exporters/blender",
33 "tracker_url": "https://github.com/mrdoob/three.js/issues",
34 "category": "Import-Export"}
35
36# To support reload properly, try to access a package var,
37# if it's there, reload everything
38
39import bpy
40
41if "bpy" in locals():
42 import imp
43 if "export_threejs" in locals():
44 imp.reload(export_threejs)
45 if "import_threejs" in locals():
46 imp.reload(import_threejs)
47
48from bpy.props import *
49from bpy_extras.io_utils import ExportHelper, ImportHelper
50
51# ################################################################
52# Custom properties
53# ################################################################
54
55bpy.types.Object.THREE_castShadow = bpy.props.BoolProperty()
56bpy.types.Object.THREE_receiveShadow = bpy.props.BoolProperty()
57bpy.types.Object.THREE_doubleSided = bpy.props.BoolProperty()
58bpy.types.Object.THREE_exportGeometry = bpy.props.BoolProperty(default = True)
59
60bpy.types.Material.THREE_useVertexColors = bpy.props.BoolProperty()
61bpy.types.Material.THREE_depthWrite = bpy.props.BoolProperty(default = True)
62bpy.types.Material.THREE_depthTest = bpy.props.BoolProperty(default = True)
63
64THREE_material_types = [("Basic", "Basic", "Basic"), ("Phong", "Phong", "Phong"), ("Lambert", "Lambert", "Lambert")]
65bpy.types.Material.THREE_materialType = EnumProperty(name = "Material type", description = "Material type", items = THREE_material_types, default = "Lambert")
66
67THREE_blending_types = [("NoBlending", "NoBlending", "NoBlending"), ("NormalBlending", "NormalBlending", "NormalBlending"),
68 ("AdditiveBlending", "AdditiveBlending", "AdditiveBlending"), ("SubtractiveBlending", "SubtractiveBlending", "SubtractiveBlending"),
69 ("MultiplyBlending", "MultiplyBlending", "MultiplyBlending"), ("AdditiveAlphaBlending", "AdditiveAlphaBlending", "AdditiveAlphaBlending")]
70bpy.types.Material.THREE_blendingType = EnumProperty(name = "Blending type", description = "Blending type", items = THREE_blending_types, default = "NormalBlending")
71
72class OBJECT_PT_hello( bpy.types.Panel ):
73
74 bl_label = "THREE"
75 bl_space_type = "PROPERTIES"
76 bl_region_type = "WINDOW"
77 bl_context = "object"
78
79 def draw(self, context):
80 layout = self.layout
81 obj = context.object
82
83 row = layout.row()
84 row.label(text="Selected object: " + obj.name )
85
86 row = layout.row()
87 row.prop( obj, "THREE_exportGeometry", text="Export geometry" )
88
89 row = layout.row()
90 row.prop( obj, "THREE_castShadow", text="Casts shadow" )
91
92 row = layout.row()
93 row.prop( obj, "THREE_receiveShadow", text="Receives shadow" )
94
95 row = layout.row()
96 row.prop( obj, "THREE_doubleSided", text="Double sided" )
97
98class MATERIAL_PT_hello( bpy.types.Panel ):
99
100 bl_label = "THREE"
101 bl_space_type = "PROPERTIES"
102 bl_region_type = "WINDOW"
103 bl_context = "material"
104
105 def draw(self, context):
106 layout = self.layout
107 mat = context.material
108
109 row = layout.row()
110 row.label(text="Selected material: " + mat.name )
111
112 row = layout.row()
113 row.prop( mat, "THREE_materialType", text="Material type" )
114
115 row = layout.row()
116 row.prop( mat, "THREE_blendingType", text="Blending type" )
117
118 row = layout.row()
119 row.prop( mat, "THREE_useVertexColors", text="Use vertex colors" )
120
121 row = layout.row()
122 row.prop( mat, "THREE_depthWrite", text="Enable depth writing" )
123
124 row = layout.row()
125 row.prop( mat, "THREE_depthTest", text="Enable depth testing" )
126
127
128# ################################################################
129# Importer
130# ################################################################
131
132class ImportTHREEJS(bpy.types.Operator, ImportHelper):
133 '''Load a Three.js ASCII JSON model'''
134
135 bl_idname = "import.threejs"
136 bl_label = "Import Three.js"
137
138 filename_ext = ".js"
139 filter_glob = StringProperty(default="*.js", options={'HIDDEN'})
140
141 option_flip_yz = BoolProperty(name="Flip YZ", description="Flip YZ", default=True)
142 recalculate_normals = BoolProperty(name="Recalculate normals", description="Recalculate vertex normals", default=True)
143 option_worker = BoolProperty(name="Worker", description="Old format using workers", default=False)
144
145 def execute(self, context):
146 import io_mesh_threejs.import_threejs
147 return io_mesh_threejs.import_threejs.load(self, context, **self.properties)
148
149
150 def draw(self, context):
151 layout = self.layout
152
153 row = layout.row()
154 row.prop(self.properties, "option_flip_yz")
155
156 row = layout.row()
157 row.prop(self.properties, "recalculate_normals")
158
159 row = layout.row()
160 row.prop(self.properties, "option_worker")
161
162
163# ################################################################
164# Exporter - settings
165# ################################################################
166
167SETTINGS_FILE_EXPORT = "threejs_settings_export.js"
168
169import os
170import json
171
172def file_exists(filename):
173 """Return true if file exists and accessible for reading.
174
175 Should be safer than just testing for existence due to links and
176 permissions magic on Unix filesystems.
177
178 @rtype: boolean
179 """
180
181 try:
182 f = open(filename, 'r')
183 f.close()
184 return True
185 except IOError:
186 return False
187
188def get_settings_fullpath():
189 return os.path.join(bpy.app.tempdir, SETTINGS_FILE_EXPORT)
190
191def save_settings_export(properties):
192
193 settings = {
194 "option_export_scene" : properties.option_export_scene,
195 "option_embed_meshes" : properties.option_embed_meshes,
196 "option_url_base_html" : properties.option_url_base_html,
197 "option_copy_textures" : properties.option_copy_textures,
198
199 "option_lights" : properties.option_lights,
200 "option_cameras" : properties.option_cameras,
201
202 "option_animation_morph" : properties.option_animation_morph,
203 "option_animation_skeletal" : properties.option_animation_skeletal,
204
205 "option_frame_step" : properties.option_frame_step,
206 "option_all_meshes" : properties.option_all_meshes,
207
208 "option_flip_yz" : properties.option_flip_yz,
209
210 "option_materials" : properties.option_materials,
211 "option_normals" : properties.option_normals,
212 "option_colors" : properties.option_colors,
213 "option_uv_coords" : properties.option_uv_coords,
214 "option_faces" : properties.option_faces,
215 "option_vertices" : properties.option_vertices,
216
217 "option_skinning" : properties.option_skinning,
218 "option_bones" : properties.option_bones,
219
220 "option_vertices_truncate" : properties.option_vertices_truncate,
221 "option_scale" : properties.option_scale,
222
223 "align_model" : properties.align_model
224 }
225
226 fname = get_settings_fullpath()
227 f = open(fname, "w")
228 json.dump(settings, f)
229
230def restore_settings_export(properties):
231
232 settings = {}
233
234 fname = get_settings_fullpath()
235 if file_exists(fname):
236 f = open(fname, "r")
237 settings = json.load(f)
238
239 properties.option_vertices = settings.get("option_vertices", True)
240 properties.option_vertices_truncate = settings.get("option_vertices_truncate", False)
241 properties.option_faces = settings.get("option_faces", True)
242 properties.option_normals = settings.get("option_normals", True)
243
244 properties.option_colors = settings.get("option_colors", True)
245 properties.option_uv_coords = settings.get("option_uv_coords", True)
246 properties.option_materials = settings.get("option_materials", True)
247
248 properties.option_skinning = settings.get("option_skinning", True)
249 properties.option_bones = settings.get("option_bones", True)
250
251 properties.align_model = settings.get("align_model", "None")
252
253 properties.option_scale = settings.get("option_scale", 1.0)
254 properties.option_flip_yz = settings.get("option_flip_yz", True)
255
256 properties.option_export_scene = settings.get("option_export_scene", False)
257 properties.option_embed_meshes = settings.get("option_embed_meshes", True)
258 properties.option_url_base_html = settings.get("option_url_base_html", False)
259 properties.option_copy_textures = settings.get("option_copy_textures", False)
260
261 properties.option_lights = settings.get("option_lights", False)
262 properties.option_cameras = settings.get("option_cameras", False)
263
264 properties.option_animation_morph = settings.get("option_animation_morph", False)
265 properties.option_animation_skeletal = settings.get("option_animation_skeletal", False)
266
267 properties.option_frame_step = settings.get("option_frame_step", 1)
268 properties.option_all_meshes = settings.get("option_all_meshes", True)
269
270# ################################################################
271# Exporter
272# ################################################################
273
274class ExportTHREEJS(bpy.types.Operator, ExportHelper):
275 '''Export selected object / scene for Three.js (ASCII JSON format).'''
276
277 bl_idname = "export.threejs"
278 bl_label = "Export Three.js"
279
280 filename_ext = ".js"
281
282 option_vertices = BoolProperty(name = "Vertices", description = "Export vertices", default = True)
283 option_vertices_deltas = BoolProperty(name = "Deltas", description = "Delta vertices", default = False)
284 option_vertices_truncate = BoolProperty(name = "Truncate", description = "Truncate vertices", default = False)
285
286 option_faces = BoolProperty(name = "Faces", description = "Export faces", default = True)
287 option_faces_deltas = BoolProperty(name = "Deltas", description = "Delta faces", default = False)
288
289 option_normals = BoolProperty(name = "Normals", description = "Export normals", default = True)
290
291 option_colors = BoolProperty(name = "Colors", description = "Export vertex colors", default = True)
292 option_uv_coords = BoolProperty(name = "UVs", description = "Export texture coordinates", default = True)
293 option_materials = BoolProperty(name = "Materials", description = "Export materials", default = True)
294
295 option_skinning = BoolProperty(name = "Skinning", description = "Export skin data", default = True)
296 option_bones = BoolProperty(name = "Bones", description = "Export bones", default = True)
297
298 align_types = [("None","None","None"), ("Center","Center","Center"), ("Bottom","Bottom","Bottom"), ("Top","Top","Top")]
299 align_model = EnumProperty(name = "Align model", description = "Align model", items = align_types, default = "None")
300
301 option_scale = FloatProperty(name = "Scale", description = "Scale vertices", min = 0.01, max = 1000.0, soft_min = 0.01, soft_max = 1000.0, default = 1.0)
302 option_flip_yz = BoolProperty(name = "Flip YZ", description = "Flip YZ", default = True)
303
304 option_export_scene = BoolProperty(name = "Scene", description = "Export scene", default = False)
305 option_embed_meshes = BoolProperty(name = "Embed meshes", description = "Embed meshes", default = True)
306 option_copy_textures = BoolProperty(name = "Copy textures", description = "Copy textures", default = False)
307 option_url_base_html = BoolProperty(name = "HTML as url base", description = "Use HTML as url base ", default = False)
308
309 option_lights = BoolProperty(name = "Lights", description = "Export default scene lights", default = False)
310 option_cameras = BoolProperty(name = "Cameras", description = "Export default scene cameras", default = False)
311
312 option_animation_morph = BoolProperty(name = "Morph animation", description = "Export animation (morphs)", default = False)
313 option_animation_skeletal = BoolProperty(name = "Skeletal animation", description = "Export animation (skeletal)", default = False)
314
315 option_frame_step = IntProperty(name = "Frame step", description = "Animation frame step", min = 1, max = 1000, soft_min = 1, soft_max = 1000, default = 1)
316 option_all_meshes = BoolProperty(name = "All meshes", description = "All meshes (merged)", default = True)
317
318 def invoke(self, context, event):
319 restore_settings_export(self.properties)
320 return ExportHelper.invoke(self, context, event)
321
322 @classmethod
323 def poll(cls, context):
324 return context.active_object is not None
325
326 def execute(self, context):
327 print("Selected: " + context.active_object.name)
328
329 if not self.properties.filepath:
330 raise Exception("filename not set")
331
332 save_settings_export(self.properties)
333
334 filepath = self.filepath
335
336 import io_mesh_threejs.export_threejs
337 return io_mesh_threejs.export_threejs.save(self, context, **self.properties)
338
339 def draw(self, context):
340 layout = self.layout
341
342 row = layout.row()
343 row.label(text="Geometry:")
344
345 row = layout.row()
346 row.prop(self.properties, "option_vertices")
347 # row = layout.row()
348 # row.enabled = self.properties.option_vertices
349 # row.prop(self.properties, "option_vertices_deltas")
350 row.prop(self.properties, "option_vertices_truncate")
351 layout.separator()
352
353 row = layout.row()
354 row.prop(self.properties, "option_faces")
355 row = layout.row()
356 row.enabled = self.properties.option_faces
357 # row.prop(self.properties, "option_faces_deltas")
358 layout.separator()
359
360 row = layout.row()
361 row.prop(self.properties, "option_normals")
362 layout.separator()
363
364 row = layout.row()
365 row.prop(self.properties, "option_bones")
366 row.prop(self.properties, "option_skinning")
367 layout.separator()
368
369 row = layout.row()
370 row.label(text="Materials:")
371
372 row = layout.row()
373 row.prop(self.properties, "option_uv_coords")
374 row.prop(self.properties, "option_colors")
375 row = layout.row()
376 row.prop(self.properties, "option_materials")
377 layout.separator()
378
379 row = layout.row()
380 row.label(text="Settings:")
381
382 row = layout.row()
383 row.prop(self.properties, "align_model")
384 row = layout.row()
385 row.prop(self.properties, "option_flip_yz")
386 row.prop(self.properties, "option_scale")
387 layout.separator()
388
389 row = layout.row()
390 row.label(text="--------- Experimental ---------")
391 layout.separator()
392
393 row = layout.row()
394 row.label(text="Scene:")
395
396 row = layout.row()
397 row.prop(self.properties, "option_export_scene")
398 row.prop(self.properties, "option_embed_meshes")
399
400 row = layout.row()
401 row.prop(self.properties, "option_lights")
402 row.prop(self.properties, "option_cameras")
403 layout.separator()
404
405 row = layout.row()
406 row.label(text="Animation:")
407
408 row = layout.row()
409 row.prop(self.properties, "option_animation_morph")
410 row = layout.row()
411 row.prop(self.properties, "option_animation_skeletal")
412 row = layout.row()
413 row.prop(self.properties, "option_frame_step")
414 layout.separator()
415
416 row = layout.row()
417 row.label(text="Settings:")
418
419 row = layout.row()
420 row.prop(self.properties, "option_all_meshes")
421
422 row = layout.row()
423 row.prop(self.properties, "option_copy_textures")
424
425 row = layout.row()
426 row.prop(self.properties, "option_url_base_html")
427
428 layout.separator()
429
430
431# ################################################################
432# Common
433# ################################################################
434
435def menu_func_export(self, context):
436 default_path = bpy.data.filepath.replace(".blend", ".js")
437 self.layout.operator(ExportTHREEJS.bl_idname, text="Three.js (.js)").filepath = default_path
438
439def menu_func_import(self, context):
440 self.layout.operator(ImportTHREEJS.bl_idname, text="Three.js (.js)")
441
442def register():
443 bpy.utils.register_module(__name__)
444 bpy.types.INFO_MT_file_export.append(menu_func_export)
445 bpy.types.INFO_MT_file_import.append(menu_func_import)
446
447def unregister():
448 bpy.utils.unregister_module(__name__)
449 bpy.types.INFO_MT_file_export.remove(menu_func_export)
450 bpy.types.INFO_MT_file_import.remove(menu_func_import)
451
452if __name__ == "__main__":
453 register()
Note: See TracBrowser for help on using the repository browser.