__init__.py 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. import os
  19. import json
  20. import logging
  21. import bpy
  22. from bpy_extras.io_utils import ExportHelper
  23. from bpy.props import (
  24. EnumProperty,
  25. BoolProperty,
  26. FloatProperty,
  27. IntProperty,
  28. StringProperty
  29. )
  30. from . import constants
  31. logging.basicConfig(
  32. format='%(levelname)s:THREE:%(message)s',
  33. level=logging.DEBUG)
  34. bl_info = {
  35. 'name': "Three.js Format",
  36. 'author': "repsac, mrdoob, yomotsu, mpk, jpweeks, rkusa, tschw, jackcaron, bhouston",
  37. 'version': (1, 5, 0),
  38. 'blender': (2, 74, 0),
  39. 'location': "File > Export",
  40. 'description': "Export Three.js formatted JSON files.",
  41. 'warning': "Importer not included.",
  42. 'wiki_url': "https://github.com/mrdoob/three.js/tree/"\
  43. "master/utils/exporters/blender",
  44. 'tracker_url': "https://github.com/mrdoob/three.js/issues",
  45. 'category': 'Import-Export'
  46. }
  47. def _geometry_types():
  48. """The valid geometry types that are supported by Three.js
  49. :return: list of tuples
  50. """
  51. keys = (constants.GLOBAL,
  52. constants.GEOMETRY,
  53. constants.BUFFER_GEOMETRY)
  54. types = []
  55. for key in keys:
  56. types.append((key, key.title(), key))
  57. return types
  58. bpy.types.Mesh.THREE_geometry_type = EnumProperty(
  59. name="Geometry type",
  60. description="Geometry type",
  61. items=_geometry_types(),
  62. default=constants.GLOBAL)
  63. class ThreeMesh(bpy.types.Panel):
  64. """Creates custom properties on a mesh node"""
  65. bl_label = 'THREE'
  66. bl_space_type = 'PROPERTIES'
  67. bl_region_type = 'WINDOW'
  68. bl_context = 'data'
  69. def draw(self, context):
  70. """
  71. :param context:
  72. """
  73. row = self.layout.row()
  74. if context.mesh:
  75. row.prop(context.mesh,
  76. 'THREE_geometry_type',
  77. text="Type")
  78. def _blending_types(index):
  79. """Supported blending types for Three.js
  80. :param index:
  81. :type index: int
  82. :returns: tuple if types (str, str, str)
  83. """
  84. types = (constants.BLENDING_TYPES.NONE,
  85. constants.BLENDING_TYPES.NORMAL,
  86. constants.BLENDING_TYPES.ADDITIVE,
  87. constants.BLENDING_TYPES.SUBTRACTIVE,
  88. constants.BLENDING_TYPES.MULTIPLY,
  89. constants.BLENDING_TYPES.CUSTOM)
  90. return (types[index], types[index], types[index])
  91. bpy.types.Material.THREE_blending_type = EnumProperty(
  92. name="Blending type",
  93. description="Blending type",
  94. items=[_blending_types(x) for x in range(6)],
  95. default=constants.BLENDING_TYPES.NORMAL)
  96. bpy.types.Material.THREE_depth_write = BoolProperty(default=True)
  97. bpy.types.Material.THREE_depth_test = BoolProperty(default=True)
  98. bpy.types.Material.THREE_double_sided = BoolProperty(default=False)
  99. class ThreeMaterial(bpy.types.Panel):
  100. """Adds custom properties to the Materials of an object"""
  101. bl_label = 'THREE'
  102. bl_space_type = 'PROPERTIES'
  103. bl_region_type = 'WINDOW'
  104. bl_context = 'material'
  105. def draw(self, context):
  106. """
  107. :param context:
  108. """
  109. layout = self.layout
  110. mat = context.material
  111. if mat is not None:
  112. row = layout.row()
  113. row.label(text="Selected material: %s" % mat.name)
  114. row = layout.row()
  115. row.prop(mat, 'THREE_blending_type',
  116. text="Blending type")
  117. row = layout.row()
  118. row.prop(mat, 'THREE_depth_write',
  119. text="Enable depth writing")
  120. row = layout.row()
  121. row.prop(mat, 'THREE_depth_test',
  122. text="Enable depth testing")
  123. row = layout.row()
  124. row.prop(mat, 'THREE_double_sided',
  125. text="Double-sided")
  126. def _mag_filters(index):
  127. """Three.js mag filters
  128. :param index:
  129. :type index: int
  130. :returns: tuple with the filter values
  131. """
  132. types = (constants.LINEAR_FILTERS.LINEAR,
  133. constants.NEAREST_FILTERS.NEAREST)
  134. return (types[index], types[index], types[index])
  135. bpy.types.Texture.THREE_mag_filter = EnumProperty(
  136. name="Mag Filter",
  137. items=[_mag_filters(x) for x in range(2)],
  138. default=constants.LINEAR_FILTERS.LINEAR)
  139. def _min_filters(index):
  140. """Three.js min filters
  141. :param index:
  142. :type index: int
  143. :returns: tuple with the filter values
  144. """
  145. types = (constants.LINEAR_FILTERS.LINEAR,
  146. constants.LINEAR_FILTERS.MIP_MAP_NEAREST,
  147. constants.LINEAR_FILTERS.MIP_MAP_LINEAR,
  148. constants.NEAREST_FILTERS.NEAREST,
  149. constants.NEAREST_FILTERS.MIP_MAP_NEAREST,
  150. constants.NEAREST_FILTERS.MIP_MAP_LINEAR)
  151. return (types[index], types[index], types[index])
  152. bpy.types.Texture.THREE_min_filter = EnumProperty(
  153. name="Min Filter",
  154. items=[_min_filters(x) for x in range(6)],
  155. default=constants.LINEAR_FILTERS.MIP_MAP_LINEAR)
  156. def _mapping(index):
  157. """Three.js texture mappings types
  158. :param index:
  159. :type index: int
  160. :returns: tuple with the mapping values
  161. """
  162. types = (constants.MAPPING_TYPES.UV,
  163. constants.MAPPING_TYPES.CUBE_REFLECTION,
  164. constants.MAPPING_TYPES.CUBE_REFRACTION,
  165. constants.MAPPING_TYPES.SPHERICAL_REFLECTION)
  166. return (types[index], types[index], types[index])
  167. bpy.types.Texture.THREE_mapping = EnumProperty(
  168. name="Mapping",
  169. items=[_mapping(x) for x in range(4)],
  170. default=constants.MAPPING_TYPES.UV)
  171. class ThreeTexture(bpy.types.Panel):
  172. """Adds custom properties to a texture"""
  173. bl_label = 'THREE'
  174. bl_space_type = 'PROPERTIES'
  175. bl_region_type = 'WINDOW'
  176. bl_context = 'texture'
  177. #@TODO: possible to make cycles compatible?
  178. def draw(self, context):
  179. """
  180. :param context:
  181. """
  182. layout = self.layout
  183. tex = context.texture
  184. if tex is not None:
  185. row = layout.row()
  186. row.prop(tex, 'THREE_mapping', text="Mapping")
  187. row = layout.row()
  188. row.prop(tex, 'THREE_mag_filter', text="Mag Filter")
  189. row = layout.row()
  190. row.prop(tex, 'THREE_min_filter', text="Min Filter")
  191. bpy.types.Object.THREE_export = bpy.props.BoolProperty(default=True)
  192. class ThreeObject(bpy.types.Panel):
  193. """Adds custom properties to an object"""
  194. bl_label = 'THREE'
  195. bl_space_type = 'PROPERTIES'
  196. bl_region_type = 'WINDOW'
  197. bl_context = 'object'
  198. def draw(self, context):
  199. """
  200. :param context:
  201. """
  202. layout = self.layout
  203. obj = context.object
  204. row = layout.row()
  205. row.prop(obj, 'THREE_export', text='Export')
  206. class ThreeExportSettings(bpy.types.Operator):
  207. """Save the current export settings (gets saved in .blend)"""
  208. bl_label = "Save Settings"
  209. bl_idname = "scene.three_export_settings_set"
  210. def execute(self, context):
  211. cycles = context.scene.cycles
  212. cycles.use_samples_final = True
  213. context.scene[constants.EXPORT_SETTINGS_KEY] = set_settings(context.active_operator.properties)
  214. self.report({"INFO"}, "Three Export Settings Saved")
  215. return {"FINISHED"}
  216. def restore_export_settings(properties, settings):
  217. """Restore the settings
  218. :param properties:
  219. """
  220. ## Geometry {
  221. properties.option_vertices = settings.get(
  222. constants.VERTICES,
  223. constants.EXPORT_OPTIONS[constants.VERTICES])
  224. properties.option_faces = settings.get(
  225. constants.FACES,
  226. constants.EXPORT_OPTIONS[constants.FACES])
  227. properties.option_normals = settings.get(
  228. constants.NORMALS,
  229. constants.EXPORT_OPTIONS[constants.NORMALS])
  230. properties.option_skinning = settings.get(
  231. constants.SKINNING,
  232. constants.EXPORT_OPTIONS[constants.SKINNING])
  233. properties.option_bones = settings.get(
  234. constants.BONES,
  235. constants.EXPORT_OPTIONS[constants.BONES])
  236. properties.option_influences = settings.get(
  237. constants.INFLUENCES_PER_VERTEX,
  238. constants.EXPORT_OPTIONS[constants.INFLUENCES_PER_VERTEX])
  239. properties.option_apply_modifiers = settings.get(
  240. constants.APPLY_MODIFIERS,
  241. constants.EXPORT_OPTIONS[constants.APPLY_MODIFIERS])
  242. properties.option_extra_vgroups = settings.get(
  243. constants.EXTRA_VGROUPS,
  244. constants.EXPORT_OPTIONS[constants.EXTRA_VGROUPS])
  245. properties.option_geometry_type = settings.get(
  246. constants.GEOMETRY_TYPE,
  247. constants.EXPORT_OPTIONS[constants.GEOMETRY_TYPE])
  248. properties.option_index_type = settings.get(
  249. constants.INDEX_TYPE,
  250. constants.EXPORT_OPTIONS[constants.INDEX_TYPE])
  251. ## }
  252. ## Materials {
  253. properties.option_materials = settings.get(
  254. constants.MATERIALS,
  255. constants.EXPORT_OPTIONS[constants.MATERIALS])
  256. properties.option_uv_coords = settings.get(
  257. constants.UVS,
  258. constants.EXPORT_OPTIONS[constants.UVS])
  259. properties.option_face_materials = settings.get(
  260. constants.FACE_MATERIALS,
  261. constants.EXPORT_OPTIONS[constants.FACE_MATERIALS])
  262. properties.option_maps = settings.get(
  263. constants.MAPS,
  264. constants.EXPORT_OPTIONS[constants.MAPS])
  265. properties.option_colors = settings.get(
  266. constants.COLORS,
  267. constants.EXPORT_OPTIONS[constants.COLORS])
  268. properties.option_mix_colors = settings.get(
  269. constants.MIX_COLORS,
  270. constants.EXPORT_OPTIONS[constants.MIX_COLORS])
  271. ## }
  272. ## Settings {
  273. properties.option_scale = settings.get(
  274. constants.SCALE,
  275. constants.EXPORT_OPTIONS[constants.SCALE])
  276. properties.option_round_off = settings.get(
  277. constants.ENABLE_PRECISION,
  278. constants.EXPORT_OPTIONS[constants.ENABLE_PRECISION])
  279. properties.option_round_value = settings.get(
  280. constants.PRECISION,
  281. constants.EXPORT_OPTIONS[constants.PRECISION])
  282. properties.option_custom_properties = settings.get(
  283. constants.CUSTOM_PROPERTIES,
  284. constants.EXPORT_OPTIONS[constants.CUSTOM_PROPERTIES])
  285. properties.option_logging = settings.get(
  286. constants.LOGGING,
  287. constants.EXPORT_OPTIONS[constants.LOGGING])
  288. properties.option_compression = settings.get(
  289. constants.COMPRESSION,
  290. constants.NONE)
  291. properties.option_indent = settings.get(
  292. constants.INDENT,
  293. constants.EXPORT_OPTIONS[constants.INDENT])
  294. properties.option_export_textures = settings.get(
  295. constants.EXPORT_TEXTURES,
  296. constants.EXPORT_OPTIONS[constants.EXPORT_TEXTURES])
  297. properties.option_embed_textures = settings.get(
  298. constants.EMBED_TEXTURES,
  299. constants.EXPORT_OPTIONS[constants.EMBED_TEXTURES])
  300. properties.option_texture_folder = settings.get(
  301. constants.TEXTURE_FOLDER,
  302. constants.EXPORT_OPTIONS[constants.TEXTURE_FOLDER])
  303. properties.option_embed_animation = settings.get(
  304. constants.EMBED_ANIMATION,
  305. constants.EXPORT_OPTIONS[constants.EMBED_ANIMATION])
  306. ## }
  307. ## Scene {
  308. properties.option_export_scene = settings.get(
  309. constants.SCENE,
  310. constants.EXPORT_OPTIONS[constants.SCENE])
  311. #properties.option_embed_geometry = settings.get(
  312. # constants.EMBED_GEOMETRY,
  313. # constants.EXPORT_OPTIONS[constants.EMBED_GEOMETRY])
  314. properties.option_lights = settings.get(
  315. constants.LIGHTS,
  316. constants.EXPORT_OPTIONS[constants.LIGHTS])
  317. properties.option_cameras = settings.get(
  318. constants.CAMERAS,
  319. constants.EXPORT_OPTIONS[constants.CAMERAS])
  320. properties.option_hierarchy = settings.get(
  321. constants.HIERARCHY,
  322. constants.EXPORT_OPTIONS[constants.HIERARCHY])
  323. ## }
  324. ## Animation {
  325. properties.option_animation_morph = settings.get(
  326. constants.MORPH_TARGETS,
  327. constants.EXPORT_OPTIONS[constants.MORPH_TARGETS])
  328. properties.option_blend_shape = settings.get(
  329. constants.BLEND_SHAPES,
  330. constants.EXPORT_OPTIONS[constants.BLEND_SHAPES])
  331. properties.option_animation_skeletal = settings.get(
  332. constants.ANIMATION,
  333. constants.EXPORT_OPTIONS[constants.ANIMATION])
  334. properties.option_keyframes = settings.get(
  335. constants.KEYFRAMES,
  336. constants.EXPORT_OPTIONS[constants.KEYFRAMES])
  337. properties.option_frame_step = settings.get(
  338. constants.FRAME_STEP,
  339. constants.EXPORT_OPTIONS[constants.FRAME_STEP])
  340. properties.option_frame_index_as_time = settings.get(
  341. constants.FRAME_INDEX_AS_TIME,
  342. constants.EXPORT_OPTIONS[constants.FRAME_INDEX_AS_TIME])
  343. ## }
  344. def set_settings(properties):
  345. """Set the export settings to the correct keys.
  346. :param properties:
  347. :returns: settings
  348. :rtype: dict
  349. """
  350. settings = {
  351. constants.VERTICES: properties.option_vertices,
  352. constants.FACES: properties.option_faces,
  353. constants.NORMALS: properties.option_normals,
  354. constants.SKINNING: properties.option_skinning,
  355. constants.BONES: properties.option_bones,
  356. constants.EXTRA_VGROUPS: properties.option_extra_vgroups,
  357. constants.APPLY_MODIFIERS: properties.option_apply_modifiers,
  358. constants.GEOMETRY_TYPE: properties.option_geometry_type,
  359. constants.INDEX_TYPE: properties.option_index_type,
  360. constants.MATERIALS: properties.option_materials,
  361. constants.UVS: properties.option_uv_coords,
  362. constants.FACE_MATERIALS: properties.option_face_materials,
  363. constants.MAPS: properties.option_maps,
  364. constants.COLORS: properties.option_colors,
  365. constants.MIX_COLORS: properties.option_mix_colors,
  366. constants.SCALE: properties.option_scale,
  367. constants.ENABLE_PRECISION: properties.option_round_off,
  368. constants.PRECISION: properties.option_round_value,
  369. constants.CUSTOM_PROPERTIES: properties.option_custom_properties,
  370. constants.LOGGING: properties.option_logging,
  371. constants.COMPRESSION: properties.option_compression,
  372. constants.INDENT: properties.option_indent,
  373. constants.EXPORT_TEXTURES: properties.option_export_textures,
  374. constants.EMBED_TEXTURES: properties.option_embed_textures,
  375. constants.TEXTURE_FOLDER: properties.option_texture_folder,
  376. constants.SCENE: properties.option_export_scene,
  377. #constants.EMBED_GEOMETRY: properties.option_embed_geometry,
  378. constants.EMBED_ANIMATION: properties.option_embed_animation,
  379. constants.LIGHTS: properties.option_lights,
  380. constants.CAMERAS: properties.option_cameras,
  381. constants.HIERARCHY: properties.option_hierarchy,
  382. constants.MORPH_TARGETS: properties.option_animation_morph,
  383. constants.BLEND_SHAPES: properties.option_blend_shape,
  384. constants.ANIMATION: properties.option_animation_skeletal,
  385. constants.KEYFRAMES: properties.option_keyframes,
  386. constants.FRAME_STEP: properties.option_frame_step,
  387. constants.FRAME_INDEX_AS_TIME: properties.option_frame_index_as_time,
  388. constants.INFLUENCES_PER_VERTEX: properties.option_influences
  389. }
  390. return settings
  391. def compression_types():
  392. """Supported compression formats
  393. :rtype: tuple
  394. """
  395. types = [(constants.NONE, constants.NONE, constants.NONE)]
  396. try:
  397. import msgpack
  398. types.append((constants.MSGPACK, constants.MSGPACK,
  399. constants.MSGPACK))
  400. except ImportError:
  401. pass
  402. return types
  403. def animation_options():
  404. """The supported skeletal animation types
  405. :returns: list of tuples
  406. """
  407. anim = [
  408. (constants.OFF, constants.OFF.title(), constants.OFF),
  409. (constants.POSE, constants.POSE.title(), constants.POSE),
  410. (constants.REST, constants.REST.title(), constants.REST)
  411. ]
  412. return anim
  413. def resolve_conflicts(self, context):
  414. if(not self.option_export_textures):
  415. self.option_embed_textures = False;
  416. class ExportThree(bpy.types.Operator, ExportHelper):
  417. """Class that handles the export properties"""
  418. bl_idname = 'export.three'
  419. bl_label = 'Export THREE'
  420. bl_options = {'PRESET'}
  421. filename_ext = constants.EXTENSION
  422. option_vertices = BoolProperty(
  423. name="Vertices",
  424. description="Export vertices",
  425. default=constants.EXPORT_OPTIONS[constants.VERTICES])
  426. option_faces = BoolProperty(
  427. name="Faces",
  428. description="Export faces (Geometry only)",
  429. default=constants.EXPORT_OPTIONS[constants.FACES])
  430. option_normals = BoolProperty(
  431. name="Normals",
  432. description="Export normals",
  433. default=constants.EXPORT_OPTIONS[constants.NORMALS])
  434. option_colors = BoolProperty(
  435. name="Vertex Colors",
  436. description="Export vertex colors",
  437. default=constants.EXPORT_OPTIONS[constants.COLORS])
  438. option_mix_colors = BoolProperty(
  439. name="Mix Colors",
  440. description="Mix material and vertex colors",
  441. default=constants.EXPORT_OPTIONS[constants.MIX_COLORS])
  442. option_uv_coords = BoolProperty(
  443. name="UVs",
  444. description="Export texture coordinates",
  445. default=constants.EXPORT_OPTIONS[constants.UVS])
  446. option_materials = BoolProperty(
  447. name="Materials",
  448. description="Export materials",
  449. default=constants.EXPORT_OPTIONS[constants.MATERIALS])
  450. option_face_materials = BoolProperty(
  451. name="Face Materials",
  452. description="Face mapping materials (Geometry only)",
  453. default=constants.EXPORT_OPTIONS[constants.FACE_MATERIALS])
  454. option_maps = BoolProperty(
  455. name="Textures",
  456. description="Include texture maps",
  457. default=constants.EXPORT_OPTIONS[constants.MAPS])
  458. option_skinning = BoolProperty(
  459. name="Skinning",
  460. description="Export skin data",
  461. default=constants.EXPORT_OPTIONS[constants.SKINNING])
  462. option_bones = BoolProperty(
  463. name="Bones",
  464. description="Export bones",
  465. default=constants.EXPORT_OPTIONS[constants.BONES])
  466. option_extra_vgroups = StringProperty(
  467. name="Extra Vertex Groups",
  468. description="Non-skinning vertex groups to export (comma-separated, w/ star wildcard, BufferGeometry only).",
  469. default=constants.EXPORT_OPTIONS[constants.EXTRA_VGROUPS])
  470. option_apply_modifiers = BoolProperty(
  471. name="Apply Modifiers",
  472. description="Apply Modifiers to mesh objects",
  473. default=constants.EXPORT_OPTIONS[constants.APPLY_MODIFIERS]
  474. )
  475. index_buffer_types = [
  476. (constants.NONE,) * 3,
  477. (constants.UINT_16,) * 3,
  478. (constants.UINT_32,) * 3]
  479. option_index_type = EnumProperty(
  480. name="Index Buffer",
  481. description="Index buffer type that will be used for BufferGeometry objects.",
  482. items=index_buffer_types,
  483. default=constants.EXPORT_OPTIONS[constants.INDEX_TYPE])
  484. option_scale = FloatProperty(
  485. name="Scale",
  486. description="Scale vertices",
  487. min=0.01,
  488. max=1000.0,
  489. soft_min=0.01,
  490. soft_max=1000.0,
  491. default=constants.EXPORT_OPTIONS[constants.SCALE])
  492. option_round_off = BoolProperty(
  493. name="Enable Precision",
  494. description="Round off floating point values",
  495. default=constants.EXPORT_OPTIONS[constants.ENABLE_PRECISION])
  496. option_round_value = IntProperty(
  497. name="",
  498. min=0,
  499. max=16,
  500. description="Floating point precision",
  501. default=constants.EXPORT_OPTIONS[constants.PRECISION])
  502. option_custom_properties = BoolProperty(
  503. name="Custom Properties",
  504. description="Export custom properties as userData",
  505. default=False)
  506. logging_types = [
  507. (constants.DISABLED, constants.DISABLED, constants.DISABLED),
  508. (constants.DEBUG, constants.DEBUG, constants.DEBUG),
  509. (constants.INFO, constants.INFO, constants.INFO),
  510. (constants.WARNING, constants.WARNING, constants.WARNING),
  511. (constants.ERROR, constants.ERROR, constants.ERROR),
  512. (constants.CRITICAL, constants.CRITICAL, constants.CRITICAL)]
  513. option_logging = EnumProperty(
  514. name="",
  515. description="Logging verbosity level",
  516. items=logging_types,
  517. default=constants.DISABLED)
  518. option_geometry_type = EnumProperty(
  519. name="Type",
  520. description="Geometry type",
  521. items=_geometry_types()[1:],
  522. default=constants.EXPORT_OPTIONS[constants.GEOMETRY_TYPE])
  523. option_export_scene = BoolProperty(
  524. name="Scene",
  525. description="Export scene",
  526. default=constants.EXPORT_OPTIONS[constants.SCENE])
  527. #@TODO: removing this option since the ObjectLoader doesn't have
  528. # support for handling external geometry data
  529. #option_embed_geometry = BoolProperty(
  530. # name="Embed geometry",
  531. # description="Embed geometry",
  532. # default=constants.EXPORT_OPTIONS[constants.EMBED_GEOMETRY])
  533. option_embed_animation = BoolProperty(
  534. name="Embed animation",
  535. description="Embed animation data with the geometry data",
  536. default=constants.EXPORT_OPTIONS[constants.EMBED_ANIMATION])
  537. option_export_textures = BoolProperty(
  538. name="Export textures",
  539. description="Export textures",
  540. default=constants.EXPORT_OPTIONS[constants.EXPORT_TEXTURES],
  541. update=resolve_conflicts)
  542. option_embed_textures = BoolProperty(
  543. name="Embed textures",
  544. description="Embed base64 textures in .json",
  545. default=constants.EXPORT_OPTIONS[constants.EMBED_TEXTURES])
  546. option_texture_folder = StringProperty(
  547. name="Texture folder",
  548. description="add this folder to textures path",
  549. default=constants.EXPORT_OPTIONS[constants.TEXTURE_FOLDER])
  550. option_lights = BoolProperty(
  551. name="Lights",
  552. description="Export default scene lights",
  553. default=False)
  554. option_cameras = BoolProperty(
  555. name="Cameras",
  556. description="Export default scene cameras",
  557. default=False)
  558. option_hierarchy = BoolProperty(
  559. name="Hierarchy",
  560. description="Export object hierarchy",
  561. default=False)
  562. option_animation_morph = BoolProperty(
  563. name="Morph animation",
  564. description="Export animation (morphs)",
  565. default=constants.EXPORT_OPTIONS[constants.MORPH_TARGETS])
  566. option_blend_shape = BoolProperty(
  567. name="Blend Shape animation",
  568. description="Export Blend Shapes",
  569. default=constants.EXPORT_OPTIONS[constants.BLEND_SHAPES])
  570. option_animation_skeletal = EnumProperty(
  571. name="",
  572. description="Export animation (skeletal)",
  573. items=animation_options(),
  574. default=constants.OFF)
  575. option_keyframes = BoolProperty(
  576. name="Keyframe animation",
  577. description="Export animation (keyframes)",
  578. default=constants.EXPORT_OPTIONS[constants.KEYFRAMES])
  579. option_frame_index_as_time = BoolProperty(
  580. name="Frame index as time",
  581. description="Use (original) frame index as frame time",
  582. default=constants.EXPORT_OPTIONS[constants.FRAME_INDEX_AS_TIME])
  583. option_frame_step = IntProperty(
  584. name="Frame step",
  585. description="Animation frame step",
  586. min=1,
  587. max=1000,
  588. soft_min=1,
  589. soft_max=1000,
  590. default=1)
  591. option_indent = BoolProperty(
  592. name="Indent JSON",
  593. description="Disable this to reduce the file size",
  594. default=constants.EXPORT_OPTIONS[constants.INDENT])
  595. option_compression = EnumProperty(
  596. name="",
  597. description="Compression options",
  598. items=compression_types(),
  599. default=constants.NONE)
  600. option_influences = IntProperty(
  601. name="Influences",
  602. description="Maximum number of bone influences",
  603. min=1,
  604. max=4,
  605. default=2)
  606. def invoke(self, context, event):
  607. settings = context.scene.get(constants.EXPORT_SETTINGS_KEY)
  608. if settings:
  609. try:
  610. restore_export_settings(self.properties, settings)
  611. except AttributeError as e:
  612. logging.error("Loading export settings failed:")
  613. logging.exception(e)
  614. logging.debug("Removed corrupted settings")
  615. del context.scene[constants.EXPORT_SETTINGS_KEY]
  616. return ExportHelper.invoke(self, context, event)
  617. @classmethod
  618. def poll(cls, context):
  619. """
  620. :param context:
  621. """
  622. return context.active_object is not None
  623. def execute(self, context):
  624. """
  625. :param context:
  626. """
  627. if not self.properties.filepath:
  628. raise Exception("filename not set")
  629. settings = set_settings(self.properties)
  630. settings['addon_version'] = bl_info['version']
  631. filepath = self.filepath
  632. if settings[constants.COMPRESSION] == constants.MSGPACK:
  633. filepath = "%s%s" % (filepath[:-4], constants.PACK)
  634. from io_three import exporter
  635. if settings[constants.SCENE]:
  636. exporter.export_scene(filepath, settings)
  637. else:
  638. exporter.export_geometry(filepath, settings)
  639. return {'FINISHED'}
  640. def draw(self, context):
  641. """
  642. :param context:
  643. """
  644. using_geometry = self.option_geometry_type == constants.GEOMETRY
  645. layout = self.layout
  646. ## Scene {
  647. box = layout.box()
  648. column = box.column(True)
  649. row = column.row(True)
  650. row.alignment = 'CENTER'
  651. row.label(text="SCENE", icon="SCENE_DATA")
  652. row = box.row()
  653. row.prop(self.properties, 'option_export_scene')
  654. row.prop(self.properties, 'option_materials')
  655. #row = box.row()
  656. #row.prop(self.properties, 'option_embed_geometry')
  657. row = box.row()
  658. row.prop(self.properties, 'option_lights')
  659. row.prop(self.properties, 'option_cameras')
  660. row = box.row()
  661. row.prop(self.properties, 'option_hierarchy')
  662. ## }
  663. layout.separator()
  664. ## Geometry {
  665. box = layout.box()
  666. column = box.column(True)
  667. row = column.row(True)
  668. row.alignment = 'CENTER'
  669. row.label(text="GEOMETRY", icon="MESH_DATA")
  670. row = box.row()
  671. row.prop(self.properties, 'option_geometry_type')
  672. row = box.row()
  673. row.prop(self.properties, 'option_index_type')
  674. row = box.row()
  675. row.prop(self.properties, 'option_vertices')
  676. col = row.column()
  677. col.prop(self.properties, 'option_faces')
  678. col.enabled = using_geometry
  679. row = box.row()
  680. row.prop(self.properties, 'option_normals')
  681. row.prop(self.properties, 'option_uv_coords')
  682. row = box.row()
  683. row.prop(self.properties, 'option_apply_modifiers')
  684. row = box.row()
  685. row.prop(self.properties, 'option_extra_vgroups')
  686. row.enabled = not using_geometry
  687. ## }
  688. layout.separator()
  689. ## Materials {
  690. box = layout.box()
  691. column = box.column(True)
  692. row = column.row(True)
  693. row.alignment = 'CENTER'
  694. row.label(text="MATERIAL", icon="MATERIAL_DATA")
  695. row = box.row()
  696. row.prop(self.properties, 'option_colors')
  697. row.prop(self.properties, 'option_mix_colors')
  698. row = box.row()
  699. row.prop(self.properties, 'option_face_materials')
  700. row.enabled = using_geometry
  701. ## }
  702. layout.separator()
  703. ## Textures {
  704. box = layout.box()
  705. column = box.column(True)
  706. row = column.row(True)
  707. row.alignment = 'CENTER'
  708. row.label(text="TEXTURE", icon="TEXTURE_DATA")
  709. row = box.row()
  710. row.prop(self.properties, 'option_maps')
  711. row.prop(self.properties, 'option_export_textures')
  712. row = box.row()
  713. row.prop(self.properties, 'option_embed_textures')
  714. row.enabled = self.properties.option_export_textures
  715. row = box.row()
  716. row.prop(self.properties, 'option_texture_folder')
  717. ## }
  718. layout.separator()
  719. ## Armature {
  720. box = layout.box()
  721. column = box.column(True)
  722. row = column.row(True)
  723. row.alignment = 'CENTER'
  724. row.label(text="ARMATURE", icon="ARMATURE_DATA")
  725. row = box.row()
  726. row.prop(self.properties, 'option_bones')
  727. row.prop(self.properties, 'option_skinning')
  728. ## }
  729. layout.separator()
  730. ## Animation {
  731. box = layout.box()
  732. column = box.column(True)
  733. row = column.row(True)
  734. row.alignment = 'CENTER'
  735. row.label(text="ANIMATION", icon="POSE_DATA")
  736. row = box.row()
  737. row.prop(self.properties, 'option_animation_morph')
  738. row.prop(self.properties, 'option_blend_shape')
  739. row = box.row()
  740. row.label(text="Skeletal animations:")
  741. row.prop(self.properties, 'option_animation_skeletal')
  742. row = box.row()
  743. row.prop(self.properties, 'option_keyframes')
  744. row = box.row()
  745. row.prop(self.properties, 'option_influences')
  746. row = box.row()
  747. row.prop(self.properties, 'option_frame_step')
  748. row = box.row()
  749. row.prop(self.properties, 'option_frame_index_as_time')
  750. row = box.row()
  751. row.prop(self.properties, 'option_embed_animation')
  752. ## }
  753. layout.separator()
  754. ## Settings {
  755. box = layout.box()
  756. column = box.column(True)
  757. row = column.row(True)
  758. row.alignment = 'CENTER'
  759. row.label(text="SETTINGS", icon="SETTINGS")
  760. row = box.row()
  761. row.prop(self.properties, 'option_scale')
  762. row = box.row()
  763. row.prop(self.properties, 'option_round_off')
  764. row.prop(self.properties, 'option_round_value')
  765. row = box.row()
  766. row.prop(self.properties, 'option_custom_properties')
  767. row = box.row()
  768. row.prop(self.properties, 'option_indent')
  769. row = box.row()
  770. row.label(text="Logging verbosity:")
  771. row.prop(self.properties, 'option_logging')
  772. row = box.row()
  773. row.label(text="File compression format:")
  774. row.prop(self.properties, 'option_compression')
  775. ## }
  776. ## Operators {
  777. has_settings = context.scene.get(constants.EXPORT_SETTINGS_KEY, False)
  778. row = layout.row()
  779. row.operator(
  780. ThreeExportSettings.bl_idname,
  781. ThreeExportSettings.bl_label,
  782. icon="%s" % "PINNED" if has_settings else "UNPINNED")
  783. ## }
  784. def menu_func_export(self, context):
  785. """
  786. :param self:
  787. :param context:
  788. """
  789. default_path = bpy.data.filepath.replace('.blend', constants.EXTENSION)
  790. text = "Three.js (%s)" % constants.EXTENSION
  791. operator = self.layout.operator(ExportThree.bl_idname, text=text)
  792. operator.filepath = default_path
  793. def register():
  794. """Registers the addon (Blender boilerplate)"""
  795. bpy.utils.register_module(__name__)
  796. bpy.types.INFO_MT_file_export.append(menu_func_export)
  797. def unregister():
  798. """Unregisters the addon (Blender boilerplate)"""
  799. bpy.utils.unregister_module(__name__)
  800. bpy.types.INFO_MT_file_export.remove(menu_func_export)
  801. if __name__ == '__main__':
  802. register()