__init__.py 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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(5)],
  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_logging = settings.get(
  283. constants.LOGGING,
  284. constants.EXPORT_OPTIONS[constants.LOGGING])
  285. properties.option_compression = settings.get(
  286. constants.COMPRESSION,
  287. constants.NONE)
  288. properties.option_indent = settings.get(
  289. constants.INDENT,
  290. constants.EXPORT_OPTIONS[constants.INDENT])
  291. properties.option_export_textures = settings.get(
  292. constants.EXPORT_TEXTURES,
  293. constants.EXPORT_OPTIONS[constants.EXPORT_TEXTURES])
  294. properties.option_embed_textures = settings.get(
  295. constants.EMBED_TEXTURES,
  296. constants.EXPORT_OPTIONS[constants.EMBED_TEXTURES])
  297. properties.option_texture_folder = settings.get(
  298. constants.TEXTURE_FOLDER,
  299. constants.EXPORT_OPTIONS[constants.TEXTURE_FOLDER])
  300. properties.option_embed_animation = settings.get(
  301. constants.EMBED_ANIMATION,
  302. constants.EXPORT_OPTIONS[constants.EMBED_ANIMATION])
  303. ## }
  304. ## Scene {
  305. properties.option_export_scene = settings.get(
  306. constants.SCENE,
  307. constants.EXPORT_OPTIONS[constants.SCENE])
  308. #properties.option_embed_geometry = settings.get(
  309. # constants.EMBED_GEOMETRY,
  310. # constants.EXPORT_OPTIONS[constants.EMBED_GEOMETRY])
  311. properties.option_lights = settings.get(
  312. constants.LIGHTS,
  313. constants.EXPORT_OPTIONS[constants.LIGHTS])
  314. properties.option_cameras = settings.get(
  315. constants.CAMERAS,
  316. constants.EXPORT_OPTIONS[constants.CAMERAS])
  317. properties.option_hierarchy = settings.get(
  318. constants.HIERARCHY,
  319. constants.EXPORT_OPTIONS[constants.HIERARCHY])
  320. ## }
  321. ## Animation {
  322. properties.option_animation_morph = settings.get(
  323. constants.MORPH_TARGETS,
  324. constants.EXPORT_OPTIONS[constants.MORPH_TARGETS])
  325. properties.option_blend_shape = settings.get(
  326. constants.BLEND_SHAPES,
  327. constants.EXPORT_OPTIONS[constants.BLEND_SHAPES])
  328. properties.option_animation_skeletal = settings.get(
  329. constants.ANIMATION,
  330. constants.EXPORT_OPTIONS[constants.ANIMATION])
  331. properties.option_keyframes = settings.get(
  332. constants.KEYFRAMES,
  333. constants.EXPORT_OPTIONS[constants.KEYFRAMES])
  334. properties.option_frame_step = settings.get(
  335. constants.FRAME_STEP,
  336. constants.EXPORT_OPTIONS[constants.FRAME_STEP])
  337. properties.option_frame_index_as_time = settings.get(
  338. constants.FRAME_INDEX_AS_TIME,
  339. constants.EXPORT_OPTIONS[constants.FRAME_INDEX_AS_TIME])
  340. ## }
  341. def set_settings(properties):
  342. """Set the export settings to the correct keys.
  343. :param properties:
  344. :returns: settings
  345. :rtype: dict
  346. """
  347. settings = {
  348. constants.VERTICES: properties.option_vertices,
  349. constants.FACES: properties.option_faces,
  350. constants.NORMALS: properties.option_normals,
  351. constants.SKINNING: properties.option_skinning,
  352. constants.BONES: properties.option_bones,
  353. constants.EXTRA_VGROUPS: properties.option_extra_vgroups,
  354. constants.APPLY_MODIFIERS: properties.option_apply_modifiers,
  355. constants.GEOMETRY_TYPE: properties.option_geometry_type,
  356. constants.INDEX_TYPE: properties.option_index_type,
  357. constants.MATERIALS: properties.option_materials,
  358. constants.UVS: properties.option_uv_coords,
  359. constants.FACE_MATERIALS: properties.option_face_materials,
  360. constants.MAPS: properties.option_maps,
  361. constants.COLORS: properties.option_colors,
  362. constants.MIX_COLORS: properties.option_mix_colors,
  363. constants.SCALE: properties.option_scale,
  364. constants.ENABLE_PRECISION: properties.option_round_off,
  365. constants.PRECISION: properties.option_round_value,
  366. constants.LOGGING: properties.option_logging,
  367. constants.COMPRESSION: properties.option_compression,
  368. constants.INDENT: properties.option_indent,
  369. constants.EXPORT_TEXTURES: properties.option_export_textures,
  370. constants.EMBED_TEXTURES: properties.option_embed_textures,
  371. constants.TEXTURE_FOLDER: properties.option_texture_folder,
  372. constants.SCENE: properties.option_export_scene,
  373. #constants.EMBED_GEOMETRY: properties.option_embed_geometry,
  374. constants.EMBED_ANIMATION: properties.option_embed_animation,
  375. constants.LIGHTS: properties.option_lights,
  376. constants.CAMERAS: properties.option_cameras,
  377. constants.HIERARCHY: properties.option_hierarchy,
  378. constants.MORPH_TARGETS: properties.option_animation_morph,
  379. constants.BLEND_SHAPES: properties.option_blend_shape,
  380. constants.ANIMATION: properties.option_animation_skeletal,
  381. constants.KEYFRAMES: properties.option_keyframes,
  382. constants.FRAME_STEP: properties.option_frame_step,
  383. constants.FRAME_INDEX_AS_TIME: properties.option_frame_index_as_time,
  384. constants.INFLUENCES_PER_VERTEX: properties.option_influences
  385. }
  386. return settings
  387. def compression_types():
  388. """Supported compression formats
  389. :rtype: tuple
  390. """
  391. types = [(constants.NONE, constants.NONE, constants.NONE)]
  392. try:
  393. import msgpack
  394. types.append((constants.MSGPACK, constants.MSGPACK,
  395. constants.MSGPACK))
  396. except ImportError:
  397. pass
  398. return types
  399. def animation_options():
  400. """The supported skeletal animation types
  401. :returns: list of tuples
  402. """
  403. anim = [
  404. (constants.OFF, constants.OFF.title(), constants.OFF),
  405. (constants.POSE, constants.POSE.title(), constants.POSE),
  406. (constants.REST, constants.REST.title(), constants.REST)
  407. ]
  408. return anim
  409. def resolve_conflicts(self, context):
  410. if(not self.option_export_textures):
  411. self.option_embed_textures = False;
  412. class ExportThree(bpy.types.Operator, ExportHelper):
  413. """Class that handles the export properties"""
  414. bl_idname = 'export.three'
  415. bl_label = 'Export THREE'
  416. bl_options = {'PRESET'}
  417. filename_ext = constants.EXTENSION
  418. option_vertices = BoolProperty(
  419. name="Vertices",
  420. description="Export vertices",
  421. default=constants.EXPORT_OPTIONS[constants.VERTICES])
  422. option_faces = BoolProperty(
  423. name="Faces",
  424. description="Export faces",
  425. default=constants.EXPORT_OPTIONS[constants.FACES])
  426. option_normals = BoolProperty(
  427. name="Normals",
  428. description="Export normals",
  429. default=constants.EXPORT_OPTIONS[constants.NORMALS])
  430. option_colors = BoolProperty(
  431. name="Vertex Colors",
  432. description="Export vertex colors",
  433. default=constants.EXPORT_OPTIONS[constants.COLORS])
  434. option_mix_colors = BoolProperty(
  435. name="Mix Colors",
  436. description="Mix material and vertex colors",
  437. default=constants.EXPORT_OPTIONS[constants.MIX_COLORS])
  438. option_uv_coords = BoolProperty(
  439. name="UVs",
  440. description="Export texture coordinates",
  441. default=constants.EXPORT_OPTIONS[constants.UVS])
  442. option_materials = BoolProperty(
  443. name="Materials",
  444. description="Export materials",
  445. default=constants.EXPORT_OPTIONS[constants.MATERIALS])
  446. option_face_materials = BoolProperty(
  447. name="Face Materials",
  448. description="Face mapping materials",
  449. default=constants.EXPORT_OPTIONS[constants.FACE_MATERIALS])
  450. option_maps = BoolProperty(
  451. name="Textures",
  452. description="Include texture maps",
  453. default=constants.EXPORT_OPTIONS[constants.MAPS])
  454. option_skinning = BoolProperty(
  455. name="Skinning",
  456. description="Export skin data",
  457. default=constants.EXPORT_OPTIONS[constants.SKINNING])
  458. option_bones = BoolProperty(
  459. name="Bones",
  460. description="Export bones",
  461. default=constants.EXPORT_OPTIONS[constants.BONES])
  462. option_extra_vgroups = StringProperty(
  463. name="Extra Vertex Groups",
  464. description="Non-skinning vertex groups to export (comma-separated, w/ star wildcard, BufferGeometry only).",
  465. default=constants.EXPORT_OPTIONS[constants.EXTRA_VGROUPS])
  466. option_apply_modifiers = BoolProperty(
  467. name="Apply Modifiers",
  468. description="Apply Modifiers to mesh objects",
  469. default=constants.EXPORT_OPTIONS[constants.APPLY_MODIFIERS]
  470. )
  471. index_buffer_types = [
  472. (constants.NONE,) * 3,
  473. (constants.UINT_16,) * 3,
  474. (constants.UINT_32,) * 3]
  475. option_index_type = EnumProperty(
  476. name="Index Buffer",
  477. description="Index buffer type that will be used for BufferGeometry objects.",
  478. items=index_buffer_types,
  479. default=constants.EXPORT_OPTIONS[constants.INDEX_TYPE])
  480. option_scale = FloatProperty(
  481. name="Scale",
  482. description="Scale vertices",
  483. min=0.01,
  484. max=1000.0,
  485. soft_min=0.01,
  486. soft_max=1000.0,
  487. default=constants.EXPORT_OPTIONS[constants.SCALE])
  488. option_round_off = BoolProperty(
  489. name="Enable Precision",
  490. description="Round off floating point values",
  491. default=constants.EXPORT_OPTIONS[constants.ENABLE_PRECISION])
  492. option_round_value = IntProperty(
  493. name="Precision",
  494. min=0,
  495. max=16,
  496. description="Floating point precision",
  497. default=constants.EXPORT_OPTIONS[constants.PRECISION])
  498. logging_types = [
  499. (constants.DISABLED, constants.DISABLED, constants.DISABLED),
  500. (constants.DEBUG, constants.DEBUG, constants.DEBUG),
  501. (constants.INFO, constants.INFO, constants.INFO),
  502. (constants.WARNING, constants.WARNING, constants.WARNING),
  503. (constants.ERROR, constants.ERROR, constants.ERROR),
  504. (constants.CRITICAL, constants.CRITICAL, constants.CRITICAL)]
  505. option_logging = EnumProperty(
  506. name="",
  507. description="Logging verbosity level",
  508. items=logging_types,
  509. default=constants.DISABLED)
  510. option_geometry_type = EnumProperty(
  511. name="Type",
  512. description="Geometry type",
  513. items=_geometry_types()[1:],
  514. default=constants.EXPORT_OPTIONS[constants.GEOMETRY_TYPE])
  515. option_export_scene = BoolProperty(
  516. name="Scene",
  517. description="Export scene",
  518. default=constants.EXPORT_OPTIONS[constants.SCENE])
  519. #@TODO: removing this option since the ObjectLoader doesn't have
  520. # support for handling external geometry data
  521. #option_embed_geometry = BoolProperty(
  522. # name="Embed geometry",
  523. # description="Embed geometry",
  524. # default=constants.EXPORT_OPTIONS[constants.EMBED_GEOMETRY])
  525. option_embed_animation = BoolProperty(
  526. name="Embed animation",
  527. description="Embed animation data with the geometry data",
  528. default=constants.EXPORT_OPTIONS[constants.EMBED_ANIMATION])
  529. option_export_textures = BoolProperty(
  530. name="Export textures",
  531. description="Export textures",
  532. default=constants.EXPORT_OPTIONS[constants.EXPORT_TEXTURES],
  533. update=resolve_conflicts)
  534. option_embed_textures = BoolProperty(
  535. name="Embed textures",
  536. description="Embed base64 textures in .json",
  537. default=constants.EXPORT_OPTIONS[constants.EMBED_TEXTURES])
  538. option_texture_folder = StringProperty(
  539. name="Texture folder",
  540. description="add this folder to textures path",
  541. default=constants.EXPORT_OPTIONS[constants.TEXTURE_FOLDER])
  542. option_lights = BoolProperty(
  543. name="Lights",
  544. description="Export default scene lights",
  545. default=False)
  546. option_cameras = BoolProperty(
  547. name="Cameras",
  548. description="Export default scene cameras",
  549. default=False)
  550. option_hierarchy = BoolProperty(
  551. name="Hierarchy",
  552. description="Export object hierarchy",
  553. default=False)
  554. option_animation_morph = BoolProperty(
  555. name="Morph animation",
  556. description="Export animation (morphs)",
  557. default=constants.EXPORT_OPTIONS[constants.MORPH_TARGETS])
  558. option_blend_shape = BoolProperty(
  559. name="Blend Shape animation",
  560. description="Export Blend Shapes",
  561. default=constants.EXPORT_OPTIONS[constants.BLEND_SHAPES])
  562. option_animation_skeletal = EnumProperty(
  563. name="",
  564. description="Export animation (skeletal)",
  565. items=animation_options(),
  566. default=constants.OFF)
  567. option_keyframes = BoolProperty(
  568. name="Keyframe animation",
  569. description="Export animation (keyframes)",
  570. default=constants.EXPORT_OPTIONS[constants.KEYFRAMES])
  571. option_frame_index_as_time = BoolProperty(
  572. name="Frame index as time",
  573. description="Use (original) frame index as frame time",
  574. default=constants.EXPORT_OPTIONS[constants.FRAME_INDEX_AS_TIME])
  575. option_frame_step = IntProperty(
  576. name="Frame step",
  577. description="Animation frame step",
  578. min=1,
  579. max=1000,
  580. soft_min=1,
  581. soft_max=1000,
  582. default=1)
  583. option_indent = BoolProperty(
  584. name="Indent JSON",
  585. description="Disable this to reduce the file size",
  586. default=constants.EXPORT_OPTIONS[constants.INDENT])
  587. option_compression = EnumProperty(
  588. name="",
  589. description="Compression options",
  590. items=compression_types(),
  591. default=constants.NONE)
  592. option_influences = IntProperty(
  593. name="Influences",
  594. description="Maximum number of bone influences",
  595. min=1,
  596. max=4,
  597. default=2)
  598. def invoke(self, context, event):
  599. settings = context.scene.get(constants.EXPORT_SETTINGS_KEY)
  600. if settings:
  601. try:
  602. restore_export_settings(self.properties, settings)
  603. except AttributeError as e:
  604. logging.error("Loading export settings failed:")
  605. logging.exception(e)
  606. logging.debug("Removed corrupted settings")
  607. del context.scene[constants.EXPORT_SETTINGS_KEY]
  608. return ExportHelper.invoke(self, context, event)
  609. @classmethod
  610. def poll(cls, context):
  611. """
  612. :param context:
  613. """
  614. return context.active_object is not None
  615. def execute(self, context):
  616. """
  617. :param context:
  618. """
  619. if not self.properties.filepath:
  620. raise Exception("filename not set")
  621. settings = set_settings(self.properties)
  622. settings['addon_version'] = bl_info['version']
  623. filepath = self.filepath
  624. if settings[constants.COMPRESSION] == constants.MSGPACK:
  625. filepath = "%s%s" % (filepath[:-4], constants.PACK)
  626. from io_three import exporter
  627. if settings[constants.SCENE]:
  628. exporter.export_scene(filepath, settings)
  629. else:
  630. exporter.export_geometry(filepath, settings)
  631. return {'FINISHED'}
  632. def draw(self, context):
  633. """
  634. :param context:
  635. """
  636. layout = self.layout
  637. ## Geometry {
  638. row = layout.row()
  639. row.label(text="GEOMETRY:")
  640. row = layout.row()
  641. row.prop(self.properties, 'option_vertices')
  642. row.prop(self.properties, 'option_faces')
  643. row = layout.row()
  644. row.prop(self.properties, 'option_normals')
  645. row.prop(self.properties, 'option_uv_coords')
  646. row = layout.row()
  647. row.prop(self.properties, 'option_bones')
  648. row.prop(self.properties, 'option_skinning')
  649. row = layout.row()
  650. row.prop(self.properties, 'option_extra_vgroups')
  651. row = layout.row()
  652. row.prop(self.properties, 'option_apply_modifiers')
  653. row = layout.row()
  654. row.prop(self.properties, 'option_geometry_type')
  655. row = layout.row()
  656. row.prop(self.properties, 'option_index_type')
  657. ## }
  658. layout.separator()
  659. ## Materials {
  660. row = layout.row()
  661. row.label(text="- Shading:")
  662. row = layout.row()
  663. row.prop(self.properties, 'option_face_materials')
  664. row = layout.row()
  665. row.prop(self.properties, 'option_colors')
  666. row = layout.row()
  667. row.prop(self.properties, 'option_mix_colors')
  668. ## }
  669. layout.separator()
  670. ## Animation {
  671. row = layout.row()
  672. row.label(text="- Animation:")
  673. row = layout.row()
  674. row.prop(self.properties, 'option_animation_morph')
  675. row = layout.row()
  676. row.prop(self.properties, 'option_blend_shape')
  677. row = layout.row()
  678. row.label(text="Skeletal animations:")
  679. row = layout.row()
  680. row.prop(self.properties, 'option_animation_skeletal')
  681. row = layout.row()
  682. row.label(text="Keyframe animations:")
  683. row = layout.row()
  684. row.prop(self.properties, 'option_keyframes')
  685. layout.row()
  686. row = layout.row()
  687. row.prop(self.properties, 'option_influences')
  688. row = layout.row()
  689. row.prop(self.properties, 'option_frame_step')
  690. row = layout.row()
  691. row.prop(self.properties, 'option_frame_index_as_time')
  692. row = layout.row()
  693. row.prop(self.properties, 'option_embed_animation')
  694. ## }
  695. layout.separator()
  696. ## Scene {
  697. row = layout.row()
  698. row.label(text="SCENE:")
  699. row = layout.row()
  700. row.prop(self.properties, 'option_export_scene')
  701. row.prop(self.properties, 'option_materials')
  702. #row = layout.row()
  703. #row.prop(self.properties, 'option_embed_geometry')
  704. row = layout.row()
  705. row.prop(self.properties, 'option_lights')
  706. row.prop(self.properties, 'option_cameras')
  707. ## }
  708. row = layout.row()
  709. row.prop(self.properties, 'option_hierarchy')
  710. layout.separator()
  711. ## Settings {
  712. row = layout.row()
  713. row.label(text="SETTINGS:")
  714. row = layout.row()
  715. row.prop(self.properties, 'option_maps')
  716. row = layout.row()
  717. row.prop(self.properties, 'option_export_textures')
  718. row = layout.row()
  719. row.prop(self.properties, 'option_embed_textures')
  720. row.enabled = self.properties.option_export_textures
  721. row = layout.row()
  722. row.prop(self.properties, 'option_texture_folder')
  723. row = layout.row()
  724. row.prop(self.properties, 'option_scale')
  725. layout.row()
  726. row = layout.row()
  727. row.prop(self.properties, 'option_round_off')
  728. row = layout.row()
  729. row.prop(self.properties, 'option_round_value')
  730. layout.row()
  731. row = layout.row()
  732. row.label(text="Logging verbosity:")
  733. row = layout.row()
  734. row.prop(self.properties, 'option_logging')
  735. row = layout.row()
  736. row.label(text="File compression format:")
  737. row = layout.row()
  738. row.prop(self.properties, 'option_compression')
  739. row = layout.row()
  740. row.prop(self.properties, 'option_indent')
  741. ## }
  742. ## Operators {
  743. has_settings = context.scene.get(constants.EXPORT_SETTINGS_KEY, False)
  744. row = layout.row()
  745. row.operator(
  746. ThreeExportSettings.bl_idname,
  747. ThreeExportSettings.bl_label,
  748. icon="%s" % "PINNED" if has_settings else "UNPINNED")
  749. ## }
  750. def menu_func_export(self, context):
  751. """
  752. :param self:
  753. :param context:
  754. """
  755. default_path = bpy.data.filepath.replace('.blend', constants.EXTENSION)
  756. text = "Three.js (%s)" % constants.EXTENSION
  757. operator = self.layout.operator(ExportThree.bl_idname, text=text)
  758. operator.filepath = default_path
  759. def register():
  760. """Registers the addon (Blender boilerplate)"""
  761. bpy.utils.register_module(__name__)
  762. bpy.types.INFO_MT_file_export.append(menu_func_export)
  763. def unregister():
  764. """Unregisters the addon (Blender boilerplate)"""
  765. bpy.utils.unregister_module(__name__)
  766. bpy.types.INFO_MT_file_export.remove(menu_func_export)
  767. if __name__ == '__main__':
  768. register()