Blender V2.61 - r43446

ImageExporter.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  *                 Nathan Letwory
00020  *
00021  * ***** END GPL LICENSE BLOCK *****
00022  */
00023 
00029 #include "COLLADABUURI.h"
00030 #include "COLLADASWImage.h"
00031 
00032 #include "ImageExporter.h"
00033 #include "MaterialExporter.h"
00034 
00035 #include "DNA_texture_types.h"
00036 
00037 #include "BKE_global.h"
00038 #include "BKE_main.h"
00039 #include "BKE_utildefines.h"
00040 #include "BLI_fileops.h"
00041 #include "BLI_path_util.h"
00042 #include "BLI_string.h"
00043 
00044 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryImages(sw), export_settings(export_settings)
00045 {}
00046 
00047 bool ImagesExporter::hasImages(Scene *sce)
00048 {
00049     Base *base = (Base *)sce->base.first;
00050     
00051     while(base) {
00052         Object *ob= base->object;
00053         int a;
00054         for(a = 0; a < ob->totcol; a++)
00055         {
00056             Material *ma = give_current_material(ob, a+1);
00057 
00058             // no material, but check all of the slots
00059             if (!ma) continue;
00060             int b;
00061             for (b = 0; b < MAX_MTEX; b++) {
00062                 MTex *mtex = ma->mtex[b];
00063                 if (mtex && mtex->tex && mtex->tex->ima) return true;
00064             }
00065 
00066         }
00067         base= base->next;
00068     }
00069     return false;
00070 }
00071 
00072 void ImagesExporter::exportImages(Scene *sce)
00073 {
00074     if(hasImages(sce)) {
00075         openLibrary();
00076         MaterialFunctor mf;
00077         mf.forEachMaterialInScene<ImagesExporter>(sce, *this, this->export_settings->selected);
00078 
00079         closeLibrary();
00080     }
00081 }
00082 
00083 void ImagesExporter::operator()(Material *ma, Object *ob)
00084 {
00085     int a;
00086     for (a = 0; a < MAX_MTEX; a++) {
00087         MTex *mtex = ma->mtex[a];
00088         if (mtex && mtex->tex && mtex->tex->ima) {
00089 
00090             Image *image = mtex->tex->ima;
00091             std::string name(id_name(image));
00092             name = translate_id(name);
00093             char rel[FILE_MAX];
00094             char abs[FILE_MAX];
00095             char src[FILE_MAX];
00096             char dir[FILE_MAX];
00097             
00098             BLI_split_dir_part(this->export_settings->filepath, dir, sizeof(dir));
00099 
00100             BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.main->name, image->name, dir);
00101 
00102             if (abs[0] != '\0') {
00103 
00104                 // make absolute source path
00105                 BLI_strncpy(src, image->name, sizeof(src));
00106                 BLI_path_abs(src, G.main->name);
00107 
00108                 // make dest directory if it doesn't exist
00109                 BLI_make_existing_file(abs);
00110             
00111                 if (BLI_copy(src, abs) != 0) {
00112                     fprintf(stderr, "Cannot copy image to file's directory. \n");
00113                 }
00114             } 
00115             
00116             if (find(mImages.begin(), mImages.end(), name) == mImages.end()) {
00117                 COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(rel)), name, name); /* set name also to mNameNC. This helps other viewers import files exported from Blender better */
00118                 img.add(mSW);
00119 
00120                 mImages.push_back(name);
00121             }
00122         }
00123     }
00124 }