Blender V4.3
ImageExporter.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "COLLADABUURI.h"
10#include "COLLADASWImage.h"
11
12#include "DNA_image_types.h"
13#include "DNA_texture_types.h"
14
15#include "BKE_customdata.hh"
16#include "BKE_global.hh"
17#include "BKE_image.hh"
18#include "BKE_image_format.hh"
19#include "BKE_main.hh"
20#include "BKE_mesh.hh"
21
22#include "BLI_fileops.h"
23#include "BLI_path_utils.hh"
24#include "BLI_string.h"
25
26#include "IMB_imbuf_types.hh"
27
28#include "ImageExporter.h"
29#include "MaterialExporter.h"
30
31ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw,
32 BCExportSettings &export_settings,
33 KeyImageMap &key_image_map)
34 : COLLADASW::LibraryImages(sw), export_settings(export_settings), key_image_map(key_image_map)
35{
36 /* pass */
37}
38
39void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
40{
41 std::string name(id_name(image));
42 std::string translated_name(translate_id(name));
43
44 ImBuf *imbuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
45 if (!imbuf) {
46 fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->filepath);
47 return;
48 }
49
50 bool is_dirty = BKE_image_is_dirty(image);
51
52 ImageFormatData imageFormat;
53 BKE_image_format_from_imbuf(&imageFormat, imbuf);
54
55 short image_source = image->source;
56 bool is_generated = image_source == IMA_SRC_GENERATED;
57 bool is_packed = BKE_image_has_packedfile(image);
58
59 char export_path[FILE_MAX];
60 char source_path[FILE_MAX];
61 char export_dir[FILE_MAX];
62 char export_file[FILE_MAX];
63
64 /* Destination folder for exported assets */
65 BLI_path_split_dir_part(this->export_settings.get_filepath(), export_dir, sizeof(export_dir));
66
67 if (is_generated || is_dirty || use_copies || is_packed) {
68
69 /* make absolute destination path */
70
71 STRNCPY(export_file, name.c_str());
72 BKE_image_path_ext_from_imformat_ensure(export_file, sizeof(export_file), &imageFormat);
73
74 BLI_path_join(export_path, sizeof(export_path), export_dir, export_file);
75
77 }
78
79 if (is_generated || is_dirty || is_packed) {
80
81 /* This image in its current state only exists in Blender memory.
82 * So we have to export it. The export will keep the image state intact,
83 * so the exported file will not be associated with the image. */
84
85 if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == false) {
86 fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
87 return;
88 }
89 STRNCPY(export_path, export_file);
90 }
91 else {
92
93 /* make absolute source path */
94 STRNCPY(source_path, image->filepath);
95 BLI_path_abs(source_path, ID_BLEND_PATH_FROM_GLOBAL(&image->id));
96 BLI_path_normalize(source_path);
97
98 if (use_copies) {
99
100 /* This image is already located on the file system.
101 * But we want to create copies here.
102 * To move images into the same export directory.
103 * NOTE: If an image is already located in the export folder,
104 * then skip the copy (as it would result in a file copy error). */
105
106 if (BLI_path_cmp(source_path, export_path) != 0) {
107 if (BLI_copy(source_path, export_path) != 0) {
108 fprintf(stderr,
109 "Collada export: Cannot copy image:\n source:%s\ndest :%s\n",
110 source_path,
111 export_path);
112 return;
113 }
114 }
115
116 STRNCPY(export_path, export_file);
117 }
118 else {
119
120 /* Do not make any copies, but use the source path directly as reference
121 * to the original image */
122
123 STRNCPY(export_path, source_path);
124 }
125 }
126
127 /* Set name also to mNameNC.
128 * This helps other viewers import files exported from Blender better. */
129 COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)),
130 translated_name,
131 translated_name);
132 img.add(mSW);
133 fprintf(stdout, "Collada export: Added image: %s\n", export_file);
134
135 BKE_image_release_ibuf(image, imbuf, nullptr);
136}
137
139{
140 bool use_texture_copies = this->export_settings.get_use_texture_copies();
141 openLibrary();
142
143 KeyImageMap::iterator iter;
144 for (iter = key_image_map.begin(); iter != key_image_map.end(); iter++) {
145
146 Image *image = iter->second;
147 std::string uid(id_name(image));
148 std::string key = translate_id(uid);
149
150 export_UV_Image(image, use_texture_copies);
151 }
152
153 closeLibrary();
154}
CustomData interface, see also DNA_customdata_types.h.
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
bool BKE_image_is_dirty(Image *image)
bool BKE_image_has_packedfile(const Image *image)
bool BKE_imbuf_write_as(ImBuf *ibuf, const char *filepath, const ImageFormatData *imf, bool save_copy)
void BKE_image_format_from_imbuf(ImageFormatData *im_format, const ImBuf *imbuf)
int BKE_image_path_ext_from_imformat_ensure(char *filepath, size_t filepath_maxncpy, const ImageFormatData *im_format)
File and directory operations.
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:429
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
#define FILE_MAX
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
#define BLI_path_join(...)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
#define BLI_path_cmp
#define STRNCPY(dst, src)
Definition BLI_string.h:593
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:649
@ IMA_SRC_GENERATED
Contains defines and structs used throughout the imbuf module.
ImagesExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings, KeyImageMap &key_image_map)
void exportImages(Scene *sce)
std::string translate_id(const char *idString)
std::string id_name(void *id)
std::map< std::string, Image * > KeyImageMap