f4c6eeff4c57114b3dbe209ae47bb01872093845
[blender.git] / source / blender / makesrna / intern / rna_main.c
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
17 /** \file
18  * \ingroup RNA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "BLI_utildefines.h"
25 #include "BLI_path_util.h"
26
27 #include "RNA_define.h"
28 #include "RNA_access.h"
29
30 #include "rna_internal.h"
31
32 #ifdef RNA_RUNTIME
33
34 #include "BKE_main.h"
35 #include "BKE_mesh.h"
36 #include "BKE_global.h"
37
38 /* all the list begin functions are added manually here, Main is not in SDNA */
39
40 static bool rna_Main_use_autopack_get(PointerRNA *UNUSED(ptr))
41 {
42         if (G.fileflags & G_FILE_AUTOPACK)
43                 return 1;
44
45         return 0;
46 }
47
48 static void rna_Main_use_autopack_set(PointerRNA *UNUSED(ptr), bool value)
49 {
50         if (value)
51                 G.fileflags |= G_FILE_AUTOPACK;
52         else
53                 G.fileflags &= ~G_FILE_AUTOPACK;
54 }
55
56 static bool rna_Main_is_saved_get(PointerRNA *UNUSED(ptr))
57 {
58         return G.relbase_valid;
59 }
60
61 static bool rna_Main_is_dirty_get(PointerRNA *ptr)
62 {
63         /* XXX, not totally nice to do it this way, should store in main ? */
64         Main *bmain = (Main *)ptr->data;
65         wmWindowManager *wm;
66         if ((wm = bmain->wm.first)) {
67                 return !wm->file_saved;
68         }
69
70         return true;
71 }
72
73 static void rna_Main_filepath_get(PointerRNA *ptr, char *value)
74 {
75         Main *bmain = (Main *)ptr->data;
76         BLI_strncpy(value, bmain->name, sizeof(bmain->name));
77 }
78
79 static int rna_Main_filepath_length(PointerRNA *ptr)
80 {
81         Main *bmain = (Main *)ptr->data;
82         return strlen(bmain->name);
83 }
84
85 #if 0
86 static void rna_Main_filepath_set(PointerRNA *ptr, const char *value)
87 {
88         Main *bmain = (Main *)ptr->data;
89         BLI_strncpy(bmain->name, value, sizeof(bmain->name));
90 }
91 #endif
92
93 static void rna_Main_scenes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
94 {
95         Main *bmain = (Main *)ptr->data;
96         rna_iterator_listbase_begin(iter, &bmain->scenes, NULL);
97 }
98
99 static void rna_Main_objects_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
100 {
101         Main *bmain = (Main *)ptr->data;
102         rna_iterator_listbase_begin(iter, &bmain->objects, NULL);
103 }
104
105 static void rna_Main_lights_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
106 {
107         Main *bmain = (Main *)ptr->data;
108         rna_iterator_listbase_begin(iter, &bmain->lights, NULL);
109 }
110
111 static void rna_Main_libraries_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
112 {
113         Main *bmain = (Main *)ptr->data;
114         rna_iterator_listbase_begin(iter, &bmain->libraries, NULL);
115 }
116
117 static void rna_Main_meshes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
118 {
119         Main *bmain = (Main *)ptr->data;
120         rna_iterator_listbase_begin(iter, &bmain->meshes, NULL);
121 }
122
123 static void rna_Main_curves_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
124 {
125         Main *bmain = (Main *)ptr->data;
126         rna_iterator_listbase_begin(iter, &bmain->curves, NULL);
127 }
128
129 static void rna_Main_metaballs_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
130 {
131         Main *bmain = (Main *)ptr->data;
132         rna_iterator_listbase_begin(iter, &bmain->metaballs, NULL);
133 }
134
135 static void rna_Main_materials_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
136 {
137         Main *bmain = (Main *)ptr->data;
138         rna_iterator_listbase_begin(iter, &bmain->materials, NULL);
139 }
140
141 static void rna_Main_textures_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
142 {
143         Main *bmain = (Main *)ptr->data;
144         rna_iterator_listbase_begin(iter, &bmain->textures, NULL);
145 }
146
147 static void rna_Main_images_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
148 {
149         Main *bmain = (Main *)ptr->data;
150         rna_iterator_listbase_begin(iter, &bmain->images, NULL);
151 }
152
153 static void rna_Main_lattices_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
154 {
155         Main *bmain = (Main *)ptr->data;
156         rna_iterator_listbase_begin(iter, &bmain->lattices, NULL);
157 }
158
159 static void rna_Main_cameras_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
160 {
161         Main *bmain = (Main *)ptr->data;
162         rna_iterator_listbase_begin(iter, &bmain->cameras, NULL);
163 }
164
165 static void rna_Main_shapekeys_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
166 {
167         Main *bmain = (Main *)ptr->data;
168         rna_iterator_listbase_begin(iter, &bmain->shapekeys, NULL);
169 }
170
171 static void rna_Main_worlds_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
172 {
173         Main *bmain = (Main *)ptr->data;
174         rna_iterator_listbase_begin(iter, &bmain->worlds, NULL);
175 }
176
177 static void rna_Main_screens_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
178 {
179         Main *bmain = (Main *)ptr->data;
180         rna_iterator_listbase_begin(iter, &bmain->screens, NULL);
181 }
182
183 static void rna_Main_fonts_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
184 {
185         Main *bmain = (Main *)ptr->data;
186         rna_iterator_listbase_begin(iter, &bmain->fonts, NULL);
187 }
188
189 static void rna_Main_texts_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
190 {
191         Main *bmain = (Main *)ptr->data;
192         rna_iterator_listbase_begin(iter, &bmain->texts, NULL);
193 }
194
195 static void rna_Main_speakers_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
196 {
197         Main *bmain = (Main *)ptr->data;
198         rna_iterator_listbase_begin(iter, &bmain->speakers, NULL);
199 }
200
201 static void rna_Main_sounds_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
202 {
203         Main *bmain = (Main *)ptr->data;
204         rna_iterator_listbase_begin(iter, &bmain->sounds, NULL);
205 }
206
207 static void rna_Main_collections_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
208 {
209         Main *bmain = (Main *)ptr->data;
210         rna_iterator_listbase_begin(iter, &bmain->collections, NULL);
211 }
212
213 static void rna_Main_armatures_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
214 {
215         Main *bmain = (Main *)ptr->data;
216         rna_iterator_listbase_begin(iter, &bmain->armatures, NULL);
217 }
218
219 static void rna_Main_actions_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
220 {
221         Main *bmain = (Main *)ptr->data;
222         rna_iterator_listbase_begin(iter, &bmain->actions, NULL);
223 }
224
225 static void rna_Main_nodetrees_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
226 {
227         Main *bmain = (Main *)ptr->data;
228         rna_iterator_listbase_begin(iter, &bmain->nodetrees, NULL);
229 }
230
231 static void rna_Main_brushes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
232 {
233         Main *bmain = (Main *)ptr->data;
234         rna_iterator_listbase_begin(iter, &bmain->brushes, NULL);
235 }
236
237 static void rna_Main_particles_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
238 {
239         Main *bmain = (Main *)ptr->data;
240         rna_iterator_listbase_begin(iter, &bmain->particles, NULL);
241 }
242
243 static void rna_Main_palettes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
244 {
245         Main *bmain = (Main *)ptr->data;
246         rna_iterator_listbase_begin(iter, &bmain->palettes, NULL);
247 }
248
249 static void rna_Main_gpencils_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
250 {
251         Main *bmain = (Main *)ptr->data;
252         rna_iterator_listbase_begin(iter, &bmain->gpencils, NULL);
253 }
254
255 static void rna_Main_wm_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
256 {
257         Main *bmain = (Main *)ptr->data;
258         rna_iterator_listbase_begin(iter, &bmain->wm, NULL);
259 }
260
261 static void rna_Main_movieclips_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
262 {
263         Main *bmain = (Main *)ptr->data;
264         rna_iterator_listbase_begin(iter, &bmain->movieclips, NULL);
265 }
266
267 static void rna_Main_masks_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
268 {
269         Main *bmain = (Main *)ptr->data;
270         rna_iterator_listbase_begin(iter, &bmain->masks, NULL);
271 }
272
273 static void rna_Main_linestyles_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
274 {
275         Main *bmain = (Main *)ptr->data;
276         rna_iterator_listbase_begin(iter, &bmain->linestyles, NULL);
277 }
278
279 static void rna_Main_cachefiles_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
280 {
281         Main *bmain = (Main *)ptr->data;
282         rna_iterator_listbase_begin(iter, &bmain->cachefiles, NULL);
283 }
284
285 static void rna_Main_paintcurves_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
286 {
287         Main *bmain = (Main *)ptr->data;
288         rna_iterator_listbase_begin(iter, &bmain->paintcurves, NULL);
289 }
290
291 static void rna_Main_workspaces_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
292 {
293         Main *bmain = (Main *)ptr->data;
294         rna_iterator_listbase_begin(iter, &bmain->workspaces, NULL);
295 }
296
297 static void rna_Main_lightprobes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
298 {
299         Main *bmain = (Main *)ptr->data;
300         rna_iterator_listbase_begin(iter, &bmain->lightprobes, NULL);
301 }
302
303 static void rna_Main_version_get(PointerRNA *ptr, int *value)
304 {
305         Main *bmain = (Main *)ptr->data;
306         value[0] = bmain->versionfile / 100;
307         value[1] = bmain->versionfile % 100;
308         value[2] = bmain->subversionfile;
309 }
310
311 #ifdef UNIT_TEST
312
313 static PointerRNA rna_Test_test_get(PointerRNA *ptr)
314 {
315         PointerRNA ret = *ptr;
316         ret.type = &RNA_Test;
317
318         return ret;
319 }
320
321 #endif
322
323 #else
324
325 /* local convenience types */
326 typedef void (CollectionDefFunc)(struct BlenderRNA *brna, struct PropertyRNA *cprop);
327
328 typedef struct MainCollectionDef {
329         const char *identifier;
330         const char *type;
331         const char *iter_begin;
332         const char *name;
333         const char *description;
334         CollectionDefFunc *func;
335 } MainCollectionDef;
336
337 void RNA_def_main(BlenderRNA *brna)
338 {
339         StructRNA *srna;
340         PropertyRNA *prop;
341         CollectionDefFunc *func;
342
343         /* plural must match idtypes in readblenentry.c */
344         MainCollectionDef lists[] = {
345                 {"cameras", "Camera", "rna_Main_cameras_begin", "Cameras", "Camera data-blocks", RNA_def_main_cameras},
346                 {"scenes", "Scene", "rna_Main_scenes_begin", "Scenes", "Scene data-blocks", RNA_def_main_scenes},
347                 {"objects", "Object", "rna_Main_objects_begin", "Objects", "Object data-blocks", RNA_def_main_objects},
348                 {"materials", "Material", "rna_Main_materials_begin", "Materials", "Material data-blocks", RNA_def_main_materials},
349                 {"node_groups", "NodeTree", "rna_Main_nodetrees_begin", "Node Groups", "Node group data-blocks", RNA_def_main_node_groups},
350                 {"meshes", "Mesh", "rna_Main_meshes_begin", "Meshes", "Mesh data-blocks", RNA_def_main_meshes},
351                 {"lights", "Light", "rna_Main_lights_begin", "Lights", "Light data-blocks", RNA_def_main_lights},
352                 {"libraries", "Library", "rna_Main_libraries_begin", "Libraries", "Library data-blocks", RNA_def_main_libraries},
353                 {"screens", "Screen", "rna_Main_screens_begin", "Screens", "Screen data-blocks", RNA_def_main_screens},
354                 {"window_managers", "WindowManager", "rna_Main_wm_begin", "Window Managers", "Window manager data-blocks", RNA_def_main_window_managers},
355                 {"images", "Image", "rna_Main_images_begin", "Images", "Image data-blocks", RNA_def_main_images},
356                 {"lattices", "Lattice", "rna_Main_lattices_begin", "Lattices", "Lattice data-blocks", RNA_def_main_lattices},
357                 {"curves", "Curve", "rna_Main_curves_begin", "Curves", "Curve data-blocks", RNA_def_main_curves},
358                 {"metaballs", "MetaBall", "rna_Main_metaballs_begin", "Metaballs", "Metaball data-blocks", RNA_def_main_metaballs},
359                 {"fonts", "VectorFont", "rna_Main_fonts_begin", "Vector Fonts", "Vector font data-blocks", RNA_def_main_fonts},
360                 {"textures", "Texture", "rna_Main_textures_begin", "Textures", "Texture data-blocks", RNA_def_main_textures},
361                 {"brushes", "Brush", "rna_Main_brushes_begin", "Brushes", "Brush data-blocks", RNA_def_main_brushes},
362                 {"worlds", "World", "rna_Main_worlds_begin", "Worlds", "World data-blocks", RNA_def_main_worlds},
363                 {"collections", "Collection", "rna_Main_collections_begin", "Collections", "Collection data-blocks", RNA_def_main_collections},
364                 {"shape_keys", "Key", "rna_Main_shapekeys_begin", "Shape Keys", "Shape Key data-blocks", NULL},
365                 {"texts", "Text", "rna_Main_texts_begin", "Texts", "Text data-blocks", RNA_def_main_texts},
366                 {"speakers", "Speaker", "rna_Main_speakers_begin", "Speakers", "Speaker data-blocks", RNA_def_main_speakers},
367                 {"sounds", "Sound", "rna_Main_sounds_begin", "Sounds", "Sound data-blocks", RNA_def_main_sounds},
368                 {"armatures", "Armature", "rna_Main_armatures_begin", "Armatures", "Armature data-blocks", RNA_def_main_armatures},
369                 {"actions", "Action", "rna_Main_actions_begin", "Actions", "Action data-blocks", RNA_def_main_actions},
370                 {"particles", "ParticleSettings", "rna_Main_particles_begin", "Particles", "Particle data-blocks", RNA_def_main_particles},
371                 {"palettes", "Palette", "rna_Main_palettes_begin", "Palettes", "Palette data-blocks", RNA_def_main_palettes},
372                 {"grease_pencils", "GreasePencil", "rna_Main_gpencils_begin", "Grease Pencil", "Grease Pencil data-blocks", RNA_def_main_gpencil},
373                 {"movieclips", "MovieClip", "rna_Main_movieclips_begin", "Movie Clips", "Movie Clip data-blocks", RNA_def_main_movieclips},
374                 {"masks", "Mask", "rna_Main_masks_begin", "Masks", "Masks data-blocks", RNA_def_main_masks},
375                 {"linestyles", "FreestyleLineStyle", "rna_Main_linestyles_begin", "Line Styles", "Line Style data-blocks", RNA_def_main_linestyles},
376                 {"cache_files", "CacheFile", "rna_Main_cachefiles_begin", "Cache Files", "Cache Files data-blocks", RNA_def_main_cachefiles},
377                 {"paint_curves", "PaintCurve", "rna_Main_paintcurves_begin", "Paint Curves", "Paint Curves data-blocks", RNA_def_main_paintcurves},
378                 {"workspaces", "WorkSpace", "rna_Main_workspaces_begin", "Workspaces", "Workspace data-blocks", RNA_def_main_workspaces},
379                 {"lightprobes", "LightProbe", "rna_Main_lightprobes_begin", "LightProbes", "LightProbe data-blocks", RNA_def_main_lightprobes},
380                 {NULL, NULL, NULL, NULL, NULL, NULL},
381         };
382
383         int i;
384
385         srna = RNA_def_struct(brna, "BlendData", NULL);
386         RNA_def_struct_ui_text(srna, "Blendfile Data",
387                                "Main data structure representing a .blend file and all its data-blocks");
388         RNA_def_struct_ui_icon(srna, ICON_BLENDER);
389
390         prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
391         RNA_def_property_string_maxlength(prop, FILE_MAX);
392         RNA_def_property_string_funcs(prop, "rna_Main_filepath_get", "rna_Main_filepath_length", "rna_Main_filepath_set");
393         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
394         RNA_def_property_ui_text(prop, "Filename", "Path to the .blend file");
395
396         prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
397         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
398         RNA_def_property_boolean_funcs(prop, "rna_Main_is_dirty_get", NULL);
399         RNA_def_property_ui_text(prop, "File Has Unsaved Changes", "Have recent edits been saved to disk");
400
401         prop = RNA_def_property(srna, "is_saved", PROP_BOOLEAN, PROP_NONE);
402         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
403         RNA_def_property_boolean_funcs(prop, "rna_Main_is_saved_get", NULL);
404         RNA_def_property_ui_text(prop, "File is Saved", "Has the current session been saved to disk as a .blend file");
405
406         prop = RNA_def_property(srna, "use_autopack", PROP_BOOLEAN, PROP_NONE);
407         RNA_def_property_boolean_funcs(prop, "rna_Main_use_autopack_get", "rna_Main_use_autopack_set");
408         RNA_def_property_ui_text(prop, "Use Autopack", "Automatically pack all external data into .blend file");
409
410         prop = RNA_def_int_vector(srna, "version", 3, NULL, 0, INT_MAX,
411                            "Version", "Version of Blender the .blend was saved with", 0, INT_MAX);
412         RNA_def_property_int_funcs(prop, "rna_Main_version_get", NULL, NULL);
413         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
414         RNA_def_property_flag(prop, PROP_THICK_WRAP);
415
416         for (i = 0; lists[i].name; i++) {
417                 prop = RNA_def_property(srna, lists[i].identifier, PROP_COLLECTION, PROP_NONE);
418                 RNA_def_property_struct_type(prop, lists[i].type);
419                 RNA_def_property_collection_funcs(prop, lists[i].iter_begin, "rna_iterator_listbase_next",
420                                                   "rna_iterator_listbase_end", "rna_iterator_listbase_get",
421                                                   NULL, NULL, NULL, NULL);
422                 RNA_def_property_ui_text(prop, lists[i].name, lists[i].description);
423
424                 /* collection functions */
425                 func = lists[i].func;
426                 if (func)
427                         func(brna, prop);
428         }
429
430         RNA_api_main(srna);
431
432 #ifdef UNIT_TEST
433
434         RNA_define_verify_sdna(0);
435
436         prop = RNA_def_property(srna, "test", PROP_POINTER, PROP_NONE);
437         RNA_def_property_struct_type(prop, "Test");
438         RNA_def_property_pointer_funcs(prop, "rna_Test_test_get", NULL, NULL, NULL);
439
440         RNA_define_verify_sdna(1);
441
442 #endif
443 }
444
445 #endif