Blender V4.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 <cmath>
10#include <cstdlib>
11#include <cstring>
12#include <optional>
13
14#include "MEM_guardedalloc.h"
15
16/* Allow using deprecated functionality for .blend file I/O. */
17#define DNA_DEPRECATED_ALLOW
18
19#include "DNA_defaults.h"
20#include "DNA_scene_types.h"
21#include "DNA_texture_types.h"
22#include "DNA_world_types.h"
23
24#include "BLI_listbase.h"
25#include "BLI_utildefines.h"
26
27#include "BKE_icons.h"
28#include "BKE_idtype.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_lib_query.hh"
31#include "BKE_node.hh"
32#include "BKE_preview_image.hh"
33#include "BKE_world.h"
34
35#include "BLT_translation.hh"
36
37#include "DRW_engine.hh"
38
39#include "DEG_depsgraph.hh"
40
41#include "GPU_material.hh"
42
43#include "BLO_read_write.hh"
44
46static void world_free_data(ID *id)
47{
48 World *wrld = (World *)id;
49
51
52 /* is no lib link block, but world extension */
53 if (wrld->nodetree) {
55 MEM_freeN(wrld->nodetree);
56 wrld->nodetree = nullptr;
57 }
58
60
61 BKE_icon_id_delete((ID *)wrld);
63
65}
66
67static void world_init_data(ID *id)
68{
69 World *wrld = (World *)id;
71
73}
74
85static void world_copy_data(Main *bmain,
86 std::optional<Library *> owner_library,
87 ID *id_dst,
88 const ID *id_src,
89 const int flag)
90{
91 World *wrld_dst = (World *)id_dst;
92 const World *wrld_src = (const World *)id_src;
93
94 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
95 /* Never handle user-count here for own sub-data. */
96 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
97 /* Always need allocation of the embedded ID data. */
98 const int flag_embedded_id_data = flag_subdata & ~LIB_ID_CREATE_NO_ALLOCATE;
99
100 if (wrld_src->nodetree) {
101 if (is_localized) {
102 wrld_dst->nodetree = blender::bke::node_tree_localize(wrld_src->nodetree, &wrld_dst->id);
103 }
104 else {
105 BKE_id_copy_in_lib(bmain,
106 owner_library,
107 &wrld_src->nodetree->id,
108 &wrld_dst->id,
109 reinterpret_cast<ID **>(&wrld_dst->nodetree),
110 flag_embedded_id_data);
111 }
112 }
113
115 BLI_listbase_clear((ListBase *)&wrld_dst->drawdata);
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) {
126 }
127}
128
130{
131 World *world = reinterpret_cast<World *>(id);
133
134 if (world->nodetree) {
135 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
138 }
139
142 }
143}
144
145static void world_blend_write(BlendWriter *writer, ID *id, const void *id_address)
146{
147 World *wrld = (World *)id;
148
149 /* Clean up runtime data, important in undo case to reduce false detection of changed
150 * datablocks. */
152 wrld->last_update = 0;
153
154 /* write LibData */
155 BLO_write_id_struct(writer, World, id_address, &wrld->id);
156 BKE_id_blend_write(writer, &wrld->id);
157
158 /* nodetree is integral part of world, no libdata */
159 if (wrld->nodetree) {
160 BLO_Write_IDBuffer *temp_embedded_id_buffer = BLO_write_allocate_id_buffer();
162 temp_embedded_id_buffer, &wrld->nodetree->id, BLO_write_is_undo(writer));
164 bNodeTree,
165 wrld->nodetree,
166 BLO_write_get_id_buffer_temp_id(temp_embedded_id_buffer));
168 writer, (bNodeTree *)BLO_write_get_id_buffer_temp_id(temp_embedded_id_buffer));
169 BLO_write_destroy_id_buffer(&temp_embedded_id_buffer);
170 }
171
172 BKE_previewimg_blend_write(writer, wrld->preview);
173
174 if (wrld->lightgroup) {
176 }
177}
178
179static void world_blend_read_data(BlendDataReader *reader, ID *id)
180{
181 World *wrld = (World *)id;
182
183 BLO_read_struct(reader, PreviewImage, &wrld->preview);
184 BKE_previewimg_blend_read(reader, wrld->preview);
186
188}
189
191 /*id_code*/ ID_WO,
192 /*id_filter*/ FILTER_ID_WO,
193 /*dependencies_id_types*/ FILTER_ID_TE,
194 /*main_listbase_index*/ INDEX_ID_WO,
195 /*struct_size*/ sizeof(World),
196 /*name*/ "World",
197 /*name_plural*/ N_("worlds"),
198 /*translation_context*/ BLT_I18NCONTEXT_ID_WORLD,
200 /*asset_type_info*/ nullptr,
201
202 /*init_data*/ world_init_data,
203 /*copy_data*/ world_copy_data,
204 /*free_data*/ world_free_data,
205 /*make_local*/ nullptr,
206 /*foreach_id*/ world_foreach_id,
207 /*foreach_cache*/ nullptr,
208 /*foreach_path*/ nullptr,
209 /*owner_pointer_get*/ nullptr,
210
211 /*blend_write*/ world_blend_write,
212 /*blend_read_data*/ world_blend_read_data,
213 /*blend_read_after_liblink*/ nullptr,
214
215 /*blend_read_undo_preserve*/ nullptr,
216
217 /*lib_override_apply_post*/ nullptr,
218};
219
220World *BKE_world_add(Main *bmain, const char *name)
221{
222 World *wrld;
223
224 wrld = static_cast<World *>(BKE_id_new(bmain, ID_WO, name));
225
226 return wrld;
227}
228
230{
231 DEG_debug_print_eval(depsgraph, __func__, world->id.name, world);
232 GPU_material_free(&world->gpumaterial);
233 world->last_update = DEG_get_update_count(depsgraph);
234}
void BKE_icon_id_delete(struct ID *id)
Definition icons.cc:451
IDTypeInfo IDType_ID_WO
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:39
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
@ 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:2560
@ IDWALK_DO_DEPRECATED_POINTERS
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
@ IDWALK_CB_USER
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:163
int BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:120
#define BKE_LIB_FOREACHID_PROCESS_ID_NOCHECK(data_, id_, cb_flag_)
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
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
#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)
#define BLO_write_struct(writer, struct_name, data_ptr)
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_WORLD
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:350
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:1187
@ INDEX_ID_WO
Definition DNA_ID.h:1282
#define FILTER_ID_WO
Definition DNA_ID.h:1190
@ ID_WO
#define DNA_struct_default_get(struct_name)
void DRW_drawdata_free(ID *id)
void GPU_material_free(ListBase *gpumaterial)
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between world
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)
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)
BPy_StructRNA * depsgraph
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
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
struct bNodeTree * nodetree
struct LightgroupMembership * lightgroup
struct PreviewImage * preview
uint64_t last_update
DrawDataList drawdata
ListBase gpumaterial
#define N_(msgid)
uint8_t flag
Definition wm_window.cc:138