Blender V5.0
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_defaults.h"
19#include "DNA_scene_types.h"
20#include "DNA_texture_types.h"
21#include "DNA_world_types.h"
22
23#include "BLI_listbase.h"
24#include "BLI_utildefines.h"
25
26#include "BKE_icons.h"
27#include "BKE_idtype.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_lib_query.hh"
30#include "BKE_node.hh"
31#include "BKE_preview_image.hh"
32#include "BKE_world.h"
33
34#include "BLT_translation.hh"
35
36#include "DRW_engine.hh"
37
38#include "DEG_depsgraph.hh"
39
40#include "GPU_material.hh"
41
42#include "BLO_read_write.hh"
43
45static void world_free_data(ID *id)
46{
47 World *wrld = (World *)id;
48
49 /* is no lib link block, but world extension */
50 if (wrld->nodetree) {
52 MEM_freeN(wrld->nodetree);
53 wrld->nodetree = nullptr;
54 }
55
57
58 BKE_icon_id_delete((ID *)wrld);
60
62}
63
64static void world_init_data(ID *id)
65{
66 World *wrld = (World *)id;
68
70}
71
82static void world_copy_data(Main *bmain,
83 std::optional<Library *> owner_library,
84 ID *id_dst,
85 const ID *id_src,
86 const int flag)
87{
88 World *wrld_dst = (World *)id_dst;
89 const World *wrld_src = (const World *)id_src;
90
91 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
92 /* Never handle user-count here for own sub-data. */
93 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
94 /* Always need allocation of the embedded ID data. */
95 const int flag_embedded_id_data = flag_subdata & ~LIB_ID_CREATE_NO_ALLOCATE;
96
97 if (wrld_src->nodetree) {
98 if (is_localized) {
99 wrld_dst->nodetree = blender::bke::node_tree_localize(wrld_src->nodetree, &wrld_dst->id);
100 }
101 else {
102 BKE_id_copy_in_lib(bmain,
103 owner_library,
104 &wrld_src->nodetree->id,
105 &wrld_dst->id,
106 reinterpret_cast<ID **>(&wrld_dst->nodetree),
107 flag_embedded_id_data);
108 }
109 }
110
112
113 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
114 BKE_previewimg_id_copy(&wrld_dst->id, &wrld_src->id);
115 }
116 else {
117 wrld_dst->preview = nullptr;
118 }
119
120 if (wrld_src->lightgroup) {
122 }
123}
124
126{
127 World *world = reinterpret_cast<World *>(id);
128
129 if (world->nodetree) {
130 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
133 }
134}
135
137{
138 World *world = reinterpret_cast<World *>(id);
139
140 fn.single(&world->horr);
141}
142
143static void world_blend_write(BlendWriter *writer, ID *id, const void *id_address)
144{
145 World *wrld = (World *)id;
146
147 /* Clean up runtime data, important in undo case to reduce false detection of changed
148 * datablocks. */
150 wrld->last_update = 0;
151
152 /* Set deprecated #use_nodes for forward compatibility. */
153 wrld->use_nodes = true;
154
155 /* write LibData */
156 BLO_write_id_struct(writer, World, id_address, &wrld->id);
157 BKE_id_blend_write(writer, &wrld->id);
158
159 /* nodetree is integral part of world, no libdata */
160 if (wrld->nodetree) {
161 BLO_Write_IDBuffer temp_embedded_id_buffer{wrld->nodetree->id, writer};
162 BLO_write_struct_at_address(writer, bNodeTree, wrld->nodetree, temp_embedded_id_buffer.get());
164 writer, reinterpret_cast<bNodeTree *>(temp_embedded_id_buffer.get()));
165 }
166
167 BKE_previewimg_blend_write(writer, wrld->preview);
168
169 if (wrld->lightgroup) {
171 }
172}
173
174static void world_blend_read_data(BlendDataReader *reader, ID *id)
175{
176 World *wrld = (World *)id;
177
178 BLO_read_struct(reader, PreviewImage, &wrld->preview);
179 BKE_previewimg_blend_read(reader, wrld->preview);
181
183}
184
186 /*id_code*/ World::id_type,
187 /*id_filter*/ FILTER_ID_WO,
188 /*dependencies_id_types*/ FILTER_ID_TE,
189 /*main_listbase_index*/ INDEX_ID_WO,
190 /*struct_size*/ sizeof(World),
191 /*name*/ "World",
192 /*name_plural*/ N_("worlds"),
193 /*translation_context*/ BLT_I18NCONTEXT_ID_WORLD,
195 /*asset_type_info*/ nullptr,
196
197 /*init_data*/ world_init_data,
198 /*copy_data*/ world_copy_data,
199 /*free_data*/ world_free_data,
200 /*make_local*/ nullptr,
201 /*foreach_id*/ world_foreach_id,
202 /*foreach_cache*/ nullptr,
203 /*foreach_path*/ nullptr,
204 /*foreach_working_space_color*/ world_foreach_working_space_color,
205 /*owner_pointer_get*/ nullptr,
206
207 /*blend_write*/ world_blend_write,
208 /*blend_read_data*/ world_blend_read_data,
209 /*blend_read_after_liblink*/ nullptr,
210
211 /*blend_read_undo_preserve*/ nullptr,
212
213 /*lib_override_apply_post*/ nullptr,
214};
215
216World *BKE_world_add(Main *bmain, const char *name)
217{
218 World *wrld;
219
220 wrld = BKE_id_new<World>(bmain, name);
221
222 return wrld;
223}
224
225void BKE_world_eval(Depsgraph *depsgraph, World *world)
226{
227 DEG_debug_print_eval(depsgraph, __func__, world->id.name, world);
230}
void BKE_icon_id_delete(struct ID *id)
Definition icons.cc:437
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:47
IDTypeInfo IDType_ID_WO
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:675
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1514
@ LIB_ID_CREATE_NO_ALLOCATE
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2631
#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:172
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:46
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLO_write_struct_at_address(writer, struct_name, address, data_ptr)
#define BLT_I18NCONTEXT_ID_WORLD
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:355
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
#define FILTER_ID_TE
Definition DNA_ID.h:1220
@ INDEX_ID_WO
Definition DNA_ID.h:1314
#define FILTER_ID_WO
Definition DNA_ID.h:1223
#define DNA_struct_default_get(struct_name)
void GPU_material_free(ListBase *gpumaterial)
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
static void world_free_data(ID *id)
static void world_blend_write(BlendWriter *writer, ID *id, const void *id_address)
void BKE_world_eval(Depsgraph *depsgraph, World *world)
static void world_blend_read_data(BlendDataReader *reader, ID *id)
static void world_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
World * BKE_world_add(Main *bmain, const char *name)
static void world_foreach_id(ID *id, LibraryForeachIDData *data)
static void world_init_data(ID *id)
static void world_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
BMesh const char void * data
BPy_StructRNA * depsgraph
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:1140
bNodeTree * node_tree_localize(bNodeTree *ntree, std::optional< ID * > new_owner_id)
Definition node.cc:4586
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:4462
const char * name
const blender::FunctionRef< void(float rgb[3])> single
Definition DNA_ID.h:414
char name[258]
Definition DNA_ID.h:432
struct bNodeTree * nodetree
struct LightgroupMembership * lightgroup
struct PreviewImage * preview
uint64_t last_update
ListBase gpumaterial
float horr
#define N_(msgid)
uint8_t flag
Definition wm_window.cc:145