Blender V4.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
10
11#include "Alembic/AbcCoreLayer/Read.h"
12#include "Alembic/AbcCoreOgawa/ReadWrite.h"
13
14#include "BKE_main.hh"
15
16#include "BLI_path_utils.hh"
17#include "BLI_string.h"
18
19#ifdef WIN32
20# include "utfconv.hh"
21#endif
22
23#include <fstream>
24#include <vector>
25
26using Alembic::Abc::ErrorHandler;
27using Alembic::Abc::Exception;
28using Alembic::Abc::IArchive;
29using Alembic::Abc::kWrapExisting;
30
31namespace blender::io::alembic {
32
33static IArchive open_archive(const std::string &filename,
34 const std::vector<std::istream *> &input_streams)
35{
36 try {
37 Alembic::AbcCoreOgawa::ReadArchive archive_reader(input_streams);
38
39 return IArchive(archive_reader(filename), kWrapExisting, ErrorHandler::kThrowPolicy);
40 }
41 catch (const Exception &e) {
42 std::cerr << e.what() << '\n';
43
44 /* Inspect the file to see whether it's actually a HDF5 file. */
45 char header[4]; /* char(0x89) + "HDF" */
46 std::ifstream the_file(filename.c_str(), std::ios::in | std::ios::binary);
47 if (!the_file) {
48 std::cerr << "Unable to open " << filename << std::endl;
49 }
50 else if (!the_file.read(header, sizeof(header))) {
51 std::cerr << "Unable to read from " << filename << std::endl;
52 }
53 else if (strncmp(header + 1, "HDF", 3) != 0) {
54 std::cerr << filename << " has an unknown file format, unable to read." << std::endl;
55 }
56 else {
57 std::cerr << filename << " is in the obsolete HDF5 format, unable to read." << std::endl;
58 }
59
60 if (the_file.is_open()) {
61 the_file.close();
62 }
63 }
64
65 return IArchive();
66}
67
68ArchiveReader *ArchiveReader::get(const Main *bmain, const std::vector<const char *> &filenames)
69{
70 std::vector<ArchiveReader *> readers;
71
72 for (const char *filename : filenames) {
73 ArchiveReader *reader = new ArchiveReader(bmain, filename);
74
75 if (!reader->valid()) {
76 delete reader;
77 continue;
78 }
79
80 readers.push_back(reader);
81 }
82
83 if (readers.empty()) {
84 return nullptr;
85 }
86
87 if (readers.size() == 1) {
88 return readers[0];
89 }
90
91 return new ArchiveReader(readers);
92}
93
94ArchiveReader::ArchiveReader(const std::vector<ArchiveReader *> &readers) : m_readers(readers)
95{
96 Alembic::AbcCoreLayer::ArchiveReaderPtrs archives;
97
98 for (ArchiveReader *reader : readers) {
99 archives.push_back(reader->m_archive.getPtr());
100 }
101
102 Alembic::AbcCoreLayer::ReadArchive layer;
103 Alembic::AbcCoreAbstract::ArchiveReaderPtr arPtr = layer(archives);
104
105 m_archive = IArchive(arPtr, kWrapExisting, ErrorHandler::kThrowPolicy);
106}
107
108ArchiveReader::ArchiveReader(const Main *bmain, const char *filename)
109{
110 char abs_filepath[FILE_MAX];
111 STRNCPY(abs_filepath, filename);
112 BLI_path_abs(abs_filepath, BKE_main_blendfile_path(bmain));
113
114#ifdef WIN32
115 UTF16_ENCODE(abs_filepath);
116 std::wstring wstr(abs_filepath_16);
117 m_infile.open(wstr.c_str(), std::ios::in | std::ios::binary);
118 UTF16_UN_ENCODE(abs_filepath);
119#else
120 m_infile.open(abs_filepath, std::ios::in | std::ios::binary);
121#endif
122
123 m_streams.push_back(&m_infile);
124
125 m_archive = open_archive(abs_filepath, m_streams);
126}
127
129{
130 for (ArchiveReader *reader : m_readers) {
131 delete reader;
132 }
133}
134
136{
137 return m_archive.valid();
138}
139
140Alembic::Abc::IObject ArchiveReader::getTop()
141{
142 return m_archive.getTop();
143}
144
145} // namespace blender::io::alembic
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:832
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
#define FILE_MAX
#define STRNCPY(dst, src)
Definition BLI_string.h:593
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static ArchiveReader * get(const struct Main *bmain, const std::vector< const char * > &filenames)
static IArchive open_archive(const std::string &filename, const std::vector< std::istream * > &input_streams)
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84