diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-03-24 18:30:17 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-03-24 18:30:17 +0400 |
commit | 7e6e45d7a7c80798a21684c278334188cce0e501 (patch) | |
tree | e603b101870c061f79aabb8b2c6ea0ad61187a00 /object_print3d_utils/export.py | |
parent | 2b3a3f56e43bc3d6c7d1f7cda8a063319f0c6c36 (diff) |
move print toolbox into trunk
[[Split portion of a mixed commit.]]
Diffstat (limited to 'object_print3d_utils/export.py')
-rw-r--r-- | object_print3d_utils/export.py | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/object_print3d_utils/export.py b/object_print3d_utils/export.py new file mode 100644 index 00000000..3a2b6677 --- /dev/null +++ b/object_print3d_utils/export.py @@ -0,0 +1,159 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +# <pep8-80 compliant> + +# Export wrappers and integration with external tools. + +import bpy +import os + + +def write_mesh(context, info, report_cb): + scene = context.scene + print_3d = scene.print_3d + + obj_base = scene.object_bases.active + obj = obj_base.object + + export_format = print_3d.export_format + + context_override = context.copy() + + obj_base_tmp = None + + # PLY can only export single mesh objects! + if export_format == 'PLY': + context_backup = context.copy() + bpy.ops.object.mode_set(mode='OBJECT', toggle=False) + + from . import mesh_helpers + obj_base_tmp = mesh_helpers.object_merge(context, context_override["selected_objects"]) + context_override["active_object"] = obj_base_tmp.object + context_override["selected_bases"] = [obj_base_tmp] + context_override["selected_objects"] = [obj_base_tmp.object] + else: + if obj_base not in context_override["selected_bases"]: + context_override["selected_bases"].append(obj_base) + if obj not in context_override["selected_objects"]: + context_override["selected_objects"].append(obj) + + export_path = bpy.path.abspath(print_3d.export_path) + + # Create name 'export_path/blendname-objname' + # add the filename component + if bpy.data.is_saved: + name = os.path.basename(bpy.data.filepath) + name = os.path.splitext(name)[0] + else: + name = "untitled" + # add object name + name += "-%s" % bpy.path.clean_name(obj.name) + + # first ensure the path is created + if export_path: + # this can fail with strange errors, + # if the dir cant be made then we get an error later. + try: + os.makedirs(export_path, exist_ok=True) + except: + import traceback + traceback.print_exc() + + filepath = os.path.join(export_path, name) + + # ensure addon is enabled + import addon_utils + + def addon_ensure(addon_id): + # Enable the addon, dont change preferences. + default_state, loaded_state = addon_utils.check(addon_id) + if not loaded_state: + addon_utils.enable(addon_id, default_set=False) + + if export_format == 'STL': + addon_ensure("io_mesh_stl") + filepath = bpy.path.ensure_ext(filepath, ".stl") + ret = bpy.ops.export_mesh.stl( + context_override, + filepath=filepath, + ascii=False, + use_mesh_modifiers=True, + ) + elif export_format == 'PLY': + addon_ensure("io_mesh_ply") + filepath = bpy.path.ensure_ext(filepath, ".ply") + ret = bpy.ops.export_mesh.ply( + context_override, + filepath=filepath, + use_mesh_modifiers=True, + ) + elif export_format == 'X3D': + addon_ensure("io_scene_x3d") + filepath = bpy.path.ensure_ext(filepath, ".x3d") + ret = bpy.ops.export_scene.x3d( + context_override, + filepath=filepath, + use_mesh_modifiers=True, + use_selection=True, + ) + elif export_format == 'WRL': + addon_ensure("io_scene_vrml2") + filepath = bpy.path.ensure_ext(filepath, ".wrl") + ret = bpy.ops.export_scene.vrml2( + context_override, + filepath=filepath, + use_mesh_modifiers=True, + use_selection=True, + ) + elif export_format == 'OBJ': + addon_ensure("io_scene_obj") + filepath = bpy.path.ensure_ext(filepath, ".obj") + ret = bpy.ops.export_scene.obj( + context_override, + filepath=filepath, + use_mesh_modifiers=True, + use_selection=True, + ) + else: + assert(0) + + if obj_base_tmp is not None: + obj = obj_base_tmp.object + mesh = obj.data + scene.objects.unlink(obj) + bpy.data.objects.remove(obj) + bpy.data.meshes.remove(mesh) + del obj_base_tmp, obj, mesh + + # restore context + base = None + for base in context_backup["selected_bases"]: + base.select = True + del base + scene.objects.active = context_backup["active_object"] + + if 'FINISHED' in ret: + info.append(("%r ok" % os.path.basename(filepath), None)) + + if report_cb is not None: + report_cb({'INFO'}, "Exported: %r" % filepath) + return True + else: + info.append(("%r fail" % os.path.basename(filepath), None)) + return False |