~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/collada/ImageExporter.cpp

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "COLLADABUURI.h"
30
30
#include "COLLADASWImage.h"
31
31
 
32
 
#include "ImageExporter.h"
33
 
#include "MaterialExporter.h"
34
 
 
 
32
extern "C" {
35
33
#include "DNA_texture_types.h"
 
34
#include "DNA_image_types.h"
 
35
#include "DNA_meshdata_types.h"
36
36
 
 
37
#include "BKE_customdata.h" 
37
38
#include "BKE_global.h"
 
39
#include "BKE_image.h"
38
40
#include "BKE_main.h"
39
 
#include "BKE_utildefines.h"
 
41
#include "BKE_mesh.h"
40
42
#include "BLI_fileops.h"
41
43
#include "BLI_path_util.h"
42
44
#include "BLI_string.h"
 
45
#include "IMB_imbuf_types.h"
 
46
}
 
47
 
 
48
#include "ImageExporter.h"
 
49
#include "MaterialExporter.h"
 
50
 
43
51
 
44
52
ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryImages(sw), export_settings(export_settings)
45
 
{}
 
53
{
 
54
}
 
55
 
 
56
void ImagesExporter::export_UV_Image(Image *image, bool use_copies) 
 
57
{
 
58
        std::string name(id_name(image));
 
59
        std::string translated_name(translate_id(name));
 
60
        bool not_yet_exported = find(mImages.begin(), mImages.end(), translated_name) == mImages.end();
 
61
 
 
62
        if (not_yet_exported) {
 
63
 
 
64
                ImBuf *imbuf       = BKE_image_acquire_ibuf(image, NULL, NULL);
 
65
                if (!imbuf) {
 
66
                        fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->name);
 
67
                        return;
 
68
                }
 
69
 
 
70
                bool  is_dirty     = imbuf->userflags & IB_BITMAPDIRTY;
 
71
 
 
72
                ImageFormatData imageFormat;
 
73
                BKE_imbuf_to_image_format(&imageFormat, imbuf);
 
74
 
 
75
                short image_source = image->source;
 
76
                bool  is_generated = image_source == IMA_SRC_GENERATED;
 
77
                bool  is_packed    = image->packedfile != NULL;
 
78
 
 
79
                char export_path[FILE_MAX];
 
80
                char source_path[FILE_MAX];
 
81
                char export_dir[FILE_MAX];
 
82
                char export_file[FILE_MAX];
 
83
 
 
84
                // Destination folder for exported assets
 
85
                BLI_split_dir_part(this->export_settings->filepath, export_dir, sizeof(export_dir));
 
86
 
 
87
                if (is_generated || is_dirty || use_copies || is_packed) {
 
88
 
 
89
                        // make absolute destination path
 
90
 
 
91
                        BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
 
92
                        BKE_add_image_extension(export_file, &imageFormat);
 
93
 
 
94
                        BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);
 
95
 
 
96
                        // make dest directory if it doesn't exist
 
97
                        BLI_make_existing_file(export_path);
 
98
                }
 
99
 
 
100
                if (is_generated || is_dirty || is_packed) {
 
101
 
 
102
                        // This image in its current state only exists in Blender memory.
 
103
                        // So we have to export it. The export will keep the image state intact,
 
104
                        // so the exported file will not be associated with the image.
 
105
 
 
106
                        if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == 0) {
 
107
                                fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
 
108
                                return;
 
109
                        }
 
110
                        BLI_strncpy(export_path, export_file, sizeof(export_path));
 
111
                }
 
112
                else {
 
113
 
 
114
                        // make absolute source path
 
115
                        BLI_strncpy(source_path, image->name, sizeof(source_path));
 
116
                        BLI_path_abs(source_path, G.main->name);
 
117
                        BLI_cleanup_path(NULL, source_path);
 
118
 
 
119
                        if (use_copies) {
 
120
                        
 
121
                                // This image is already located on the file system.
 
122
                                // But we want to create copies here.
 
123
                                // To move images into the same export directory.
 
124
                                // Note: If an image is already located in the export folder,
 
125
                                // then skip the copy (as it would result in a file copy error).
 
126
 
 
127
                                if (BLI_path_cmp(source_path, export_path) != 0) {
 
128
                                        if (BLI_copy(source_path, export_path) != 0) {
 
129
                                                fprintf(stderr, "Collada export: Cannot copy image:\n source:%s\ndest :%s\n", source_path, export_path);
 
130
                                                return;
 
131
                                        }
 
132
                                }
 
133
 
 
134
                                BLI_strncpy(export_path, export_file, sizeof(export_path));
 
135
 
 
136
                        }
 
137
                        else {
 
138
 
 
139
                                // Do not make any copies, but use the source path directly as reference
 
140
                                // to the original image
 
141
 
 
142
                                BLI_strncpy(export_path, source_path, sizeof(export_path));
 
143
                        }
 
144
                }
 
145
 
 
146
                COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)), translated_name, translated_name); /* set name also to mNameNC. This helps other viewers import files exported from Blender better */
 
147
                img.add(mSW);
 
148
                fprintf(stdout, "Collada export: Added image: %s\n", export_file);
 
149
                mImages.push_back(translated_name);
 
150
 
 
151
                BKE_image_release_ibuf(image, imbuf, NULL);
 
152
        }
 
153
}
 
154
 
 
155
void ImagesExporter::export_UV_Images()
 
156
{
 
157
        std::set<Image *> uv_textures;
 
158
        LinkNode *node;
 
159
        bool use_texture_copies = this->export_settings->use_texture_copies;
 
160
        bool active_uv_only     = this->export_settings->active_uv_only;
 
161
 
 
162
        for (node = this->export_settings->export_set; node; node = node->next) {
 
163
                Object *ob = (Object *)node->link;
 
164
                if (ob->type == OB_MESH && ob->totcol) {
 
165
                        Mesh *me     = (Mesh *) ob->data;
 
166
                        BKE_mesh_tessface_ensure(me);
 
167
                        int active_uv_layer = CustomData_get_active_layer_index(&me->pdata, CD_MTEXPOLY);
 
168
                        for (int i = 0; i < me->pdata.totlayer; i++) {
 
169
                                if (me->pdata.layers[i].type == CD_MTEXPOLY) {
 
170
                                        if (!active_uv_only || active_uv_layer == i)
 
171
                                        {
 
172
                                                MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data;
 
173
                                                for (int j = 0; j < me->totpoly; j++, txface++) {
 
174
 
 
175
                                                        Image *ima = txface->tpage;
 
176
                                                        if (ima == NULL)
 
177
                                                                continue;
 
178
 
 
179
                                                        bool not_in_list = uv_textures.find(ima) == uv_textures.end();
 
180
                                                        if (not_in_list) {
 
181
                                                                        uv_textures.insert(ima);
 
182
                                                                        export_UV_Image(ima, use_texture_copies);
 
183
                                                        }
 
184
                                                }
 
185
                                        }
 
186
                                }
 
187
                        }
 
188
                }
 
189
        }
 
190
}
 
191
 
46
192
 
47
193
bool ImagesExporter::hasImages(Scene *sce)
48
194
{
49
 
        Base *base = (Base *)sce->base.first;
 
195
        LinkNode *node;
50
196
        
51
 
        while(base) {
52
 
                Object *ob= base->object;
 
197
        for (node = this->export_settings->export_set; node; node = node->next) {
 
198
                Object *ob = (Object *)node->link;
53
199
                int a;
54
 
                for (a = 0; a < ob->totcol; a++)
55
 
                {
56
 
                        Material *ma = give_current_material(ob, a+1);
 
200
                for (a = 0; a < ob->totcol; a++) {
 
201
                        Material *ma = give_current_material(ob, a + 1);
57
202
 
58
203
                        // no material, but check all of the slots
59
204
                        if (!ma) continue;
64
209
                        }
65
210
 
66
211
                }
67
 
                base= base->next;
 
212
                if (ob->type == OB_MESH) {
 
213
                        Mesh *me     = (Mesh *) ob->data;
 
214
                        BKE_mesh_tessface_ensure(me);
 
215
                        bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
 
216
                        if (has_uvs) {
 
217
                                int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
 
218
                                for (int a = 0; a < num_layers; a++) {
 
219
                                        MTFace *tface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, a);
 
220
                                        Image *img = tface->tpage;
 
221
                                        if (img) return true;
 
222
                                }
 
223
                        }
 
224
                }
 
225
 
68
226
        }
69
227
        return false;
70
228
}
71
229
 
72
230
void ImagesExporter::exportImages(Scene *sce)
73
231
{
74
 
        if (hasImages(sce)) {
75
 
                openLibrary();
76
 
                MaterialFunctor mf;
77
 
                mf.forEachMaterialInScene<ImagesExporter>(sce, *this, this->export_settings->selected);
78
 
 
79
 
                closeLibrary();
80
 
        }
 
232
        openLibrary();
 
233
 
 
234
        MaterialFunctor mf;
 
235
        if (this->export_settings->include_material_textures) {
 
236
                mf.forEachMaterialInExportSet<ImagesExporter>(sce, *this, this->export_settings->export_set);
 
237
        }
 
238
 
 
239
        if (this->export_settings->include_uv_textures) {
 
240
                export_UV_Images();
 
241
        }
 
242
 
 
243
        closeLibrary();
81
244
}
82
245
 
 
246
 
 
247
 
83
248
void ImagesExporter::operator()(Material *ma, Object *ob)
84
249
{
85
250
        int a;
 
251
        bool use_texture_copies = this->export_settings->use_texture_copies;
86
252
        for (a = 0; a < MAX_MTEX; a++) {
87
253
                MTex *mtex = ma->mtex[a];
88
254
                if (mtex && mtex->tex && mtex->tex->ima) {
89
 
 
90
255
                        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
 
                        }
 
256
                        export_UV_Image(image, use_texture_copies);
122
257
                }
123
258
        }
124
259
}