From f72eef5de7fd109e9d71021adc0990b040b750b2 Mon Sep 17 00:00:00 2001 From: Nathan Letwory Date: Sat, 9 Oct 2010 21:17:14 +0000 Subject: COLLADA Import unit_settings to scene. Note: I use here RNA to do this, and I think I might slowly work on replacing low-level DNA usage with RNA where possible. --- source/blender/collada/DocumentImporter.cpp | 29 ++++++++++++++++++++++---- source/blender/collada/MeshImporter.cpp | 7 ++++--- source/blender/collada/MeshImporter.h | 4 +++- source/blender/collada/collada_internal.cpp | 32 ++++++++++++++++++++++++++--- source/blender/collada/collada_internal.h | 14 ++++++++++--- source/blender/makesrna/RNA_access.h | 4 ++++ 6 files changed, 76 insertions(+), 14 deletions(-) (limited to 'source/blender') diff --git a/source/blender/collada/DocumentImporter.cpp b/source/blender/collada/DocumentImporter.cpp index 40e4d389b88..a406845c8a2 100644 --- a/source/blender/collada/DocumentImporter.cpp +++ b/source/blender/collada/DocumentImporter.cpp @@ -72,6 +72,8 @@ #include "DNA_camera_types.h" #include "DNA_lamp_types.h" +#include "RNA_access.h" + #include "MEM_guardedalloc.h" #include "DocumentImporter.h" @@ -130,7 +132,7 @@ public: /** Constructor. */ Writer(bContext *C, const char *filename) : mFilename(filename), mContext(C), armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(C)), - mesh_importer(&armature_importer, CTX_data_scene(C)), + mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)), anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C)) {} /** Destructor. */ @@ -172,8 +174,30 @@ public: { std::vector::iterator it; for (it = vscenes.begin(); it != vscenes.end(); it++) { + PointerRNA sceneptr, unit_settings; + PropertyRNA *system, *scale; // TODO: create a new scene except the selected - use current blender scene for it 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; + } + RNA_property_float_set(&unit_settings, scale, unit_converter.getLinearMeter()); + const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes(); for (unsigned int i = 0; i < roots.getCount(); i++) { @@ -253,9 +277,6 @@ public: @return The writer should return true, if writing succeeded, false otherwise.*/ virtual bool writeGlobalAsset ( const COLLADAFW::FileInfo* asset ) { - // XXX take up_axis, unit into account - // COLLADAFW::FileInfo::Unit unit = asset->getUnit(); - // COLLADAFW::FileInfo::UpAxisType upAxis = asset->getUpAxisType(); unit_converter.read_asset(asset); return true; diff --git a/source/blender/collada/MeshImporter.cpp b/source/blender/collada/MeshImporter.cpp index df5136f1cb4..44ac310e353 100644 --- a/source/blender/collada/MeshImporter.cpp +++ b/source/blender/collada/MeshImporter.cpp @@ -323,8 +323,9 @@ void MeshImporter::read_vertices(COLLADAFW::Mesh *mesh, Mesh *me) MVert *mvert; int i; - for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) + for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) { get_vector(mvert->co, pos, i); + } } int MeshImporter::triangulate_poly(unsigned int *indices, int totvert, MVert *verts, std::vector& tri) @@ -631,7 +632,7 @@ void MeshImporter::read_faces(COLLADAFW::Mesh *mesh, Mesh *me, int new_tris) } } - mat_prim_map[mp->getMaterialId()].push_back(prim); + mat_prim_map[mp->getMaterialId()].push_back(prim); } geom_uid_mat_mapping_map[mesh->getUniqueId()] = mat_prim_map; @@ -689,7 +690,7 @@ bool MeshImporter::flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, return true; } -MeshImporter::MeshImporter(ArmatureImporter *arm, Scene *sce) : scene(sce), armature_importer(arm) {} +MeshImporter::MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce) : unitconverter(unitconv), scene(sce), armature_importer(arm) {} Object *MeshImporter::get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid) { diff --git a/source/blender/collada/MeshImporter.h b/source/blender/collada/MeshImporter.h index 1fa9ebfbabc..055c4798855 100644 --- a/source/blender/collada/MeshImporter.h +++ b/source/blender/collada/MeshImporter.h @@ -119,10 +119,12 @@ private: void get_vector(float v[3], COLLADAFW::MeshVertexData& arr, int i); bool flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, int count); + + UnitConverter *unitconverter; public: - MeshImporter(ArmatureImporter *arm, Scene *sce); + MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce); virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid); diff --git a/source/blender/collada/collada_internal.cpp b/source/blender/collada/collada_internal.cpp index 67ee4818f7e..6f401c1856f 100644 --- a/source/blender/collada/collada_internal.cpp +++ b/source/blender/collada/collada_internal.cpp @@ -28,12 +28,38 @@ UnitConverter::UnitConverter() : unit(), up_axis(COLLADAFW::FileInfo::Z_UP) {} void UnitConverter::read_asset(const COLLADAFW::FileInfo* asset) { + unit = asset->getUnit(); + up_axis = asset->getUpAxisType(); } -// TODO -// convert vector vec from COLLADA format to Blender -void UnitConverter::convertVec3(float *vec) +UnitConverter::UnitSystem UnitConverter::isMetricSystem() { + switch(unit.getLinearUnitUnit()) { + case COLLADAFW::FileInfo::Unit::MILLIMETER: + case COLLADAFW::FileInfo::Unit::CENTIMETER: + case COLLADAFW::FileInfo::Unit::DECIMETER: + case COLLADAFW::FileInfo::Unit::METER: + case COLLADAFW::FileInfo::Unit::KILOMETER: + return UnitConverter::Metric; + case COLLADAFW::FileInfo::Unit::INCH: + case COLLADAFW::FileInfo::Unit::FOOT: + case COLLADAFW::FileInfo::Unit::YARD: + return UnitConverter::Imperial; + default: + return UnitConverter::None; + } +} + +float UnitConverter::getLinearMeter() +{ + return (float)unit.getLinearUnitMeter(); +} + +void UnitConverter::convertVector3(COLLADABU::Math::Vector3 &vec, float *v) +{ + v[0] = vec.x; + v[1] = vec.y; + v[2] = vec.z; } // TODO need also for angle conversion, time conversion... diff --git a/source/blender/collada/collada_internal.h b/source/blender/collada/collada_internal.h index 1e3546263da..72b03a3d090 100644 --- a/source/blender/collada/collada_internal.h +++ b/source/blender/collada/collada_internal.h @@ -43,14 +43,22 @@ private: public: + enum UnitSystem { + None, + Metric, + Imperial + }; + // Initialize with Z_UP, since Blender uses right-handed, z-up UnitConverter(); void read_asset(const COLLADAFW::FileInfo* asset); - // TODO - // convert vector vec from COLLADA format to Blender - void convertVec3(float *vec); + void convertVector3(COLLADABU::Math::Vector3 &vec, float *v); + + UnitConverter::UnitSystem isMetricSystem(void); + + float getLinearMeter(void); // TODO need also for angle conversion, time conversion... diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index e454dda0ae6..ab1319653b8 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -834,7 +834,11 @@ char *RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, in int RNA_string_length(PointerRNA *ptr, const char *name); void RNA_string_set(PointerRNA *ptr, const char *name, const char *value); +/** + * Retrieve the named property from PointerRNA. + */ PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name); +/* Set the property name of PointerRNA ptr to ptr_value */ void RNA_pointer_set(PointerRNA *ptr, const char *name, PointerRNA ptr_value); void RNA_pointer_add(PointerRNA *ptr, const char *name); -- cgit v1.2.3