MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ModelExport.cpp
Go to the documentation of this file.
1#include "ModelExport.hpp"
2
5
10
13
16
18
19#include <chrono>
20
21namespace MayaFlux::IO {
22
23namespace {
24
25 bool write_via_registry(
26 const std::string& filepath,
27 const std::vector<Kakshya::MeshData>& meshes,
28 const ModelWriteOptions& options)
29 {
30 auto writer = ModelWriterRegistry::instance().create_writer(filepath);
31 if (!writer) {
33 "save_mesh: no writer registered for '{}'", filepath);
34 return false;
35 }
36
37 const bool ok = writer->write(filepath, meshes, options);
38 if (!ok) {
40 "save_mesh: writer failed for '{}': {}", filepath, writer->get_last_error());
41 } else {
43 "save_mesh: wrote '{}'", filepath);
44 }
45 return ok;
46 }
47
48 /**
49 * @brief Download a bound diffuse texture and write it next to the model
50 * file, so the submesh's diffuse path has
51 * somewhere to point.
52 *
53 * Named "<model-stem>_diffuse.png", or "<model-stem>_<suffix>_diffuse.png"
54 * when more than one texture is being written alongside the same model
55 * file (one per MeshNetwork slot).
56 *
57 * @return The sibling path on success, empty string if there is nothing
58 * to write or the download/encode fails.
59 */
60 std::string pack_diffuse_texture(
61 const std::shared_ptr<Core::VKImage>& texture,
62 const std::string& model_filepath,
63 const std::string& suffix)
64 {
65 if (!texture) {
66 return {};
67 }
68
69 const std::filesystem::path model_path(model_filepath);
70 std::string name = model_path.stem().string() + "_diffuse";
71 if (!suffix.empty()) {
72 name += "_" + suffix;
73 }
74 name += ".png";
75
76 const auto image_path = (model_path.parent_path() / name).string();
77 if (!save_image(texture, image_path)) {
79 "save_mesh: failed to write diffuse texture to '{}'", image_path);
80 return {};
81 }
82 return image_path;
83 }
84
85 /**
86 * @brief Give mesh_data a single full-mesh submesh Region if it has none,
87 * so a caller-supplied diffuse_path has a descriptor to update.
88 */
89 void ensure_submesh_region(Kakshya::MeshData& mesh_data, const std::string& name)
90 {
91 if (mesh_data.submeshes && !mesh_data.submeshes->regions.empty()) {
92 return;
93 }
94
95 Kakshya::MeshSubrange sub;
96 sub.index_start = 0;
97 sub.index_count = mesh_data.face_count() * 3;
98 sub.name = name;
99
100 Kakshya::RegionGroup rg("submeshes");
101 rg.add_region(sub.to_region());
102 mesh_data.submeshes = std::move(rg);
103 }
104
105 /**
106 * @brief Pack one MeshWriterNode's current vertices/indices into a
107 * MeshData, baking @p world into position/normal/tangent.
108 * @return nullopt if the node has no vertices yet, or insertion fails.
109 */
110 std::optional<Kakshya::MeshData> pack_node(
111 const std::shared_ptr<Nodes::GpuSync::MeshWriterNode>& node,
112 const glm::mat4& world,
113 const std::string& name,
114 const std::string& diffuse_path = {})
115 {
116 auto mesh_data = node->get_mesh_data();
117 if (!mesh_data) {
118 return std::nullopt;
119 }
120
121 const glm::mat3 normal_matrix(world);
122
123 auto& vertex_bytes = std::get<std::vector<uint8_t>>(mesh_data->vertex_variant);
124 std::vector<Kakshya::MeshVertex> verts(vertex_bytes.size() / sizeof(Kakshya::MeshVertex));
125 std::memcpy(verts.data(), vertex_bytes.data(), vertex_bytes.size());
126 for (auto& v : verts) {
127 v.position = glm::vec3(world * glm::vec4(v.position, 1.0F));
128 v.normal = glm::normalize(normal_matrix * v.normal);
129 v.tangent = glm::vec4(glm::normalize(normal_matrix * glm::vec3(v.tangent)), v.tangent.w);
130 }
131 std::memcpy(vertex_bytes.data(), verts.data(), vertex_bytes.size());
132
133 const auto index_count = std::get<std::vector<uint32_t>>(mesh_data->index_variant).size();
134
135 Kakshya::MeshSubrange sub;
136 sub.index_start = 0;
137 sub.index_count = static_cast<uint32_t>(index_count);
138 sub.name = name;
139 sub.diffuse_path = diffuse_path;
140 Kakshya::RegionGroup rg("submeshes");
141 rg.add_region(sub.to_region());
142 mesh_data->submeshes = std::move(rg);
143
144 return mesh_data;
145 }
146
147 std::string splice_timestamp(const std::string& pattern)
148 {
149 const auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
150 std::chrono::system_clock::now().time_since_epoch())
151 .count();
152 return resolve_sequence_path(pattern, static_cast<uint64_t>(now_ms));
153 }
154
155} // namespace
156
158 const std::shared_ptr<Buffers::MeshBuffer>& buffer,
159 const std::string& filepath,
160 const ModelWriteOptions& options)
161{
162 if (!buffer) {
163 MF_ERROR(Journal::Component::IO, Journal::Context::FileIO, "save_mesh: null buffer");
164 return false;
165 }
166
167 auto mesh_data = buffer->get_mesh_data();
168 if (buffer->has_diffuse_texture()) {
169 const auto image_path = pack_diffuse_texture(buffer->get_diffuse_texture(), filepath, "");
170 if (!image_path.empty()) {
171 ensure_submesh_region(mesh_data, "mesh");
172 auto& region = mesh_data.submeshes->regions.front();
173 const auto* existing = region.get<Kakshya::MeshSubrange>();
174 auto sub = existing
175 ? *existing
177 sub.diffuse_path = image_path;
178 region.set(std::move(sub));
179 }
180 }
181
182 return write_via_registry(filepath, { mesh_data }, options);
183}
184
186 const std::shared_ptr<Nodes::Network::MeshNetwork>& network,
187 const std::string& filepath,
188 const ModelWriteOptions& options)
189{
190 if (!network) {
191 MF_ERROR(Journal::Component::IO, Journal::Context::FileIO, "save_mesh: null network");
192 return false;
193 }
194
195 std::vector<Kakshya::MeshData> meshes;
196 meshes.reserve(network->slots().size());
197 for (const auto& slot : network->slots()) {
198 if (!slot.node) {
199 continue;
200 }
201 std::string diffuse_path;
202 if (slot.diffuse_texture) {
203 const auto suffix = !slot.name.empty() ? slot.name : std::to_string(slot.index);
204 diffuse_path = pack_diffuse_texture(slot.diffuse_texture, filepath, suffix);
205 }
206 if (auto packed = pack_node(slot.node, slot.world_transform, slot.name, diffuse_path)) {
207 meshes.push_back(std::move(*packed));
208 }
209 }
210
211 if (meshes.empty()) {
213 "save_mesh: no exportable slots in network");
214 return false;
215 }
216
217 return write_via_registry(filepath, meshes, options);
218}
219
221 const std::shared_ptr<Buffers::MeshNetworkBuffer>& network_buffer,
222 const std::string& filepath,
223 const ModelWriteOptions& options)
224{
225 if (!network_buffer) {
226 MF_ERROR(Journal::Component::IO, Journal::Context::FileIO, "save_mesh: null network_buffer");
227 return false;
228 }
229
230 auto net = network_buffer->get_network();
231 if (!net) {
233 "save_mesh: MeshNetworkBuffer has no network");
234 return false;
235 }
236
237 std::vector<Kakshya::MeshData> meshes;
238 meshes.reserve(net->slots().size());
239 const std::string shared_diffuse_path = network_buffer->has_diffuse_texture()
240 ? pack_diffuse_texture(network_buffer->get_diffuse_texture(), filepath, "")
241 : std::string {};
242 for (const auto& slot : net->slots()) {
243 if (!slot.node) {
244 continue;
245 }
246 std::string diffuse_path = shared_diffuse_path;
247 if (slot.diffuse_texture) {
248 const auto suffix = !slot.name.empty() ? slot.name : std::to_string(slot.index);
249 diffuse_path = pack_diffuse_texture(slot.diffuse_texture, filepath, suffix);
250 }
251 if (auto packed = pack_node(slot.node, slot.world_transform, slot.name, diffuse_path)) {
252 meshes.push_back(std::move(*packed));
253 }
254 }
255
256 if (meshes.empty()) {
258 "save_mesh: no exportable slots in network");
259 return false;
260 }
261
262 return write_via_registry(filepath, meshes, options);
263}
264
266 const std::shared_ptr<Nodes::GpuSync::MeshWriterNode>& node,
267 const std::string& filepath,
268 const ModelWriteOptions& options)
269{
270 if (!node) {
272 return false;
273 }
274
275 auto packed = pack_node(node, glm::mat4(1.0F), "mesh");
276 if (!packed) {
278 "save_mesh: node has no vertices/indices yet");
279 return false;
280 }
281
282 return write_via_registry(filepath, { *packed }, options);
283}
284
285std::optional<Kakshya::MeshData> download_compute_mesh(
286 const std::shared_ptr<Buffers::ComputeMeshBuffer>& buffer)
287{
288 return Buffers::download_compute_mesh(buffer);
289}
290
292 const std::shared_ptr<Buffers::ComputeMeshBuffer>& buffer,
293 const std::string& filepath,
294 const ModelWriteOptions& options)
295{
296 auto mesh_data = IO::download_compute_mesh(buffer);
297 if (!mesh_data) {
298 return false;
299 }
300
301 if (buffer->has_diffuse_texture()) {
302 const auto image_path = pack_diffuse_texture(buffer->get_diffuse_texture(), filepath, "");
303 if (!image_path.empty()) {
304 ensure_submesh_region(*mesh_data, "mesh");
305 auto& region = mesh_data->submeshes->regions.front();
306 const auto* existing = region.get<Kakshya::MeshSubrange>();
307 auto sub = existing
308 ? *existing
310 sub.diffuse_path = image_path;
311 region.set(std::move(sub));
312 }
313 }
314
315 return write_via_registry(filepath, { *mesh_data }, options);
316}
317
319 const std::shared_ptr<Buffers::ComputeMeshBuffer>& buffer,
320 const std::string& path_pattern,
321 const ModelWriteOptions& options)
322{
323 return save_mesh(buffer, splice_timestamp(path_pattern), options);
324}
325
327 const std::shared_ptr<Buffers::MeshBuffer>& buffer,
328 const std::string& path_pattern,
329 const ModelWriteOptions& options)
330{
331 return save_mesh(buffer, splice_timestamp(path_pattern), options);
332}
333
335 const std::shared_ptr<Buffers::MeshNetworkBuffer>& network_buffer,
336 const std::string& path_pattern,
337 const ModelWriteOptions& options)
338{
339 return save_mesh(network_buffer, splice_timestamp(path_pattern), options);
340}
341
343 const std::shared_ptr<Nodes::Network::MeshNetwork>& network,
344 const std::string& path_pattern,
345 const ModelWriteOptions& options)
346{
347 return save_mesh(network, splice_timestamp(path_pattern), options);
348}
349
351 const std::shared_ptr<Nodes::GpuSync::MeshWriterNode>& node,
352 const std::string& path_pattern,
353 const ModelWriteOptions& options)
354{
355 return save_mesh(node, splice_timestamp(path_pattern), options);
356}
357
358} // namespace MayaFlux::IO
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
std::unique_ptr< Writer > create_writer(const std::string &filepath) const
std::optional< Kakshya::MeshData > download_compute_mesh(const std::shared_ptr< ComputeMeshBuffer > &buffer)
Download a ComputeMeshBuffer's current live geometry to CPU.
bool save_image(const std::shared_ptr< Core::VKImage > &image, const std::string &filepath, const ImageWriteOptions &options)
Save a VKImage directly to disk via the ImageWriter registry.
std::optional< Kakshya::MeshData > download_compute_mesh(const std::shared_ptr< Buffers::ComputeMeshBuffer > &buffer)
Download a ComputeMeshBuffer's current live geometry to CPU.
std::string resolve_sequence_path(std::string_view pattern, uint64_t frame)
Substitute a frame index into a numbered output pattern.
bool save_mesh(const std::shared_ptr< Buffers::MeshBuffer > &buffer, const std::string &filepath, const ModelWriteOptions &options)
Save a MeshBuffer's current mesh data to disk via ModelWriterRegistry.
bool save_mesh_snapshot(const std::shared_ptr< Buffers::ComputeMeshBuffer > &buffer, const std::string &path_pattern, const ModelWriteOptions &options)
Save a ComputeMeshBuffer with a millisecond epoch timestamp spliced into the path.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
Configuration for model writing.
Byte-range descriptor for one submesh within the shared index buffer.