Blender V5.3
light_linking.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_light_linking.h"
10
11#include <string>
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_ID.h"
17#include "DNA_object_types.h"
18#include "DNA_scene_types.h"
19
20#include "BLI_assert.hh"
21#include "BLI_listbase.hh"
22#include "BLI_string_utf8.hh"
23
24#include "BKE_collection.hh"
25#include "BKE_layer.hh"
26#include "BKE_lib_id.hh"
27#include "BKE_report.hh"
28
29#include "BLT_translation.hh"
30
31#include "DEG_depsgraph.hh"
33
34namespace blender {
35
37{
38 if (object->light_linking == nullptr) {
39 object->light_linking = MEM_new<LightLinking>(__func__);
40 }
41}
42
43void BKE_light_linking_copy(Object *object_dst, const Object *object_src, const int copy_flags)
44{
45 BLI_assert(ELEM(object_dst->light_linking, nullptr, object_src->light_linking));
46 if (object_src->light_linking) {
47 object_dst->light_linking = MEM_new<LightLinking>(__func__, *(object_src->light_linking));
48 if ((copy_flags & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
49 id_us_plus(id_cast<ID *>(object_dst->light_linking->receiver_collection));
50 id_us_plus(id_cast<ID *>(object_dst->light_linking->blocker_collection));
51 }
52 }
53}
54
56 Object &object_dst,
57 const Object &object_src,
58 const LightLinkingType link_type)
59{
60 Collection *receiver_collection = BKE_light_linking_collection_get(&object_src, link_type);
61 BKE_light_linking_collection_assign(bmain, &object_dst, receiver_collection, link_type);
62
65}
66
68 Object &object_dst,
69 const Object &object_src)
70{
71 BKE_light_linking_copy_collection(bmain, object_dst, object_src, LIGHT_LINKING_RECEIVER);
72}
73
75 Object &object_dst,
76 const Object &object_src)
77{
78 BKE_light_linking_copy_collection(bmain, object_dst, object_src, LIGHT_LINKING_BLOCKER);
79}
80
81void BKE_light_linking_delete(Object *object, const int delete_flags)
82{
83 if (object->light_linking) {
84 if ((delete_flags & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
85 id_us_min(id_cast<ID *>(object->light_linking->receiver_collection));
86 id_us_min(id_cast<ID *>(object->light_linking->blocker_collection));
87 }
88 MEM_SAFE_DELETE(object->light_linking);
89 }
90}
91
93{
94 if (object->light_linking->receiver_collection == nullptr &&
95 object->light_linking->blocker_collection == nullptr)
96 {
98 }
99}
100
102 const LightLinkingType link_type)
103{
104 if (!object->light_linking) {
105 return nullptr;
106 }
107
108 switch (link_type) {
110 return object->light_linking->receiver_collection;
112 return object->light_linking->blocker_collection;
113 }
114
115 return nullptr;
116}
117
118static std::string get_default_collection_name(const Object *object,
119 const LightLinkingType link_type)
120{
121 const char *format;
122
123 switch (link_type) {
125 format = DATA_("Light Linking for %s");
126 break;
128 format = DATA_("Shadow Linking for %s");
129 break;
130 }
131
132 char name[MAX_ID_NAME - 2];
133 SNPRINTF_UTF8(name, format, object->id.name + 2);
134
135 return name;
136}
137
139 Object *object,
140 const LightLinkingType link_type)
141{
142 const std::string collection_name = get_default_collection_name(object, link_type);
143
144 Collection *new_collection = BKE_collection_add(bmain, nullptr, collection_name.c_str());
145
146 BKE_light_linking_collection_assign(bmain, object, new_collection, link_type);
147
148 return new_collection;
149}
150
152 Collection *new_collection,
153 const LightLinkingType link_type)
154{
155 /* Remove user from old collection. */
156 Collection *old_collection = BKE_light_linking_collection_get(object, link_type);
157 if (old_collection) {
158 id_us_min(&old_collection->id);
159 }
160
161 /* Allocate light linking on demand. */
162 if (new_collection) {
164 }
165
166 if (object->light_linking) {
167 /* Assign and increment user of new collection. */
168 switch (link_type) {
170 object->light_linking->receiver_collection = new_collection;
171 break;
173 object->light_linking->blocker_collection = new_collection;
174 break;
175 default:
177 break;
178 }
179
180 if (new_collection) {
181 id_us_plus(&new_collection->id);
182 }
183
185 }
186}
187
189 Object *object,
190 Collection *new_collection,
191 const LightLinkingType link_type)
192{
193 BKE_light_linking_collection_assign_only(object, new_collection, link_type);
194
197}
198
199static CollectionObject *find_collection_object(const Collection *collection, const Object *object)
200{
201 for (CollectionObject &collection_object : collection->gobject) {
202 if (collection_object.ob == object) {
203 return &collection_object;
204 }
205 }
206
207 return nullptr;
208}
209
211 const Collection *child)
212{
213 for (CollectionChild &collection_child : collection->children) {
214 if (collection_child.collection == child) {
215 return &collection_child;
216 }
217 }
218
219 return nullptr;
220}
221
222/* Add object to the light linking collection and return corresponding CollectionLightLinking
223 * settings.
224 *
225 * If the object is already in the collection then the content of the collection is not modified,
226 * and the existing light linking settings are returned. */
228 Collection *collection,
229 Object *object)
230{
231 BKE_collection_object_add(bmain, collection, object);
232
233 CollectionObject *collection_object = find_collection_object(collection, object);
234
235 if (!collection_object) {
236 BLI_assert_msg(0, "Object was not found after added to the light linking collection");
237 return nullptr;
238 }
239
240 return &collection_object->light_linking;
241}
242
243/* Add child collection to the light linking collection and return corresponding
244 * CollectionLightLinking settings.
245 *
246 * If the child collection is already in the collection then the content of the collection is
247 * not modified, and the existing light linking settings are returned. */
249 Collection *collection,
250 Collection *child)
251{
252 BKE_collection_child_add(bmain, collection, child);
253
254 CollectionChild *collection_child = find_collection_child(collection, child);
255
256 if (!collection_child) {
257 BLI_assert_msg(0, "Collection was not found after added to the light linking collection");
258 return nullptr;
259 }
260
261 return &collection_child->light_linking;
262}
263
265 Collection *collection,
266 ID *receiver,
267 const eCollectionLightLinkingState link_state,
268 const bool allow_all_objects)
269{
270 const ID_Type id_type = GS(receiver->name);
271
272 CollectionLightLinking *collection_light_linking = nullptr;
273
274 if (id_type == ID_OB) {
275 Object *object = reinterpret_cast<Object *>(receiver);
276
277 if (!allow_all_objects) {
278 if (object->type == OB_EMPTY && object->instance_collection) {
279 if (!BKE_collection_contains_geometry_recursive(object->instance_collection) &&
281 {
282 return;
283 }
284 }
285 else if (!DEG_object_has_geometry_component(object)) {
286 return;
287 }
288 }
289
290 collection_light_linking = light_linking_collection_add_object(bmain, collection, object);
291 }
292 else if (id_type == ID_GR) {
293 collection_light_linking = light_linking_collection_add_collection(
294 bmain, collection, reinterpret_cast<Collection *>(receiver));
295 }
296 else {
297 return;
298 }
299
300 if (!collection_light_linking) {
301 return;
302 }
303
304 collection_light_linking->link_state = link_state;
305
308
310}
311
313 Collection *collection,
314 ID *receiver,
315 const eCollectionLightLinkingState link_state)
316{
317 light_linking_add_receiver_to_collection(bmain, collection, receiver, link_state, false);
318}
319
321 Collection *receiver,
322 const ID *before)
323{
324 CollectionChild *receiver_collection_child = find_collection_child(collection, receiver);
325 if (!receiver_collection_child) {
326 BLI_assert_msg(0, "Receiver child was not found after adding collection to light linking");
327 return;
328 }
329
330 const ID_Type before_id_type = GS(before->name);
331
332 if (before_id_type != ID_GR) {
333 /* Adding before object: move the collection to the very bottom.
334 * This is as far to the bottom as the receiver can be in the flattened list of the collection.
335 */
336 BLI_remlink(&collection->children, receiver_collection_child);
337 BLI_addtail(&collection->children, receiver_collection_child);
338 return;
339 }
340
341 CollectionChild *before_collection_child = find_collection_child(
342 collection, reinterpret_cast<const Collection *>(before));
343 if (!before_collection_child) {
344 BLI_assert_msg(0, "Before child was not found");
345 return;
346 }
347
348 BLI_remlink(&collection->children, receiver_collection_child);
349 BLI_insertlinkbefore(&collection->children, before_collection_child, receiver_collection_child);
350}
351
353 Collection *receiver,
354 const ID *after)
355{
356 CollectionChild *receiver_collection_child = find_collection_child(collection, receiver);
357 if (!receiver_collection_child) {
358 BLI_assert_msg(0, "Receiver child was not found after adding collection to light linking");
359 return;
360 }
361
362 const ID_Type after_id_type = GS(after->name);
363
364 if (after_id_type != ID_GR) {
365 /* Adding before object: move the collection to the very bottom.
366 * This is as far to the bottom as the receiver can be in the flattened list of the collection.
367 */
368 BLI_remlink(&collection->children, receiver_collection_child);
369 BLI_addtail(&collection->children, receiver_collection_child);
370 return;
371 }
372
373 CollectionChild *after_collection_child = find_collection_child(
374 collection, reinterpret_cast<const Collection *>(after));
375 if (!after_collection_child) {
376 BLI_assert_msg(0, "After child was not found");
377 return;
378 }
379
380 BLI_remlink(&collection->children, receiver_collection_child);
381 BLI_insertlinkafter(&collection->children, after_collection_child, receiver_collection_child);
382}
383
385 Object *receiver,
386 const ID *before)
387{
388 CollectionObject *receiver_collection_object = find_collection_object(collection, receiver);
389 if (!receiver_collection_object) {
391 0, "Receiver collection object was not found after adding collection to light linking");
392 return;
393 }
394
395 const ID_Type before_id_type = GS(before->name);
396
397 if (before_id_type != ID_OB) {
398 /* Adding before collection: move the receiver to the very beginning of the child objects list.
399 * This is as close to the top of the flattened list of the collection content the object can
400 * possibly be. */
401 BLI_remlink(&collection->gobject, receiver_collection_object);
402 BLI_addhead(&collection->gobject, receiver_collection_object);
403 return;
404 }
405
406 CollectionObject *before_collection_object = find_collection_object(
407 collection, reinterpret_cast<const Object *>(before));
408 if (!before_collection_object) {
409 BLI_assert_msg(0, "Before collection object was not found");
410 return;
411 }
412
413 BLI_remlink(&collection->gobject, receiver_collection_object);
414 BLI_insertlinkbefore(&collection->gobject, before_collection_object, receiver_collection_object);
415}
416
417static void order_object_receiver_after(Collection *collection, Object *receiver, const ID *after)
418{
419 CollectionObject *receiver_collection_object = find_collection_object(collection, receiver);
420 if (!receiver_collection_object) {
422 0, "Receiver collection object was not found after adding collection to light linking");
423 return;
424 }
425
426 const ID_Type after_id_type = GS(after->name);
427
428 if (after_id_type != ID_OB) {
429 /* Adding after collection: move the receiver to the very beginning of the child objects list.
430 * This is as close to the top of the flattened list of the collection content the object can
431 * possibly be. */
432 BLI_remlink(&collection->gobject, receiver_collection_object);
433 BLI_addhead(&collection->gobject, receiver_collection_object);
434 return;
435 }
436
437 CollectionObject *after_collection_object = find_collection_object(
438 collection, reinterpret_cast<const Object *>(after));
439 if (!after_collection_object) {
440 BLI_assert_msg(0, "After collection object was not found");
441 return;
442 }
443
444 BLI_remlink(&collection->gobject, receiver_collection_object);
445 BLI_insertlinkafter(&collection->gobject, after_collection_object, receiver_collection_object);
446}
447
449 Main *bmain,
450 Collection *collection,
451 ID *receiver,
452 const ID *before,
453 const eCollectionLightLinkingState link_state)
454{
455 BLI_assert(before);
456
457 light_linking_add_receiver_to_collection(bmain, collection, receiver, link_state, true);
458
459 if (!before) {
460 return;
461 }
462
463 const ID_Type id_type = GS(receiver->name);
464 if (id_type == ID_OB) {
465 order_object_receiver_before(collection, reinterpret_cast<Object *>(receiver), before);
466 }
467 else if (id_type == ID_GR) {
468 order_collection_receiver_before(collection, reinterpret_cast<Collection *>(receiver), before);
469 }
470}
471
473 Main *bmain,
474 Collection *collection,
475 ID *receiver,
476 const ID *after,
477 const eCollectionLightLinkingState link_state)
478{
479 BLI_assert(after);
480
481 light_linking_add_receiver_to_collection(bmain, collection, receiver, link_state, true);
482
483 if (!after) {
484 return;
485 }
486
487 const ID_Type id_type = GS(receiver->name);
488 if (id_type == ID_OB) {
489 order_object_receiver_after(collection, reinterpret_cast<Object *>(receiver), after);
490 }
491 else if (id_type == ID_GR) {
492 order_collection_receiver_after(collection, reinterpret_cast<Collection *>(receiver), after);
493 }
494}
495
497 Collection *collection,
498 ID *id,
499 ReportList *reports)
500{
501 const ID_Type id_type = GS(id->name);
502
503 if (id_type == ID_OB) {
504 BKE_collection_object_remove(bmain, collection, reinterpret_cast<Object *>(id), false);
505 }
506 else if (id_type == ID_GR) {
507 BKE_collection_child_remove(bmain, collection, reinterpret_cast<Collection *>(id));
508 }
509 else {
510 BKE_reportf(reports,
511 RPT_ERROR,
512 "Cannot unlink unsupported '%s' from light linking collection '%s'",
513 id->name + 2,
514 collection->id.name + 2);
515 return false;
516 }
517
519 if (id_type == ID_OB) {
521 }
522
524
525 return true;
526}
527
529 Object *emitter,
530 Object *receiver,
531 const LightLinkingType link_type,
532 const eCollectionLightLinkingState link_state)
533{
534 if (receiver->type == OB_EMPTY && receiver->instance_collection) {
537 {
538 return;
539 }
540 }
541 else if (!DEG_object_has_geometry_component(receiver)) {
542 return;
543 }
544
545 Collection *collection = BKE_light_linking_collection_get(emitter, link_type);
546
547 if (!collection) {
548 collection = BKE_light_linking_collection_new(bmain, emitter, link_type);
549 }
550
551 BKE_light_linking_add_receiver_to_collection(bmain, collection, &receiver->id, link_state);
552}
553
555 Scene *scene,
556 ViewLayer *view_layer,
557 Object *emitter,
558 const LightLinkingType link_type)
559{
560 Collection *collection = BKE_light_linking_collection_get(emitter, link_type);
561 if (!collection) {
562 return;
563 }
564
565 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
566
567 /* Deselect all currently selected objects in the view layer, but keep the emitter selected.
568 * This is because the operation is called from the emitter being active, and it will be
569 * confusing to deselect it but keep active. */
570 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
571 if (base.object == emitter) {
572 continue;
573 }
574 base.flag &= ~BASE_SELECTED;
575 }
576
577 /* Select objects which are reachable via the receiver collection hierarchy. */
578 for (CollectionObject &cob : collection->gobject) {
579 Base *base = BKE_view_layer_base_find(view_layer, cob.ob);
580 if (!base) {
581 continue;
582 }
583
584 /* TODO(sergey): Check whether the object is configured to receive light. */
585
586 base->flag |= BASE_SELECTED;
587 }
588
590}
591
592} // namespace blender
#define SNPRINTF_UTF8(dst, format,...)
#define DATA_(msgid)
ID and Library types, which are fundamental for SDNA.
#define MAX_ID_NAME
Definition DNA_ID.h:367
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
BMesh Iterators.
struct Collection * BKE_light_linking_collection_new(struct Main *bmain, struct Object *object, LightLinkingType link_type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_light_linking_copy(struct Object *object_dst, const struct Object *object_src, const int copy_flags)
static CollectionLightLinking * light_linking_collection_add_collection(Main *bmain, Collection *collection, Collection *child)
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
void BKE_light_linking_add_receiver_to_collection_after(struct Main *bmain, struct Collection *collection, struct ID *receiver, const struct ID *after, const eCollectionLightLinkingState link_state)
void id_us_plus(ID *id)
Definition lib_id.cc:433
void BKE_light_linking_copy_collection(Main *bmain, Object &object_dst, const Object &object_src, LightLinkingType link_type)
bool BKE_collection_child_add(Main *bmain, Collection *parent, Collection *child)
void BKE_light_linking_free_if_empty(struct Object *object)
void BKE_light_linking_delete(struct Object *object, const int delete_flags)
static CollectionLightLinking * light_linking_collection_add_object(Main *bmain, Collection *collection, Object *object)
static void order_collection_receiver_before(Collection *collection, Collection *receiver, const ID *before)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void BKE_light_linking_copy_receiver_collection(Main *bmain, Object &object_dst, const Object &object_src)
void BKE_light_linking_collection_assign_only(struct Object *object, struct Collection *new_collection, LightLinkingType link_type)
void BKE_light_linking_copy_blocker_collection(Main *bmain, Object &object_dst, const Object &object_src)
static CollectionObject * find_collection_object(const Collection *collection, const Object *object)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
struct Collection * BKE_light_linking_collection_get(const struct Object *object, LightLinkingType link_type)
bool BKE_collection_contains_geometry_recursive(const Collection *collection)
void BKE_light_linking_select_receivers_of_emitter(const Main &bmain, struct Scene *scene, struct ViewLayer *view_layer, struct Object *emitter, LightLinkingType link_type)
static void light_linking_add_receiver_to_collection(Main *bmain, Collection *collection, ID *receiver, const eCollectionLightLinkingState link_state, const bool allow_all_objects)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool BKE_collection_child_remove(Main *bmain, Collection *parent, Collection *child)
static void order_object_receiver_after(Collection *collection, Object *receiver, const ID *after)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
static void order_collection_receiver_after(Collection *collection, Collection *receiver, const ID *after)
static std::string get_default_collection_name(const Object *object, const LightLinkingType link_type)
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
void BKE_light_linking_ensure(struct Object *object)
bool BKE_light_linking_unlink_id_from_collection(struct Main *bmain, struct Collection *collection, struct ID *id, struct ReportList *reports)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
bool BKE_collection_object_remove(Main *bmain, Collection *collection, Object *ob, bool free_us)
void BKE_light_linking_add_receiver_to_collection_before(struct Main *bmain, struct Collection *collection, struct ID *receiver, const struct ID *before, const eCollectionLightLinkingState link_state)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_light_linking_link_receiver_to_emitter(struct Main *bmain, struct Object *emitter, struct Object *receiver, LightLinkingType link_type, eCollectionLightLinkingState link_state)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_HIERARCHY
Definition DNA_ID.h:1158
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
void BKE_light_linking_add_receiver_to_collection(struct Main *bmain, struct Collection *collection, struct ID *receiver, const eCollectionLightLinkingState link_state)
bool DEG_object_has_geometry_component(Object *object)
void DEG_relations_tag_update(Main *bmain)
void BKE_light_linking_collection_assign(struct Main *bmain, struct Object *object, struct Collection *new_collection, LightLinkingType link_type)
void id_us_min(ID *id)
Definition lib_id.cc:441
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
static void order_object_receiver_before(Collection *collection, Object *receiver, const ID *before)
static CollectionChild * find_collection_child(const Collection *collection, const Collection *child)
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
CollectionLightLinking light_linking
eCollectionLightLinkingState link_state
CollectionLightLinking light_linking
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
char name[258]
Definition DNA_ID.h:433
struct Collection * blocker_collection
struct Collection * receiver_collection
LightLinking * light_linking
struct Collection * instance_collection