After talking with Matt about this, added notifier NC_ID to handle ID changes, specif...
[blender.git] / source / blender / makesrna / intern / rna_ID.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Blender Foundation (2008).
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include "RNA_access.h"
29 #include "RNA_define.h"
30 #include "RNA_types.h"
31
32 #include "DNA_ID.h"
33
34 #include "WM_types.h"
35
36 #include "rna_internal.h"
37
38 /* enum of ID-block types 
39  * NOTE: need to keep this in line with the other defines for these
40  */
41 EnumPropertyItem id_type_items[] = {
42         {ID_AC, "ACTION", ICON_ACTION, "Action", ""},
43         {ID_AR, "ARMATURE", ICON_ARMATURE_DATA, "Armature", ""},
44         {ID_BR, "BRUSH", ICON_BRUSH_DATA, "Brush", ""},
45         {ID_CA, "CAMERA", ICON_CAMERA_DATA, "Camera", ""},
46         {ID_CU, "CURVE", ICON_CURVE_DATA, "Curve", ""},
47         {ID_VF, "FONT", ICON_FONT_DATA, "Font", ""},
48         {ID_GD, "GREASEPENCIL", ICON_GREASEPENCIL, "Grease Pencil", ""},
49         {ID_GR, "GROUP", ICON_GROUP, "Group", ""},
50         {ID_IM, "IMAGE", ICON_IMAGE_DATA, "Image", ""},
51         {ID_KE, "KEY", ICON_SHAPEKEY_DATA, "Key", ""},
52         {ID_LA, "LAMP", ICON_LAMP_DATA, "Lamp", ""},
53         {ID_LI, "LIBRARY", ICON_LIBRARY_DATA_DIRECT, "Library", ""},
54         {ID_LT, "LATTICE", ICON_LATTICE_DATA, "Lattice", ""},
55         {ID_MA, "MATERIAL", ICON_MATERIAL_DATA, "Material", ""},
56         {ID_MB, "META", ICON_META_DATA, "MetaBall", ""},
57         {ID_ME, "MESH", ICON_MESH_DATA, "Mesh", ""},
58         {ID_NT, "NODETREE", ICON_NODETREE, "NodeTree", ""},
59         {ID_OB, "OBJECT", ICON_OBJECT_DATA, "Object", ""},
60         {ID_PA, "PARTICLE", ICON_PARTICLE_DATA, "Particle", ""},
61         {ID_SCE, "SCENE", ICON_SCENE_DATA, "Scene", ""},
62         {ID_SCR, "SCREEN", ICON_SPLITSCREEN, "Screen", ""},
63         {ID_SO, "SOUND", ICON_PLAY_AUDIO, "Sound", ""},
64         {ID_TXT, "TEXT", ICON_TEXT, "Text", ""},
65         {ID_TE, "TEXTURE", ICON_TEXTURE_DATA, "Texture", ""},
66         {ID_WO, "WORLD", ICON_WORLD_DATA, "World", ""},
67         {ID_WM, "WINDOWMANAGER", ICON_FULLSCREEN, "Window Manager", ""},
68         {0, NULL, 0, NULL, NULL}};
69
70 #ifdef RNA_RUNTIME
71
72 #include "BKE_idprop.h"
73 #include "BKE_library.h"
74 #include "BKE_animsys.h"
75
76 /* name functions that ignore the first two ID characters */
77 void rna_ID_name_get(PointerRNA *ptr, char *value)
78 {
79         ID *id= (ID*)ptr->data;
80         BLI_strncpy(value, id->name+2, sizeof(id->name)-2);
81 }
82
83 int rna_ID_name_length(PointerRNA *ptr)
84 {
85         ID *id= (ID*)ptr->data;
86         return strlen(id->name+2);
87 }
88
89 void rna_ID_name_set(PointerRNA *ptr, const char *value)
90 {
91         ID *id= (ID*)ptr->data;
92         BLI_strncpy(id->name+2, value, sizeof(id->name)-2);
93         test_idbutton(id->name+2);
94 }
95
96 short RNA_type_to_ID_code(StructRNA *type)
97 {
98         if(RNA_struct_is_a(type, &RNA_Action)) return ID_AC;
99         if(RNA_struct_is_a(type, &RNA_Armature)) return ID_AR;
100         if(RNA_struct_is_a(type, &RNA_Brush)) return ID_BR;
101         if(RNA_struct_is_a(type, &RNA_Camera)) return ID_CA;
102         if(RNA_struct_is_a(type, &RNA_Curve)) return ID_CU;
103         if(RNA_struct_is_a(type, &RNA_GreasePencil)) return ID_GD;
104         if(RNA_struct_is_a(type, &RNA_Group)) return ID_GR;
105         if(RNA_struct_is_a(type, &RNA_Image)) return ID_IM;
106         if(RNA_struct_is_a(type, &RNA_Key)) return ID_KE;
107         if(RNA_struct_is_a(type, &RNA_Lamp)) return ID_LA;
108         if(RNA_struct_is_a(type, &RNA_Library)) return ID_LI;
109         if(RNA_struct_is_a(type, &RNA_Lattice)) return ID_LT;
110         if(RNA_struct_is_a(type, &RNA_Material)) return ID_MA;
111         if(RNA_struct_is_a(type, &RNA_MetaBall)) return ID_MB;
112         if(RNA_struct_is_a(type, &RNA_NodeTree)) return ID_NT;
113         if(RNA_struct_is_a(type, &RNA_Mesh)) return ID_ME;
114         if(RNA_struct_is_a(type, &RNA_Object)) return ID_OB;
115         if(RNA_struct_is_a(type, &RNA_ParticleSettings)) return ID_PA;
116         if(RNA_struct_is_a(type, &RNA_Scene)) return ID_SCE;
117         if(RNA_struct_is_a(type, &RNA_Screen)) return ID_SCR;
118         if(RNA_struct_is_a(type, &RNA_Sound)) return ID_SO;
119         if(RNA_struct_is_a(type, &RNA_Text)) return ID_TXT;
120         if(RNA_struct_is_a(type, &RNA_Texture)) return ID_TE;
121         if(RNA_struct_is_a(type, &RNA_VectorFont)) return ID_VF;
122         if(RNA_struct_is_a(type, &RNA_World)) return ID_WO;
123         if(RNA_struct_is_a(type, &RNA_WindowManager)) return ID_WM;
124
125         return 0;
126 }
127
128 StructRNA *ID_code_to_RNA_type(short idcode)
129 {
130         switch(idcode) {
131                 case ID_AC: return &RNA_Action;
132                 case ID_AR: return &RNA_Armature;
133                 case ID_BR: return &RNA_Brush;
134                 case ID_CA: return &RNA_Camera;
135                 case ID_CU: return &RNA_Curve;
136                 case ID_GD: return &RNA_GreasePencil;
137                 case ID_GR: return &RNA_Group;
138                 case ID_IM: return &RNA_Image;
139                 case ID_KE: return &RNA_Key;
140                 case ID_LA: return &RNA_Lamp;
141                 case ID_LI: return &RNA_Library;
142                 case ID_LT: return &RNA_Lattice;
143                 case ID_MA: return &RNA_Material;
144                 case ID_MB: return &RNA_MetaBall;
145                 case ID_NT: return &RNA_NodeTree;
146                 case ID_ME: return &RNA_Mesh;
147                 case ID_OB: return &RNA_Object;
148                 case ID_PA: return &RNA_ParticleSettings;
149                 case ID_SCE: return &RNA_Scene;
150                 case ID_SCR: return &RNA_Screen;
151                 case ID_SO: return &RNA_Sound;
152                 case ID_TXT: return &RNA_Text;
153                 case ID_TE: return &RNA_Texture;
154                 case ID_VF: return &RNA_VectorFont;
155                 case ID_WO: return &RNA_World;
156                 case ID_WM: return &RNA_WindowManager;
157                 default: return &RNA_ID;
158         }
159 }
160
161 StructRNA *rna_ID_refine(PointerRNA *ptr)
162 {
163         ID *id= (ID*)ptr->data;
164
165         return ID_code_to_RNA_type(GS(id->name));
166 }
167
168 IDProperty *rna_ID_idproperties(PointerRNA *ptr, int create)
169 {
170         return IDP_GetProperties(ptr->data, create);
171 }
172
173 void rna_ID_fake_user_set(PointerRNA *ptr, int value)
174 {
175         ID *id= (ID*)ptr->data;
176
177         if(value && !(id->flag & LIB_FAKEUSER)) {
178                 id->flag |= LIB_FAKEUSER;
179                 id->us++;
180         }
181         else if(!value && (id->flag & LIB_FAKEUSER)) {
182                 id->flag &= ~LIB_FAKEUSER;
183                 id->us--;
184         }
185 }
186
187 IDProperty *rna_IDPropertyGroup_idproperties(PointerRNA *ptr, int create)
188 {
189         return ptr->data;
190 }
191
192 void rna_IDPropertyGroup_unregister(const bContext *C, StructRNA *type)
193 {
194         RNA_struct_free(&BLENDER_RNA, type);
195 }
196
197 StructRNA *rna_IDPropertyGroup_register(const bContext *C, ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
198 {
199         PointerRNA dummyptr;
200
201         /* create dummy pointer */
202         RNA_pointer_create(NULL, &RNA_IDPropertyGroup, NULL, &dummyptr);
203
204         /* validate the python class */
205         if(validate(&dummyptr, data, NULL) != 0)
206                 return NULL;
207
208         return RNA_def_struct(&BLENDER_RNA, identifier, "IDPropertyGroup");  // XXX
209 }
210
211 StructRNA* rna_IDPropertyGroup_refine(PointerRNA *ptr)
212 {
213         return ptr->type;
214 }
215
216 ID *rna_ID_copy(ID *id)
217 {
218         ID *newid;
219
220         if(id_copy(id, &newid, 0)) {
221                 if(newid) newid->us--;
222                 return newid;
223         }
224         
225         return NULL;
226 }
227
228 #else
229
230 static void rna_def_ID_properties(BlenderRNA *brna)
231 {
232         StructRNA *srna;
233         PropertyRNA *prop;
234
235         /* this is struct is used for holding the virtual
236          * PropertyRNA's for ID properties */
237         srna= RNA_def_struct(brna, "IDProperty", NULL);
238         RNA_def_struct_ui_text(srna, "ID Property", "Property that stores arbitrary, user defined properties.");
239         
240         /* IDP_STRING */
241         prop= RNA_def_property(srna, "string", PROP_STRING, PROP_NONE);
242         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
243
244         /* IDP_INT */
245         prop= RNA_def_property(srna, "int", PROP_INT, PROP_NONE);
246         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
247
248         prop= RNA_def_property(srna, "int_array", PROP_INT, PROP_NONE);
249         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
250         RNA_def_property_array(prop, 1);
251
252         /* IDP_FLOAT */
253         prop= RNA_def_property(srna, "float", PROP_FLOAT, PROP_NONE);
254         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
255
256         prop= RNA_def_property(srna, "float_array", PROP_FLOAT, PROP_NONE);
257         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
258         RNA_def_property_array(prop, 1);
259
260         /* IDP_DOUBLE */
261         prop= RNA_def_property(srna, "double", PROP_FLOAT, PROP_NONE);
262         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
263
264         prop= RNA_def_property(srna, "double_array", PROP_FLOAT, PROP_NONE);
265         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
266         RNA_def_property_array(prop, 1);
267
268         /* IDP_GROUP */
269         prop= RNA_def_property(srna, "group", PROP_POINTER, PROP_NONE);
270         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
271         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
272         RNA_def_property_struct_type(prop, "IDPropertyGroup");
273
274         prop= RNA_def_property(srna, "collection", PROP_COLLECTION, PROP_NONE);
275         RNA_def_property_flag(prop, PROP_EXPORT|PROP_IDPROPERTY);
276         RNA_def_property_struct_type(prop, "IDPropertyGroup");
277
278         /* IDP_ID -- not implemented yet in id properties */
279
280         /* ID property groups > level 0, since level 0 group is merged
281          * with native RNA properties. the builtin_properties will take
282          * care of the properties here */
283         srna= RNA_def_struct(brna, "IDPropertyGroup", NULL);
284         RNA_def_struct_ui_text(srna, "ID Property Group", "Group of ID properties.");
285         RNA_def_struct_idproperties_func(srna, "rna_IDPropertyGroup_idproperties");
286         RNA_def_struct_register_funcs(srna, "rna_IDPropertyGroup_register", "rna_IDPropertyGroup_unregister");
287         RNA_def_struct_refine_func(srna, "rna_IDPropertyGroup_refine");
288 }
289
290 static void rna_def_ID(BlenderRNA *brna)
291 {
292         StructRNA *srna;
293         FunctionRNA *func;
294         PropertyRNA *prop, *parm;
295
296         srna= RNA_def_struct(brna, "ID", NULL);
297         RNA_def_struct_ui_text(srna, "ID", "Base type for datablocks, defining a unique name, linking from other libraries and garbage collection.");
298         RNA_def_struct_flag(srna, STRUCT_ID|STRUCT_ID_REFCOUNT);
299         RNA_def_struct_refine_func(srna, "rna_ID_refine");
300         RNA_def_struct_idproperties_func(srna, "rna_ID_idproperties");
301
302         prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
303         RNA_def_property_ui_text(prop, "Name", "Unique datablock ID name.");
304         RNA_def_property_string_funcs(prop, "rna_ID_name_get", "rna_ID_name_length", "rna_ID_name_set");
305         RNA_def_property_string_maxlength(prop, sizeof(((ID*)NULL)->name)-2);
306         RNA_def_property_update(prop, NC_ID|ND_ID_RENAME, NULL);
307         RNA_def_struct_name_property(srna, prop);
308
309         prop= RNA_def_property(srna, "users", PROP_INT, PROP_UNSIGNED);
310         RNA_def_property_int_sdna(prop, NULL, "us");
311         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
312         RNA_def_property_ui_text(prop, "Users", "Number of times this datablock is referenced.");
313
314         prop= RNA_def_property(srna, "fake_user", PROP_BOOLEAN, PROP_NONE);
315         RNA_def_property_boolean_sdna(prop, NULL, "flag", LIB_FAKEUSER);
316         RNA_def_property_ui_text(prop, "Fake User", "Saves this datablock even if it has no users");
317         RNA_def_property_boolean_funcs(prop, NULL, "rna_ID_fake_user_set");
318
319         prop= RNA_def_property(srna, "tag", PROP_BOOLEAN, PROP_NONE);
320         RNA_def_property_boolean_sdna(prop, NULL, "flag", LIB_DOIT);
321         RNA_def_property_ui_text(prop, "Tag", "Tools can use this to tag data, (initial state is undefined).");
322
323         prop= RNA_def_property(srna, "library", PROP_POINTER, PROP_NONE);
324         RNA_def_property_pointer_sdna(prop, NULL, "lib");
325         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
326         RNA_def_property_ui_text(prop, "Library", "Library file the datablock is linked from.");
327
328         /* functions */
329         func= RNA_def_function(srna, "copy", "rna_ID_copy");
330         RNA_def_function_ui_description(func, "Create a copy of this datablock (not supported for all datablocks).");
331         parm= RNA_def_pointer(func, "id", "ID", "", "New copy of the ID.");
332         RNA_def_function_return(func, parm);
333
334         func= RNA_def_function(srna, "animation_data_create", "BKE_id_add_animdata");
335         RNA_def_function_ui_description(func, "Create animation data to this ID, note that not all ID types support this.");
336         parm= RNA_def_pointer(func, "anim_data", "AnimData", "", "New animation data or NULL.");
337         RNA_def_function_return(func, parm);
338
339         func= RNA_def_function(srna, "animation_data_clear", "BKE_free_animdata");
340         RNA_def_function_ui_description(func, "Clear animation on this this ID.");
341
342 }
343
344 static void rna_def_library(BlenderRNA *brna)
345 {
346         StructRNA *srna;
347         PropertyRNA *prop;
348
349         srna= RNA_def_struct(brna, "Library", "ID");
350         RNA_def_struct_ui_text(srna, "Library", "External .blend file from which data is linked.");
351         RNA_def_struct_ui_icon(srna, ICON_LIBRARY_DATA_DIRECT);
352
353         prop= RNA_def_property(srna, "filename", PROP_STRING, PROP_FILEPATH);
354         RNA_def_property_string_sdna(prop, NULL, "name");
355         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
356         RNA_def_property_ui_text(prop, "Filename", "Path to the library .blend file.");
357 }
358 void RNA_def_ID(BlenderRNA *brna)
359 {
360         StructRNA *srna;
361
362         /* built-in unknown type */
363         srna= RNA_def_struct(brna, "UnknownType", NULL);
364         RNA_def_struct_ui_text(srna, "Unknown Type", "Stub RNA type used for pointers to unknown or internal data.");
365
366         /* built-in any type */
367         srna= RNA_def_struct(brna, "AnyType", NULL);
368         RNA_def_struct_ui_text(srna, "Any Type", "RNA type used for pointers to any possible data.");
369
370         rna_def_ID(brna);
371         rna_def_ID_properties(brna);
372         rna_def_library(brna);
373 }
374
375 #endif
376