[#33437](partial fix) Collada: importing a scene changes units. Added an option to...
authorGaia Clary <gaia.clary@machinimatrix.org>
Sat, 9 Feb 2013 16:19:30 +0000 (16:19 +0000)
committerGaia Clary <gaia.clary@machinimatrix.org>
Sat, 9 Feb 2013 16:19:30 +0000 (16:19 +0000)
source/blender/collada/CMakeLists.txt
source/blender/collada/DocumentImporter.cpp
source/blender/collada/DocumentImporter.h
source/blender/collada/ImportSettings.cpp [new file with mode: 0644]
source/blender/collada/ImportSettings.h [new file with mode: 0644]
source/blender/collada/collada.cpp
source/blender/collada/collada.h
source/blender/editors/io/io_collada.c

index 7f38934..326ca2b 100644 (file)
@@ -57,6 +57,7 @@ set(SRC
        EffectExporter.cpp
        ErrorHandler.cpp
        ExportSettings.cpp
+       ImportSettings.cpp
        ExtraHandler.cpp
        ExtraTags.cpp
        GeometryExporter.cpp
@@ -84,6 +85,7 @@ set(SRC
        EffectExporter.h
        ErrorHandler.h
        ExportSettings.h
+       ImportSettings.h
        ExtraHandler.h
        ExtraTags.h
        GeometryExporter.h
index a234d85..7437401 100644 (file)
@@ -99,9 +99,9 @@ extern "C" {
 // creates empties for each imported bone on layer 2, for debugging
 // #define ARMATURE_TEST
 
-DocumentImporter::DocumentImporter(bContext *C, const char *filename) :
+DocumentImporter::DocumentImporter(bContext *C, const ImportSettings *import_settings) :
+    import_settings(import_settings),
        mImportStage(General),
-       mFilename(filename),
        mContext(C),
        armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(C)),
        mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)),
@@ -131,6 +131,7 @@ bool DocumentImporter::import()
        // deselect all to select new objects
        BKE_scene_base_deselect_all(CTX_data_scene(mContext));
 
+       std::string mFilename = std::string(this->import_settings->filepath);
        const std::string encodedFilename = bc_url_encode(mFilename);
        if (!root.loadDocument(encodedFilename)) {
                fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 1st pass\n");
@@ -188,30 +189,42 @@ void DocumentImporter::finish()
                Scene *sce = CTX_data_scene(mContext);
                
                // for scene unit settings: system, scale_length
+
                RNA_id_pointer_create(&sce->id, &sceneptr);
                unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
                system = RNA_struct_find_property(&unit_settings, "system");
                scale = RNA_struct_find_property(&unit_settings, "scale_length");
-               
-               switch (unit_converter.isMetricSystem()) {
-                       case UnitConverter::Metric:
-                               RNA_property_enum_set(&unit_settings, system, USER_UNIT_METRIC);
-                               break;
-                       case UnitConverter::Imperial:
-                               RNA_property_enum_set(&unit_settings, system, USER_UNIT_IMPERIAL);
-                               break;
-                       default:
-                               RNA_property_enum_set(&unit_settings, system, USER_UNIT_NONE);
-                               break;
+
+               if (this->import_settings->import_units) {
+                       
+                       switch (unit_converter.isMetricSystem()) {
+                               case UnitConverter::Metric:
+                                       RNA_property_enum_set(&unit_settings, system, USER_UNIT_METRIC);
+                                       break;
+                               case UnitConverter::Imperial:
+                                       RNA_property_enum_set(&unit_settings, system, USER_UNIT_IMPERIAL);
+                                       break;
+                               default:
+                                       RNA_property_enum_set(&unit_settings, system, USER_UNIT_NONE);
+                                       break;
+                       }
+                       float unit_factor = unit_converter.getLinearMeter();
+                       RNA_property_float_set(&unit_settings, scale, unit_factor);
+                       fprintf(stdout, "Collada: Adjusting Blender units to Importset units: %f.\n", unit_factor);
+
+               }
+               else {
+                       // TODO: add automatic scaling for the case when Blender units 
+                       //       and import units are set to different values.
                }
-               RNA_property_float_set(&unit_settings, scale, unit_converter.getLinearMeter());
-               
-               const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
 
+               // Write nodes to scene
+               const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
                for (unsigned int i = 0; i < roots.getCount(); i++) {
                        write_node(roots[i], NULL, sce, NULL, false);
                }
 
+               // update scene
                Main *bmain = CTX_data_main(mContext);
                DAG_scene_sort(bmain, sce);
                DAG_ids_flush_update(bmain, 0);
@@ -974,7 +987,7 @@ bool DocumentImporter::writeImage(const COLLADAFW::Image *image)
                
        // XXX maybe it is necessary to check if the path is absolute or relative
        const std::string& filepath = image->getImageURI().toNativePath();
-       const char *filename = (const char *)mFilename.c_str();
+       const char *filename = (const char *)filepath.c_str();
        char dir[FILE_MAX];
        char full_path[FILE_MAX];
        
index 7e3476f..76d16f3 100644 (file)
@@ -47,6 +47,7 @@
 #include "ArmatureImporter.h"
 #include "ControllerExporter.h"
 #include "MeshImporter.h"
+#include "ImportSettings.h"
 
 
 
@@ -63,7 +64,7 @@ public:
                Controller,             //!< Second pass to collect controller data
        };
        /** Constructor */
-       DocumentImporter(bContext *C, const char *filename);
+       DocumentImporter(bContext *C, const ImportSettings *import_settings);
 
        /** Destructor */
        ~DocumentImporter();
@@ -137,10 +138,10 @@ public:
 
 
 private:
+       const ImportSettings *import_settings;
 
        /** Current import stage we're in. */
        ImportStage mImportStage;
-       std::string mFilename;
 
        bContext *mContext;
 
diff --git a/source/blender/collada/ImportSettings.cpp b/source/blender/collada/ImportSettings.cpp
new file mode 100644 (file)
index 0000000..7460778
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * ***** 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.
+ *
+ * Contributor(s): Gaia Clary.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/collada/ExportSettings.cpp
+ *  \ingroup collada
+ */
+
+#include "ImportSettings.h"
diff --git a/source/blender/collada/ImportSettings.h b/source/blender/collada/ImportSettings.h
new file mode 100644 (file)
index 0000000..3f3a9fb
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * ***** 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.
+ *
+ * Contributor(s): Gaia Clary
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file ExportSettings.h
+ *  \ingroup collada
+ */
+
+#ifndef __IMPORTSETTINGS_H__
+#define __IMPORTSETTINGS_H__
+
+#include "collada.h"
+
+struct ImportSettings {
+public:
+       bool import_units;
+
+       char *filepath;
+};
+
+#endif
index ef34c55..1aff0f1 100644 (file)
@@ -31,6 +31,7 @@
 #include "DocumentExporter.h"
 #include "DocumentImporter.h"
 #include "ExportSettings.h"
+#include "ImportSettings.h"
 
 extern "C"
 {
@@ -42,9 +43,17 @@ extern "C"
 #include "BLI_path_util.h"
 #include "BLI_linklist.h"
 
-int collada_import(bContext *C, const char *filepath)
+int collada_import(bContext *C,
+                                  const char *filepath,
+                                  int import_units)
 {
-       DocumentImporter imp(C, filepath);
+
+       ImportSettings import_settings;
+       import_settings.filepath = (char *)filepath;
+
+       import_settings.import_units =  import_units != 0;
+
+       DocumentImporter imp(C, &import_settings);
        if (imp.import()) return 1;
 
        return 0;
index a02e3e0..587dc37 100644 (file)
@@ -46,7 +46,10 @@ struct Scene;
 /*
  * both return 1 on success, 0 on error
  */
-int collada_import(bContext *C, const char *filepath);
+int collada_import(bContext *C,
+                                  const char *filepath,
+                                  int import_units);
+
 int collada_export(Scene *sce,
                    const char *filepath,
                    int apply_modifiers,
index f53672b..0982b4f 100644 (file)
@@ -308,18 +308,47 @@ void WM_OT_collada_export(wmOperatorType *ot)
 static int wm_collada_import_exec(bContext *C, wmOperator *op)
 {
        char filename[FILE_MAX];
+       int import_units;
 
        if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
                BKE_report(op->reports, RPT_ERROR, "No filename given");
                return OPERATOR_CANCELLED;
        }
 
-       RNA_string_get(op->ptr, "filepath", filename);
-       if (collada_import(C, filename)) return OPERATOR_FINISHED;
+       /* Options panel */
+       import_units = RNA_boolean_get(op->ptr, "import_units");
 
+       RNA_string_get(op->ptr, "filepath", filename);
+       if (collada_import( C,
+                                               filename,
+                                               import_units))  {
+                       return OPERATOR_FINISHED;
+       }
+       else {
        BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document (see console for details)");
+       return OPERATOR_CANCELLED;
+       }
+}
+
+static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr)
+{
+       uiLayout *box, *row;
+
+       /* Import Options: */
+       box = uiLayoutBox(layout);
+       row = uiLayoutRow(box, FALSE);
+       uiItemL(row, IFACE_("Import Data Options:"), ICON_MESH_DATA);
 
-       return OPERATOR_FINISHED;
+       row = uiLayoutRow(box, FALSE);
+       uiItemR(row, imfptr, "import_units", 0, NULL, ICON_NONE);
+}
+
+static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op)
+{
+       PointerRNA ptr;
+
+       RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
+       uiCollada_importSettings(op->layout, &ptr);
 }
 
 void WM_OT_collada_import(wmOperatorType *ot)
@@ -332,7 +361,16 @@ void WM_OT_collada_import(wmOperatorType *ot)
        ot->exec = wm_collada_import_exec;
        ot->poll = WM_operator_winactive;
 
+       //ot->flag |= OPTYPE_PRESET;
+
+       ot->ui = wm_collada_import_draw;
+
        WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_OPENFILE,
                                       WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
+
+       RNA_def_boolean(ot->srna,
+                       "import_units", 0, "Import Units",
+                                       "If enabled use Units as defined in Collada Import, else keep Blender's current Units settings. ");
+
 }
 #endif