__init__.py 30 KB

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