3926cb48253ca9029c4848e49fb0993c415ca5d6
[blender-staging.git] / source / blender / collada / ImageExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/ImageExporter.cpp
25  *  \ingroup collada
26  */
27
28
29 #include "COLLADABUURI.h"
30 #include "COLLADASWImage.h"
31
32 #include "ImageExporter.h"
33 #include "MaterialExporter.h"
34
35 #include "DNA_texture_types.h"
36
37 #include "BKE_global.h"
38 #include "BKE_main.h"
39 #include "BKE_utildefines.h"
40 #include "BLI_fileops.h"
41 #include "BLI_path_util.h"
42 #include "BLI_string.h"
43
44 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryImages(sw), export_settings(export_settings)
45 {}
46
47 bool ImagesExporter::hasImages(Scene *sce)
48 {
49         Base *base = (Base *)sce->base.first;
50         
51         while(base) {
52                 Object *ob= base->object;
53                 int a;
54                 for(a = 0; a < ob->totcol; a++)
55                 {
56                         Material *ma = give_current_material(ob, a+1);
57
58                         // no material, but check all of the slots
59                         if (!ma) continue;
60                         int b;
61                         for (b = 0; b < MAX_MTEX; b++) {
62                                 MTex *mtex = ma->mtex[b];
63                                 if (mtex && mtex->tex && mtex->tex->ima) return true;
64                         }
65
66                 }
67                 base= base->next;
68         }
69         return false;
70 }
71
72 void ImagesExporter::exportImages(Scene *sce)
73 {
74         if(hasImages(sce)) {
75                 openLibrary();
76                 MaterialFunctor mf;
77                 mf.forEachMaterialInScene<ImagesExporter>(sce, *this, this->export_settings->selected);
78
79                 closeLibrary();
80         }
81 }
82
83 void ImagesExporter::operator()(Material *ma, Object *ob)
84 {
85         int a;
86         for (a = 0; a < MAX_MTEX; a++) {
87                 MTex *mtex = ma->mtex[a];
88                 if (mtex && mtex->tex && mtex->tex->ima) {
89
90                         Image *image = mtex->tex->ima;
91                         std::string name(id_name(image));
92                         name = translate_id(name);
93                         char rel[FILE_MAX];
94                         char abs[FILE_MAX];
95                         char src[FILE_MAX];
96                         char dir[FILE_MAX];
97                         
98                         BLI_split_dir_part(this->export_settings->filepath, dir, sizeof(dir));
99
100                         BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.main->name, image->name, dir);
101
102                         if (abs[0] != '\0') {
103
104                                 // make absolute source path
105                                 BLI_strncpy(src, image->name, sizeof(src));
106                                 BLI_path_abs(src, G.main->name);
107
108                                 // make dest directory if it doesn't exist
109                                 BLI_make_existing_file(abs);
110                         
111                                 if (BLI_copy(src, abs) != 0) {
112                                         fprintf(stderr, "Cannot copy image to file's directory. \n");
113                                 }
114                         } 
115                         
116                         if (find(mImages.begin(), mImages.end(), name) == mImages.end()) {
117                                 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 */
118                                 img.add(mSW);
119
120                                 mImages.push_back(name);
121                         }
122                 }
123         }
124 }