4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): Blender Foundation (2008).
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/makesrna/intern/rna_ID.c
33 #include "RNA_access.h"
34 #include "RNA_define.h"
37 #include "DNA_vfont_types.h"
38 #include "DNA_material_types.h"
39 #include "DNA_object_types.h"
43 #include "rna_internal.h"
45 /* enum of ID-block types
46 * NOTE: need to keep this in line with the other defines for these
48 EnumPropertyItem id_type_items[] = {
49 {ID_AC, "ACTION", ICON_ACTION, "Action", ""},
50 {ID_AR, "ARMATURE", ICON_ARMATURE_DATA, "Armature", ""},
51 {ID_BR, "BRUSH", ICON_BRUSH_DATA, "Brush", ""},
52 {ID_CA, "CAMERA", ICON_CAMERA_DATA, "Camera", ""},
53 {ID_CU, "CURVE", ICON_CURVE_DATA, "Curve", ""},
54 {ID_VF, "FONT", ICON_FONT_DATA, "Font", ""},
55 {ID_GD, "GREASEPENCIL", ICON_GREASEPENCIL, "Grease Pencil", ""},
56 {ID_GR, "GROUP", ICON_GROUP, "Group", ""},
57 {ID_IM, "IMAGE", ICON_IMAGE_DATA, "Image", ""},
58 {ID_KE, "KEY", ICON_SHAPEKEY_DATA, "Key", ""},
59 {ID_LA, "LAMP", ICON_LAMP_DATA, "Lamp", ""},
60 {ID_LI, "LIBRARY", ICON_LIBRARY_DATA_DIRECT, "Library", ""},
61 {ID_LT, "LATTICE", ICON_LATTICE_DATA, "Lattice", ""},
62 {ID_MA, "MATERIAL", ICON_MATERIAL_DATA, "Material", ""},
63 {ID_MB, "META", ICON_META_DATA, "MetaBall", ""},
64 {ID_ME, "MESH", ICON_MESH_DATA, "Mesh", ""},
65 {ID_NT, "NODETREE", ICON_NODETREE, "NodeTree", ""},
66 {ID_OB, "OBJECT", ICON_OBJECT_DATA, "Object", ""},
67 {ID_PA, "PARTICLE", ICON_PARTICLE_DATA, "Particle", ""},
68 {ID_SCE, "SCENE", ICON_SCENE_DATA, "Scene", ""},
69 {ID_SCR, "SCREEN", ICON_SPLITSCREEN, "Screen", ""},
70 {ID_SPK, "SPEAKER", ICON_SPEAKER, "Speaker", ""},
71 {ID_SO, "SOUND", ICON_PLAY_AUDIO, "Sound", ""},
72 {ID_TXT, "TEXT", ICON_TEXT, "Text", ""},
73 {ID_TE, "TEXTURE", ICON_TEXTURE_DATA, "Texture", ""},
74 {ID_WO, "WORLD", ICON_WORLD_DATA, "World", ""},
75 {ID_WM, "WINDOWMANAGER", ICON_FULLSCREEN, "Window Manager", ""},
76 {0, NULL, 0, NULL, NULL}};
80 #include "BKE_idprop.h"
81 #include "BKE_library.h"
82 #include "BKE_animsys.h"
83 #include "BKE_material.h"
84 #include "BKE_depsgraph.h"
86 /* name functions that ignore the first two ID characters */
87 void rna_ID_name_get(PointerRNA *ptr, char *value)
89 ID *id= (ID*)ptr->data;
90 BLI_strncpy(value, id->name+2, sizeof(id->name)-2);
93 int rna_ID_name_length(PointerRNA *ptr)
95 ID *id= (ID*)ptr->data;
96 return strlen(id->name+2);
99 void rna_ID_name_set(PointerRNA *ptr, const char *value)
101 ID *id= (ID*)ptr->data;
102 BLI_strncpy(id->name+2, value, sizeof(id->name)-2);
103 test_idbutton(id->name+2);
106 static int rna_ID_name_editable(PointerRNA *ptr)
108 ID *id= (ID*)ptr->data;
110 if (GS(id->name) == ID_VF) {
111 VFont *vf= (VFont *)id;
112 if (strcmp(vf->name, FO_BUILTIN_NAME)==0)
119 short RNA_type_to_ID_code(StructRNA *type)
121 if(RNA_struct_is_a(type, &RNA_Action)) return ID_AC;
122 if(RNA_struct_is_a(type, &RNA_Armature)) return ID_AR;
123 if(RNA_struct_is_a(type, &RNA_Brush)) return ID_BR;
124 if(RNA_struct_is_a(type, &RNA_Camera)) return ID_CA;
125 if(RNA_struct_is_a(type, &RNA_Curve)) return ID_CU;
126 if(RNA_struct_is_a(type, &RNA_GreasePencil)) return ID_GD;
127 if(RNA_struct_is_a(type, &RNA_Group)) return ID_GR;
128 if(RNA_struct_is_a(type, &RNA_Image)) return ID_IM;
129 if(RNA_struct_is_a(type, &RNA_Key)) return ID_KE;
130 if(RNA_struct_is_a(type, &RNA_Lamp)) return ID_LA;
131 if(RNA_struct_is_a(type, &RNA_Library)) return ID_LI;
132 if(RNA_struct_is_a(type, &RNA_Lattice)) return ID_LT;
133 if(RNA_struct_is_a(type, &RNA_Material)) return ID_MA;
134 if(RNA_struct_is_a(type, &RNA_MetaBall)) return ID_MB;
135 if(RNA_struct_is_a(type, &RNA_NodeTree)) return ID_NT;
136 if(RNA_struct_is_a(type, &RNA_Mesh)) return ID_ME;
137 if(RNA_struct_is_a(type, &RNA_Object)) return ID_OB;
138 if(RNA_struct_is_a(type, &RNA_ParticleSettings)) return ID_PA;
139 if(RNA_struct_is_a(type, &RNA_Scene)) return ID_SCE;
140 if(RNA_struct_is_a(type, &RNA_Screen)) return ID_SCR;
141 if(RNA_struct_is_a(type, &RNA_Speaker)) return ID_SPK;
142 if(RNA_struct_is_a(type, &RNA_Sound)) return ID_SO;
143 if(RNA_struct_is_a(type, &RNA_Text)) return ID_TXT;
144 if(RNA_struct_is_a(type, &RNA_Texture)) return ID_TE;
145 if(RNA_struct_is_a(type, &RNA_VectorFont)) return ID_VF;
146 if(RNA_struct_is_a(type, &RNA_World)) return ID_WO;
147 if(RNA_struct_is_a(type, &RNA_WindowManager)) return ID_WM;
152 StructRNA *ID_code_to_RNA_type(short idcode)
155 case ID_AC: return &RNA_Action;
156 case ID_AR: return &RNA_Armature;
157 case ID_BR: return &RNA_Brush;
158 case ID_CA: return &RNA_Camera;
159 case ID_CU: return &RNA_Curve;
160 case ID_GD: return &RNA_GreasePencil;
161 case ID_GR: return &RNA_Group;
162 case ID_IM: return &RNA_Image;
163 case ID_KE: return &RNA_Key;
164 case ID_LA: return &RNA_Lamp;
165 case ID_LI: return &RNA_Library;
166 case ID_LT: return &RNA_Lattice;
167 case ID_MA: return &RNA_Material;
168 case ID_MB: return &RNA_MetaBall;
169 case ID_NT: return &RNA_NodeTree;
170 case ID_ME: return &RNA_Mesh;
171 case ID_OB: return &RNA_Object;
172 case ID_PA: return &RNA_ParticleSettings;
173 case ID_SCE: return &RNA_Scene;
174 case ID_SCR: return &RNA_Screen;
175 case ID_SPK: return &RNA_Speaker;
176 case ID_SO: return &RNA_Sound;
177 case ID_TXT: return &RNA_Text;
178 case ID_TE: return &RNA_Texture;
179 case ID_VF: return &RNA_VectorFont;
180 case ID_WO: return &RNA_World;
181 case ID_WM: return &RNA_WindowManager;
182 default: return &RNA_ID;
186 StructRNA *rna_ID_refine(PointerRNA *ptr)
188 ID *id= (ID*)ptr->data;
190 return ID_code_to_RNA_type(GS(id->name));
193 IDProperty *rna_ID_idprops(PointerRNA *ptr, int create)
195 return IDP_GetProperties(ptr->data, create);
198 void rna_ID_fake_user_set(PointerRNA *ptr, int value)
200 ID *id= (ID*)ptr->data;
202 if(value && !(id->flag & LIB_FAKEUSER)) {
203 id->flag |= LIB_FAKEUSER;
206 else if(!value && (id->flag & LIB_FAKEUSER)) {
207 id->flag &= ~LIB_FAKEUSER;
212 IDProperty *rna_PropertyGroup_idprops(PointerRNA *ptr, int UNUSED(create))
217 void rna_PropertyGroup_unregister(Main *UNUSED(bmain), StructRNA *type)
219 RNA_struct_free(&BLENDER_RNA, type);
222 StructRNA *rna_PropertyGroup_register(Main *UNUSED(bmain), ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc UNUSED(call), StructFreeFunc UNUSED(free))
226 /* create dummy pointer */
227 RNA_pointer_create(NULL, &RNA_PropertyGroup, NULL, &dummyptr);
229 /* validate the python class */
230 if(validate(&dummyptr, data, NULL) != 0)
233 /* note: it looks like there is no length limit on the srna id since its
234 * just a char pointer, but take care here, also be careful that python
235 * owns the string pointer which it could potentually free while blender
237 if(BLI_strnlen(identifier, MAX_IDPROP_NAME) == MAX_IDPROP_NAME) {
238 BKE_reportf(reports, RPT_ERROR, "registering id property class: '%s' is too long, maximum length is " STRINGIFY(MAX_IDPROP_NAME) ".", identifier);
242 return RNA_def_struct(&BLENDER_RNA, identifier, "PropertyGroup"); // XXX
245 StructRNA* rna_PropertyGroup_refine(PointerRNA *ptr)
250 ID *rna_ID_copy(ID *id)
254 if(id_copy(id, &newid, 0)) {
255 if(newid) id_us_min(newid);
262 static void rna_ID_update_tag(ID *id, ReportList *reports, int flag)
264 /* XXX, new function for this! */
265 /*if (ob->type == OB_FONT) {
266 Curve *cu = ob->data;
267 freedisplist(&cu->disp);
268 BKE_text_to_curve(sce, ob, CU_LEFT);
275 /* ensure flag us correct for the type */
276 switch(GS(id->name)) {
278 if(flag & ~(OB_RECALC_ALL)) {
279 BKE_report(reports, RPT_ERROR, "'refresh' incompatible with Object ID type");
283 /* Could add particle updates later */
285 if(flag & ~(OB_RECALC_ALL|PSYS_RECALC)) {
286 BKE_report(reports, RPT_ERROR, "'refresh' incompatible with ParticleSettings ID type");
291 BKE_report(reports, RPT_ERROR, "This ID type is not compatible with any 'refresh' options");
296 DAG_id_tag_update(id, flag);
299 void rna_ID_user_clear(ID *id)
301 id->us= 0; /* dont save */
302 id->flag &= ~LIB_FAKEUSER;
305 static void rna_IDPArray_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
307 IDProperty *prop= (IDProperty *)ptr->data;
308 rna_iterator_array_begin(iter, IDP_IDPArray(prop), sizeof(IDProperty), prop->len, 0, NULL);
311 static int rna_IDPArray_length(PointerRNA *ptr)
313 IDProperty *prop= (IDProperty *)ptr->data;
319 static void rna_def_ID_properties(BlenderRNA *brna)
324 /* this is struct is used for holding the virtual
325 * PropertyRNA's for ID properties */
326 srna= RNA_def_struct(brna, "PropertyGroupItem", NULL);
327 RNA_def_struct_sdna(srna, "IDProperty");
328 RNA_def_struct_ui_text(srna, "ID Property", "Property that stores arbitrary, user defined properties");
331 prop= RNA_def_property(srna, "string", PROP_STRING, PROP_NONE);
332 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
335 prop= RNA_def_property(srna, "int", PROP_INT, PROP_NONE);
336 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
338 prop= RNA_def_property(srna, "int_array", PROP_INT, PROP_NONE);
339 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
340 RNA_def_property_array(prop, 1);
343 prop= RNA_def_property(srna, "float", PROP_FLOAT, PROP_NONE);
344 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
346 prop= RNA_def_property(srna, "float_array", PROP_FLOAT, PROP_NONE);
347 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
348 RNA_def_property_array(prop, 1);
351 prop= RNA_def_property(srna, "double", PROP_FLOAT, PROP_NONE);
352 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
354 prop= RNA_def_property(srna, "double_array", PROP_FLOAT, PROP_NONE);
355 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
356 RNA_def_property_array(prop, 1);
359 prop= RNA_def_property(srna, "group", PROP_POINTER, PROP_NONE);
360 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
361 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
362 RNA_def_property_struct_type(prop, "PropertyGroup");
364 prop= RNA_def_property(srna, "collection", PROP_COLLECTION, PROP_NONE);
365 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
366 RNA_def_property_struct_type(prop, "PropertyGroup");
368 prop= RNA_def_property(srna, "idp_array", PROP_COLLECTION, PROP_NONE);
369 RNA_def_property_struct_type(prop, "PropertyGroup");
370 RNA_def_property_collection_funcs(prop, "rna_IDPArray_begin", "rna_iterator_array_next", "rna_iterator_array_end", "rna_iterator_array_get", "rna_IDPArray_length", NULL, NULL);
371 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
373 // never tested, maybe its useful to have this?
375 prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
376 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
377 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
378 RNA_def_property_ui_text(prop, "Name", "Unique name used in the code and scripting");
379 RNA_def_struct_name_property(srna, prop);
382 /* IDP_ID -- not implemented yet in id properties */
384 /* ID property groups > level 0, since level 0 group is merged
385 * with native RNA properties. the builtin_properties will take
386 * care of the properties here */
387 srna= RNA_def_struct(brna, "PropertyGroup", NULL);
388 RNA_def_struct_sdna(srna, "IDPropertyGroup");
389 RNA_def_struct_ui_text(srna, "ID Property Group", "Group of ID properties");
390 RNA_def_struct_idprops_func(srna, "rna_PropertyGroup_idprops");
391 RNA_def_struct_register_funcs(srna, "rna_PropertyGroup_register", "rna_PropertyGroup_unregister", NULL);
392 RNA_def_struct_refine_func(srna, "rna_PropertyGroup_refine");
394 /* important so python types can have their name used in list views
395 * however this isnt prefect because it overrides how python would set the name
396 * when we only really want this so RNA_def_struct_name_property() is set to something useful */
397 prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
398 RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
399 //RNA_def_property_clear_flag(prop, PROP_EDITABLE);
400 RNA_def_property_ui_text(prop, "Name", "Unique name used in the code and scripting");
401 RNA_def_struct_name_property(srna, prop);
405 static void rna_def_ID_materials(BlenderRNA *brna)
411 /* for mesh/mball/curve materials */
412 srna= RNA_def_struct(brna, "IDMaterials", NULL);
413 RNA_def_struct_sdna(srna, "ID");
414 RNA_def_struct_ui_text(srna, "ID Materials", "Collection of materials");
416 func= RNA_def_function(srna, "append", "material_append_id");
417 RNA_def_function_ui_description(func, "Add a new material to the data block.");
418 parm= RNA_def_pointer(func, "material", "Material", "", "Material to add.");
419 RNA_def_property_flag(parm, PROP_REQUIRED);
421 func= RNA_def_function(srna, "pop", "material_pop_id");
422 RNA_def_function_ui_description(func, "Remove a material from the data block.");
423 parm= RNA_def_int(func, "index", 0, 0, MAXMAT, "", "Index of material to remove.", 0, MAXMAT);
424 RNA_def_property_flag(parm, PROP_REQUIRED);
425 RNA_def_boolean(func, "update_data", 0, "", "Update data by re-adjusting the material slots assigned.");
426 parm= RNA_def_pointer(func, "material", "Material", "", "Material to remove.");
427 RNA_def_function_return(func, parm);
430 static void rna_def_ID(BlenderRNA *brna)
434 PropertyRNA *prop, *parm;
436 static EnumPropertyItem update_flag_items[] = {
437 {OB_RECALC_OB, "OBJECT", 0, "Object", ""},
438 {OB_RECALC_DATA, "DATA", 0, "Data", ""},
439 {OB_RECALC_TIME, "TIME", 0, "Time", ""},
440 {0, NULL, 0, NULL, NULL}};
442 srna= RNA_def_struct(brna, "ID", NULL);
443 RNA_def_struct_ui_text(srna, "ID", "Base type for datablocks, defining a unique name, linking from other libraries and garbage collection");
444 RNA_def_struct_flag(srna, STRUCT_ID|STRUCT_ID_REFCOUNT);
445 RNA_def_struct_refine_func(srna, "rna_ID_refine");
446 RNA_def_struct_idprops_func(srna, "rna_ID_idprops");
448 prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
449 RNA_def_property_ui_text(prop, "Name", "Unique datablock ID name");
450 RNA_def_property_string_funcs(prop, "rna_ID_name_get", "rna_ID_name_length", "rna_ID_name_set");
451 RNA_def_property_string_maxlength(prop, MAX_ID_NAME-2);
452 RNA_def_property_editable_func(prop, "rna_ID_name_editable");
453 RNA_def_property_update(prop, NC_ID|NA_RENAME, NULL);
454 RNA_def_struct_name_property(srna, prop);
456 prop= RNA_def_property(srna, "users", PROP_INT, PROP_UNSIGNED);
457 RNA_def_property_int_sdna(prop, NULL, "us");
458 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
459 RNA_def_property_ui_text(prop, "Users", "Number of times this datablock is referenced");
461 prop= RNA_def_property(srna, "use_fake_user", PROP_BOOLEAN, PROP_NONE);
462 RNA_def_property_boolean_sdna(prop, NULL, "flag", LIB_FAKEUSER);
463 RNA_def_property_ui_text(prop, "Fake User", "Saves this datablock even if it has no users");
464 RNA_def_property_boolean_funcs(prop, NULL, "rna_ID_fake_user_set");
466 prop= RNA_def_property(srna, "tag", PROP_BOOLEAN, PROP_NONE);
467 RNA_def_property_boolean_sdna(prop, NULL, "flag", LIB_DOIT);
468 RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
469 RNA_def_property_ui_text(prop, "Tag", "Tools can use this to tag data, (initial state is undefined)");
471 prop= RNA_def_property(srna, "library", PROP_POINTER, PROP_NONE);
472 RNA_def_property_pointer_sdna(prop, NULL, "lib");
473 RNA_def_property_clear_flag(prop, PROP_EDITABLE);
474 RNA_def_property_ui_text(prop, "Library", "Library file the datablock is linked from");
477 func= RNA_def_function(srna, "copy", "rna_ID_copy");
478 RNA_def_function_ui_description(func, "Create a copy of this datablock (not supported for all datablocks).");
479 parm= RNA_def_pointer(func, "id", "ID", "", "New copy of the ID.");
480 RNA_def_function_return(func, parm);
482 func= RNA_def_function(srna, "user_clear", "rna_ID_user_clear");
483 RNA_def_function_ui_description(func, "Clears the user count of a datablock so its not saved, on reload the data will be removed.");
485 func= RNA_def_function(srna, "animation_data_create", "BKE_id_add_animdata");
486 RNA_def_function_ui_description(func, "Create animation data to this ID, note that not all ID types support this.");
487 parm= RNA_def_pointer(func, "anim_data", "AnimData", "", "New animation data or NULL.");
488 RNA_def_function_return(func, parm);
490 func= RNA_def_function(srna, "animation_data_clear", "BKE_free_animdata");
491 RNA_def_function_ui_description(func, "Clear animation on this this ID.");
493 func= RNA_def_function(srna, "update_tag", "rna_ID_update_tag");
494 RNA_def_function_flag(func, FUNC_USE_REPORTS);
495 RNA_def_function_ui_description(func, "Tag the id to update its display data.");
496 RNA_def_enum_flag(func, "refresh", update_flag_items, 0, "", "Type of updates to perform.");
499 static void rna_def_library(BlenderRNA *brna)
504 srna= RNA_def_struct(brna, "Library", "ID");
505 RNA_def_struct_ui_text(srna, "Library", "External .blend file from which data is linked");
506 RNA_def_struct_ui_icon(srna, ICON_LIBRARY_DATA_DIRECT);
508 prop= RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
509 RNA_def_property_string_sdna(prop, NULL, "name");
510 RNA_def_property_ui_text(prop, "File Path", "Path to the library .blend file");
511 /* TODO - lib->filename isnt updated, however the outliner also skips this, probably only needed on read. */
513 prop= RNA_def_property(srna, "parent", PROP_POINTER, PROP_NONE);
514 RNA_def_property_struct_type(prop, "Library");
515 RNA_def_property_ui_text(prop, "Parent", "");
517 void RNA_def_ID(BlenderRNA *brna)
521 /* built-in unknown type */
522 srna= RNA_def_struct(brna, "UnknownType", NULL);
523 RNA_def_struct_ui_text(srna, "Unknown Type", "Stub RNA type used for pointers to unknown or internal data");
525 /* built-in any type */
526 srna= RNA_def_struct(brna, "AnyType", NULL);
527 RNA_def_struct_ui_text(srna, "Any Type", "RNA type used for pointers to any possible data");
530 rna_def_ID_properties(brna);
531 rna_def_ID_materials(brna);
532 rna_def_library(brna);