29
29
#include "COLLADABUURI.h"
30
30
#include "COLLADASWImage.h"
32
#include "ImageExporter.h"
33
#include "MaterialExporter.h"
35
33
#include "DNA_texture_types.h"
34
#include "DNA_image_types.h"
35
#include "DNA_meshdata_types.h"
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"
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"
48
#include "ImageExporter.h"
49
#include "MaterialExporter.h"
44
52
ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryImages(sw), export_settings(export_settings)
56
void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
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();
62
if (not_yet_exported) {
64
ImBuf *imbuf = BKE_image_acquire_ibuf(image, NULL, NULL);
66
fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->name);
70
bool is_dirty = imbuf->userflags & IB_BITMAPDIRTY;
72
ImageFormatData imageFormat;
73
BKE_imbuf_to_image_format(&imageFormat, imbuf);
75
short image_source = image->source;
76
bool is_generated = image_source == IMA_SRC_GENERATED;
77
bool is_packed = image->packedfile != NULL;
79
char export_path[FILE_MAX];
80
char source_path[FILE_MAX];
81
char export_dir[FILE_MAX];
82
char export_file[FILE_MAX];
84
// Destination folder for exported assets
85
BLI_split_dir_part(this->export_settings->filepath, export_dir, sizeof(export_dir));
87
if (is_generated || is_dirty || use_copies || is_packed) {
89
// make absolute destination path
91
BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
92
BKE_add_image_extension(export_file, &imageFormat);
94
BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);
96
// make dest directory if it doesn't exist
97
BLI_make_existing_file(export_path);
100
if (is_generated || is_dirty || is_packed) {
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.
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);
110
BLI_strncpy(export_path, export_file, sizeof(export_path));
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);
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).
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);
134
BLI_strncpy(export_path, export_file, sizeof(export_path));
139
// Do not make any copies, but use the source path directly as reference
140
// to the original image
142
BLI_strncpy(export_path, source_path, sizeof(export_path));
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 */
148
fprintf(stdout, "Collada export: Added image: %s\n", export_file);
149
mImages.push_back(translated_name);
151
BKE_image_release_ibuf(image, imbuf, NULL);
155
void ImagesExporter::export_UV_Images()
157
std::set<Image *> uv_textures;
159
bool use_texture_copies = this->export_settings->use_texture_copies;
160
bool active_uv_only = this->export_settings->active_uv_only;
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)
172
MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data;
173
for (int j = 0; j < me->totpoly; j++, txface++) {
175
Image *ima = txface->tpage;
179
bool not_in_list = uv_textures.find(ima) == uv_textures.end();
181
uv_textures.insert(ima);
182
export_UV_Image(ima, use_texture_copies);
47
193
bool ImagesExporter::hasImages(Scene *sce)
49
Base *base = (Base *)sce->base.first;
52
Object *ob= base->object;
197
for (node = this->export_settings->export_set; node; node = node->next) {
198
Object *ob = (Object *)node->link;
54
for (a = 0; a < ob->totcol; a++)
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);
58
203
// no material, but check all of the slots
59
204
if (!ma) continue;
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);
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;
72
230
void ImagesExporter::exportImages(Scene *sce)
77
mf.forEachMaterialInScene<ImagesExporter>(sce, *this, this->export_settings->selected);
235
if (this->export_settings->include_material_textures) {
236
mf.forEachMaterialInExportSet<ImagesExporter>(sce, *this, this->export_settings->export_set);
239
if (this->export_settings->include_uv_textures) {
83
248
void ImagesExporter::operator()(Material *ma, Object *ob)
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) {
90
255
Image *image = mtex->tex->ima;
91
std::string name(id_name(image));
92
name = translate_id(name);
98
BLI_split_dir_part(this->export_settings->filepath, dir, sizeof(dir));
100
BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.main->name, image->name, dir);
102
if (abs[0] != '\0') {
104
// make absolute source path
105
BLI_strncpy(src, image->name, sizeof(src));
106
BLI_path_abs(src, G.main->name);
108
// make dest directory if it doesn't exist
109
BLI_make_existing_file(abs);
111
if (BLI_copy(src, abs) != 0) {
112
fprintf(stderr, "Cannot copy image to file's directory.\n");
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 */
120
mImages.push_back(name);
256
export_UV_Image(image, use_texture_copies);