Blender  V2.59
ImageExporter.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ImageExporter.cpp 38079 2011-07-04 08:59:28Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
00021  *                 Nathan Letwory
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include "COLLADABUURI.h"
00032 #include "COLLADASWImage.h"
00033 
00034 #include "ImageExporter.h"
00035 #include "MaterialExporter.h"
00036 
00037 #include "DNA_texture_types.h"
00038 
00039 #include "BKE_global.h"
00040 #include "BKE_main.h"
00041 #include "BKE_utildefines.h"
00042 #include "BLI_fileops.h"
00043 #include "BLI_path_util.h"
00044 #include "BLI_string.h"
00045 
00046 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const char* filename) : COLLADASW::LibraryImages(sw), mfilename(filename)
00047 {}
00048 
00049 bool ImagesExporter::hasImages(Scene *sce)
00050 {
00051         Base *base = (Base *)sce->base.first;
00052         
00053         while(base) {
00054                 Object *ob= base->object;
00055                 int a;
00056                 for(a = 0; a < ob->totcol; a++)
00057                 {
00058                         Material *ma = give_current_material(ob, a+1);
00059 
00060                         // no material, but check all of the slots
00061                         if (!ma) continue;
00062                         int b;
00063                         for (b = 0; b < MAX_MTEX; b++) {
00064                                 MTex *mtex = ma->mtex[b];
00065                                 if (mtex && mtex->tex && mtex->tex->ima) return true;
00066                         }
00067 
00068                 }
00069                 base= base->next;
00070         }
00071         return false;
00072 }
00073 
00074 void ImagesExporter::exportImages(Scene *sce, bool export_selected)
00075 {
00076         if(hasImages(sce)) {
00077                 openLibrary();
00078                 MaterialFunctor mf;
00079                 mf.forEachMaterialInScene<ImagesExporter>(sce, *this, export_selected);
00080 
00081                 closeLibrary();
00082         }
00083 }
00084 
00085 void ImagesExporter::operator()(Material *ma, Object *ob)
00086 {
00087         int a;
00088         for (a = 0; a < MAX_MTEX; a++) {
00089                 MTex *mtex = ma->mtex[a];
00090                 if (mtex && mtex->tex && mtex->tex->ima) {
00091 
00092                         Image *image = mtex->tex->ima;
00093                         std::string name(id_name(image));
00094                         name = translate_id(name);
00095                         char rel[FILE_MAX];
00096                         char abs[FILE_MAX];
00097                         char src[FILE_MAX];
00098                         char dir[FILE_MAX];
00099                         
00100                         BLI_split_dirfile(mfilename, dir, NULL);
00101 
00102                         BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.main->name, image->name, dir);
00103 
00104                         if (abs[0] != '\0') {
00105 
00106                                 // make absolute source path
00107                                 BLI_strncpy(src, image->name, sizeof(src));
00108                                 BLI_path_abs(src, G.main->name);
00109 
00110                                 // make dest directory if it doesn't exist
00111                                 BLI_make_existing_file(abs);
00112                         
00113                                 if (BLI_copy_fileops(src, abs) != 0) {
00114                                         fprintf(stderr, "Cannot copy image to file's directory. \n");
00115                                 }
00116                         } 
00117                         
00118                         if (find(mImages.begin(), mImages.end(), name) == mImages.end()) {
00119                                 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 */
00120                                 img.add(mSW);
00121 
00122                                 mImages.push_back(name);
00123                         }
00124                 }
00125         }
00126 }