[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4406] contrib/py/scripts/addons/ object_print3d_utils: workaround for PLY only exporting active mesh.

Campbell Barton ideasman42 at gmail.com
Fri Mar 22 01:09:59 CET 2013


Revision: 4406
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4406
Author:   campbellbarton
Date:     2013-03-22 00:09:58 +0000 (Fri, 22 Mar 2013)
Log Message:
-----------
workaround for PLY only exporting active mesh.

Modified Paths:
--------------
    contrib/py/scripts/addons/object_print3d_utils/export.py
    contrib/py/scripts/addons/object_print3d_utils/mesh_helpers.py
    contrib/py/scripts/addons/object_print3d_utils/ui.py

Modified: contrib/py/scripts/addons/object_print3d_utils/export.py
===================================================================
--- contrib/py/scripts/addons/object_print3d_utils/export.py	2013-03-22 00:08:36 UTC (rev 4405)
+++ contrib/py/scripts/addons/object_print3d_utils/export.py	2013-03-22 00:09:58 UTC (rev 4406)
@@ -31,15 +31,28 @@
     obj_base = scene.object_bases.active
     obj = obj_base.object
 
+    export_format = print_3d.export_format
+
     context_override = context.copy()
 
-    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)
+    obj_base_tmp = None
 
-    export_format = print_3d.export_format
+    # 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'
@@ -120,6 +133,21 @@
     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))
 

Modified: contrib/py/scripts/addons/object_print3d_utils/mesh_helpers.py
===================================================================
--- contrib/py/scripts/addons/object_print3d_utils/mesh_helpers.py	2013-03-22 00:08:36 UTC (rev 4405)
+++ contrib/py/scripts/addons/object_print3d_utils/mesh_helpers.py	2013-03-22 00:09:58 UTC (rev 4406)
@@ -255,3 +255,69 @@
 
     return array.array('i', faces_error)
 
+
+
+def object_merge(context, objects):
+    """
+    Caller must remove.
+    """
+
+    import bpy
+
+    def cd_remove_all_but_active(seq):
+        tot = len(seq)
+        if tot > 1:
+            act = seq.active_index
+            for i in range(tot - 1, -1, -1):
+                if i != act:
+                    seq.remove(seq[i])
+
+    scene = context.scene
+
+    # deselect all
+    for obj in scene.objects:
+        obj.select = False
+
+    # add empty object
+    mesh_base = bpy.data.meshes.new(name="~tmp~")
+    obj_base = bpy.data.objects.new(name="~tmp~", object_data=mesh_base)
+    base_base = scene.objects.link(obj_base)
+    scene.objects.active = obj_base
+    obj_base.select = True
+
+    # loop over all meshes
+    for obj in objects:
+        if obj.type != 'MESH':
+            continue
+
+        # convert each to a mesh
+        mesh_new = obj.to_mesh(scene=scene,
+                               apply_modifiers=True,
+                               settings='PREVIEW',
+                               calc_tessface=False)
+
+        # remove non-active uvs/vcols
+        cd_remove_all_but_active(mesh_new.vertex_colors)
+        cd_remove_all_but_active(mesh_new.uv_textures)
+
+        # join into base mesh
+        obj_new = bpy.data.objects.new(name="~tmp-new~", object_data=mesh_new)
+        base_new = scene.objects.link(obj_new)
+        obj_new.matrix_world = obj.matrix_world
+
+        fake_context = context.copy()
+        fake_context["active_object"] = obj_base
+        fake_context["selected_editable_bases"] = [base_base, base_new]
+
+        bpy.ops.object.join(fake_context)
+        del base_new, obj_new
+
+        # remove object and its mesh, join does this
+        #~ scene.objects.unlink(obj_new)
+        #~ bpy.data.objects.remove(obj_new)
+
+        bpy.data.meshes.remove(mesh_new)
+
+    # return new object
+    return base_base
+

Modified: contrib/py/scripts/addons/object_print3d_utils/ui.py
===================================================================
--- contrib/py/scripts/addons/object_print3d_utils/ui.py	2013-03-22 00:08:36 UTC (rev 4405)
+++ contrib/py/scripts/addons/object_print3d_utils/ui.py	2013-03-22 00:09:58 UTC (rev 4406)
@@ -103,7 +103,7 @@
         col = layout.column(align=True)
         col.operator("mesh.print3d_clean_isolated", text="Isolated")
         rowsub = col.row()
-        rowsub.operator("mesh.print3d_clean_distorted", text="Distorted Faces")
+        rowsub.operator("mesh.print3d_clean_distorted", text="Distorted")
         rowsub.prop(print_3d, "angle_distort", text="")
         # XXX TODO
         # col.operator("mesh.print3d_clean_thin", text="Wall Thickness")



More information about the Bf-extensions-cvs mailing list