Apply patch 4636051. COLLADA: Export selection.
authorNathan Letwory <nathan@letworyinteractive.com>
Mon, 4 Jul 2011 08:59:28 +0000 (08:59 +0000)
committerNathan Letwory <nathan@letworyinteractive.com>
Mon, 4 Jul 2011 08:59:28 +0000 (08:59 +0000)
Original patch by Jan Diederich, adapted by Pelle Johnsen. Review assistance by Daniel Tavares.

This patch adds an option to export only the selection.

21 files changed:
source/blender/collada/ArmatureExporter.cpp
source/blender/collada/ArmatureExporter.h
source/blender/collada/CameraExporter.cpp
source/blender/collada/CameraExporter.h
source/blender/collada/DocumentExporter.cpp
source/blender/collada/DocumentExporter.h
source/blender/collada/EffectExporter.cpp
source/blender/collada/EffectExporter.h
source/blender/collada/GeometryExporter.cpp
source/blender/collada/GeometryExporter.h
source/blender/collada/ImageExporter.cpp
source/blender/collada/ImageExporter.h
source/blender/collada/LightExporter.cpp
source/blender/collada/LightExporter.h
source/blender/collada/MaterialExporter.cpp
source/blender/collada/MaterialExporter.h
source/blender/collada/collada.cpp
source/blender/collada/collada.h
source/blender/collada/collada_internal.cpp
source/blender/makesrna/intern/rna_scene_api.c
source/blender/windowmanager/intern/wm_operators.c

index 90c3cfbb001e41e2b4f12c6f5e4978ac39a50e4b..ad9098db3d853dfaa32549b575d33284fdc6cf34 100644 (file)
@@ -89,14 +89,14 @@ void ArmatureExporter::add_instance_controller(Object *ob)
        ins.add();
 }
 
-void ArmatureExporter::export_controllers(Scene *sce)
+void ArmatureExporter::export_controllers(Scene *sce, bool export_selected)
 {
        scene = sce;
 
        openLibrary();
 
        GeometryFunctor gf;
-       gf.forEachMeshObjectInScene<ArmatureExporter>(sce, *this);
+       gf.forEachMeshObjectInScene<ArmatureExporter>(sce, *this, export_selected);
 
        closeLibrary();
 }
@@ -351,12 +351,17 @@ std::string ArmatureExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase
 
                        bPoseChannel *pchan = get_pose_channel(pose, def->name);
 
+                       float pose_mat[4][4];
                        float mat[4][4];
                        float world[4][4];
                        float inv_bind_mat[4][4];
 
+                       // pose_mat is the same as pchan->pose_mat, but without the rotation
+                       unit_m4(pose_mat);
+                       translate_m4(pose_mat, pchan->pose_head[0], pchan->pose_head[1], pchan->pose_head[2]);
+
                        // make world-space matrix, pose_mat is armature-space
-                       mul_m4_m4m4(world, pchan->pose_mat, ob_arm->obmat);
+                       mul_m4_m4m4(world, pose_mat, ob_arm->obmat);
                        
                        invert_m4_m4(mat, world);
                        converter.mat4_to_dae(inv_bind_mat, mat);
index f72e5244a3684d8e228b7655cd9bf17c0ec6af71..f4488942f7b905d0ed72d9032568d8879b30ccac 100644 (file)
@@ -65,7 +65,7 @@ public:
 
        void add_instance_controller(Object *ob);
 
-       void export_controllers(Scene *sce);
+       void export_controllers(Scene *sce, bool export_selected);
 
        void operator()(Object *ob);
 
index f8fa0fd55c078c7c2d40d3c8a575c95a2eb90e4c..6ce9eb782d3c1738d83410ab2028f15dfa638c4d 100644 (file)
 CamerasExporter::CamerasExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryCameras(sw){}
 
 template<class Functor>
-void forEachCameraObjectInScene(Scene *sce, Functor &f)
+void forEachCameraObjectInScene(Scene *sce, Functor &f, bool export_selected)
 {
        Base *base= (Base*) sce->base.first;
        while(base) {
                Object *ob = base->object;
                        
-               if (ob->type == OB_CAMERA && ob->data) {
+               if (ob->type == OB_CAMERA && ob->data
+                       && !(export_selected && !(ob->flag & SELECT))) {
                        f(ob, sce);
                }
                base= base->next;
        }
 }
 
-void CamerasExporter::exportCameras(Scene *sce)
+void CamerasExporter::exportCameras(Scene *sce, bool export_selected)
 {
        openLibrary();
        
-       forEachCameraObjectInScene(sce, *this);
+       forEachCameraObjectInScene(sce, *this, export_selected);
        
        closeLibrary();
 }
index 922eaf6b1d05386641f16868e03cf41b56761f69..999a6ddd3e5aab2593e64e5a34a43ae49f708fe0 100644 (file)
@@ -40,7 +40,7 @@ class CamerasExporter: COLLADASW::LibraryCameras
 {
 public:
        CamerasExporter(COLLADASW::StreamWriter *sw);
-       void exportCameras(Scene *sce);
+       void exportCameras(Scene *sce, bool export_selected);
        void operator()(Object *ob, Scene *sce);
 };
 
index 00daac6028182d5ec270791dd8e295b940a0db57..e6e0953680c9530e98363400308783d4aba5a14c 100644 (file)
@@ -170,7 +170,7 @@ public:
        SceneExporter(COLLADASW::StreamWriter *sw, ArmatureExporter *arm) : COLLADASW::LibraryVisualScenes(sw),
                                                                                                                                                arm_exporter(arm) {}
        
-       void exportScene(Scene *sce) {
+       void exportScene(Scene *sce, bool export_selected) {
                // <library_visual_scenes> <visual_scene>
                std::string id_naming = id_name(sce);
                openVisualScene(translate_id(id_naming), id_naming);
@@ -179,7 +179,7 @@ public:
                //forEachMeshObjectInScene(sce, *this);
                //forEachCameraObjectInScene(sce, *this);
                //forEachLampObjectInScene(sce, *this);
-               exportHierarchy(sce);
+               exportHierarchy(sce, export_selected);
 
                // </visual_scene> </library_visual_scenes>
                closeVisualScene();
@@ -187,7 +187,7 @@ public:
                closeLibrary();
        }
 
-       void exportHierarchy(Scene *sce)
+       void exportHierarchy(Scene *sce, bool export_selected)
        {
                Base *base= (Base*) sce->base.first;
                while(base) {
@@ -198,8 +198,11 @@ public:
                                case OB_MESH:
                                case OB_CAMERA:
                                case OB_LAMP:
-                               case OB_EMPTY:
                                case OB_ARMATURE:
+                               case OB_EMPTY:
+                                       if (export_selected && !(ob->flag & SELECT)) {
+                                               break;
+                                       }
                                        // write nodes....
                                        writeNodes(ob, sce);
                                        break;
@@ -929,7 +932,7 @@ protected:
        }
 };
 
-void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename)
+void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename, bool selected)
 {
        PointerRNA sceneptr, unit_settings;
        PropertyRNA *system; /* unused , *scale; */
@@ -1011,31 +1014,31 @@ void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename)
        // <library_cameras>
        if(has_object_type(sce, OB_CAMERA)) {
                CamerasExporter ce(&sw);
-               ce.exportCameras(sce);
+               ce.exportCameras(sce, selected);
        }
        
        // <library_lights>
        if(has_object_type(sce, OB_LAMP)) {
                LightsExporter le(&sw);
-               le.exportLights(sce);
+               le.exportLights(sce, selected);
        }
 
        // <library_images>
        ImagesExporter ie(&sw, filename);
-       ie.exportImages(sce);
+       ie.exportImages(sce, selected);
        
        // <library_effects>
        EffectsExporter ee(&sw);
-       ee.exportEffects(sce);
+       ee.exportEffects(sce, selected);
        
        // <library_materials>
        MaterialsExporter me(&sw);
-       me.exportMaterials(sce);
+       me.exportMaterials(sce, selected);
 
        // <library_geometries>
        if(has_object_type(sce, OB_MESH)) {
                GeometryExporter ge(&sw);
-               ge.exportGeom(sce);
+               ge.exportGeom(sce, selected);
        }
 
        // <library_animations>
@@ -1045,12 +1048,12 @@ void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename)
        // <library_controllers>
        ArmatureExporter arm_exporter(&sw);
        if(has_object_type(sce, OB_ARMATURE)) {
-               arm_exporter.export_controllers(sce);
+               arm_exporter.export_controllers(sce, selected);
        }
 
        // <library_visual_scenes>
        SceneExporter se(&sw, &arm_exporter);
-       se.exportScene(sce);
+       se.exportScene(sce, selected);
        
        // <scene>
        std::string scene_name(translate_id(id_name(sce)));
index 9d6d2114cd8ac6f5dec04c3218c1cc27b0f6500f..923313c4ed9c841edb93d9770a24a9466bd0de6e 100644 (file)
@@ -34,7 +34,7 @@ struct Scene;
 class DocumentExporter
 {
  public:
-       void exportCurrentScene(Scene *sce, const char* filename);
+       void exportCurrentScene(Scene *sce, const char* filename, bool selected);
        void exportScenes(const char* filename);
 };
 
index 74756859d3b92a58c1247c04aa95f87b36cf1f6b..f51330165f367a5cf68145bd286ef3b2cc08d247 100644 (file)
@@ -78,12 +78,12 @@ bool EffectsExporter::hasEffects(Scene *sce)
        return false;
 }
 
-void EffectsExporter::exportEffects(Scene *sce)
+void EffectsExporter::exportEffects(Scene *sce, bool export_selected)
 {
        if(hasEffects(sce)) {
                openLibrary();
                MaterialFunctor mf;
-               mf.forEachMaterialInScene<EffectsExporter>(sce, *this);
+               mf.forEachMaterialInScene<EffectsExporter>(sce, *this, export_selected);
 
                closeLibrary();
        }
index 2b25d1b889f10e69e119ccdd35282d65a10ad5c3..86143ae4d07759dc2361ebe8452c43dc318c6de2 100644 (file)
@@ -47,7 +47,7 @@ class EffectsExporter: COLLADASW::LibraryEffects
 {
 public:
        EffectsExporter(COLLADASW::StreamWriter *sw);
-       void exportEffects(Scene *sce);
+       void exportEffects(Scene *sce, bool export_selected);
 
        void operator()(Material *ma, Object *ob);
        
index 5df5ab99b91bc02d550f040df5ea9a54a25720d6..b724844b1ec6f4536c50e9396fc994d811cc82c8 100644 (file)
 GeometryExporter::GeometryExporter(COLLADASW::StreamWriter *sw) : COLLADASW::LibraryGeometries(sw) {}
 
 
-void GeometryExporter::exportGeom(Scene *sce)
+void GeometryExporter::exportGeom(Scene *sce, bool export_selected)
 {
        openLibrary();
 
        mScene = sce;
        GeometryFunctor gf;
-       gf.forEachMeshObjectInScene<GeometryExporter>(sce, *this);
+       gf.forEachMeshObjectInScene<GeometryExporter>(sce, *this, export_selected);
 
        closeLibrary();
 }
index 0b9abaebc25223c61dd62f526fe5f3d091274884..7f3426a19155fea894a6af7fb0b8051993144898 100644 (file)
@@ -60,7 +60,7 @@ class GeometryExporter : COLLADASW::LibraryGeometries
 public:
        GeometryExporter(COLLADASW::StreamWriter *sw);
 
-       void exportGeom(Scene *sce);
+       void exportGeom(Scene *sce, bool export_selected);
 
        void operator()(Object *ob);
 
@@ -102,14 +102,15 @@ struct GeometryFunctor {
        // f should have
        // void operator()(Object* ob)
        template<class Functor>
-       void forEachMeshObjectInScene(Scene *sce, Functor &f)
+       void forEachMeshObjectInScene(Scene *sce, Functor &f, bool export_selected)
        {
                
                Base *base= (Base*) sce->base.first;
                while(base) {
                        Object *ob = base->object;
                        
-                       if (ob->type == OB_MESH && ob->data) {
+                       if (ob->type == OB_MESH && ob->data
+                               && !(export_selected && !(ob->flag && SELECT))) {
                                f(ob);
                        }
                        base= base->next;
index b7a5ef4c4e453f4303a18147a39dc9b62a98b120..8e426e9dba86cfaf9efc6c35e23fe5112ad63f35 100644 (file)
@@ -71,12 +71,12 @@ bool ImagesExporter::hasImages(Scene *sce)
        return false;
 }
 
-void ImagesExporter::exportImages(Scene *sce)
+void ImagesExporter::exportImages(Scene *sce, bool export_selected)
 {
        if(hasImages(sce)) {
                openLibrary();
                MaterialFunctor mf;
-               mf.forEachMaterialInScene<ImagesExporter>(sce, *this);
+               mf.forEachMaterialInScene<ImagesExporter>(sce, *this, export_selected);
 
                closeLibrary();
        }
index 04e3010dc7a2bf84fb3b5143da4094184f16e9b9..6b81c099259bc9ecba588d5b6746da670d74246a 100644 (file)
@@ -47,7 +47,7 @@ class ImagesExporter: COLLADASW::LibraryImages
 public:
        ImagesExporter(COLLADASW::StreamWriter *sw, const char* filename);
        
-       void exportImages(Scene *sce);
+       void exportImages(Scene *sce, bool export_selected);
        void operator()(Material *ma, Object *ob);
 private:
        bool hasImages(Scene *sce);
index 13eb62ca9694c1bfa1f550e79372e1ca361fd75c..c2cc0c1e157bd0818147069b80d7991ef5246b9a 100644 (file)
 #include "collada_internal.h"
 
 template<class Functor>
-void forEachLampObjectInScene(Scene *sce, Functor &f)
+void forEachLampObjectInScene(Scene *sce, Functor &f, bool export_selected)
 {
        Base *base= (Base*) sce->base.first;
        while(base) {
                Object *ob = base->object;
                        
-               if (ob->type == OB_LAMP && ob->data) {
+               if (ob->type == OB_LAMP && ob->data
+                       && !(export_selected && !(ob->flag & SELECT))) {
                        f(ob);
                }
                base= base->next;
@@ -53,11 +54,11 @@ void forEachLampObjectInScene(Scene *sce, Functor &f)
 
 LightsExporter::LightsExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryLights(sw){}
 
-void LightsExporter::exportLights(Scene *sce)
+void LightsExporter::exportLights(Scene *sce, bool export_selected)
 {
        openLibrary();
        
-       forEachLampObjectInScene(sce, *this);
+       forEachLampObjectInScene(sce, *this, export_selected);
        
        closeLibrary();
 }
index 3706582e52cb6df575c3cf8d971ca3962ba047fe..2ae1a19fdb183e7e639d86225a68483749bb32d7 100644 (file)
@@ -41,7 +41,7 @@ class LightsExporter: COLLADASW::LibraryLights
 {
 public:
        LightsExporter(COLLADASW::StreamWriter *sw);
-       void exportLights(Scene *sce);
+       void exportLights(Scene *sce, bool export_selected);
        void operator()(Object *ob);
 private:
        bool exportBlenderProfile(COLLADASW::Light &cla, Lamp *la);
index 0030f2a6285afe0bd194d94fc8de3e5bc9e672eb..a44fa6802f20a3a460d2007455d69e1f84abfa5d 100644 (file)
 
 MaterialsExporter::MaterialsExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryMaterials(sw){}
 
-void MaterialsExporter::exportMaterials(Scene *sce)
+void MaterialsExporter::exportMaterials(Scene *sce, bool export_selected)
 {
        openLibrary();
 
        MaterialFunctor mf;
-       mf.forEachMaterialInScene<MaterialsExporter>(sce, *this);
+       mf.forEachMaterialInScene<MaterialsExporter>(sce, *this, export_selected);
 
        closeLibrary();
 }
index 033c8526346c5bf0f1cb5e884ae6359a46667e78..0a7a276d8576c5cde574c81f7edbfaaa97bc0fa5 100644 (file)
@@ -49,7 +49,7 @@ class MaterialsExporter: COLLADASW::LibraryMaterials
 {
 public:
        MaterialsExporter(COLLADASW::StreamWriter *sw);
-       void exportMaterials(Scene *sce);
+       void exportMaterials(Scene *sce, bool export_selected);
        void operator()(Material *ma, Object *ob);
 };
 
@@ -86,11 +86,11 @@ struct MaterialFunctor {
        // f should have
        // void operator()(Material* ma)
        template<class Functor>
-       void forEachMaterialInScene(Scene *sce, Functor &f)
+       void forEachMaterialInScene(Scene *sce, Functor &f, bool export_selected)
        {
                ForEachMaterialFunctor<Functor> matfunc(&f);
                GeometryFunctor gf;
-               gf.forEachMeshObjectInScene<ForEachMaterialFunctor<Functor> >(sce, matfunc);
+               gf.forEachMeshObjectInScene<ForEachMaterialFunctor<Functor> >(sce, matfunc, export_selected);
        }
 };
 
index 51caf62f6e7b97b45aa8536e169e6e726075f0ef..c15e608c3603deb2acdefc1d548b0af4e74f9442 100644 (file)
@@ -51,7 +51,7 @@ extern "C"
                return 1;
        }
 
-       int collada_export(Scene *sce, const char *filepath)
+       int collada_export(Scene *sce, const char *filepath, int selected)
        {
                DocumentExporter exp;
 
@@ -64,7 +64,7 @@ extern "C"
                }
                /* end! */
 
-               exp.exportCurrentScene(sce, filepath);
+               exp.exportCurrentScene(sce, filepath, selected);
 
                return 1;
        }
index a167784e217f7aebf9f6fdeb35708e08e3d45597..915a77354e328576bd4653ac8c354404a74139fb 100644 (file)
@@ -39,7 +39,7 @@ extern "C" {
         * both return 1 on success, 0 on error
         */
        int collada_import(bContext *C, const char *filepath);
-       int collada_export(Scene *sce, const char *filepath);
+       int collada_export(Scene *sce, const char *filepath, int selected);
 #ifdef __cplusplus
 }
 #endif
index 9cb6a227fc906f2c219bf0e5ca2490ded1a6502a..27397c3008e7e2b1970d7c724a147969ad16625e 100644 (file)
@@ -265,7 +265,7 @@ std::string get_light_id(Object *ob)
 
 std::string get_joint_id(Bone *bone, Object *ob_arm)
 {
-       return translate_id(bone->name);
+       return translate_id(/*id_name(ob_arm) + "_" +*/ bone->name);
 }
 
 std::string get_camera_id(Object *ob)
index c2194636cd332855dd460402c7e82d1335170b8f..1220c4f34a1323bdb633aae706ec96374e77cdb4 100644 (file)
@@ -85,9 +85,9 @@ static void rna_SceneRender_get_frame_path(RenderData *rd, int frame, char *name
 /* don't remove this, as COLLADA exporting cannot be done through operators in render() callback. */
 #include "../../collada/collada.h"
 
-static void rna_Scene_collada_export(Scene *scene, const char *filepath)
+static void rna_Scene_collada_export(Scene *scene, const char *filepath, int selected)
 {
-       collada_export(scene, filepath);
+       collada_export(scene, filepath, selected);
 }
 
 #endif
@@ -112,6 +112,7 @@ void RNA_api_scene(StructRNA *srna)
        /* don't remove this, as COLLADA exporting cannot be done through operators in render() callback. */
        func= RNA_def_function(srna, "collada_export", "rna_Scene_collada_export");
        parm= RNA_def_string(func, "filepath", "", FILE_MAX, "File Path", "File path to write Collada file.");
+       parm= RNA_def_boolean(func, "selected", 0, "Export only selected", "Export only selected elements.");
        RNA_def_property_flag(parm, PROP_REQUIRED);
        RNA_def_property_subtype(parm, PROP_FILEPATH); /* allow non utf8 */
        RNA_def_function_ui_description(func, "Export to collada file.");
index f65485b84dda1d229ffe33c3625de162a91cc806..35afdf29b53f4b25b468c68e93db4f2704345158 100644 (file)
@@ -2004,6 +2004,8 @@ static void WM_OT_save_mainfile(wmOperatorType *ot)
 
 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {      
+       int selected = 0;
+
        if(!RNA_property_is_set(op->ptr, "filepath")) {
                char filepath[FILE_MAX];
                BLI_strncpy(filepath, G.main->name, sizeof(filepath));
@@ -2020,6 +2022,7 @@ static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED
 static int wm_collada_export_exec(bContext *C, wmOperator *op)
 {
        char filename[FILE_MAX];
+       int selected;
        
        if(!RNA_property_is_set(op->ptr, "filepath")) {
                BKE_report(op->reports, RPT_ERROR, "No filename given");
@@ -2027,7 +2030,8 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op)
        }
 
        RNA_string_get(op->ptr, "filepath", filename);
-       if(collada_export(CTX_data_scene(C), filename)) {
+       selected = RNA_boolean_get(op->ptr, "selected");
+       if(collada_export(CTX_data_scene(C), filename, selected)) {
                return OPERATOR_FINISHED;
        }
        else {
@@ -2045,6 +2049,8 @@ static void WM_OT_collada_export(wmOperatorType *ot)
        ot->poll= WM_operator_winactive;
        
        WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
+       RNA_def_boolean(ot->srna, "selected", 0, "Export only selected",
+               "Export only selected elements");
 }
 
 /* function used for WM_OT_save_mainfile too */