2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Blender Foundation (2008).
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/makesrna/intern/rna_ID.c
31 #include "DNA_vfont_types.h"
32 #include "DNA_material_types.h"
33 #include "DNA_object_types.h"
35 #include "BLI_utildefines.h"
37 #include "BKE_icons.h"
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41 #include "RNA_enum_types.h"
45 #include "rna_internal.h"
47 /* enum of ID-block types
48 * NOTE: need to keep this in line with the other defines for these
50 EnumPropertyItem rna_enum_id_type_items[] = {
51 {ID_AC, "ACTION", ICON_ACTION, "Action", ""},
52 {ID_AR, "ARMATURE", ICON_ARMATURE_DATA, "Armature", ""},
53 {ID_BR, "BRUSH", ICON_BRUSH_DATA, "Brush", ""},
54 {ID_CA, "CAMERA", ICON_CAMERA_DATA, "Camera", ""},
55 {ID_CF, "CACHEFILE", ICON_FILE, "Cache File", ""},
56 {ID_CU, "CURVE", ICON_CURVE_DATA, "Curve", ""},
57 {ID_VF, "FONT", ICON_FONT_DATA, "Font", ""},
58 {ID_GD, "GREASEPENCIL", ICON_GREASEPENCIL, "Grease Pencil", ""},
59 {ID_GR, "GROUP", ICON_GROUP, "Group", ""},
60 {ID_IM, "IMAGE", ICON_IMAGE_DATA, "Image", ""},
61 {ID_KE, "KEY", ICON_SHAPEKEY_DATA, "Key", ""},
62 {ID_LA, "LAMP", ICON_LAMP_DATA, "Lamp", ""},
63 {ID_LI, "LIBRARY", ICON_LIBRARY_DATA_DIRECT, "Library", ""},
64 {ID_LS, "LINESTYLE", ICON_LINE_DATA, "Line Style", ""},
65 {ID_LT, "LATTICE", ICON_LATTICE_DATA, "Lattice", ""},
66 {ID_MSK, "MASK", ICON_MOD_MASK, "Mask", ""},
67 {ID_MA, "MATERIAL", ICON_MATERIAL_DATA, "Material", ""},
68 {ID_MB, "META", ICON_META_DATA, "Metaball", ""},
69 {ID_ME, "MESH", ICON_MESH_DATA, "Mesh", ""},
70 {ID_MC, "MOVIECLIP", ICON_CLIP, "Movie Clip", ""},
71 {ID_NT, "NODETREE", ICON_NODETREE, "Node Tree", ""},
72 {ID_OB, "OBJECT", ICON_OBJECT_DATA, "Object", ""},
73 {ID_PC, "PAINTCURVE", ICON_CURVE_BEZCURVE, "Paint Curve", ""},
74 {ID_PAL, "PALETTE", ICON_COLOR, "Palette", ""},
75 {ID_PA, "PARTICLE", ICON_PARTICLE_DATA, "Particle", ""},
76 {ID_SCE, "SCENE", ICON_SCENE_DATA, "Scene", ""},
77 {ID_SCR, "SCREEN", ICON_SPLITSCREEN, "Screen", ""},
78 {ID_SO, "SOUND", ICON_PLAY_AUDIO, "Sound", ""},
79 {ID_SPK, "SPEAKER", ICON_SPEAKER, "Speaker", ""},
80 {ID_TXT, "TEXT", ICON_TEXT, "Text", ""},
81 {ID_TE, "TEXTURE", ICON_TEXTURE_DATA, "Texture", ""},
82 {ID_WM, "WINDOWMANAGER", ICON_FULLSCREEN, "Window Manager", ""},
83 {ID_WO, "WORLD", ICON_WORLD_DATA, "World", ""},
84 {0, NULL, 0, NULL, NULL}
89 #include "DNA_anim_types.h"
91 #include "BLI_listbase.h"
94 #include "BKE_idprop.h"
95 #include "BKE_library.h"
96 #include "BKE_library_query.h"
97 #include "BKE_library_remap.h"
98 #include "BKE_animsys.h"
99 #include "BKE_material.h"
100 #include "BKE_depsgraph.h"
101 #include "BKE_global.h" /* XXX, remove me */
105 /* name functions that ignore the first two ID characters */
106 void rna_ID_name_get(PointerRNA *ptr, char *value)
108 ID *id = (ID *)ptr->data;
109 BLI_strncpy(value, id->name + 2, sizeof(id->name) - 2);
112 int rna_ID_name_length(PointerRNA *ptr)
114 ID *id = (ID *)ptr->data;
115 return strlen(id->name + 2);
118 void rna_ID_name_set(PointerRNA *ptr, const char *value)
120 ID *id = (ID *)ptr->data;
121 BLI_strncpy_utf8(id->name + 2, value, sizeof(id->name) - 2);
122 BLI_libblock_ensure_unique_name(G.main, id->name);
125 static int rna_ID_name_editable(PointerRNA *ptr)
127 ID *id = (ID *)ptr->data;
129 if (GS(id->name) == ID_VF) {
130 VFont *vfont = (VFont *)id;
131 if (BKE_vfont_is_builtin(vfont))
135 return PROP_EDITABLE;
138 short RNA_type_to_ID_code(StructRNA *type)
140 if (RNA_struct_is_a(type, &RNA_Action)) return ID_AC;
141 if (RNA_struct_is_a(type, &RNA_Armature)) return ID_AR;
142 if (RNA_struct_is_a(type, &RNA_Brush)) return ID_BR;
143 if (RNA_struct_is_a(type, &RNA_CacheFile)) return ID_CF;
144 if (RNA_struct_is_a(type, &RNA_Camera)) return ID_CA;
145 if (RNA_struct_is_a(type, &RNA_Curve)) return ID_CU;
146 if (RNA_struct_is_a(type, &RNA_GreasePencil)) return ID_GD;
147 if (RNA_struct_is_a(type, &RNA_Group)) return ID_GR;
148 if (RNA_struct_is_a(type, &RNA_Image)) return ID_IM;
149 if (RNA_struct_is_a(type, &RNA_Key)) return ID_KE;
150 if (RNA_struct_is_a(type, &RNA_Lamp)) return ID_LA;
151 if (RNA_struct_is_a(type, &RNA_Library)) return ID_LI;
152 if (RNA_struct_is_a(type, &RNA_FreestyleLineStyle)) return ID_LS;
153 if (RNA_struct_is_a(type, &RNA_Lattice)) return ID_LT;
154 if (RNA_struct_is_a(type, &RNA_Material)) return ID_MA;
155 if (RNA_struct_is_a(type, &RNA_MetaBall)) return ID_MB;
156 if (RNA_struct_is_a(type, &RNA_MovieClip)) return ID_MC;
157 if (RNA_struct_is_a(type, &RNA_Mesh)) return ID_ME;
158 if (RNA_struct_is_a(type, &RNA_Mask)) return ID_MSK;
159 if (RNA_struct_is_a(type, &RNA_NodeTree)) return ID_NT;
160 if (RNA_struct_is_a(type, &RNA_Object)) return ID_OB;
161 if (RNA_struct_is_a(type, &RNA_ParticleSettings)) return ID_PA;
162 if (RNA_struct_is_a(type, &RNA_Palette)) return ID_PAL;
163 if (RNA_struct_is_a(type, &RNA_PaintCurve)) return ID_PC;
164 if (RNA_struct_is_a(type, &RNA_Scene)) return ID_SCE;
165 if (RNA_struct_is_a(type, &RNA_Screen)) return ID_SCR;
166 if (RNA_struct_is_a(type, &RNA_Sound)) return ID_SO;
167 if (RNA_struct_is_a(type, &RNA_Speaker)) return ID_SPK;
168 if (RNA_struct_is_a(type, &RNA_Texture)) return ID_TE;
169 if (RNA_struct_is_a(type, &RNA_Text)) return ID_TXT;
170 if (RNA_struct_is_a(type, &RNA_VectorFont)) return ID_VF;
171 if (RNA_struct_is_a(type, &RNA_World)) return ID_WO;
172 if (RNA_struct_is_a(type, &RNA_WindowManager)) return ID_WM;
177 StructRNA *ID_code_to_RNA_type(short idcode)
180 case ID_AC: return &RNA_Action;
181 case ID_AR: return &RNA_Armature;
182 case ID_BR: return &RNA_Brush;
183 case ID_CA: return &RNA_Camera;
184 case ID_CF: return &RNA_CacheFile;
185 case ID_CU: return &RNA_Curve;
186 case ID_GD: return &RNA_GreasePencil;
187 case ID_GR: return &RNA_Group;
188 case ID_IM: return &RNA_Image;
189 case ID_KE: return &RNA_Key;
190 case ID_LA: return &RNA_Lamp;
191 case ID_LI: return &RNA_Library;
192 case ID_LS: return &RNA_FreestyleLineStyle;
193 case ID_LT: return &RNA_Lattice;
194 case ID_MA: return &RNA_Material;
195 case ID_MB: return &RNA_MetaBall;
196 case ID_MC: return &RNA_MovieClip;
197 case ID_ME: return &RNA_Mesh;
198 case ID_MSK: return &RNA_Mask;
199 case ID_NT: return &RNA_NodeTree;
200 case ID_OB: return &RNA_Object;
201 case ID_PA: return &RNA_ParticleSettings;
202 case ID_PAL: return &RNA_Palette;
203 case ID_PC: return &RNA_PaintCurve;
204 case ID_SCE: return &RNA_Scene;
205 case ID_SCR: return &RNA_Screen;
206 case ID_SO: return &RNA_Sound;
207 case ID_SPK: return &RNA_Speaker;
208 case ID_TE: return &RNA_Texture;
209 case ID_TXT: return &RNA_Text;
210 case ID_VF: return &RNA_VectorFont;
211 case ID_WM: return &RNA_WindowManager;
212 case ID_WO: return &RNA_World;
214 default: return &RNA_ID;
218 StructRNA *rna_ID_refine(PointerRNA *ptr)
220 ID *id = (ID *)ptr->data;
222 return ID_code_to_RNA_type(GS(id->name));
225 IDProperty *rna_ID_idprops(PointerRNA *ptr, bool create)
227 return IDP_GetProperties(ptr->data, create);
230 void rna_ID_fake_user_set(PointerRNA *ptr, int value)
232 ID *id = (ID *)ptr->data;
235 id_fake_user_set(id);
238 id_fake_user_clear(id);
242 IDProperty *rna_PropertyGroup_idprops(PointerRNA *ptr, bool UNUSED(create))
247 void rna_PropertyGroup_unregister(Main *UNUSED(bmain), StructRNA *type)
249 RNA_struct_free(&BLENDER_RNA, type);
252 StructRNA *rna_PropertyGroup_register(Main *UNUSED(bmain), ReportList *reports, void *data, const char *identifier,
253 StructValidateFunc validate, StructCallbackFunc UNUSED(call),
254 StructFreeFunc UNUSED(free))
258 /* create dummy pointer */
259 RNA_pointer_create(NULL, &RNA_PropertyGroup, NULL, &dummyptr);
261 /* validate the python class */
262 if (validate(&dummyptr, data, NULL) != 0)
265 /* note: it looks like there is no length limit on the srna id since its
266 * just a char pointer, but take care here, also be careful that python
267 * owns the string pointer which it could potentially free while blender
269 if (BLI_strnlen(identifier, MAX_IDPROP_NAME) == MAX_IDPROP_NAME) {
270 BKE_reportf(reports, RPT_ERROR, "Registering id property class: '%s' is too long, maximum length is %d",
271 identifier, MAX_IDPROP_NAME);
275 return RNA_def_struct_ptr(&BLENDER_RNA, identifier, &RNA_PropertyGroup); /* XXX */
278 StructRNA *rna_PropertyGroup_refine(PointerRNA *ptr)
283 static ID *rna_ID_copy(ID *id, Main *bmain)
287 if (id_copy(bmain, id, &newid, false)) {
288 if (newid) id_us_min(newid);
295 static void rna_ID_update_tag(ID *id, ReportList *reports, int flag)
297 /* XXX, new function for this! */
299 if (ob->type == OB_FONT) {
300 Curve *cu = ob->data;
301 freedisplist(&cu->disp);
302 BKE_vfont_to_curve(bmain, sce, ob, FO_EDIT, NULL);
310 /* ensure flag us correct for the type */
311 switch (GS(id->name)) {
313 if (flag & ~(OB_RECALC_ALL)) {
314 BKE_report(reports, RPT_ERROR, "'Refresh' incompatible with Object ID type");
318 /* Could add particle updates later */
321 if (flag & ~(OB_RECALC_ALL | PSYS_RECALC)) {
322 BKE_report(reports, RPT_ERROR, "'Refresh' incompatible with ParticleSettings ID type");
328 BKE_report(reports, RPT_ERROR, "This ID type is not compatible with any 'refresh' options");
333 DAG_id_tag_update(id, flag);
336 static void rna_ID_user_clear(ID *id)
338 id_fake_user_clear(id);
339 id->us = 0; /* don't save */
342 static void rna_ID_user_remap(ID *id, Main *bmain, ID *new_id)
344 if (GS(id->name) == GS(new_id->name)) {
345 /* For now, do not allow remapping data in linked data from here... */
346 BKE_libblock_remap(bmain, id, new_id, ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_NEVER_NULL_USAGE);
350 static AnimData * rna_ID_animation_data_create(ID *id, Main *bmain)
352 AnimData *adt = BKE_animdata_add_id(id);
353 DAG_relations_tag_update(bmain);
357 static void rna_ID_animation_data_free(ID *id, Main *bmain)
359 BKE_animdata_free(id, true);
360 DAG_relations_tag_update(bmain);
363 static void rna_IDPArray_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
365 IDProperty *prop = (IDProperty *)ptr->data;
366 rna_iterator_array_begin(iter, IDP_IDPArray(prop), sizeof(IDProperty), prop->len, 0, NULL);
369 static int rna_IDPArray_length(PointerRNA *ptr)
371 IDProperty *prop = (IDProperty *)ptr->data;
375 int rna_IDMaterials_assign_int(PointerRNA *ptr, int key, const PointerRNA *assign_ptr)
377 ID *id = ptr->id.data;
378 short *totcol = give_totcolp_id(id);
379 Material *mat_id = assign_ptr->id.data;
380 if (totcol && (key >= 0 && key < *totcol)) {
381 assign_material_id(id, mat_id, key + 1);
389 static void rna_IDMaterials_append_id(ID *id, Main *bmain, Material *ma)
391 BKE_material_append_id(bmain, id, ma);
393 WM_main_add_notifier(NC_OBJECT | ND_DRAW, id);
394 WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id);
397 static Material *rna_IDMaterials_pop_id(ID *id, Main *bmain, ReportList *reports, int index_i, int remove_material_slot)
400 short *totcol = give_totcolp_id(id);
401 const short totcol_orig = *totcol;
403 index_i += (*totcol);
406 if ((index_i < 0) || (index_i >= (*totcol))) {
407 BKE_report(reports, RPT_ERROR, "Index out of range");
411 ma = BKE_material_pop_id(bmain, id, index_i, remove_material_slot);
413 if (*totcol == totcol_orig) {
414 BKE_report(reports, RPT_ERROR, "No material to removed");
418 DAG_id_tag_update(id, OB_RECALC_DATA);
419 WM_main_add_notifier(NC_OBJECT | ND_DRAW, id);
420 WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id);
425 static void rna_IDMaterials_clear_id(ID *id, int remove_material_slot)
427 BKE_material_clear_id(G.main, id, remove_material_slot);
429 DAG_id_tag_update(id, OB_RECALC_DATA);
430 WM_main_add_notifier(NC_OBJECT | ND_DRAW, id);
431 WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id);
434 static void rna_Library_filepath_set(PointerRNA *ptr, const char *value)
436 Library *lib = (Library *)ptr->data;
437 BKE_library_filepath_set(lib, value);
440 /* ***** ImagePreview ***** */
442 static void rna_ImagePreview_is_custom_set(PointerRNA *ptr, int value, enum eIconSizes size)
444 ID *id = ptr->id.data;
445 PreviewImage *prv_img = (PreviewImage *)ptr->data;
448 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
451 if ((value && (prv_img->flag[size] & PRV_USER_EDITED)) || (!value && !(prv_img->flag[size] & PRV_USER_EDITED))) {
456 prv_img->flag[size] |= PRV_USER_EDITED;
458 prv_img->flag[size] &= ~PRV_USER_EDITED;
460 prv_img->flag[size] |= PRV_CHANGED;
462 BKE_previewimg_clear_single(prv_img, size);
465 static void rna_ImagePreview_size_get(PointerRNA *ptr, int *values, enum eIconSizes size)
467 ID *id = (ID *)ptr->id.data;
468 PreviewImage *prv_img = (PreviewImage *)ptr->data;
471 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
474 BKE_previewimg_ensure(prv_img, size);
476 values[0] = prv_img->w[size];
477 values[1] = prv_img->h[size];
480 static void rna_ImagePreview_size_set(PointerRNA *ptr, const int *values, enum eIconSizes size)
482 ID *id = (ID *)ptr->id.data;
483 PreviewImage *prv_img = (PreviewImage *)ptr->data;
486 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
489 BKE_previewimg_clear_single(prv_img, size);
491 if (values[0] && values[1]) {
492 prv_img->rect[size] = MEM_callocN(values[0] * values[1] * sizeof(unsigned int), "prv_rect");
494 prv_img->w[size] = values[0];
495 prv_img->h[size] = values[1];
498 prv_img->flag[size] |= (PRV_CHANGED | PRV_USER_EDITED);
502 static int rna_ImagePreview_pixels_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION], enum eIconSizes size)
504 ID *id = ptr->id.data;
505 PreviewImage *prv_img = (PreviewImage *)ptr->data;
508 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
511 BKE_previewimg_ensure(prv_img, size);
513 length[0] = prv_img->w[size] * prv_img->h[size];
518 static void rna_ImagePreview_pixels_get(PointerRNA *ptr, int *values, enum eIconSizes size)
520 ID *id = ptr->id.data;
521 PreviewImage *prv_img = (PreviewImage *)ptr->data;
524 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
527 BKE_previewimg_ensure(prv_img, size);
529 memcpy(values, prv_img->rect[size], prv_img->w[size] * prv_img->h[size] * sizeof(unsigned int));
532 static void rna_ImagePreview_pixels_set(PointerRNA *ptr, const int *values, enum eIconSizes size)
534 ID *id = ptr->id.data;
535 PreviewImage *prv_img = (PreviewImage *)ptr->data;
538 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
541 memcpy(prv_img->rect[size], values, prv_img->w[size] * prv_img->h[size] * sizeof(unsigned int));
542 prv_img->flag[size] |= PRV_USER_EDITED;
546 static int rna_ImagePreview_pixels_float_get_length(
547 PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION], enum eIconSizes size)
549 ID *id = ptr->id.data;
550 PreviewImage *prv_img = (PreviewImage *)ptr->data;
552 BLI_assert(sizeof(unsigned int) == 4);
555 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
558 BKE_previewimg_ensure(prv_img, size);
560 length[0] = prv_img->w[size] * prv_img->h[size] * 4;
565 static void rna_ImagePreview_pixels_float_get(PointerRNA *ptr, float *values, enum eIconSizes size)
567 ID *id = ptr->id.data;
568 PreviewImage *prv_img = (PreviewImage *)ptr->data;
570 unsigned char *data = (unsigned char *)prv_img->rect[size];
571 const size_t len = prv_img->w[size] * prv_img->h[size] * 4;
574 BLI_assert(sizeof(unsigned int) == 4);
577 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
580 BKE_previewimg_ensure(prv_img, size);
582 for (i = 0; i < len; i++) {
583 values[i] = data[i] * (1.0f / 255.0f);
587 static void rna_ImagePreview_pixels_float_set(PointerRNA *ptr, const float *values, enum eIconSizes size)
589 ID *id = ptr->id.data;
590 PreviewImage *prv_img = (PreviewImage *)ptr->data;
592 unsigned char *data = (unsigned char *)prv_img->rect[size];
593 const size_t len = prv_img->w[size] * prv_img->h[size] * 4;
596 BLI_assert(sizeof(unsigned int) == 4);
599 BLI_assert(prv_img == BKE_previewimg_id_ensure(id));
602 for (i = 0; i < len; i++) {
603 data[i] = FTOCHAR(values[i]);
605 prv_img->flag[size] |= PRV_USER_EDITED;
609 static void rna_ImagePreview_is_image_custom_set(PointerRNA *ptr, int value)
611 rna_ImagePreview_is_custom_set(ptr, value, ICON_SIZE_PREVIEW);
614 static void rna_ImagePreview_image_size_get(PointerRNA *ptr, int *values)
616 rna_ImagePreview_size_get(ptr, values, ICON_SIZE_PREVIEW);
619 static void rna_ImagePreview_image_size_set(PointerRNA *ptr, const int *values)
621 rna_ImagePreview_size_set(ptr, values, ICON_SIZE_PREVIEW);
624 static int rna_ImagePreview_image_pixels_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
626 return rna_ImagePreview_pixels_get_length(ptr, length, ICON_SIZE_PREVIEW);
629 static void rna_ImagePreview_image_pixels_get(PointerRNA *ptr, int *values)
631 rna_ImagePreview_pixels_get(ptr, values, ICON_SIZE_PREVIEW);
634 static void rna_ImagePreview_image_pixels_set(PointerRNA *ptr, const int *values)
636 rna_ImagePreview_pixels_set(ptr, values, ICON_SIZE_PREVIEW);
639 static int rna_ImagePreview_image_pixels_float_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
641 return rna_ImagePreview_pixels_float_get_length(ptr, length, ICON_SIZE_PREVIEW);
644 static void rna_ImagePreview_image_pixels_float_get(PointerRNA *ptr, float *values)
646 rna_ImagePreview_pixels_float_get(ptr, values, ICON_SIZE_PREVIEW);
649 static void rna_ImagePreview_image_pixels_float_set(PointerRNA *ptr, const float *values)
651 rna_ImagePreview_pixels_float_set(ptr, values, ICON_SIZE_PREVIEW);
655 static void rna_ImagePreview_is_icon_custom_set(PointerRNA *ptr, int value)
657 rna_ImagePreview_is_custom_set(ptr, value, ICON_SIZE_ICON);
660 static void rna_ImagePreview_icon_size_get(PointerRNA *ptr, int *values)
662 rna_ImagePreview_size_get(ptr, values, ICON_SIZE_ICON);
665 static void rna_ImagePreview_icon_size_set(PointerRNA *ptr, const int *values)
667 rna_ImagePreview_size_set(ptr, values, ICON_SIZE_ICON);
670 static int rna_ImagePreview_icon_pixels_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
672 return rna_ImagePreview_pixels_get_length(ptr, length, ICON_SIZE_ICON);
675 static void rna_ImagePreview_icon_pixels_get(PointerRNA *ptr, int *values)
677 rna_ImagePreview_pixels_get(ptr, values, ICON_SIZE_ICON);
680 static void rna_ImagePreview_icon_pixels_set(PointerRNA *ptr, const int *values)
682 rna_ImagePreview_pixels_set(ptr, values, ICON_SIZE_ICON);
685 static int rna_ImagePreview_icon_pixels_float_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
687 return rna_ImagePreview_pixels_float_get_length(ptr, length, ICON_SIZE_ICON);
690 static void rna_ImagePreview_icon_pixels_float_get(PointerRNA *ptr, float *values)
692 rna_ImagePreview_pixels_float_get(ptr, values, ICON_SIZE_ICON);
695 static void rna_ImagePreview_icon_pixels_float_set(PointerRNA *ptr, const float *values)
697 rna_ImagePreview_pixels_float_set(ptr, values, ICON_SIZE_ICON);
701 static int rna_ImagePreview_icon_id_get(PointerRNA *ptr)
703 /* Using a callback here allows us to only generate icon matching that preview when icon_id is requested. */
704 return BKE_icon_preview_ensure(ptr->id.data, (PreviewImage *)(ptr->data));
706 static void rna_ImagePreview_icon_reload(PreviewImage *prv)
708 /* will lazy load on next use, but only in case icon is not user-modified! */
709 if (!(prv->flag[ICON_SIZE_ICON] & PRV_USER_EDITED) && !(prv->flag[ICON_SIZE_PREVIEW] & PRV_USER_EDITED)) {
710 BKE_previewimg_clear(prv);
714 static PointerRNA rna_IDPreview_get(PointerRNA *ptr)
716 ID *id = (ID *)ptr->data;
717 PreviewImage *prv_img = BKE_previewimg_id_ensure(id);
719 return rna_pointer_inherit_refine(ptr, &RNA_ImagePreview, prv_img);
724 static void rna_def_ID_properties(BlenderRNA *brna)
729 /* this is struct is used for holding the virtual
730 * PropertyRNA's for ID properties */
731 srna = RNA_def_struct(brna, "PropertyGroupItem", NULL);
732 RNA_def_struct_sdna(srna, "IDProperty");
733 RNA_def_struct_ui_text(srna, "ID Property", "Property that stores arbitrary, user defined properties");
736 prop = RNA_def_property(srna, "string", PROP_STRING, PROP_NONE);
737 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
740 prop = RNA_def_property(srna, "int", PROP_INT, PROP_NONE);
741 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
743 prop = RNA_def_property(srna, "int_array", PROP_INT, PROP_NONE);
744 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
745 RNA_def_property_array(prop, 1);
748 prop = RNA_def_property(srna, "float", PROP_FLOAT, PROP_NONE);
749 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
751 prop = RNA_def_property(srna, "float_array", PROP_FLOAT, PROP_NONE);
752 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
753 RNA_def_property_array(prop, 1);
756 prop = RNA_def_property(srna, "double", PROP_FLOAT, PROP_NONE);
757 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
759 prop = RNA_def_property(srna, "double_array", PROP_FLOAT, PROP_NONE);
760 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
761 RNA_def_property_array(prop, 1);
764 prop = RNA_def_property(srna, "group", PROP_POINTER, PROP_NONE);
765 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
766 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
767 RNA_def_property_struct_type(prop, "PropertyGroup");
769 prop = RNA_def_property(srna, "collection", PROP_COLLECTION, PROP_NONE);
770 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
771 RNA_def_property_struct_type(prop, "PropertyGroup");
773 prop = RNA_def_property(srna, "idp_array", PROP_COLLECTION, PROP_NONE);
774 RNA_def_property_struct_type(prop, "PropertyGroup");
775 RNA_def_property_collection_funcs(prop, "rna_IDPArray_begin", "rna_iterator_array_next", "rna_iterator_array_end",
776 "rna_iterator_array_get", "rna_IDPArray_length", NULL, NULL, NULL);
777 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
779 /* never tested, maybe its useful to have this? */
781 prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
782 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
783 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
784 RNA_def_property_ui_text(prop, "Name", "Unique name used in the code and scripting");
785 RNA_def_struct_name_property(srna, prop);
788 /* IDP_ID -- not implemented yet in id properties */
790 /* ID property groups > level 0, since level 0 group is merged
791 * with native RNA properties. the builtin_properties will take
792 * care of the properties here */
793 srna = RNA_def_struct(brna, "PropertyGroup", NULL);
794 RNA_def_struct_sdna(srna, "IDPropertyGroup");
795 RNA_def_struct_ui_text(srna, "ID Property Group", "Group of ID properties");
796 RNA_def_struct_idprops_func(srna, "rna_PropertyGroup_idprops");
797 RNA_def_struct_register_funcs(srna, "rna_PropertyGroup_register", "rna_PropertyGroup_unregister", NULL);
798 RNA_def_struct_refine_func(srna, "rna_PropertyGroup_refine");
800 /* important so python types can have their name used in list views
801 * however this isn't prefect because it overrides how python would set the name
802 * when we only really want this so RNA_def_struct_name_property() is set to something useful */
803 prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
804 RNA_def_property_flag(prop, PROP_EXPORT | PROP_IDPROPERTY);
805 /*RNA_def_property_clear_flag(prop, PROP_EDITABLE); */
806 RNA_def_property_ui_text(prop, "Name", "Unique name used in the code and scripting");
807 RNA_def_struct_name_property(srna, prop);
811 static void rna_def_ID_materials(BlenderRNA *brna)
817 /* for mesh/mball/curve materials */
818 srna = RNA_def_struct(brna, "IDMaterials", NULL);
819 RNA_def_struct_sdna(srna, "ID");
820 RNA_def_struct_ui_text(srna, "ID Materials", "Collection of materials");
822 func = RNA_def_function(srna, "append", "rna_IDMaterials_append_id");
823 RNA_def_function_flag(func, FUNC_USE_MAIN);
824 RNA_def_function_ui_description(func, "Add a new material to the data block");
825 parm = RNA_def_pointer(func, "material", "Material", "", "Material to add");
826 RNA_def_property_flag(parm, PROP_REQUIRED);
828 func = RNA_def_function(srna, "pop", "rna_IDMaterials_pop_id");
829 RNA_def_function_flag(func, FUNC_USE_REPORTS | FUNC_USE_MAIN);
830 RNA_def_function_ui_description(func, "Remove a material from the data block");
831 parm = RNA_def_int(func, "index", -1, -MAXMAT, MAXMAT, "", "Index of material to remove", 0, MAXMAT);
832 RNA_def_boolean(func, "update_data", 0, "", "Update data by re-adjusting the material slots assigned");
833 parm = RNA_def_pointer(func, "material", "Material", "", "Material to remove");
834 RNA_def_function_return(func, parm);
836 func = RNA_def_function(srna, "clear", "rna_IDMaterials_clear_id");
837 RNA_def_function_ui_description(func, "Remove all materials from the data block");
838 RNA_def_boolean(func, "update_data", 0, "", "Update data by re-adjusting the material slots assigned");
841 static void rna_def_image_preview(BlenderRNA *brna)
847 srna = RNA_def_struct(brna, "ImagePreview", NULL);
848 RNA_def_struct_sdna(srna, "PreviewImage");
849 RNA_def_struct_ui_text(srna, "Image Preview", "Preview image and icon");
851 prop = RNA_def_property(srna, "is_image_custom", PROP_BOOLEAN, PROP_NONE);
852 RNA_def_property_boolean_sdna(prop, NULL, "flag[ICON_SIZE_PREVIEW]", PRV_USER_EDITED);
853 RNA_def_property_boolean_funcs(prop, NULL, "rna_ImagePreview_is_image_custom_set");
854 RNA_def_property_ui_text(prop, "Custom Image", "True if this preview image has been modified by py script,"
855 "and is no more auto-generated by Blender");
857 prop = RNA_def_int_vector(srna, "image_size", 2, NULL, 0, 0, "Image Size",
858 "Width and height in pixels", 0, 0);
859 RNA_def_property_subtype(prop, PROP_PIXEL);
860 RNA_def_property_int_funcs(prop, "rna_ImagePreview_image_size_get", "rna_ImagePreview_image_size_set", NULL);
862 prop = RNA_def_property(srna, "image_pixels", PROP_INT, PROP_NONE);
863 RNA_def_property_flag(prop, PROP_DYNAMIC);
864 RNA_def_property_multi_array(prop, 1, NULL);
865 RNA_def_property_ui_text(prop, "Image Pixels", "Image pixels, as bytes (always RGBA 32bits)");
866 RNA_def_property_dynamic_array_funcs(prop, "rna_ImagePreview_image_pixels_get_length");
867 RNA_def_property_int_funcs(prop, "rna_ImagePreview_image_pixels_get", "rna_ImagePreview_image_pixels_set", NULL);
869 prop = RNA_def_property(srna, "image_pixels_float", PROP_FLOAT, PROP_NONE);
870 RNA_def_property_flag(prop, PROP_DYNAMIC);
871 RNA_def_property_multi_array(prop, 1, NULL);
872 RNA_def_property_ui_text(prop, "Float Image Pixels",
873 "Image pixels components, as floats (RGBA concatenated values)");
874 RNA_def_property_dynamic_array_funcs(prop, "rna_ImagePreview_image_pixels_float_get_length");
875 RNA_def_property_float_funcs(prop, "rna_ImagePreview_image_pixels_float_get",
876 "rna_ImagePreview_image_pixels_float_set", NULL);
879 prop = RNA_def_property(srna, "is_icon_custom", PROP_BOOLEAN, PROP_NONE);
880 RNA_def_property_boolean_sdna(prop, NULL, "flag[ICON_SIZE_ICON]", PRV_USER_EDITED);
881 RNA_def_property_boolean_funcs(prop, NULL, "rna_ImagePreview_is_icon_custom_set");
882 RNA_def_property_ui_text(prop, "Custom Icon", "True if this preview icon has been modified by py script,"
883 "and is no more auto-generated by Blender");
885 prop = RNA_def_int_vector(srna, "icon_size", 2, NULL, 0, 0, "Icon Size",
886 "Width and height in pixels", 0, 0);
887 RNA_def_property_subtype(prop, PROP_PIXEL);
888 RNA_def_property_int_funcs(prop, "rna_ImagePreview_icon_size_get", "rna_ImagePreview_icon_size_set", NULL);
890 prop = RNA_def_property(srna, "icon_pixels", PROP_INT, PROP_NONE);
891 RNA_def_property_flag(prop, PROP_DYNAMIC);
892 RNA_def_property_multi_array(prop, 1, NULL);
893 RNA_def_property_ui_text(prop, "Icon Pixels", "Icon pixels, as bytes (always RGBA 32bits)");
894 RNA_def_property_dynamic_array_funcs(prop, "rna_ImagePreview_icon_pixels_get_length");
895 RNA_def_property_int_funcs(prop, "rna_ImagePreview_icon_pixels_get", "rna_ImagePreview_icon_pixels_set", NULL);
897 prop = RNA_def_property(srna, "icon_pixels_float", PROP_FLOAT, PROP_NONE);
898 RNA_def_property_flag(prop, PROP_DYNAMIC);
899 RNA_def_property_multi_array(prop, 1, NULL);
900 RNA_def_property_ui_text(prop, "Float Icon Pixels", "Icon pixels components, as floats (RGBA concatenated values)");
901 RNA_def_property_dynamic_array_funcs(prop, "rna_ImagePreview_icon_pixels_float_get_length");
902 RNA_def_property_float_funcs(prop, "rna_ImagePreview_icon_pixels_float_get",
903 "rna_ImagePreview_icon_pixels_float_set", NULL);
905 prop = RNA_def_int(srna, "icon_id", 0, INT_MIN, INT_MAX, "Icon ID",
906 "Unique integer identifying this preview as an icon (zero means invalid)", INT_MIN, INT_MAX);
907 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
908 RNA_def_property_int_funcs(prop, "rna_ImagePreview_icon_id_get", NULL, NULL);
910 func = RNA_def_function(srna, "reload", "rna_ImagePreview_icon_reload");
911 RNA_def_function_ui_description(func, "Reload the preview from its source path");
914 static void rna_def_ID(BlenderRNA *brna)
918 PropertyRNA *prop, *parm;
920 static EnumPropertyItem update_flag_items[] = {
921 {OB_RECALC_OB, "OBJECT", 0, "Object", ""},
922 {OB_RECALC_DATA, "DATA", 0, "Data", ""},
923 {OB_RECALC_TIME, "TIME", 0, "Time", ""},
924 {0, NULL, 0, NULL, NULL}
927 srna = RNA_def_struct(brna, "ID", NULL);
928 RNA_def_struct_ui_text(srna, "ID",
929 "Base type for data-blocks, defining a unique name, linking from other libraries "
930 "and garbage collection");
931 RNA_def_struct_flag(srna, STRUCT_ID | STRUCT_ID_REFCOUNT);
932 RNA_def_struct_refine_func(srna, "rna_ID_refine");
933 RNA_def_struct_idprops_func(srna, "rna_ID_idprops");
935 prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
936 RNA_def_property_ui_text(prop, "Name", "Unique data-block ID name");
937 RNA_def_property_string_funcs(prop, "rna_ID_name_get", "rna_ID_name_length", "rna_ID_name_set");
938 RNA_def_property_string_maxlength(prop, MAX_ID_NAME - 2);
939 RNA_def_property_editable_func(prop, "rna_ID_name_editable");
940 RNA_def_property_update(prop, NC_ID | NA_RENAME, NULL);
941 RNA_def_struct_name_property(srna, prop);
943 prop = RNA_def_property(srna, "users", PROP_INT, PROP_UNSIGNED);
944 RNA_def_property_int_sdna(prop, NULL, "us");
945 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
946 RNA_def_property_ui_text(prop, "Users", "Number of times this data-block is referenced");
948 prop = RNA_def_property(srna, "use_fake_user", PROP_BOOLEAN, PROP_NONE);
949 RNA_def_property_boolean_sdna(prop, NULL, "flag", LIB_FAKEUSER);
950 RNA_def_property_ui_text(prop, "Fake User", "Save this data-block even if it has no users");
951 RNA_def_property_boolean_funcs(prop, NULL, "rna_ID_fake_user_set");
953 prop = RNA_def_property(srna, "tag", PROP_BOOLEAN, PROP_NONE);
954 RNA_def_property_boolean_sdna(prop, NULL, "tag", LIB_TAG_DOIT);
955 RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
956 RNA_def_property_ui_text(prop, "Tag",
957 "Tools can use this to tag data for their own purposes "
958 "(initial state is undefined)");
960 prop = RNA_def_property(srna, "is_updated", PROP_BOOLEAN, PROP_NONE);
961 RNA_def_property_boolean_sdna(prop, NULL, "tag", LIB_TAG_ID_RECALC);
962 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
963 RNA_def_property_ui_text(prop, "Is Updated", "Datablock is tagged for recalculation");
965 prop = RNA_def_property(srna, "is_updated_data", PROP_BOOLEAN, PROP_NONE);
966 RNA_def_property_boolean_sdna(prop, NULL, "tag", LIB_TAG_ID_RECALC_DATA);
967 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
968 RNA_def_property_ui_text(prop, "Is Updated Data", "Datablock data is tagged for recalculation");
970 prop = RNA_def_property(srna, "is_library_indirect", PROP_BOOLEAN, PROP_NONE);
971 RNA_def_property_boolean_sdna(prop, NULL, "tag", LIB_TAG_INDIRECT);
972 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
973 RNA_def_property_ui_text(prop, "Is Indirect", "Is this ID block linked indirectly");
975 prop = RNA_def_property(srna, "library", PROP_POINTER, PROP_NONE);
976 RNA_def_property_pointer_sdna(prop, NULL, "lib");
977 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
978 RNA_def_property_ui_text(prop, "Library", "Library file the data-block is linked from");
980 prop = RNA_def_pointer(srna, "preview", "ImagePreview", "Preview",
981 "Preview image and icon of this data-block (None if not supported for this type of data)");
982 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
983 RNA_def_property_pointer_funcs(prop, "rna_IDPreview_get", NULL, NULL, NULL);
986 func = RNA_def_function(srna, "copy", "rna_ID_copy");
987 RNA_def_function_ui_description(func, "Create a copy of this data-block (not supported for all data-blocks)");
988 RNA_def_function_flag(func, FUNC_USE_MAIN);
989 parm = RNA_def_pointer(func, "id", "ID", "", "New copy of the ID");
990 RNA_def_function_return(func, parm);
992 func = RNA_def_function(srna, "user_clear", "rna_ID_user_clear");
993 RNA_def_function_ui_description(func, "Clear the user count of a data-block so its not saved, "
994 "on reload the data will be removed");
996 func = RNA_def_function(srna, "user_remap", "rna_ID_user_remap");
997 RNA_def_function_ui_description(func, "Replace all usage in the .blend file of this ID by new given one");
998 RNA_def_function_flag(func, FUNC_USE_MAIN);
999 parm = RNA_def_pointer(func, "new_id", "ID", "", "New ID to use");
1000 RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
1002 func = RNA_def_function(srna, "user_of_id", "BKE_library_ID_use_ID");
1003 RNA_def_function_ui_description(func, "Count the number of times that ID uses/references given one");
1004 parm = RNA_def_pointer(func, "id", "ID", "", "ID to count usages");
1005 RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
1006 parm = RNA_def_int(func, "count", 0, 0, INT_MAX,
1007 "", "Number of usages/references of given id by current datablock", 0, INT_MAX);
1008 RNA_def_function_return(func, parm);
1010 func = RNA_def_function(srna, "animation_data_create", "rna_ID_animation_data_create");
1011 RNA_def_function_flag(func, FUNC_USE_MAIN);
1012 RNA_def_function_ui_description(func, "Create animation data to this ID, note that not all ID types support this");
1013 parm = RNA_def_pointer(func, "anim_data", "AnimData", "", "New animation data or NULL");
1014 RNA_def_function_return(func, parm);
1016 func = RNA_def_function(srna, "animation_data_clear", "rna_ID_animation_data_free");
1017 RNA_def_function_flag(func, FUNC_USE_MAIN);
1018 RNA_def_function_ui_description(func, "Clear animation on this this ID");
1020 func = RNA_def_function(srna, "update_tag", "rna_ID_update_tag");
1021 RNA_def_function_flag(func, FUNC_USE_REPORTS);
1022 RNA_def_function_ui_description(func,
1023 "Tag the ID to update its display data, "
1024 "e.g. when calling :class:`bpy.types.Scene.update`");
1025 RNA_def_enum_flag(func, "refresh", update_flag_items, 0, "", "Type of updates to perform");
1028 static void rna_def_library(BlenderRNA *brna)
1034 srna = RNA_def_struct(brna, "Library", "ID");
1035 RNA_def_struct_ui_text(srna, "Library", "External .blend file from which data is linked");
1036 RNA_def_struct_ui_icon(srna, ICON_LIBRARY_DATA_DIRECT);
1038 prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
1039 RNA_def_property_string_sdna(prop, NULL, "name");
1040 RNA_def_property_ui_text(prop, "File Path", "Path to the library .blend file");
1041 RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Library_filepath_set");
1043 prop = RNA_def_property(srna, "parent", PROP_POINTER, PROP_NONE);
1044 RNA_def_property_struct_type(prop, "Library");
1045 RNA_def_property_ui_text(prop, "Parent", "");
1047 prop = RNA_def_property(srna, "packed_file", PROP_POINTER, PROP_NONE);
1048 RNA_def_property_pointer_sdna(prop, NULL, "packedfile");
1049 RNA_def_property_ui_text(prop, "Packed File", "");
1051 func = RNA_def_function(srna, "reload", "WM_lib_reload");
1052 RNA_def_function_flag(func, FUNC_USE_REPORTS | FUNC_USE_CONTEXT);
1053 RNA_def_function_ui_description(func, "Reload this library and all its linked datablocks");
1055 void RNA_def_ID(BlenderRNA *brna)
1059 /* built-in unknown type */
1060 srna = RNA_def_struct(brna, "UnknownType", NULL);
1061 RNA_def_struct_ui_text(srna, "Unknown Type", "Stub RNA type used for pointers to unknown or internal data");
1063 /* built-in any type */
1064 srna = RNA_def_struct(brna, "AnyType", NULL);
1065 RNA_def_struct_ui_text(srna, "Any Type", "RNA type used for pointers to any possible data");
1068 rna_def_image_preview(brna);
1069 rna_def_ID_properties(brna);
1070 rna_def_ID_materials(brna);
1071 rna_def_library(brna);