Blender V5.3
abc_reader_archive.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Kévin Dietrich. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "Alembic/Abc/ArchiveInfo.h"
12#include "Alembic/AbcCoreAbstract/MetaData.h"
13#include "Alembic/AbcCoreLayer/Read.h"
14#include "Alembic/AbcCoreOgawa/ReadWrite.h"
15
16#include "BKE_main.hh"
17
18#include "BLI_path_utils.hh"
19#include "BLI_string.hh"
20
21#ifdef WIN32
22# include "utfconv.hh"
23#endif
24
25#include <fstream>
26#include <vector>
27
28namespace blender {
29
30using Alembic::Abc::chrono_t;
31using Alembic::Abc::ErrorHandler;
32using Alembic::Abc::Exception;
33using Alembic::Abc::IArchive;
34using Alembic::Abc::kWrapExisting;
35using Alembic::Abc::MetaData;
36
37namespace io::alembic {
38
39static IArchive open_archive(const std::string &filename,
40 const std::vector<std::istream *> &input_streams)
41{
42 try {
43 Alembic::AbcCoreOgawa::ReadArchive archive_reader(input_streams);
44
45 return IArchive(archive_reader(filename), kWrapExisting, ErrorHandler::kThrowPolicy);
46 }
47 catch (const Exception &e) {
48 std::cerr << e.what() << '\n';
49
50 /* Inspect the file to see whether it's actually a HDF5 file. */
51 char header[4]; /* char(0x89) + "HDF" */
52 std::ifstream the_file(filename.c_str(), std::ios::in | std::ios::binary);
53 if (!the_file) {
54 std::cerr << "Unable to open " << filename << std::endl;
55 }
56 else if (!the_file.read(header, sizeof(header))) {
57 std::cerr << "Unable to read from " << filename << std::endl;
58 }
59 else if (strncmp(header + 1, "HDF", 3) != 0) {
60 std::cerr << filename << " has an unknown file format, unable to read." << std::endl;
61 }
62 else {
63 std::cerr << filename << " is in the obsolete HDF5 format, unable to read." << std::endl;
64 }
65
66 if (the_file.is_open()) {
67 the_file.close();
68 }
69 }
70
71 return IArchive();
72}
73
74ArchiveReader *ArchiveReader::get(const Main *bmain, const std::vector<const char *> &filenames)
75{
76 std::vector<ArchiveReader *> readers;
77
78 for (const char *filename : filenames) {
79 ArchiveReader *reader = new ArchiveReader(bmain, filename);
80
81 if (!reader->valid()) {
82 delete reader;
83 continue;
84 }
85
86 readers.push_back(reader);
87 }
88
89 if (readers.empty()) {
90 return nullptr;
91 }
92
93 if (readers.size() == 1) {
94 return readers[0];
95 }
96
97 return new ArchiveReader(readers);
98}
99
100ArchiveReader::ArchiveReader(const std::vector<ArchiveReader *> &readers) : m_readers(readers)
101{
102 Alembic::AbcCoreLayer::ArchiveReaderPtrs archives;
103
104 for (ArchiveReader *reader : readers) {
105 archives.push_back(reader->m_archive.getPtr());
106 }
107
108 Alembic::AbcCoreLayer::ReadArchive layer;
109 Alembic::AbcCoreAbstract::ArchiveReaderPtr arPtr = layer(archives);
110
111 m_archive = IArchive(arPtr, kWrapExisting, ErrorHandler::kThrowPolicy);
112}
113
114ArchiveReader::ArchiveReader(const Main *bmain, const char *filename)
115{
116 char abs_filepath[FILE_MAX];
117 STRNCPY(abs_filepath, filename);
118 BLI_path_abs(abs_filepath, BKE_main_blendfile_path(bmain));
119
120#ifdef WIN32
121 UTF16_ENCODE(abs_filepath);
122 std::wstring wstr(abs_filepath_16);
123 m_infile.open(wstr.c_str(), std::ios::in | std::ios::binary);
124 UTF16_UN_ENCODE(abs_filepath);
125#else
126 m_infile.open(abs_filepath, std::ios::in | std::ios::binary);
127#endif
128
129 m_streams.push_back(&m_infile);
130
131 m_archive = open_archive(abs_filepath, m_streams);
132}
133
135{
136 for (ArchiveReader *reader : m_readers) {
137 delete reader;
138 }
139}
140
142{
143 return m_archive.valid();
144}
145
146Alembic::Abc::IObject ArchiveReader::getTop()
147{
148 return m_archive.getTop();
149}
150
152{
153 const MetaData &abc_metadata = m_archive.getPtr()->getMetaData();
154
155 /* Was the incoming Archive written by Blender? If so, make the version check. */
156 if (abc_metadata.get(Alembic::Abc::kApplicationNameKey) == "Blender") {
157 return abc_metadata.get("blender_version") < "v4.4";
158 }
159
160 return false;
161}
162
164{
165 chrono_t min_time = std::numeric_limits<chrono_t>::max();
166 chrono_t max_time = -std::numeric_limits<chrono_t>::max();
167
168 Alembic::Abc::GetArchiveStartAndEndTime(m_archive, min_time, max_time);
169
170 return {min_time, max_time};
171}
172
173} // namespace io::alembic
174} // namespace blender
#define FILE_MAX
static ArchiveReader * get(const struct Main *bmain, const std::vector< const char * > &filenames)
#define layer
std::string filename(const std::string_view &str, size_t pos)
static IArchive open_archive(const std::string &filename, const std::vector< std::istream * > &input_streams)
BMesh Iterators.
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84