Blender V5.3
image_sequence.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_listbase.hh"
14#include "BLI_path_utils.hh"
15#include "BLI_string.hh"
16#include "BLI_utildefines.hh"
17
19
20#include "RNA_access.hh"
21
22#include "BKE_image.hh"
23
24#include "ED_image.hh"
25
26namespace blender {
27
38 StringRefNull root_path,
39 wmOperator *op,
40 ListBaseT<ImageFrameRange> *ranges,
41 bool *r_was_relative)
42{
43 char dir[FILE_MAX];
44 const bool do_frame_range = RNA_boolean_get(op->ptr, "use_sequence_detection");
45 ImageFrameRange *range = nullptr;
46 int range_first_frame = 0;
47 /* Track when a new series of files are found that aren't compatible with the previous file. */
48 char base_head[FILE_MAX], base_tail[FILE_MAX];
49
50 RNA_string_get(op->ptr, "directory", dir);
51 /* Make absolute so we can be sure a relative path is always `root_path` relative. */
52 BLI_path_abs(dir, blendfile_path.c_str());
53 /* Operators using `ED_image_filesel_detect_sequences` should have a `relative_path` option. */
54 BLI_assert(RNA_struct_find_property(op->ptr, "relative_path"));
55 if (RNA_boolean_get(op->ptr, "relative_path")) {
56 BLI_path_rel(dir, root_path.c_str());
57 }
58
59 RNA_BEGIN (op->ptr, itemptr, "files") {
60 char head[FILE_MAX], tail[FILE_MAX];
61 ushort digits;
62 char *filename = RNA_string_get_alloc(&itemptr, "name", nullptr, 0, nullptr);
63 ImageFrame *frame = MEM_new_zeroed<ImageFrame>("image_frame");
64
65 /* use the first file in the list as base filename */
67 filename, head, sizeof(head), tail, sizeof(tail), &digits);
68
69 /* still in the same sequence */
70 if (do_frame_range && (range != nullptr) && STREQLEN(base_head, head, FILE_MAX) &&
71 STREQLEN(base_tail, tail, FILE_MAX))
72 {
73 /* Set filepath to first frame in the range. */
74 if (frame->framenr < range_first_frame) {
75 BLI_path_join(range->filepath, sizeof(range->filepath), dir, filename);
76 range_first_frame = frame->framenr;
77 }
78 }
79 else {
80 /* start a new frame range */
81 range = MEM_new_zeroed<ImageFrameRange>(__func__);
82 BLI_path_join(range->filepath, sizeof(range->filepath), dir, filename);
83 BLI_addtail(ranges, range);
84
85 STRNCPY(base_head, head);
86 STRNCPY(base_tail, tail);
87
88 range_first_frame = frame->framenr;
89 }
90
91 BLI_addtail(&range->frames, frame);
92 MEM_delete(filename);
93 }
94 RNA_END;
95
96 *r_was_relative = BLI_path_is_rel(dir);
97}
98
99static int image_cmp_frame(const void *a, const void *b)
100{
101 const ImageFrame *frame_a = static_cast<const ImageFrame *>(a);
102 const ImageFrame *frame_b = static_cast<const ImageFrame *>(b);
103
104 if (frame_a->framenr < frame_b->framenr) {
105 return -1;
106 }
107 if (frame_a->framenr > frame_b->framenr) {
108 return 1;
109 }
110 return 0;
111}
112
117static void image_detect_frame_range(ImageFrameRange *range, const bool detect_udim)
118{
119 /* UDIM detection relies on the paths resolving on the file-system (being absolute). */
121
122 /* UDIM */
123 if (detect_udim) {
124 int udim_start, udim_range;
126 range->filepath, &range->udim_tiles, &udim_start, &udim_range);
127
128 if (range->udims_detected) {
129 range->offset = udim_start;
130 range->length = udim_range;
131 return;
132 }
133 }
134
135 /* Image Sequence */
137
138 ImageFrame *frame = range->frames.first();
139 if (frame != nullptr) {
140 int frame_curr = frame->framenr;
141 range->offset = frame_curr;
142
143 while (frame != nullptr && (frame->framenr == frame_curr)) {
144 frame_curr++;
145 frame = frame->next;
146 }
147
148 range->length = frame_curr - range->offset;
149 }
150 else {
151 range->length = 1;
152 range->offset = 0;
153 }
154
155 ImageFrame *frame_last = range->frames.last();
156 if (frame_last != nullptr) {
157 range->max_framenr = frame_last->framenr;
158 }
159}
160
161ListBaseT<ImageFrameRange> ED_image_filesel_detect_sequences(StringRefNull blendfile_path,
162 StringRefNull root_path,
163 wmOperator *op,
164 const bool detect_udim)
165{
166 ListBaseT<ImageFrameRange> ranges;
167 ranges.clear_no_delete();
168
169 bool was_relative = false;
170 StringRefNull base_path = blendfile_path;
171
172 /* File browser. */
173 if (RNA_struct_property_is_set(op->ptr, "directory") &&
174 RNA_struct_property_is_set(op->ptr, "files"))
175 {
176 image_sequence_get_frame_ranges(blendfile_path, root_path, op, &ranges, &was_relative);
177 /* The `root_path` will be used as the blend-file path, if it is relative. */
178 base_path = root_path;
179 }
180 /* File-path property for drag & drop etc. */
181 else {
182 char filepath[FILE_MAX];
183 RNA_string_get(op->ptr, "filepath", filepath);
184
185 /* Treat the file-path as a single selected file, equivalent to the `directory` & `files`. */
186 const char *filename = BLI_path_basename(filepath);
187 if (filename[0]) {
189 BLI_addtail(&ranges, range);
190
191 STRNCPY(range->filepath, filepath);
192
193 /* Add a single frame so callers building per-frame data have a frame to load.
194 * The frame number is decoded from the file-part (matching the `files` case). */
195 char head[FILE_MAX], tail[FILE_MAX];
196 ushort digits;
197 ImageFrame *frame = MEM_new_zeroed<ImageFrame>("image_frame");
199 filename, head, sizeof(head), tail, sizeof(tail), &digits);
200 BLI_addtail(&range->frames, frame);
201
202 was_relative = BLI_path_is_rel(filepath);
203 }
204 }
205
206 for (ImageFrameRange &range : ranges) {
207 /* Expand the path if necessary so UDIM files can be resolved. */
208 if (was_relative) {
209 BLI_path_abs(range.filepath, base_path.c_str());
210 }
211 image_detect_frame_range(&range, detect_udim);
212 if (was_relative) {
213 BLI_path_rel(range.filepath, root_path.c_str());
214 }
215 }
216
217 return ranges;
218}
219
220} // namespace blender
#define FILE_MAX
#define BLI_path_join(...)
Read Blender memory allocation and freeing.
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define STREQLEN(a, b, n)
constexpr const char * c_str() const
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
static void image_detect_frame_range(ImageFrameRange *range, const bool detect_udim)
unsigned short ushort
Definition BKE_text.h:56
char * RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
ListBaseT< ImageFrameRange > ED_image_filesel_detect_sequences(StringRefNull blendfile_path, StringRefNull root_path, wmOperator *op, bool detect_udim)
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
static int image_cmp_frame(const void *a, const void *b)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
bool BKE_image_get_tile_info(char *filepath, ListBaseT< LinkData > *tiles, int *r_tile_start, int *r_tile_range)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void image_sequence_get_frame_ranges(StringRefNull blendfile_path, StringRefNull root_path, wmOperator *op, ListBaseT< ImageFrameRange > *ranges, bool *r_was_relative)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_sequence_decode(const char *path, char *head, size_t head_maxncpy, char *tail, size_t tail_maxncpy, unsigned short *r_digits_len)
Definition path_utils.cc:61
std::string RNA_string_get(PointerRNA *ptr, const char *name)
char filepath[FILE_MAX]
Definition ED_image.hh:214
ListBaseT< ImageFrame > frames
Definition ED_image.hh:225
ListBaseT< LinkData > udim_tiles
Definition ED_image.hh:222
ImageFrame * next
Definition ED_image.hh:229