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