Blender V5.3
depsgraph_physics.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
12
13#include "BLI_enum_flags.hh"
14#include "BLI_listbase.hh"
15
16#include "BKE_collision.h"
17#include "BKE_dynamicpaint.h"
18#include "BKE_effect.h"
19#include "BKE_modifier.hh"
20#include "BKE_object.hh"
21
24#include "DNA_object_types.h"
25
29
30#include "depsgraph.hh"
31
32namespace blender {
33
34/*************************** Evaluation Query API *****************************/
35
37{
38 switch (modifier_type) {
45 }
46
47 BLI_assert_msg(0, "Unknown collision modifier type");
49}
50/* Get ID from an ID type object, in a safe manner. This means that object can be nullptr,
51 * in which case the function returns nullptr.
52 */
53template<class T> static ID *object_id_safe(T *object)
54{
55 if (object == nullptr) {
56 return nullptr;
57 }
58 return &object->id;
59}
60
61ListBaseT<EffectorRelation> *DEG_get_effector_relations(const Depsgraph *graph,
62 Collection *collection)
63{
64 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
66 if (hash == nullptr) {
67 return nullptr;
68 }
69 /* NOTE: nullptr is a valid lookup key here as it means that the relation is not bound to a
70 * specific collection. */
71 ID *collection_orig = DEG_get_original(object_id_safe(collection));
72 return hash->lookup_default(collection_orig, nullptr);
73}
74
75ListBaseT<CollisionRelation> *DEG_get_collision_relations(const Depsgraph *graph,
76 Collection *collection,
77 uint modifier_type)
78{
79 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
80 const ePhysicsCollisionType type = modifier_to_collision_type(modifier_type);
82 deg_graph->physics_relations_collision[type];
83 if (hash == nullptr) {
84 return nullptr;
85 }
86 /* NOTE: nullptr is a valid lookup key here as it means that the relation is not bound to a
87 * specific collection. */
88 ID *collection_orig = DEG_get_original(object_id_safe(collection));
89 return hash->lookup_default(collection_orig, nullptr);
90}
91
92/********************** Depsgraph Building API ************************/
93
102 None = 0,
104 Transform = 1 << 0,
106 Geometry = 1 << 1,
108 EvalPose = 1 << 2,
109};
111
112void DEG_add_collision_relations(DepsNodeHandle *handle,
113 Object *object,
114 Collection *collection,
115 uint modifier_type,
116 DEG_CollobjFilterFunction filter_function,
117 const char *name)
118{
119 Depsgraph *depsgraph = DEG_get_graph_from_handle(handle);
120 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
121 ListBaseT<CollisionRelation> *relations = build_collision_relations(
122 deg_graph, collection, modifier_type);
123
124 /* Expand tag objects, matching: #BKE_object_modifier_update_subframe behavior. */
125
126 /* NOTE: #eModifierType_Fluid should be included,
127 * leave out for the purpose of validating the fix for dynamic paint only. */
128 const bool use_recursive_parents = (modifier_type == eModifierType_DynamicPaint);
129
130 Map<Object *, CollisionComponentFlag> *object_component_map = nullptr;
131 if (use_recursive_parents) {
132 object_component_map = MEM_new<Map<Object *, CollisionComponentFlag>>(__func__);
133 }
134
135 for (CollisionRelation &relation : *relations) {
136 Object *ob1 = relation.ob;
137 if (ob1 == object) {
138 continue;
139 }
140 if (filter_function &&
141 !filter_function(ob1, BKE_modifiers_findby_type(ob1, ModifierType(modifier_type))))
142 {
143 continue;
144 }
145
146 if (use_recursive_parents) {
147 /* Add relations for `ob1` and other objects it references,
148 * using `object_component_map` to avoid redundant calls.
149 *
150 * When #BKE_object_modifier_update_subframe is used by a modifier,
151 * it's important the depsgraph tags objects this modifier uses.
152 *
153 * Without this, access to objects is not thread-safe, see: #142137.
154 *
155 * NOTE(@ideasman42): #BKE_object_modifier_update_subframe calls
156 * #BKE_animsys_evaluate_animdata, depending on the object type.
157 * Equivalent relations could be added here.
158 * This was not done and there are no bug reports relating to this,
159 * so leave as-is unless the current code is failing in a real world scenario. */
160
162 ob1,
163 true,
165 modifier_type,
166 [&handle, &name, &object_component_map](Object *ob, const bool update_mesh) {
167 CollisionComponentFlag &update_flag = object_component_map->lookup_or_add_default(ob);
168 {
170 if (!flag_is_set(update_flag, test_flag)) {
171 update_flag |= test_flag;
173 }
174 }
175 if (update_mesh) {
177 if (!flag_is_set(update_flag, test_flag)) {
178 update_flag |= test_flag;
180 }
181 }
182 if (ob->type == OB_ARMATURE) {
184 if (!flag_is_set(update_flag, test_flag)) {
185 update_flag |= test_flag;
187 }
188 }
189 });
190
191 continue;
192 }
193
196 }
197
198 if (use_recursive_parents) {
199 MEM_delete(object_component_map);
200 }
201}
202
203void DEG_add_forcefield_relations(DepsNodeHandle *handle,
204 Object *object,
205 EffectorWeights *effector_weights,
206 bool add_absorption,
207 int skip_forcefield,
208 const char *name)
209{
210 Depsgraph *depsgraph = DEG_get_graph_from_handle(handle);
211 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
212 ListBaseT<EffectorRelation> *relations = build_effector_relations(deg_graph,
213 effector_weights->group);
214 for (EffectorRelation &relation : *relations) {
215 if (relation.ob == object) {
216 continue;
217 }
218 if (relation.pd->forcefield == skip_forcefield) {
219 continue;
220 }
221
222 /* Relation to forcefield object, optionally including geometry.
223 * Use special point cache relations for automatic cache clearing. */
225
226 if (relation.psys || ELEM(relation.pd->shape, PFIELD_SHAPE_SURFACE, PFIELD_SHAPE_POINTS) ||
227 relation.pd->forcefield == PFIELD_GUIDE)
228 {
229 /* TODO(sergey): Consider going more granular with more dedicated
230 * particle system operation. */
232 }
233
234 /* Smoke flow relations. */
235 if (relation.pd->forcefield == PFIELD_FLUIDFLOW && relation.pd->f_source != nullptr) {
237 handle, relation.pd->f_source, DEG_OB_COMP_TRANSFORM, "Fluid Force Domain");
239 handle, relation.pd->f_source, DEG_OB_COMP_GEOMETRY, "Fluid Force Domain");
240 }
241
242 /* Absorption forces need collision relation. */
243 if (add_absorption && (relation.pd->flag & PFIELD_VISIBILITY)) {
245 handle, object, nullptr, eModifierType_Collision, nullptr, "Force Absorption");
246 }
247 }
248}
249
250/******************************** Internal API ********************************/
251
252namespace deg {
253
254ListBaseT<EffectorRelation> *build_effector_relations(Depsgraph *graph, Collection *collection)
255{
257 if (hash == nullptr) {
260 }
261 /* If collection is nullptr still use it as a key.
262 * In this case the BKE_effector_relations_create() will create relates for all bases in the
263 * view layer.
264 */
265 ID *collection_id = object_id_safe(collection);
266 return hash->lookup_or_add_cb(collection_id, [&]() {
267 ::blender::Depsgraph *depsgraph = reinterpret_cast<::blender::Depsgraph *>(graph);
268 return BKE_effector_relations_create(depsgraph, graph->scene, graph->view_layer, collection);
269 });
270}
271
272ListBaseT<CollisionRelation> *build_collision_relations(Depsgraph *graph,
273 Collection *collection,
274 uint modifier_type)
275{
276 const ePhysicsCollisionType type = modifier_to_collision_type(modifier_type);
278 if (hash == nullptr) {
279 graph->physics_relations_collision[type] =
281 hash = graph->physics_relations_collision[type];
282 }
283 /* If collection is nullptr still use it as a key.
284 * In this case the BKE_collision_relations_create() will create relates for all bases in the
285 * view layer.
286 */
287 ID *collection_id = object_id_safe(collection);
288 return hash->lookup_or_add_cb(collection_id, [&]() {
289 ::blender::Depsgraph *depsgraph = reinterpret_cast<::blender::Depsgraph *>(graph);
290 return BKE_collision_relations_create(depsgraph, collection, modifier_type);
291 });
292}
293
295{
296 if (graph->physics_relations_effector) {
297 for (ListBaseT<EffectorRelation> *list : graph->physics_relations_effector->values()) {
299 }
300 delete graph->physics_relations_effector;
301 graph->physics_relations_effector = nullptr;
302 }
303
304 for (int i = 0; i < DEG_PHYSICS_COLLISION_NUM; i++) {
306 if (hash) {
307 for (ListBaseT<CollisionRelation> *list : hash->values()) {
309 }
310 delete hash;
311 graph->physics_relations_collision[i] = nullptr;
312 }
313 }
314}
315
316} // namespace deg
317} // namespace blender
General operations, lookup, etc. for blender objects.
#define OBJECT_MODIFIER_UPDATE_SUBFRAME_RECURSION_DEFAULT
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define ELEM(...)
#define BLI_assert_msg(a, msg)
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
#define T
ENUM_OPERATORS(Layer::Flags)
ListBaseT< CollisionRelation > * build_collision_relations(Depsgraph *graph, Collection *collection, uint modifier_type)
void clear_physics_relations(Depsgraph *graph)
ListBaseT< EffectorRelation > * build_effector_relations(Depsgraph *graph, Collection *collection)
BMesh Iterators.
Depsgraph * DEG_get_graph_from_handle(DepsNodeHandle *node_handle)
unsigned int uint
Definition BKE_text.h:55
void DEG_add_object_pointcache_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
static ID * object_id_safe(T *object)
@ eModifierType_DynamicPaint
void BKE_object_modifier_update_subframe_only_callback(Object *ob, bool update_mesh, int parent_recursion_limit, int modifier_type, FunctionRef< void(Object *object, bool update_mesh)> update_or_tag_fn)
Definition object.cc:5610
void BKE_collision_relations_free(ListBaseT< CollisionRelation > *relations)
ListBaseT< EffectorRelation > * DEG_get_effector_relations(const Depsgraph *depsgraph, Collection *collection)
void DEG_add_collision_relations(DepsNodeHandle *handle, Object *object, Collection *collection, unsigned int modifier_type, DEG_CollobjFilterFunction filter_function, const char *name)
unsigned char uint8_t
Definition BKE_text.h:79
static ePhysicsCollisionType modifier_to_collision_type(uint modifier_type)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
T * DEG_get_original(T *id)
ListBaseT< CollisionRelation > * DEG_get_collision_relations(const Depsgraph *depsgraph, Collection *collection, unsigned int modifier_type)
void BKE_effector_relations_free(ListBaseT< EffectorRelation > *lb)
Definition effect.cc:249
ListBaseT< CollisionRelation > * BKE_collision_relations_create(struct Depsgraph *depsgraph, struct Collection *collection, unsigned int modifier_type)
ListBaseT< EffectorRelation > * BKE_effector_relations_create(struct Depsgraph *depsgraph, const struct Scene *scene, struct ViewLayer *view_layer, struct Collection *collection)
bool(*)(Object *obj, ModifierData *md) DEG_CollobjFilterFunction
void DEG_add_forcefield_relations(DepsNodeHandle *handle, Object *object, EffectorWeights *eff, bool add_absorption, int skip_forcefield, const char *name)
#define hash
Definition noise_c.cc:156
Map< const ID *, ListBaseT< CollisionRelation > * > * physics_relations_collision[DEG_PHYSICS_COLLISION_NUM]
Definition depsgraph.hh:185
Map< const ID *, ListBaseT< EffectorRelation > * > * physics_relations_effector
Definition depsgraph.hh:183