Blender V5.3
abc_archive.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_archive.h"
10
11#include "BKE_blender_version.h"
12#include "BKE_main.hh"
13
14#include "DNA_scene_types.h"
15
16#include <Alembic/Abc/ArchiveInfo.h>
17#include <Alembic/Abc/ErrorHandler.h>
18#include <Alembic/Abc/Foundation.h>
19#include <Alembic/Abc/OArchive.h>
20#include <Alembic/AbcCoreAbstract/MetaData.h>
21#include <Alembic/AbcCoreAbstract/TimeSampling.h>
22#include <Alembic/AbcCoreAbstract/TimeSamplingType.h>
23#include <Alembic/AbcCoreOgawa/ReadWrite.h>
24#include <Alembic/AbcGeom/ArchiveBounds.h>
25
26#ifdef WIN32
27# include "BLI_path_utils.hh"
28# include "BLI_string.hh"
29
30# include "utfconv.hh"
31#endif
32
34
35using Alembic::Abc::ErrorHandler;
36using Alembic::Abc::kWrapExisting;
37using Alembic::Abc::MetaData;
38using Alembic::Abc::OArchive;
39using Alembic::Abc::TimeSampling;
40using Alembic::Abc::TimeSamplingPtr;
41using Alembic::Abc::TimeSamplingType;
42
43static MetaData create_abc_metadata(const Main *bmain, double scene_fps)
44{
45 MetaData abc_metadata;
46
47 std::string abc_user_description(bmain->filepath);
48 if (abc_user_description.empty()) {
49 abc_user_description = "unknown";
50 }
51
52 abc_metadata.set(Alembic::Abc::kApplicationNameKey, "Blender");
53 abc_metadata.set(Alembic::Abc::kUserDescriptionKey, abc_user_description);
54 abc_metadata.set("blender_version", std::string("v") + BKE_blender_version_string());
55 abc_metadata.set("FramesPerTimeUnit", std::to_string(scene_fps));
56
57 time_t raw_time;
58 time(&raw_time);
59 char buffer[128];
60
61#if defined _WIN32 || defined _WIN64
62 ctime_s(buffer, 128, &raw_time);
63#else
64 ctime_r(&raw_time, buffer);
65#endif
66
67 const std::size_t buffer_len = strlen(buffer);
68 if (buffer_len > 0 && buffer[buffer_len - 1] == '\n') {
69 buffer[buffer_len - 1] = '\0';
70 }
71
72 abc_metadata.set(Alembic::Abc::kDateWrittenKey, buffer);
73 return abc_metadata;
74}
75
76static OArchive *create_archive(std::ofstream *abc_ostream,
77 const std::string &filepath,
78 MetaData &abc_metadata)
79{
80 /* Use stream to support unicode character paths on Windows. */
81#ifdef WIN32
82 char filepath_cstr[FILE_MAX];
83 BLI_strncpy(filepath_cstr, filepath.c_str(), FILE_MAX);
84
85 UTF16_ENCODE(filepath_cstr);
86 std::wstring wstr(filepath_cstr_16);
87 abc_ostream->open(wstr.c_str(), std::ios::out | std::ios::binary);
88 UTF16_UN_ENCODE(filepath_cstr);
89#else
90 abc_ostream->open(filepath, std::ios::out | std::ios::binary);
91#endif
92
93 ErrorHandler::Policy policy = ErrorHandler::kThrowPolicy;
94
95 Alembic::AbcCoreOgawa::WriteArchive archive_writer;
96 return new OArchive(archive_writer(abc_ostream, abc_metadata), kWrapExisting, policy);
97}
98
99/* Construct list of shutter samples.
100 *
101 * These are taken from the interval [shutter open, shutter close),
102 * uniformly sampled with 'nr_of_samples' samples.
103 *
104 * TODO(Sybren): test that the above interval is indeed half-open.
105 *
106 * If 'time_relative' is true, samples are returned as time (in seconds) from params.frame_start.
107 * If 'time_relative' is false, samples are returned as fractional frames from 0.
108 */
109static void get_shutter_samples(double scene_fps,
110 const AlembicExportParams &params,
111 int nr_of_samples,
112 bool time_relative,
113 std::vector<double> &r_samples)
114{
115 double frame_offset = time_relative ? params.frame_start : 0.0;
116 double time_factor = time_relative ? scene_fps : 1.0;
117 double shutter_open = params.shutter_open;
118 double shutter_close = params.shutter_close;
119 double time_inc = (shutter_close - shutter_open) / nr_of_samples;
120
121 /* sample between shutter open & close */
122 for (int sample = 0; sample < nr_of_samples; sample++) {
123 double sample_time = shutter_open + time_inc * sample;
124 double time = (frame_offset + sample_time) / time_factor;
125
126 r_samples.push_back(time);
127 }
128}
129
130static TimeSamplingPtr create_time_sampling(double scene_fps,
131 const AlembicExportParams &params,
132 int nr_of_samples)
133{
134 std::vector<double> samples;
135
136 if (params.frame_start == params.frame_end) {
137 return TimeSamplingPtr(new TimeSampling()); // NOLINT: modernize-make-shared
138 }
139
140 get_shutter_samples(scene_fps, params, nr_of_samples, true, samples);
141
142 TimeSamplingType ts(uint32_t(samples.size()), 1.0 / scene_fps);
143 return TimeSamplingPtr(new TimeSampling(ts, samples)); // NOLINT: modernize-make-shared
144}
145
146static void get_frames(double scene_fps,
147 const AlembicExportParams &params,
148 uint nr_of_samples,
149 std::set<double> &r_frames)
150{
151 /* Get one set of shutter samples, then add those around each frame to export. */
152 std::vector<double> shutter_samples;
153 get_shutter_samples(scene_fps, params, nr_of_samples, false, shutter_samples);
154
155 for (double frame = params.frame_start; frame <= params.frame_end; frame += 1.0) {
156 for (size_t j = 0; j < nr_of_samples; j++) {
157 r_frames.insert(frame + shutter_samples[j]);
158 }
159 }
160}
161
162/* ****************************************************************** */
163
165 const Scene *scene,
166 AlembicExportParams params,
167 const std::string &filepath)
169{
170 double scene_fps = scene->frames_per_second();
171 MetaData abc_metadata = create_abc_metadata(bmain, scene_fps);
172
173 /* Create the Archive. */
174 archive = create_archive(&abc_ostream_, filepath, abc_metadata);
175
176 /* Create time samples for transforms and shapes. */
177 TimeSamplingPtr ts_xform;
178 TimeSamplingPtr ts_shapes;
179
180 ts_xform = create_time_sampling(scene_fps, params, params.frame_samples_xform);
181 time_sampling_index_transforms_ = archive->addTimeSampling(*ts_xform);
182
183 const bool export_animation = params.frame_start != params.frame_end;
184 if (!export_animation || params.frame_samples_shape == params.frame_samples_xform) {
185 ts_shapes = ts_xform;
186 time_sampling_index_shapes_ = time_sampling_index_transforms_;
187 }
188 else {
189 ts_shapes = create_time_sampling(scene_fps, params, params.frame_samples_shape);
190 time_sampling_index_shapes_ = archive->addTimeSampling(*ts_shapes);
191 }
192
193 /* Construct the frames to export. */
194 get_frames(scene_fps, params, params.frame_samples_xform, xform_frames_);
195 get_frames(scene_fps, params, params.frame_samples_shape, shape_frames_);
196
197 /* Merge all frames to get the final set of frames to export. */
198 export_frames_.insert(xform_frames_.begin(), xform_frames_.end());
199 export_frames_.insert(shape_frames_.begin(), shape_frames_.end());
200
201 abc_archive_bbox_ = Alembic::AbcGeom::CreateOArchiveBounds(*archive,
202 time_sampling_index_transforms_);
203}
204
206{
207 delete archive;
208}
209
211{
212 return time_sampling_index_transforms_;
213}
214
216{
217 return time_sampling_index_shapes_;
218}
219
220ABCArchive::Frames::const_iterator ABCArchive::frames_begin() const
221{
222 return export_frames_.begin();
223}
224ABCArchive::Frames::const_iterator ABCArchive::frames_end() const
225{
226 return export_frames_.end();
227}
229{
230 return export_frames_.size();
231}
232
233bool ABCArchive::is_xform_frame(double frame) const
234{
235 return xform_frames_.contains(frame);
236}
237bool ABCArchive::is_shape_frame(double frame) const
238{
239 return shape_frames_.contains(frame);
240}
242{
243 ExportSubset subset;
244 subset.transforms = is_xform_frame(frame);
245 subset.shapes = is_shape_frame(frame);
246 return subset;
247}
248
250{
251 abc_archive_bbox_.set(bounds);
252}
253
254} // namespace blender::io::alembic
#define FILE_MAX
Frames::const_iterator frames_end() const
bool is_xform_frame(double frame) const
ExportSubset export_subset_for_frame(double frame) const
bool is_shape_frame(double frame) const
Frames::const_iterator frames_begin() const
uint32_t time_sampling_index_shapes() const
ABCArchive(const Main *bmain, const Scene *scene, AlembicExportParams params, const std::string &filepath)
Alembic::Abc::OArchive * archive
Definition abc_archive.h:36
void update_bounding_box(const Imath::Box3d &bounds)
uint32_t time_sampling_index_transforms() const
static void get_frames(double scene_fps, const AlembicExportParams &params, uint nr_of_samples, std::set< double > &r_frames)
static MetaData create_abc_metadata(const Main *bmain, double scene_fps)
static OArchive * create_archive(std::ofstream *abc_ostream, const std::string &filepath, MetaData &abc_metadata)
static TimeSamplingPtr create_time_sampling(double scene_fps, const AlembicExportParams &params, int nr_of_samples)
static void get_shutter_samples(double scene_fps, const AlembicExportParams &params, int nr_of_samples, bool time_relative, std::vector< double > &r_samples)
unsigned int uint
Definition BKE_text.h:55
unsigned int uint32_t
Definition BKE_text.h:81
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
const char * BKE_blender_version_string()
Definition blender.cc:147
char filepath[1024]
Definition BKE_main.hh:267
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84