Blender V4.3
blenkernel/intern/light.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstdlib>
10#include <optional>
11
12#include "MEM_guardedalloc.h"
13
14/* Allow using deprecated functionality for .blend file I/O. */
15#define DNA_DEPRECATED_ALLOW
16
17#include "DNA_defaults.h"
18#include "DNA_light_types.h"
19#include "DNA_node_types.h"
20#include "DNA_scene_types.h"
21#include "DNA_texture_types.h"
22
23#include "BLI_utildefines.h"
24
25#include "BKE_icons.h"
26#include "BKE_idtype.hh"
27#include "BKE_lib_id.hh"
28#include "BKE_lib_query.hh"
29#include "BKE_light.h"
30#include "BKE_node.hh"
31#include "BKE_preview_image.hh"
32
33#include "BLT_translation.hh"
34
35#include "DEG_depsgraph.hh"
36
37#include "BLO_read_write.hh"
38
39static void light_init_data(ID *id)
40{
41 Light *la = (Light *)id;
43
45}
46
57static void light_copy_data(Main *bmain,
58 std::optional<Library *> owner_library,
59 ID *id_dst,
60 const ID *id_src,
61 const int flag)
62{
63 Light *la_dst = (Light *)id_dst;
64 const Light *la_src = (const Light *)id_src;
65
66 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
67 /* We always need allocation of our private ID data.
68 * User reference-counting is also handled by calling code,
69 * so the duplication calls for embedded data should _never_ handle it from here. */
70 const int flag_embedded_id_data = (flag & ~LIB_ID_CREATE_NO_ALLOCATE) |
72
73 if (la_src->nodetree) {
74 if (is_localized) {
75 la_dst->nodetree = blender::bke::node_tree_localize(la_src->nodetree, &la_dst->id);
76 }
77 else {
79 owner_library,
80 &la_src->nodetree->id,
81 &la_dst->id,
82 reinterpret_cast<ID **>(&la_dst->nodetree),
83 flag_embedded_id_data);
84 }
85 }
86
87 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
88 BKE_previewimg_id_copy(&la_dst->id, &la_src->id);
89 }
90 else {
91 la_dst->preview = nullptr;
92 }
93}
94
95static void light_free_data(ID *id)
96{
97 Light *la = (Light *)id;
98
99 /* is no lib link block, but light extension */
100 if (la->nodetree) {
102 MEM_freeN(la->nodetree);
103 la->nodetree = nullptr;
104 }
105
108 la->id.icon_id = 0;
109}
110
112{
113 Light *lamp = reinterpret_cast<Light *>(id);
115
116 if (lamp->nodetree) {
117 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
120 }
121
124 }
125}
126
127static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
128{
129 Light *la = (Light *)id;
130
131 /* Forward compatibility for energy. */
132 la->energy_deprecated = la->energy;
133 if (la->type == LA_AREA) {
134 la->energy_deprecated /= M_PI_4;
135 }
136
137 /* write LibData */
138 BLO_write_id_struct(writer, Light, id_address, &la->id);
139 BKE_id_blend_write(writer, &la->id);
140
141 /* Node-tree is integral part of lights, no libdata. */
142 if (la->nodetree) {
143 BLO_Write_IDBuffer *temp_embedded_id_buffer = BLO_write_allocate_id_buffer();
145 temp_embedded_id_buffer, &la->nodetree->id, BLO_write_is_undo(writer));
147 writer, bNodeTree, la->nodetree, BLO_write_get_id_buffer_temp_id(temp_embedded_id_buffer));
149 writer, (bNodeTree *)BLO_write_get_id_buffer_temp_id(temp_embedded_id_buffer));
150 BLO_write_destroy_id_buffer(&temp_embedded_id_buffer);
151 }
152
154}
155
156static void light_blend_read_data(BlendDataReader *reader, ID *id)
157{
158 Light *la = (Light *)id;
159
160 BLO_read_struct(reader, PreviewImage, &la->preview);
162}
163
165 /*id_code*/ ID_LA,
166 /*id_filter*/ FILTER_ID_LA,
167 /*dependencies_id_types*/ FILTER_ID_TE,
168 /*main_listbase_index*/ INDEX_ID_LA,
169 /*struct_size*/ sizeof(Light),
170 /*name*/ "Light",
171 /*name_plural*/ N_("lights"),
172 /*translation_context*/ BLT_I18NCONTEXT_ID_LIGHT,
174 /*asset_type_info*/ nullptr,
175
176 /*init_data*/ light_init_data,
177 /*copy_data*/ light_copy_data,
178 /*free_data*/ light_free_data,
179 /*make_local*/ nullptr,
180 /*foreach_id*/ light_foreach_id,
181 /*foreach_cache*/ nullptr,
182 /*foreach_path*/ nullptr,
183 /*owner_pointer_get*/ nullptr,
184
185 /*blend_write*/ light_blend_write,
186 /*blend_read_data*/ light_blend_read_data,
187 /*blend_read_after_liblink*/ nullptr,
188
189 /*blend_read_undo_preserve*/ nullptr,
190
191 /*lib_override_apply_post*/ nullptr,
192};
193
194Light *BKE_light_add(Main *bmain, const char *name)
195{
196 Light *la;
197
198 la = static_cast<Light *>(BKE_id_new(bmain, ID_LA, name));
199
200 return la;
201}
202
203void BKE_light_eval(Depsgraph *depsgraph, Light *la)
204{
205 DEG_debug_print_eval(depsgraph, __func__, la->id.name, la);
206}
void BKE_icon_id_delete(struct ID *id)
Definition icons.cc:451
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:39
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_USER_REFCOUNT
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, const ID *new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:656
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1482
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2560
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:163
@ IDWALK_CB_USER
int BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:120
@ IDWALK_DO_DEPRECATED_POINTERS
#define BKE_LIB_FOREACHID_PROCESS_ID_NOCHECK(data_, id_, cb_flag_)
General operations, lookup, etc. for blender lights.
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
void BKE_previewimg_free(PreviewImage **prv)
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
#define BLI_assert(a)
Definition BLI_assert.h:50
#define M_PI_4
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
BLO_Write_IDBuffer * BLO_write_allocate_id_buffer()
#define BLO_write_id_struct(writer, struct_name, id_address, id)
void BLO_write_init_id_buffer_from_id(BLO_Write_IDBuffer *id_buffer, ID *id, const bool is_undo)
void BLO_write_destroy_id_buffer(BLO_Write_IDBuffer **id_buffer)
ID * BLO_write_get_id_buffer_temp_id(BLO_Write_IDBuffer *id_buffer)
#define BLO_read_struct(reader, struct_name, ptr_p)
bool BLO_write_is_undo(BlendWriter *writer)
#define BLO_write_struct_at_address(writer, struct_name, address, data_ptr)
#define BLT_I18NCONTEXT_ID_LIGHT
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
#define FILTER_ID_LA
Definition DNA_ID.h:1172
#define FILTER_ID_TE
Definition DNA_ID.h:1187
@ INDEX_ID_LA
Definition DNA_ID.h:1300
@ ID_LA
#define DNA_struct_default_get(struct_name)
struct Light Light
@ LA_AREA
Read Guarded memory(de)allocation.
static void light_foreach_id(ID *id, LibraryForeachIDData *data)
IDTypeInfo IDType_ID_LA
void BKE_light_eval(Depsgraph *depsgraph, Light *la)
static void light_blend_read_data(BlendDataReader *reader, ID *id)
static void light_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
static void light_free_data(ID *id)
Light * BKE_light_add(Main *bmain, const char *name)
static void light_init_data(ID *id)
const Depsgraph * depsgraph
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
bNodeTree * node_tree_localize(bNodeTree *ntree, ID *new_owner_id)
Definition node.cc:3750
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:760
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:3632
Definition DNA_ID.h:413
int icon_id
Definition DNA_ID.h:436
char name[66]
Definition DNA_ID.h:425
float energy
struct PreviewImage * preview
struct bNodeTree * nodetree
short type
#define N_(msgid)
uint8_t flag
Definition wm_window.cc:138