Blender V2.61 - r43446

DocumentExporter.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <math.h>
00030 
00031 extern "C" 
00032 {
00033 #include "DNA_scene_types.h"
00034 #include "DNA_object_types.h"
00035 #include "DNA_group_types.h"
00036 #include "DNA_meshdata_types.h"
00037 #include "DNA_mesh_types.h"
00038 #include "DNA_image_types.h"
00039 #include "DNA_material_types.h"
00040 #include "DNA_texture_types.h"
00041 #include "DNA_anim_types.h"
00042 #include "DNA_action_types.h"
00043 #include "DNA_curve_types.h"
00044 #include "DNA_armature_types.h"
00045 #include "DNA_modifier_types.h"
00046 #include "DNA_userdef_types.h"
00047 
00048 #include "BKE_DerivedMesh.h"
00049 #include "BKE_fcurve.h"
00050 #include "BKE_animsys.h"
00051 #include "BLI_path_util.h"
00052 #include "BLI_fileops.h"
00053 #include "ED_keyframing.h"
00054 #ifdef WITH_BUILDINFO
00055 extern char build_rev[];
00056 #endif
00057 }
00058 
00059 #include "MEM_guardedalloc.h"
00060 
00061 #include "BKE_blender.h" // version info
00062 #include "BKE_scene.h"
00063 #include "BKE_global.h"
00064 #include "BKE_main.h"
00065 #include "BKE_material.h"
00066 #include "BKE_action.h" // pose functions
00067 #include "BKE_armature.h"
00068 #include "BKE_image.h"
00069 #include "BKE_utildefines.h"
00070 #include "BKE_object.h"
00071 
00072 #include "BLI_math.h"
00073 #include "BLI_string.h"
00074 #include "BLI_listbase.h"
00075 
00076 #include "RNA_access.h"
00077 
00078 #include "COLLADASWAsset.h"
00079 #include "COLLADASWLibraryVisualScenes.h"
00080 #include "COLLADASWNode.h"
00081 #include "COLLADASWSource.h"
00082 #include "COLLADASWInstanceGeometry.h"
00083 #include "COLLADASWInputList.h"
00084 #include "COLLADASWPrimitves.h"
00085 #include "COLLADASWVertices.h"
00086 #include "COLLADASWLibraryAnimations.h"
00087 #include "COLLADASWLibraryImages.h"
00088 #include "COLLADASWLibraryEffects.h"
00089 #include "COLLADASWImage.h"
00090 #include "COLLADASWEffectProfile.h"
00091 #include "COLLADASWColorOrTexture.h"
00092 #include "COLLADASWParamTemplate.h"
00093 #include "COLLADASWParamBase.h"
00094 #include "COLLADASWSurfaceInitOption.h"
00095 #include "COLLADASWSampler.h"
00096 #include "COLLADASWScene.h"
00097 #include "COLLADASWTechnique.h"
00098 #include "COLLADASWTexture.h"
00099 #include "COLLADASWLibraryMaterials.h"
00100 #include "COLLADASWBindMaterial.h"
00101 #include "COLLADASWInstanceCamera.h"
00102 #include "COLLADASWInstanceLight.h"
00103 #include "COLLADASWConstants.h"
00104 #include "COLLADASWLibraryControllers.h"
00105 #include "COLLADASWInstanceController.h"
00106 #include "COLLADASWInstanceNode.h"
00107 #include "COLLADASWBaseInputElement.h"
00108 
00109 #include "collada_internal.h"
00110 #include "DocumentExporter.h"
00111 #include "ExportSettings.h"
00112 
00113 // can probably go after refactor is complete
00114 #include "InstanceWriter.h"
00115 #include "TransformWriter.h"
00116 
00117 #include "SceneExporter.h"
00118 #include "ArmatureExporter.h"
00119 #include "AnimationExporter.h"
00120 #include "CameraExporter.h"
00121 #include "EffectExporter.h"
00122 #include "GeometryExporter.h"
00123 #include "ImageExporter.h"
00124 #include "LightExporter.h"
00125 #include "MaterialExporter.h"
00126 
00127 #include <vector>
00128 #include <algorithm> // std::find
00129 
00130 char *bc_CustomData_get_layer_name(const struct CustomData *data, int type, int n)
00131 {
00132     int layer_index = CustomData_get_layer_index(data, type);
00133     if(layer_index < 0) return NULL;
00134 
00135     return data->layers[layer_index+n].name;
00136 }
00137 
00138 char *bc_CustomData_get_active_layer_name(const CustomData *data, int type)
00139 {
00140     /* get the layer index of the active layer of type */
00141     int layer_index = CustomData_get_active_layer_index(data, type);
00142     if(layer_index < 0) return NULL;
00143 
00144     return data->layers[layer_index].name;
00145 }
00146 
00147 DocumentExporter::DocumentExporter(const ExportSettings *export_settings) : export_settings(export_settings) {}
00148 
00149 // TODO: it would be better to instantiate animations rather than create a new one per object
00150 // COLLADA allows this through multiple <channel>s in <animation>.
00151 // For this to work, we need to know objects that use a certain action.
00152 
00153 void DocumentExporter::exportCurrentScene(Scene *sce)
00154 {
00155     PointerRNA sceneptr, unit_settings;
00156     PropertyRNA *system; /* unused , *scale; */
00157 
00158     clear_global_id_map();
00159     
00160     COLLADABU::NativeString native_filename =
00161         COLLADABU::NativeString(std::string(this->export_settings->filepath));
00162     COLLADASW::StreamWriter sw(native_filename);
00163 
00164     // open <collada>
00165     sw.startDocument();
00166 
00167     // <asset>
00168     COLLADASW::Asset asset(&sw);
00169 
00170     RNA_id_pointer_create(&(sce->id), &sceneptr);
00171     unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
00172     system = RNA_struct_find_property(&unit_settings, "system");
00173     //scale = RNA_struct_find_property(&unit_settings, "scale_length");
00174 
00175     std::string unitname = "meter";
00176     float linearmeasure = RNA_float_get(&unit_settings, "scale_length");
00177 
00178     switch(RNA_property_enum_get(&unit_settings, system)) {
00179         case USER_UNIT_NONE:
00180         case USER_UNIT_METRIC:
00181             if(linearmeasure == 0.001f) {
00182                 unitname = "millimeter";
00183             }
00184             else if(linearmeasure == 0.01f) {
00185                 unitname = "centimeter";
00186             }
00187             else if(linearmeasure == 0.1f) {
00188                 unitname = "decimeter";
00189             }
00190             else if(linearmeasure == 1.0f) {
00191                 unitname = "meter";
00192             }
00193             else if(linearmeasure == 1000.0f) {
00194                 unitname = "kilometer";
00195             }
00196             break;
00197         case USER_UNIT_IMPERIAL:
00198             if(linearmeasure == 0.0254f) {
00199                 unitname = "inch";
00200             }
00201             else if(linearmeasure == 0.3048f) {
00202                 unitname = "foot";
00203             }
00204             else if(linearmeasure == 0.9144f) {
00205                 unitname = "yard";
00206             }
00207             break;
00208         default:
00209             break;
00210     }
00211 
00212     asset.setUnit(unitname, linearmeasure);
00213     asset.setUpAxisType(COLLADASW::Asset::Z_UP);
00214     if(U.author[0] != '\0') {
00215         asset.getContributor().mAuthor = U.author;
00216     }
00217     else {
00218         asset.getContributor().mAuthor = "Blender User";
00219     }
00220 #ifdef WITH_BUILDINFO
00221     char version_buf[128];
00222     sprintf(version_buf, "Blender %d.%02d.%d r%s", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION, build_rev);
00223     asset.getContributor().mAuthoringTool = version_buf;
00224 #else
00225     asset.getContributor().mAuthoringTool = "Blender 2.5x";
00226 #endif
00227     asset.add();
00228     
00229     // <library_cameras>
00230     if(has_object_type(sce, OB_CAMERA)) {
00231         CamerasExporter ce(&sw, this->export_settings);
00232         ce.exportCameras(sce);
00233     }
00234     
00235     // <library_lights>
00236     if(has_object_type(sce, OB_LAMP)) {
00237         LightsExporter le(&sw, this->export_settings);
00238         le.exportLights(sce);
00239     }
00240 
00241     // <library_images>
00242     ImagesExporter ie(&sw, this->export_settings);
00243     ie.exportImages(sce);
00244     
00245     // <library_effects>
00246     EffectsExporter ee(&sw, this->export_settings);
00247     ee.exportEffects(sce);
00248     
00249     // <library_materials>
00250     MaterialsExporter me(&sw, this->export_settings);
00251     me.exportMaterials(sce);
00252 
00253     // <library_geometries>
00254     if(has_object_type(sce, OB_MESH)) {
00255         GeometryExporter ge(&sw, this->export_settings);
00256         ge.exportGeom(sce);
00257     }
00258 
00259     // <library_animations>
00260     AnimationExporter ae(&sw);
00261     ae.exportAnimations(sce);
00262 
00263     // <library_controllers>
00264     ArmatureExporter arm_exporter(&sw, this->export_settings);
00265     if(has_object_type(sce, OB_ARMATURE)) {
00266         arm_exporter.export_controllers(sce);
00267     }
00268 
00269     // <library_visual_scenes>
00270     SceneExporter se(&sw, &arm_exporter, this->export_settings);
00271     se.exportScene(sce);
00272     
00273     // <scene>
00274     std::string scene_name(translate_id(id_name(sce)));
00275     COLLADASW::Scene scene(&sw, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING,
00276                                                scene_name));
00277     scene.add();
00278     
00279     // close <Collada>
00280     sw.endDocument();
00281 
00282 }
00283 
00284 void DocumentExporter::exportScenes(const char* filename)
00285 {
00286 }
00287 
00288 /*
00289 
00290 NOTES:
00291 
00292 * AnimationExporter::sample_animation enables all curves on armature, this is undesirable for a user
00293 
00294  */