__init__.py 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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_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",
  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",
  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="Precision",
  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 Props",
  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. layout = self.layout
  645. ## Geometry {
  646. row = layout.row()
  647. row.label(text="GEOMETRY:")
  648. row = layout.row()
  649. row.prop(self.properties, 'option_vertices')
  650. row.prop(self.properties, 'option_faces')
  651. row = layout.row()
  652. row.prop(self.properties, 'option_normals')
  653. row.prop(self.properties, 'option_uv_coords')
  654. row = layout.row()
  655. row.prop(self.properties, 'option_bones')
  656. row.prop(self.properties, 'option_skinning')
  657. row = layout.row()
  658. row.prop(self.properties, 'option_extra_vgroups')
  659. row = layout.row()
  660. row.prop(self.properties, 'option_apply_modifiers')
  661. row = layout.row()
  662. row.prop(self.properties, 'option_geometry_type')
  663. row = layout.row()
  664. row.prop(self.properties, 'option_index_type')
  665. ## }
  666. layout.separator()
  667. ## Materials {
  668. row = layout.row()
  669. row.label(text="- Shading:")
  670. row = layout.row()
  671. row.prop(self.properties, 'option_face_materials')
  672. row = layout.row()
  673. row.prop(self.properties, 'option_colors')
  674. row = layout.row()
  675. row.prop(self.properties, 'option_mix_colors')
  676. ## }
  677. layout.separator()
  678. ## Animation {
  679. row = layout.row()
  680. row.label(text="- Animation:")
  681. row = layout.row()
  682. row.prop(self.properties, 'option_animation_morph')
  683. row = layout.row()
  684. row.prop(self.properties, 'option_blend_shape')
  685. row = layout.row()
  686. row.label(text="Skeletal animations:")
  687. row = layout.row()
  688. row.prop(self.properties, 'option_animation_skeletal')
  689. row = layout.row()
  690. row.label(text="Keyframe animations:")
  691. row = layout.row()
  692. row.prop(self.properties, 'option_keyframes')
  693. layout.row()
  694. row = layout.row()
  695. row.prop(self.properties, 'option_influences')
  696. row = layout.row()
  697. row.prop(self.properties, 'option_frame_step')
  698. row = layout.row()
  699. row.prop(self.properties, 'option_frame_index_as_time')
  700. row = layout.row()
  701. row.prop(self.properties, 'option_embed_animation')
  702. ## }
  703. layout.separator()
  704. ## Scene {
  705. row = layout.row()
  706. row.label(text="SCENE:")
  707. row = layout.row()
  708. row.prop(self.properties, 'option_export_scene')
  709. row.prop(self.properties, 'option_materials')
  710. #row = layout.row()
  711. #row.prop(self.properties, 'option_embed_geometry')
  712. row = layout.row()
  713. row.prop(self.properties, 'option_lights')
  714. row.prop(self.properties, 'option_cameras')
  715. ## }
  716. row = layout.row()
  717. row.prop(self.properties, 'option_hierarchy')
  718. layout.separator()
  719. ## Settings {
  720. row = layout.row()
  721. row.label(text="SETTINGS:")
  722. row = layout.row()
  723. row.prop(self.properties, 'option_maps')
  724. row = layout.row()
  725. row.prop(self.properties, 'option_export_textures')
  726. row = layout.row()
  727. row.prop(self.properties, 'option_embed_textures')
  728. row.enabled = self.properties.option_export_textures
  729. row = layout.row()
  730. row.prop(self.properties, 'option_texture_folder')
  731. row = layout.row()
  732. row.prop(self.properties, 'option_scale')
  733. layout.row()
  734. row = layout.row()
  735. row.prop(self.properties, 'option_round_off')
  736. row = layout.row()
  737. row.prop(self.properties, 'option_round_value')
  738. layout.row()
  739. row = layout.row()
  740. row.label(text="Custom Properties")
  741. row = layout.row()
  742. row.prop(self.properties, 'option_custom_properties')
  743. layout.row()
  744. row = layout.row()
  745. row.label(text="Logging verbosity:")
  746. row = layout.row()
  747. row.prop(self.properties, 'option_logging')
  748. row = layout.row()
  749. row.label(text="File compression format:")
  750. row = layout.row()
  751. row.prop(self.properties, 'option_compression')
  752. row = layout.row()
  753. row.prop(self.properties, 'option_indent')
  754. ## }
  755. ## Operators {
  756. has_settings = context.scene.get(constants.EXPORT_SETTINGS_KEY, False)
  757. row = layout.row()
  758. row.operator(
  759. ThreeExportSettings.bl_idname,
  760. ThreeExportSettings.bl_label,
  761. icon="%s" % "PINNED" if has_settings else "UNPINNED")
  762. ## }
  763. def menu_func_export(self, context):
  764. """
  765. :param self:
  766. :param context:
  767. """
  768. default_path = bpy.data.filepath.replace('.blend', constants.EXTENSION)
  769. text = "Three.js (%s)" % constants.EXTENSION
  770. operator = self.layout.operator(ExportThree.bl_idname, text=text)
  771. operator.filepath = default_path
  772. def register():
  773. """Registers the addon (Blender boilerplate)"""
  774. bpy.utils.register_module(__name__)
  775. bpy.types.INFO_MT_file_export.append(menu_func_export)
  776. def unregister():
  777. """Unregisters the addon (Blender boilerplate)"""
  778. bpy.utils.unregister_module(__name__)
  779. bpy.types.INFO_MT_file_export.remove(menu_func_export)
  780. if __name__ == '__main__':
  781. register()