Blender V5.3
blenkernel/intern/world.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
8
9#include <cstdlib>
10#include <cstring>
11#include <optional>
12
13#include "MEM_guardedalloc.h"
14
15/* Allow using deprecated functionality for .blend file I/O. */
16#define DNA_DEPRECATED_ALLOW
17
18#include "DNA_layer_types.h"
19#include "DNA_scene_types.h"
20#include "DNA_world_types.h"
21
22#include "BLI_listbase.hh"
23#include "BLI_utildefines.hh"
24
25#include "BKE_icons.hh"
26#include "BKE_idtype.hh"
27#include "BKE_lib_id.hh"
28#include "BKE_lib_query.hh"
29#include "BKE_node.hh"
30#include "BKE_preview_image.hh"
31#include "BKE_world.h"
32
33#include "BLT_translation.hh"
34
35#include "DRW_engine.hh"
36
37#include "DEG_depsgraph.hh"
38
39#include "GPU_material.hh"
40
41#include "BLO_read_write.hh"
42
43#include "NOD_shader.h"
44
45namespace blender {
46
48static void world_free_data(ID *id)
49{
50 World *wrld = id_cast<World *>(id);
51
52 /* is no lib link block, but world extension */
53 if (wrld->nodetree) {
55 MEM_delete(wrld->nodetree);
56 wrld->nodetree = nullptr;
57 }
58
59 GPU_material_free(&wrld->gpumaterial);
60
61 BKE_icon_id_delete(id_cast<ID *>(wrld));
62 BKE_previewimg_id_free(&wrld->id);
63
64 MEM_SAFE_DELETE(wrld->lightgroup);
65}
66
67static void world_init_data(ID *id)
68{
69 World *wrld = id_cast<World *>(id);
71
72 wrld->nodetree = bke::node_tree_add_tree_embedded(
73 nullptr, &wrld->id, "World Nodetree", ntreeType_Shader->idname.ref());
74}
75
86static void world_copy_data(Main *bmain,
87 std::optional<Library *> owner_library,
88 ID *id_dst,
89 const ID *id_src,
90 const int flag)
91{
92 World *wrld_dst = id_cast<World *>(id_dst);
93 const World *wrld_src = id_cast<const World *>(id_src);
94
95 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
96 /* Never handle user-count here for own sub-data. */
97 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
98 /* Always need allocation of the embedded ID data. */
99 const int flag_embedded_id_data = flag_subdata & ~LIB_ID_CREATE_NO_ALLOCATE;
100
101 if (wrld_src->nodetree) {
102 if (is_localized) {
103 wrld_dst->nodetree = bke::node_tree_localize(wrld_src->nodetree, &wrld_dst->id);
104 }
105 else {
106 BKE_id_copy_in_lib(bmain,
107 owner_library,
108 &wrld_src->nodetree->id,
109 &wrld_dst->id,
110 reinterpret_cast<ID **>(&wrld_dst->nodetree),
111 flag_embedded_id_data);
112 }
113 }
114
115 wrld_dst->gpumaterial.clear_no_delete();
116
117 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
118 BKE_previewimg_id_copy(&wrld_dst->id, &wrld_src->id);
119 }
120 else {
121 wrld_dst->preview = nullptr;
122 }
123
124 if (wrld_src->lightgroup) {
125 wrld_dst->lightgroup = MEM_dupalloc(wrld_src->lightgroup);
126 }
127}
128
130{
131 World *world = reinterpret_cast<World *>(id);
132
133 if (world->nodetree) {
134 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
136 data, BKE_library_foreach_ID_embedded(data, (ID **)&world->nodetree));
137 }
138}
139
141{
142 World *world = reinterpret_cast<World *>(id);
143
144 fn.single(&world->horr);
145}
146
147static void world_blend_write(BlendWriter *writer, ID *id, const void *id_address)
148{
149 World *wrld = id_cast<World *>(id);
150
151 /* Clean up runtime data, important in undo case to reduce false detection of changed
152 * datablocks. */
153 wrld->gpumaterial.clear_no_delete();
154 wrld->last_update = 0;
155
156 /* Set deprecated #use_nodes for forward compatibility. */
157 wrld->use_nodes = true;
158
159 /* write LibData */
160 writer->write_id_struct(id_address, wrld);
161 BKE_id_blend_write(writer, &wrld->id);
162
163 /* nodetree is integral part of world, no libdata */
164 if (wrld->nodetree) {
165 BLO_Write_IDBuffer temp_embedded_id_buffer{wrld->nodetree->id, writer};
166 bNodeTree *temp_ntree = reinterpret_cast<bNodeTree *>(temp_embedded_id_buffer.get());
167 writer->write_embedded_id_struct(wrld->nodetree, temp_ntree);
168 bke::node_tree_blend_write(writer, temp_ntree);
169 }
170
171 BKE_previewimg_blend_write(writer, wrld->preview);
172
173 if (wrld->lightgroup) {
174 writer->write_struct(wrld->lightgroup);
175 }
176}
177
178static void world_blend_read_data(BlendDataReader *reader, ID *id)
179{
180 World *wrld = id_cast<World *>(id);
181
182 BLO_read_struct(reader, PreviewImage, &wrld->preview);
183 BKE_previewimg_blend_read(reader, wrld->preview);
184 wrld->gpumaterial.clear_no_delete();
185
186 BLO_read_struct(reader, LightgroupMembership, &wrld->lightgroup);
187}
188
190 .id_code = World::id_type,
191 .id_filter = FILTER_ID_WO,
192 .dependencies_id_types = FILTER_ID_TE,
193 .main_listbase_index = INDEX_ID_WO,
194 .struct_size = sizeof(World),
195 .name = "World",
196 .name_plural = N_("worlds"),
197 .translation_context = BLT_I18NCONTEXT_ID_WORLD,
199 .asset_type_info = nullptr,
200
201 .init_data = world_init_data,
202 .copy_data = world_copy_data,
203 .free_data = world_free_data,
204 .make_local = nullptr,
205 .foreach_id = world_foreach_id,
206 .foreach_cache = nullptr,
207 .foreach_path = nullptr,
208 .foreach_working_space_color = world_foreach_working_space_color,
209 .foreach_asset_weak_reference = nullptr,
210 .owner_pointer_get = nullptr,
211
212 .blend_write = world_blend_write,
213 .blend_read_data = world_blend_read_data,
214 .blend_read_after_liblink = nullptr,
215
216 .blend_read_undo_preserve = nullptr,
217
218 .lib_override_apply_post = nullptr,
219};
220
221World *BKE_world_add(Main *bmain, const char *name)
222{
223 World *wrld;
224
225 wrld = BKE_id_new<World>(bmain, name);
226
227 return wrld;
228}
229
230void BKE_world_eval(Depsgraph *depsgraph, World *world)
231{
232 DEG_debug_print_eval(depsgraph, __func__, world->id.name, world);
233 GPU_material_free(&world->gpumaterial);
234 world->last_update = DEG_get_update_count(depsgraph);
235}
236
237} // namespace blender
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_WORLD
#define FILTER_ID_TE
Definition DNA_ID.h:1222
#define FILTER_ID_WO
Definition DNA_ID.h:1225
Read Blender memory allocation and freeing.
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
StringRefNull ref() const
bNodeTree * node_tree_add_tree_embedded(Main *bmain, ID *owner_id, StringRefNull name, StringRefNull idname)
Definition node.cc:5005
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:1361
bNodeTree * node_tree_localize(bNodeTree *ntree, std::optional< ID * > new_owner_id)
Definition node.cc:5355
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:5231
BMesh Iterators.
static void world_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
void BKE_icon_id_delete(ID *id)
Definition icons.cc:524
void BKE_previewimg_id_free(ID *id)
static void world_blend_read_data(BlendDataReader *reader, ID *id)
static void world_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:356
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
void BKE_world_eval(struct Depsgraph *depsgraph, struct World *world)
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
static void world_foreach_id(ID *id, LibraryForeachIDData *data)
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_ALLOCATE
static void world_blend_write(BlendWriter *writer, ID *id, const void *id_address)
void GPU_material_free(ListBaseT< LinkData > *gpumaterial)
BMesh const char void * data
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:176
IDTypeInfo IDType_ID_WO
struct World * BKE_world_add(struct Main *bmain, const char *name)
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
struct bke::bNodeTreeType * ntreeType_Shader
@ INDEX_ID_WO
Definition DNA_ID.h:1316
static void world_init_data(ID *id)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
static void world_free_data(ID *id)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void write_embedded_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
#define N_(msgid)