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.
This commit is contained in:
Nathan Letwory 2010-10-09 21:17:14 +00:00
parent 6b8ca3ccdf
commit f72eef5de7
6 changed files with 76 additions and 14 deletions

@ -72,6 +72,8 @@
#include "DNA_camera_types.h" #include "DNA_camera_types.h"
#include "DNA_lamp_types.h" #include "DNA_lamp_types.h"
#include "RNA_access.h"
#include "MEM_guardedalloc.h" #include "MEM_guardedalloc.h"
#include "DocumentImporter.h" #include "DocumentImporter.h"
@ -130,7 +132,7 @@ public:
/** Constructor. */ /** Constructor. */
Writer(bContext *C, const char *filename) : mFilename(filename), mContext(C), Writer(bContext *C, const char *filename) : mFilename(filename), mContext(C),
armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(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)) {} anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C)) {}
/** Destructor. */ /** Destructor. */
@ -172,8 +174,30 @@ public:
{ {
std::vector<const COLLADAFW::VisualScene*>::iterator it; std::vector<const COLLADAFW::VisualScene*>::iterator it;
for (it = vscenes.begin(); it != vscenes.end(); it++) { for (it = vscenes.begin(); it != vscenes.end(); it++) {
PointerRNA sceneptr, unit_settings;
PropertyRNA *system, *scale;
// TODO: create a new scene except the selected <visual_scene> - use current blender scene for it // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it
Scene *sce = CTX_data_scene(mContext); 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(); const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
for (unsigned int i = 0; i < roots.getCount(); i++) { 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.*/ @return The writer should return true, if writing succeeded, false otherwise.*/
virtual bool writeGlobalAsset ( const COLLADAFW::FileInfo* asset ) 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); unit_converter.read_asset(asset);
return true; return true;

@ -323,8 +323,9 @@ void MeshImporter::read_vertices(COLLADAFW::Mesh *mesh, Mesh *me)
MVert *mvert; MVert *mvert;
int i; 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); get_vector(mvert->co, pos, i);
}
} }
int MeshImporter::triangulate_poly(unsigned int *indices, int totvert, MVert *verts, std::vector<unsigned int>& tri) int MeshImporter::triangulate_poly(unsigned int *indices, int totvert, MVert *verts, std::vector<unsigned int>& 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; 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; 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) Object *MeshImporter::get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid)
{ {

@ -119,10 +119,12 @@ private:
void get_vector(float v[3], COLLADAFW::MeshVertexData& arr, int i); void get_vector(float v[3], COLLADAFW::MeshVertexData& arr, int i);
bool flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, int count); bool flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, int count);
UnitConverter *unitconverter;
public: 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); virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid);

@ -28,12 +28,38 @@ UnitConverter::UnitConverter() : unit(), up_axis(COLLADAFW::FileInfo::Z_UP) {}
void UnitConverter::read_asset(const COLLADAFW::FileInfo* asset) void UnitConverter::read_asset(const COLLADAFW::FileInfo* asset)
{ {
unit = asset->getUnit();
up_axis = asset->getUpAxisType();
} }
// TODO UnitConverter::UnitSystem UnitConverter::isMetricSystem()
// convert vector vec from COLLADA format to Blender
void UnitConverter::convertVec3(float *vec)
{ {
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... // TODO need also for angle conversion, time conversion...

@ -43,14 +43,22 @@ private:
public: public:
enum UnitSystem {
None,
Metric,
Imperial
};
// Initialize with Z_UP, since Blender uses right-handed, z-up // Initialize with Z_UP, since Blender uses right-handed, z-up
UnitConverter(); UnitConverter();
void read_asset(const COLLADAFW::FileInfo* asset); void read_asset(const COLLADAFW::FileInfo* asset);
// TODO void convertVector3(COLLADABU::Math::Vector3 &vec, float *v);
// convert vector vec from COLLADA format to Blender
void convertVec3(float *vec); UnitConverter::UnitSystem isMetricSystem(void);
float getLinearMeter(void);
// TODO need also for angle conversion, time conversion... // TODO need also for angle conversion, time conversion...

@ -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); int RNA_string_length(PointerRNA *ptr, const char *name);
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value); 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); 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_set(PointerRNA *ptr, const char *name, PointerRNA ptr_value);
void RNA_pointer_add(PointerRNA *ptr, const char *name); void RNA_pointer_add(PointerRNA *ptr, const char *name);