Blender V5.3
MOD_cloth.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "BLI_utildefines.hh"
12
13#include "BLI_listbase.hh"
14
15#include "BLT_translation.hh"
16
17#include "DNA_cloth_types.h"
18#include "DNA_key_types.h"
19#include "DNA_mesh_types.h"
21#include "DNA_object_types.h"
22#include "DNA_screen_types.h"
23
24#include "MEM_guardedalloc.h"
25
26#include "BKE_cloth.hh"
27#include "BKE_customdata.hh"
28#include "BKE_effect.h"
29#include "BKE_global.hh"
30#include "BKE_key.hh"
31#include "BKE_lib_id.hh"
32#include "BKE_lib_query.hh"
33#include "BKE_modifier.hh"
34#include "BKE_pointcache.h"
35
37#include "UI_resources.hh"
38
39#include "RNA_prototypes.hh"
40
43
44#include "MOD_ui_common.hh"
45
46namespace blender {
47
48static void init_data(ModifierData *md)
49{
50 ClothModifierData *clmd = reinterpret_cast<ClothModifierData *>(md);
51
53 clmd->sim_parms = MEM_new<ClothSimSettings>(__func__);
54 clmd->coll_parms = MEM_new<ClothCollSettings>(__func__);
55
56 clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
57
58 /* check for alloc failing */
59 if (!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache) {
60 return;
61 }
62
63 if (!clmd->sim_parms->effector_weights) {
65 }
66
67 if (clmd->point_cache) {
68 clmd->point_cache->step = 1;
69 }
70}
71
72static void deform_verts(ModifierData *md,
73 const ModifierEvalContext *ctx,
74 Mesh *mesh,
75 MutableSpan<float3> positions)
76{
77 ClothModifierData *clmd = reinterpret_cast<ClothModifierData *>(md);
79
80 /* check for alloc failing */
81 if (!clmd->sim_parms || !clmd->coll_parms) {
82 init_data(md);
83
84 if (!clmd->sim_parms || !clmd->coll_parms) {
85 return;
86 }
87 }
88
89 /* TODO(sergey): For now it actually duplicates logic from mesh_data_update.cc
90 * and needs some more generic solution. But starting experimenting with
91 * this so close to the release is not that nice..
92 *
93 * Also hopefully new cloth system will arrive soon..
94 */
95 if (mesh == nullptr && clmd->sim_parms->shapekey_rest) {
98 if (kb && kb->data != nullptr) {
99 float (*layerorco)[3] = static_cast<float (*)[3]>(
100 CustomData_get_layer_for_write(&mesh->vert_data, CD_CLOTH_ORCO, mesh->verts_num));
101 if (!layerorco) {
102 layerorco = static_cast<float (*)[3]>(CustomData_add_layer(
103 &mesh->vert_data, CD_CLOTH_ORCO, CD_SET_DEFAULT, mesh->verts_num));
104 }
105
106 memcpy(layerorco, kb->data, sizeof(float[3]) * positions.size());
107 }
108 }
109
110 mesh->vert_positions_for_write().copy_from(positions);
111 mesh->tag_positions_changed();
112
113 clothModifier_do(clmd,
114 ctx->depsgraph,
115 scene,
116 ctx->object,
117 mesh,
118 reinterpret_cast<float (*)[3]>(positions.data()));
119}
120
122{
123 ClothModifierData *clmd = reinterpret_cast<ClothModifierData *>(md);
124 if (clmd != nullptr) {
127 ctx->object,
128 clmd->coll_parms->group,
130 nullptr,
131 "Cloth Collision");
132 }
134 ctx->node, ctx->object, clmd->sim_parms->effector_weights, true, 0, "Cloth Field");
135 }
136 DEG_add_depends_on_transform_relation(ctx->node, "Cloth Modifier");
137}
138
139static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
140{
141 ClothModifierData *clmd = reinterpret_cast<ClothModifierData *>(md);
142
143 if (cloth_uses_vgroup(clmd)) {
144 r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
145 }
146
147 if (clmd->sim_parms->shapekey_rest != 0) {
148 r_cddata_masks->vmask |= CD_MASK_CLOTH_ORCO;
149 }
150}
151
152static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
153{
154 const ClothModifierData *clmd = reinterpret_cast<const ClothModifierData *>(md);
155 ClothModifierData *tclmd = reinterpret_cast<ClothModifierData *>(target);
156
157 if (tclmd->sim_parms) {
158 if (tclmd->sim_parms->effector_weights) {
159 MEM_delete(tclmd->sim_parms->effector_weights);
160 }
161 MEM_delete(tclmd->sim_parms);
162 }
163
164 if (tclmd->coll_parms) {
165 MEM_delete(tclmd->coll_parms);
166 }
167
170 /* Share the cache with the original object's modifier. */
172 tclmd->ptcaches = clmd->ptcaches;
173 tclmd->point_cache = clmd->point_cache;
174 }
175 else {
176 const int clmd_point_cache_index = BLI_findindex(&clmd->ptcaches, clmd->point_cache);
177 BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches, flag);
178 tclmd->point_cache = static_cast<PointCache *>(
179 BLI_findlink(&tclmd->ptcaches, clmd_point_cache_index));
180 }
181
182 tclmd->sim_parms = MEM_dupalloc(clmd->sim_parms);
183 if (clmd->sim_parms->effector_weights) {
184 tclmd->sim_parms->effector_weights = static_cast<EffectorWeights *>(
185 MEM_dupalloc(clmd->sim_parms->effector_weights));
186 }
187 tclmd->coll_parms = MEM_dupalloc(clmd->coll_parms);
188 tclmd->clothObject = nullptr;
189 tclmd->hairdata = nullptr;
190 tclmd->solver_result = nullptr;
191}
192
193static bool depends_on_time(Scene * /*scene*/, ModifierData * /*md*/)
194{
195 return true;
196}
197
198static void free_data(ModifierData *md)
199{
200 ClothModifierData *clmd = reinterpret_cast<ClothModifierData *>(md);
201
202 if (clmd) {
203 if (G.debug & G_DEBUG_SIMDATA) {
204 printf("clothModifier_freeData\n");
205 }
206
208
209 if (clmd->sim_parms) {
210 if (clmd->sim_parms->effector_weights) {
211 MEM_delete(clmd->sim_parms->effector_weights);
212 }
213 MEM_delete(clmd->sim_parms);
214 }
215 if (clmd->coll_parms) {
216 MEM_delete(clmd->coll_parms);
217 }
218
220 clmd->ptcaches.clear_no_delete();
221 }
222 else {
224 }
225 clmd->point_cache = nullptr;
226
227 if (clmd->hairdata) {
228 MEM_delete(clmd->hairdata);
229 }
230
231 if (clmd->solver_result) {
232 MEM_delete(clmd->solver_result);
233 }
234 }
235}
236
237static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
238{
239 ClothModifierData *clmd = reinterpret_cast<ClothModifierData *>(md);
240
241 if (clmd->coll_parms) {
242 walk(user_data, ob, reinterpret_cast<ID **>(&clmd->coll_parms->group), IDWALK_CB_NOP);
243 }
244
245 if (clmd->sim_parms && clmd->sim_parms->effector_weights) {
246 walk(user_data,
247 ob,
248 reinterpret_cast<ID **>(&clmd->sim_parms->effector_weights->group),
250 }
251}
252
253static void panel_draw(const bContext * /*C*/, Panel *panel)
254{
255 ui::Layout &layout = *panel->layout;
256
258
259 layout.label(RPT_("Settings are inside the Physics tab"), ICON_NONE);
260
261 modifier_error_message_draw(layout, ptr);
262}
263
264static void panel_register(ARegionType *region_type)
265{
267}
268
270 /*idname*/ "Cloth",
271 /*name*/ N_("Cloth"),
272 /*struct_name*/ "ClothModifierData",
273 /*struct_size*/ sizeof(ClothModifierData),
274 /*srna*/ &RNA_ClothModifier,
278 /*icon*/ ICON_MOD_CLOTH,
279
280 /*copy_data*/ copy_data,
281
282 /*deform_verts*/ deform_verts,
283 /*deform_matrices*/ nullptr,
284 /*deform_verts_EM*/ nullptr,
285 /*deform_matrices_EM*/ nullptr,
286 /*modify_mesh*/ nullptr,
287 /*modify_geometry_set*/ nullptr,
288
289 /*init_data*/ init_data,
290 /*required_data_mask*/ required_data_mask,
291 /*free_data*/ free_data,
292 /*is_disabled*/ nullptr,
293 /*update_depsgraph*/ update_depsgraph,
294 /*depends_on_time*/ depends_on_time,
295 /*depends_on_normals*/ nullptr,
296 /*foreach_ID_link*/ foreach_ID_link,
297 /*foreach_tex_link*/ nullptr,
298 /*free_runtime_data*/ nullptr,
299 /*panel_register*/ panel_register,
300 /*blend_write*/ nullptr,
301 /*blend_read*/ nullptr,
302 /*foreach_cache*/ nullptr,
303 /*foreach_working_space_color*/ nullptr,
304};
305
306} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define RPT_(msgid)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define CD_MASK_MDEFORMVERT
#define CD_MASK_CLOTH_ORCO
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
BMesh Iterators.
int cloth_uses_vgroup(ClothModifierData *clmd)
Definition cloth.cc:599
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
ModifierTypeInfo modifierType_Cloth
Definition MOD_cloth.cc:269
static bool depends_on_time(Scene *, ModifierData *)
Definition MOD_build.cc:46
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)
void DEG_add_depends_on_transform_relation(DepsNodeHandle *node_handle, const char *description)
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
@ G_DEBUG_SIMDATA
static void required_data_mask(ModifierData *, CustomData_MeshMasks *r_cddata_masks)
Global G
Definition blender.cc:57
void clothModifier_do(ClothModifierData *clmd, Depsgraph *depsgraph, Scene *scene, Object *ob, const Mesh *mesh, float(*vertexCos)[3])
Definition cloth.cc:327
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
KeyBlock * BKE_keyblock_find_by_index(Key *key, int index)
Definition key.cc:1425
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
static void free_data(ModifierData *md)
Definition MOD_cloth.cc:198
static void panel_register(ARegionType *region_type)
void DEG_add_collision_relations(DepsNodeHandle *handle, Object *object, Collection *collection, unsigned int modifier_type, DEG_CollobjFilterFunction filter_function, const char *name)
@ eModifierTypeFlag_UsesPointCache
@ eModifierTypeFlag_AcceptsMesh
@ eModifierTypeFlag_Single
void cloth_free_modifier_extern(ClothModifierData *clmd)
Definition cloth.cc:506
void BKE_ptcache_free_list(ListBaseT< PointCache > *ptcaches)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
struct PointCache * BKE_ptcache_add(ListBaseT< PointCache > *ptcaches)
struct PointCache * BKE_ptcache_copy_list(ListBaseT< PointCache > *ptcaches_new, const ListBaseT< PointCache > *ptcaches_old, int flag)
static void panel_draw(const bContext *, Panel *panel)
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, MutableSpan< float3 > positions)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
@ CLOTH_COLLSETTINGS_FLAG_ENABLED
struct EffectorWeights * BKE_effector_add_weights(struct Collection *collection)
Definition effect.cc:63
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
void DEG_add_forcefield_relations(DepsNodeHandle *handle, Object *object, EffectorWeights *eff, bool add_absorption, int skip_forcefield, const char *name)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CLOTH_COLLISIONSETTINGS_FLAGS flags
struct Collection * group
struct ClothSolverResult * solver_result
struct ClothHairData * hairdata
struct ClothSimSettings * sim_parms
struct PointCache * point_cache
struct ListBaseT< PointCache > ptcaches
struct ClothCollSettings * coll_parms
struct EffectorWeights * effector_weights
ui::Layout * layout
void label(StringRef name, int icon)
#define N_(msgid)