Blender  V2.93
light.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include <stdlib.h>
25 
26 #include "MEM_guardedalloc.h"
27 
28 /* Allow using deprecated functionality for .blend file I/O. */
29 #define DNA_DEPRECATED_ALLOW
30 
31 #include "DNA_anim_types.h"
32 #include "DNA_defaults.h"
33 #include "DNA_light_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_texture_types.h"
39 
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42 
43 #include "BKE_anim_data.h"
44 #include "BKE_colortools.h"
45 #include "BKE_icons.h"
46 #include "BKE_idtype.h"
47 #include "BKE_lib_id.h"
48 #include "BKE_lib_query.h"
49 #include "BKE_light.h"
50 #include "BKE_main.h"
51 #include "BKE_node.h"
52 
53 #include "BLT_translation.h"
54 
55 #include "DEG_depsgraph.h"
56 
57 #include "BLO_read_write.h"
58 
59 static void light_init_data(ID *id)
60 {
61  Light *la = (Light *)id;
63 
65 
66  la->curfalloff = BKE_curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
68 }
69 
80 static void light_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
81 {
82  Light *la_dst = (Light *)id_dst;
83  const Light *la_src = (const Light *)id_src;
84 
85  const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
86  /* We always need allocation of our private ID data. */
87  const int flag_private_id_data = flag & ~LIB_ID_CREATE_NO_ALLOCATE;
88 
89  la_dst->curfalloff = BKE_curvemapping_copy(la_src->curfalloff);
90 
91  if (la_src->nodetree) {
92  if (is_localized) {
93  la_dst->nodetree = ntreeLocalize(la_src->nodetree);
94  }
95  else {
97  bmain, (ID *)la_src->nodetree, (ID **)&la_dst->nodetree, flag_private_id_data);
98  }
99  }
100 
101  if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
102  BKE_previewimg_id_copy(&la_dst->id, &la_src->id);
103  }
104  else {
105  la_dst->preview = NULL;
106  }
107 }
108 
109 static void light_free_data(ID *id)
110 {
111  Light *la = (Light *)id;
112 
114 
115  /* is no lib link block, but light extension */
116  if (la->nodetree) {
118  MEM_freeN(la->nodetree);
119  la->nodetree = NULL;
120  }
121 
123  BKE_icon_id_delete(&la->id);
124  la->id.icon_id = 0;
125 }
126 
128 {
129  Light *lamp = (Light *)id;
130  if (lamp->nodetree) {
131  /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
133  }
134 }
135 
136 static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
137 {
138  Light *la = (Light *)id;
139  if (la->id.us > 0 || BLO_write_is_undo(writer)) {
140  /* write LibData */
141  BLO_write_id_struct(writer, Light, id_address, &la->id);
142  BKE_id_blend_write(writer, &la->id);
143 
144  if (la->adt) {
145  BKE_animdata_blend_write(writer, la->adt);
146  }
147 
148  if (la->curfalloff) {
150  }
151 
152  /* Node-tree is integral part of lights, no libdata. */
153  if (la->nodetree) {
154  BLO_write_struct(writer, bNodeTree, la->nodetree);
155  ntreeBlendWrite(writer, la->nodetree);
156  }
157 
158  BKE_previewimg_blend_write(writer, la->preview);
159  }
160 }
161 
162 static void light_blend_read_data(BlendDataReader *reader, ID *id)
163 {
164  Light *la = (Light *)id;
165  BLO_read_data_address(reader, &la->adt);
166  BKE_animdata_blend_read_data(reader, la->adt);
167 
168  BLO_read_data_address(reader, &la->curfalloff);
169  if (la->curfalloff) {
171  }
172 
173  BLO_read_data_address(reader, &la->preview);
174  BKE_previewimg_blend_read(reader, la->preview);
175 }
176 
177 static void light_blend_read_lib(BlendLibReader *reader, ID *id)
178 {
179  Light *la = (Light *)id;
180  BLO_read_id_address(reader, la->id.lib, &la->ipo); // XXX deprecated - old animation system
181 }
182 
183 static void light_blend_read_expand(BlendExpander *expander, ID *id)
184 {
185  Light *la = (Light *)id;
186  BLO_expand(expander, la->ipo); // XXX deprecated - old animation system
187 }
188 
190  .id_code = ID_LA,
191  .id_filter = FILTER_ID_LA,
192  .main_listbase_index = INDEX_ID_LA,
193  .struct_size = sizeof(Light),
194  .name = "Light",
195  .name_plural = "lights",
196  .translation_context = BLT_I18NCONTEXT_ID_LIGHT,
197  .flags = 0,
198 
200  .copy_data = light_copy_data,
201  .free_data = light_free_data,
202  .make_local = NULL,
203  .foreach_id = light_foreach_id,
204  .foreach_cache = NULL,
205  .owner_get = NULL,
206 
207  .blend_write = light_blend_write,
208  .blend_read_data = light_blend_read_data,
209  .blend_read_lib = light_blend_read_lib,
210  .blend_read_expand = light_blend_read_expand,
211 
212  .blend_read_undo_preserve = NULL,
213 
214  .lib_override_apply_post = NULL,
215 };
216 
217 Light *BKE_light_add(Main *bmain, const char *name)
218 {
219  Light *la;
220 
221  la = BKE_id_new(bmain, ID_LA, name);
222 
223  return la;
224 }
225 
227 {
228  DEG_debug_print_eval(depsgraph, __func__, la->id.name, la);
229 }
void BKE_animdata_blend_read_data(struct BlendDataReader *reader, struct AnimData *adt)
Definition: anim_data.c:1574
void BKE_animdata_blend_write(struct BlendWriter *writer, struct AnimData *adt)
Definition: anim_data.c:1552
void BKE_curvemapping_init(struct CurveMapping *cumap)
Definition: colortools.c:1200
struct CurveMapping * BKE_curvemapping_copy(const struct CurveMapping *cumap)
void BKE_curvemapping_blend_read(struct BlendDataReader *reader, struct CurveMapping *cumap)
Definition: colortools.c:1252
void BKE_curvemapping_blend_write(struct BlendWriter *writer, const struct CurveMapping *cumap)
void BKE_curvemapping_free(struct CurveMapping *cumap)
Definition: colortools.c:119
struct CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition: colortools.c:88
void BKE_icon_id_delete(struct ID *id)
Definition: icons.cc:919
void BKE_previewimg_free(struct PreviewImage **prv)
Definition: icons.cc:295
void BKE_previewimg_id_copy(struct ID *new_id, const struct ID *old_id)
void BKE_previewimg_blend_read(struct BlendDataReader *reader, struct PreviewImage *prv)
Definition: icons.cc:651
void BKE_previewimg_blend_write(struct BlendWriter *writer, const struct PreviewImage *prv)
@ LIB_ID_CREATE_NO_ALLOCATE
Definition: BKE_lib_id.h:96
@ LIB_ID_COPY_NO_PREVIEW
Definition: BKE_lib_id.h:116
@ LIB_ID_CREATE_LOCAL
Definition: BKE_lib_id.h:105
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, const int flag)
void BKE_id_blend_write(struct BlendWriter *writer, struct ID *id)
Definition: lib_id.c:2395
void * BKE_id_new(struct Main *bmain, const short type, const char *name)
Definition: lib_id.c:1177
bool BKE_library_foreach_ID_embedded(struct LibraryForeachIDData *data, struct ID **id_pp)
Definition: lib_query.c:161
General operations, lookup, etc. for blender lights.
void ntreeBlendWrite(struct BlendWriter *writer, struct bNodeTree *ntree)
Definition: node.cc:472
void ntreeFreeEmbeddedTree(struct bNodeTree *ntree)
Definition: node.cc:3021
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define BLO_read_id_address(reader, lib, id_ptr_p)
#define BLO_expand(expander, id)
bool BLO_write_is_undo(BlendWriter *writer)
Definition: writefile.c:1412
#define BLT_I18NCONTEXT_ID_LIGHT
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
void DEG_debug_print_eval(struct Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
#define FILTER_ID_LA
Definition: DNA_ID.h:713
@ INDEX_ID_LA
Definition: DNA_ID.h:834
@ ID_LA
Definition: DNA_ID_enums.h:67
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
struct Light Light
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
static void init_data(ModifierData *md)
Light lamp
const Depsgraph * depsgraph
static void light_blend_read_expand(BlendExpander *expander, ID *id)
Definition: light.c:183
static void light_foreach_id(ID *id, LibraryForeachIDData *data)
Definition: light.c:127
Light * BKE_light_add(Main *bmain, const char *name)
Definition: light.c:217
void BKE_light_eval(struct Depsgraph *depsgraph, Light *la)
Definition: light.c:226
IDTypeInfo IDType_ID_LA
Definition: light.c:189
static void light_blend_read_data(BlendDataReader *reader, ID *id)
Definition: light.c:162
static void light_blend_read_lib(BlendLibReader *reader, ID *id)
Definition: light.c:177
static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition: light.c:136
static void light_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
Definition: light.c:80
static void light_free_data(ID *id)
Definition: light.c:109
static void light_init_data(ID *id)
Definition: light.c:59
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
short id_code
Definition: BKE_idtype.h:120
Definition: DNA_ID.h:273
struct Library * lib
Definition: DNA_ID.h:277
int us
Definition: DNA_ID.h:293
int icon_id
Definition: DNA_ID.h:294
char name[66]
Definition: DNA_ID.h:283
struct PreviewImage * preview
struct AnimData * adt
struct CurveMapping * curfalloff
struct bNodeTree * nodetree
Definition: BKE_main.h:116