__init__.py 29 KB

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