Blender V5.3
MOD_meshsequencecache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "BLI_math_vector.hh"
12#include "BLI_string.hh"
13#include "BLI_utildefines.hh"
14
15#include "BLT_translation.hh"
16
17#include "DNA_cachefile_types.h"
18#include "DNA_mesh_types.h"
19#include "DNA_modifier_types.h"
20#include "DNA_object_types.h"
22#include "DNA_scene_types.h"
23#include "DNA_screen_types.h"
24
25#include "MEM_guardedalloc.h"
26
27#include "BKE_cachefile.hh"
28#include "BKE_geometry_set.hh"
29#include "BKE_lib_query.hh"
30#include "BKE_mesh.hh"
31
32#include "UI_interface.hh"
34#include "UI_resources.hh"
35
36#include "RNA_access.hh"
37#include "RNA_prototypes.hh"
38
41
43
44#include "MOD_modifiertypes.hh"
45#include "MOD_ui_common.hh"
46
47#if defined(WITH_USD) || defined(WITH_ALEMBIC)
48# include "BKE_lib_id.hh"
49#endif
50
51#ifdef WITH_ALEMBIC
52# include "ABC_alembic.h"
53#endif
54
55#ifdef WITH_USD
56# include "usd_api_modifier.hh"
57#endif
58
59namespace blender {
60
61static void init_data(ModifierData *md)
62{
63 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
66}
67
68static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
69{
70#if 0
71 const MeshSeqCacheModifierData *mcmd = (const MeshSeqCacheModifierData *)md;
72#endif
73 MeshSeqCacheModifierData *tmcmd = reinterpret_cast<MeshSeqCacheModifierData *>(target);
74
75 BKE_modifier_copydata_generic(md, target, flag);
76
77 tmcmd->reader = nullptr;
78 tmcmd->reader_object_path[0] = '\0';
79}
80
81static void free_data(ModifierData *md)
82{
83 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
84
85 if (mcmd->reader) {
86 mcmd->reader_object_path[0] = '\0';
88 }
89}
90
91static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
92{
93 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
94
95 /* leave it up to the modifier to check the file is valid on calculation */
96 return (mcmd->cache_file == nullptr) || (mcmd->object_path[0] == '\0');
97}
98
99#if defined(WITH_USD) || defined(WITH_ALEMBIC)
100
101/* Return true if the modifier evaluation is for the ORCO mesh and the mesh hasn't changed
102 * topology.
103 */
104static bool can_use_mesh_for_orco_evaluation(MeshSeqCacheModifierData *mcmd,
105 const ModifierEvalContext *ctx,
106 const Mesh *mesh,
107 const double frame_offset,
108 const double time_offset,
109 const char **r_err_str)
110{
111 if ((ctx->flag & MOD_APPLY_ORCO) == 0) {
112 return false;
113 }
114
115 CacheFile *cache_file = mcmd->cache_file;
116
117 switch (cache_file->type) {
119# ifdef WITH_ALEMBIC
120 if (!ABC_mesh_topology_changed(mcmd->reader, ctx->object, mesh, time_offset, r_err_str)) {
121 return true;
122 }
123# else
124 UNUSED_VARS(time_offset);
125# endif
126 break;
128# ifdef WITH_USD
130 mcmd->reader, ctx->object, mesh, frame_offset, r_err_str))
131 {
132 return true;
133 }
134# else
135 UNUSED_VARS(frame_offset);
136# endif
137 break;
139 break;
140 }
141
142 return false;
143}
144#endif
145
147 const ModifierEvalContext *ctx,
148 bke::GeometrySet *geometry_set)
149{
150#if defined(WITH_USD) || defined(WITH_ALEMBIC)
151 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
152
154 CacheFile *cache_file = mcmd->cache_file;
155 const double frame = double(DEG_get_ctime(ctx->depsgraph));
156 const double frame_offset = BKE_cachefile_frame_offset(cache_file, frame);
157 const double time_offset = BKE_cachefile_time_offset(
158 cache_file, frame, scene->frames_per_second());
159 const char *err_str = nullptr;
160
161 if (!mcmd->reader || !STREQ(mcmd->reader_object_path, mcmd->object_path)) {
163 BKE_cachefile_reader_open(cache_file, &mcmd->reader, ctx->object, mcmd->object_path);
164 if (!mcmd->reader) {
166 ctx->object, md, "Could not create cache reader for file %s", cache_file->filepath);
167 return;
168 }
169 }
170
171 if (geometry_set->has_mesh()) {
172 const Mesh *mesh = geometry_set->get_mesh();
173 if (can_use_mesh_for_orco_evaluation(mcmd, ctx, mesh, frame_offset, time_offset, &err_str)) {
174 return;
175 }
176 }
177
178 /* Time (in frames or seconds) between two velocity samples. Automatically computed to
179 * scale the velocity vectors at render time for generating proper motion blur data. */
180# ifdef WITH_ALEMBIC
181 float velocity_scale = mcmd->velocity_scale;
183 velocity_scale *= scene->frames_per_second();
184 }
185# endif
186
187 switch (cache_file->type) {
189# ifdef WITH_ALEMBIC
190 ABCReadParams params;
191 params.time = time_offset;
192 params.read_flags = mcmd->read_flag;
193 params.velocity_name = mcmd->cache_file->velocity_name;
194 params.velocity_scale = velocity_scale;
195 ABC_read_geometry(mcmd->reader, ctx->object, *geometry_set, &params, &err_str);
196# endif
197 break;
198 }
199 case CACHEFILE_TYPE_USD: {
200# ifdef WITH_USD
202 mcmd->read_flag);
203 io::usd::USD_read_geometry(mcmd->reader, ctx->object, *geometry_set, params, &err_str);
204# endif
205 break;
206 }
208 break;
209 }
210
211 if (err_str) {
212 BKE_modifier_set_error(ctx->object, md, "%s", err_str);
213 }
214
215#else
216 UNUSED_VARS(ctx, md, geometry_set);
217 return;
218#endif
219}
220
222{
223#if defined(WITH_USD) || defined(WITH_ALEMBIC)
224 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
225
226 /* Only used to check whether we are operating on org data or not... */
227 Mesh *object_mesh = (ctx->object->type == OB_MESH) ? id_cast<Mesh *>(ctx->object->data) :
228 nullptr;
229 Mesh *org_mesh = mesh;
230
232 CacheFile *cache_file = mcmd->cache_file;
233 const double frame = double(DEG_get_ctime(ctx->depsgraph));
234 const double frame_offset = BKE_cachefile_frame_offset(cache_file, frame);
235 const double time_offset = BKE_cachefile_time_offset(
236 cache_file, frame, scene->frames_per_second());
237 const char *err_str = nullptr;
238
239 if (!mcmd->reader || !STREQ(mcmd->reader_object_path, mcmd->object_path)) {
241 BKE_cachefile_reader_open(cache_file, &mcmd->reader, ctx->object, mcmd->object_path);
242 if (!mcmd->reader) {
244 ctx->object, md, "Could not create reader for file %s", cache_file->filepath);
245 return mesh;
246 }
247 }
248
249 /* If this invocation is for the ORCO mesh, and the mesh hasn't changed topology, we
250 * must return the mesh as-is instead of deforming it. */
251 if (can_use_mesh_for_orco_evaluation(mcmd, ctx, mesh, frame_offset, time_offset, &err_str)) {
252 return mesh;
253 }
254
255 if (object_mesh != nullptr) {
256 const Span<float3> mesh_positions = mesh->vert_positions();
257 const Span<int2> mesh_edges = mesh->edges();
258 const OffsetIndices mesh_faces = mesh->faces();
259 const Span<float3> me_positions = object_mesh->vert_positions();
260 const Span<int2> me_edges = object_mesh->edges();
261 const OffsetIndices me_faces = object_mesh->faces();
262
263 /* TODO(sybren+bastien): possibly check relevant custom data layers (UV/color depending on
264 * flags) and duplicate those too.
265 * XXX(Hans): This probably isn't true anymore with various copy-on-eval improvements, etc. */
266 if ((me_positions.data() == mesh_positions.data()) || (me_edges.data() == mesh_edges.data()) ||
267 (me_faces.data() == mesh_faces.data()))
268 {
269 /* We need to duplicate data here, otherwise we'll modify org mesh, see #51701. */
270 mesh = reinterpret_cast<Mesh *>(
271 BKE_id_copy_ex(nullptr,
272 &mesh->id,
273 nullptr,
276 }
277 }
278
281 modify_geometry_set(md, ctx, &geometry_set);
282 Mesh *result = geometry_set.get_component_for_write<bke::MeshComponent>().release();
283
284 if (!ELEM(result, nullptr, mesh) && (mesh != org_mesh)) {
285 BKE_id_free(nullptr, mesh);
286 mesh = org_mesh;
287 }
288
289 return result ? result : mesh;
290#else
291 UNUSED_VARS(ctx, md);
292 return mesh;
293#endif
294}
295
296static bool depends_on_time(Scene * /*scene*/, ModifierData *md)
297{
298#if defined(WITH_USD) || defined(WITH_ALEMBIC)
299 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
300 return (mcmd->cache_file != nullptr);
301#else
302 UNUSED_VARS(md);
303 return false;
304#endif
305}
306
307static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
308{
309 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
310
311 walk(user_data, ob, reinterpret_cast<ID **>(&mcmd->cache_file), IDWALK_CB_USER);
312}
313
315{
316 MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
317
318 if (mcmd->cache_file != nullptr) {
320 ctx->node, mcmd->cache_file, DEG_OB_COMP_CACHE, "Mesh Cache File");
321 }
322}
323
324static void panel_draw(const bContext *C, Panel *panel)
325{
326 ui::Layout &layout = *panel->layout;
327
328 PointerRNA ob_ptr;
330
331 PointerRNA cache_file_ptr = RNA_pointer_get(ptr, "cache_file");
332 bool has_cache_file = cache_file_ptr.has_data();
333
334 layout.use_property_split_set(true);
335
336 template_cache_file(&layout, C, ptr, "cache_file");
337
338 if (has_cache_file) {
339 layout.prop_search(
340 ptr, "object_path", &cache_file_ptr, "object_paths", std::nullopt, ICON_NONE);
341 }
342
343 if (RNA_enum_get(&ob_ptr, "type") == OB_MESH) {
344 layout.prop(ptr, "read_data", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
345 layout.prop(ptr, "use_vertex_interpolation", UI_ITEM_NONE, std::nullopt, ICON_NONE);
346 }
347 else if (RNA_enum_get(&ob_ptr, "type") == OB_CURVES) {
348 layout.prop(ptr, "use_vertex_interpolation", UI_ITEM_NONE, std::nullopt, ICON_NONE);
349 }
350
351 modifier_error_message_draw(layout, ptr);
352}
353
354static void velocity_panel_draw(const bContext * /*C*/, Panel *panel)
355{
356 ui::Layout &layout = *panel->layout;
357
358 PointerRNA ob_ptr;
360
361 PointerRNA fileptr;
362 if (!ui::template_cache_file_pointer(ptr, "cache_file", &fileptr)) {
363 return;
364 }
365
366 layout.use_property_split_set(true);
367 template_cache_file_velocity(&layout, &fileptr);
368 layout.prop(ptr, "velocity_scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
369}
370
371static void time_panel_draw(const bContext * /*C*/, Panel *panel)
372{
373 ui::Layout &layout = *panel->layout;
374
375 PointerRNA ob_ptr;
377
378 PointerRNA fileptr;
379 if (!ui::template_cache_file_pointer(ptr, "cache_file", &fileptr)) {
380 return;
381 }
382
383 layout.use_property_split_set(true);
384 template_cache_file_time_settings(&layout, &fileptr);
385}
386
387static void override_layers_panel_draw(const bContext *C, Panel *panel)
388{
389 ui::Layout &layout = *panel->layout;
390
391 PointerRNA ob_ptr;
393
394 PointerRNA fileptr;
395 if (!ui::template_cache_file_pointer(ptr, "cache_file", &fileptr)) {
396 return;
397 }
398
399 layout.use_property_split_set(true);
400 template_uilist_flags(&layout, C, &fileptr);
401}
402
403static void panel_register(ARegionType *region_type)
404{
407 modifier_subpanel_register(region_type, "time", "Time", nullptr, time_panel_draw, panel_type);
409 region_type, "velocity", "Velocity", nullptr, velocity_panel_draw, panel_type);
410 modifier_subpanel_register(region_type,
411 "override_layers",
412 "Override Layers",
413 nullptr,
415 panel_type);
416}
417
418static void blend_read(BlendDataReader * /*reader*/, ModifierData *md)
419{
420 MeshSeqCacheModifierData *msmcd = reinterpret_cast<MeshSeqCacheModifierData *>(md);
421 msmcd->reader = nullptr;
422 msmcd->reader_object_path[0] = '\0';
423}
424
426 /*idname*/ "MeshSequenceCache",
427 /*name*/ N_("MeshSequenceCache"),
428 /*struct_name*/ "MeshSeqCacheModifierData",
429 /*struct_size*/ sizeof(MeshSeqCacheModifierData),
430 /*srna*/ &RNA_MeshSequenceCacheModifier,
432 /*flags*/
434 /*icon*/ ICON_MOD_MESHDEFORM, /* TODO: Use correct icon. */
435
436 /*copy_data*/ copy_data,
437
438 /*deform_verts*/ nullptr,
439 /*deform_matrices*/ nullptr,
440 /*deform_verts_EM*/ nullptr,
441 /*deform_matrices_EM*/ nullptr,
442 /*modify_mesh*/ modify_mesh,
443 /*modify_geometry_set*/ modify_geometry_set,
444
445 /*init_data*/ init_data,
446 /*required_data_mask*/ nullptr,
447 /*free_data*/ free_data,
448 /*is_disabled*/ is_disabled,
449 /*update_depsgraph*/ update_depsgraph,
450 /*depends_on_time*/ depends_on_time,
451 /*depends_on_normals*/ nullptr,
452 /*foreach_ID_link*/ foreach_ID_link,
453 /*foreach_tex_link*/ nullptr,
454 /*free_runtime_data*/ nullptr,
455 /*panel_register*/ panel_register,
456 /*blend_write*/ nullptr,
457 /*blend_read*/ blend_read,
458 /*foreach_cache*/ nullptr,
459 /*foreach_working_space_color*/ nullptr,
460};
461
462} // namespace blender
#define MOD_MESHSEQ_READ_ALL
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define UNUSED_VARS(...)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define STREQ(a, b)
constexpr const T * data() const
Definition BLI_span.hh:219
bool USD_mesh_topology_changed(CacheReader *reader, const Object *ob, const Mesh *existing_mesh, const double time, const char **r_err_str)
USDMeshReadParams create_mesh_read_params(const double motion_sample_time, const int read_flags)
void USD_read_geometry(CacheReader *reader, const Object *ob, bke::GeometrySet &geometry_set, const USDMeshReadParams params, const char **r_err_str)
bool template_cache_file_pointer(PointerRNA *ptr, StringRefNull propname, PointerRNA *r_file_ptr)
BMesh Iterators.
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
double BKE_cachefile_frame_offset(const CacheFile *cache_file, double time)
Definition cachefile.cc:406
char * STRNCPY(char(&dst)[N], const char *src)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
static bool depends_on_time(Scene *, ModifierData *)
Definition MOD_build.cc:46
static void velocity_panel_draw(const bContext *, Panel *panel)
@ eModifierType_MeshSequenceCache
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void update_depsgraph(tGraphSliderOp *gso)
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
static void override_layers_panel_draw(const bContext *C, Panel *panel)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_NO_DEG_TAG
@ LIB_ID_CREATE_NO_MAIN
void ABC_read_geometry(CacheReader *reader, Object *ob, bke::GeometrySet &geometry_set, const ABCReadParams *params, const char **r_err_str)
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
void DEG_add_object_cache_relation(DepsNodeHandle *handle, CacheFile *cache_file, eDepsObjectComponentType component, const char *description)
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
@ CACHEFILE_VELOCITY_UNIT_FRAME
bool ABC_mesh_topology_changed(struct CacheReader *reader, struct Object *ob, const struct Mesh *existing_mesh, double time, const char **r_err_str)
static Mesh * modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
Definition MOD_array.cc:873
void BKE_id_free(Main *bmain, void *idv)
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_AcceptsMesh
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
static void blend_read(BlendDataReader *, ModifierData *md)
double BKE_cachefile_time_offset(const CacheFile *cache_file, double time, double fps)
Definition cachefile.cc:399
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void time_panel_draw(const bContext *, Panel *panel)
static void panel_draw(const bContext *, Panel *panel)
PanelType * modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
static bool is_disabled(const Scene *, ModifierData *md, bool)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
float DEG_get_ctime(const Depsgraph *graph)
ModifierTypeInfo modifierType_MeshSequenceCache
void BKE_cachefile_reader_free(CacheFile *cache_file, CacheReader **reader)
Definition cachefile.cc:211
void BKE_cachefile_reader_open(CacheFile *cache_file, CacheReader **reader, Object *object, const char *object_path)
Definition cachefile.cc:161
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
const char * velocity_name
eCacheFile_VelocityUnit velocity_unit
MeshSeqCacheModifierReadFlag read_flag
ui::Layout * layout
bool has_data() const
Definition RNA_types.hh:169
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Mesh * get_mesh() const
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
#define N_(msgid)