Apply patch 4636051. COLLADA: Export selection.

Original patch by Jan Diederich, adapted by Pelle Johnsen. Review assistance by Daniel Tavares.

This patch adds an option to export only the selection.
This commit is contained in:
Nathan Letwory 2011-07-04 08:59:28 +00:00
parent aa1668c6f8
commit cf43e48fc7
21 changed files with 69 additions and 51 deletions

View 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);

View 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);

View File

@ -42,24 +42,25 @@
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();
}

View 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);
};

View 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)));

View 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);
};

View 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();
}

View 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);

View File

@ -47,13 +47,13 @@
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();
}

View 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;

View 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();
}

View 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);

View File

@ -38,13 +38,14 @@
#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();
}

View 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);

View File

@ -35,12 +35,12 @@
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();
}

View 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);
}
};

View 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;
}

View 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

View 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)

View 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.");

View 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 */