style cleanup: spelling.
[blender.git] / source / blender / makesrna / intern / makesrna.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Contributor(s): Blender Foundation (2008).
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/makesrna/intern/makesrna.c
24  *  \ingroup RNA
25  */
26
27
28 #include <float.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "RNA_access.h"
38 #include "RNA_define.h"
39 #include "RNA_types.h"
40
41 #include "rna_internal.h"
42
43 #ifdef _WIN32
44 #  ifndef snprintf
45 #    define snprintf _snprintf
46 #  endif
47 #endif
48
49 /* so we can use __func__ everywhere */
50 #if defined(_MSC_VER)
51 #  define __func__ __FUNCTION__
52 #endif
53
54 /* Replace if different */
55 #define TMP_EXT ".tmp"
56
57
58 /* copied from BLI_file_older */
59 #include <sys/stat.h>
60 static int file_older(const char *file1, const char *file2)
61 {
62         struct stat st1, st2;
63         /* printf("compare: %s %s\n", file1, file2); */
64
65         if (stat(file1, &st1)) return 0;
66         if (stat(file2, &st2)) return 0;
67
68         return (st1.st_mtime < st2.st_mtime);
69 }
70 static const char *makesrna_path = NULL;
71
72 static int replace_if_different(char *tmpfile, const char *dep_files[])
73 {
74         /* return 0;  *//* use for testing had edited rna */
75
76 #define REN_IF_DIFF                                                           \
77         {                                                                         \
78                 FILE *file_test = fopen(orgfile, "rb");                                \
79                 if (file_test) {                                                       \
80                         fclose(file_test);                                                \
81                         if (fp_org) fclose(fp_org);                                        \
82                         if (fp_new) fclose(fp_new);                                        \
83                         if (remove(orgfile) != 0) {                                        \
84                                 fprintf(stderr, "%s:%d, Remove Error (%s): \"%s\"\n",         \
85                                         __FILE__, __LINE__, strerror(errno), orgfile);        \
86                                 return -1;                                                    \
87                         }                                                                 \
88                 }                                                                     \
89         }                                                                         \
90         if (rename(tmpfile, orgfile) != 0) {                                       \
91                 fprintf(stderr, "%s:%d, Rename Error (%s): \"%s\" -> \"%s\"\n",       \
92                         __FILE__, __LINE__, strerror(errno), tmpfile, orgfile);       \
93                 return -1;                                                            \
94         }                                                                         \
95         remove(tmpfile);                                                          \
96         return 1;                                                                 \
97
98 /* end REN_IF_DIFF */
99
100
101         FILE *fp_new = NULL, *fp_org = NULL;
102         int len_new, len_org;
103         char *arr_new, *arr_org;
104         int cmp;
105
106         char orgfile[4096];
107
108         strcpy(orgfile, tmpfile);
109         orgfile[strlen(orgfile) - strlen(TMP_EXT)] = '\0'; /* strip '.tmp' */
110
111         fp_org = fopen(orgfile, "rb");
112
113         if (fp_org == NULL) {
114                 REN_IF_DIFF;
115         }
116
117
118         /* XXX, trick to work around dependency problem
119          * assumes dep_files is in the same dir as makesrna.c, which is true for now. */
120
121         if (1) {
122                 /* first check if makesrna.c is newer then generated files
123                  * for development on makesrna.c you may want to disable this */
124                 if (file_older(orgfile, __FILE__)) {
125                         REN_IF_DIFF;
126                 }
127
128                 if (file_older(orgfile, makesrna_path)) {
129                         REN_IF_DIFF;
130                 }
131
132                 /* now check if any files we depend on are newer then any generated files */
133                 if (dep_files) {
134                         int pass;
135                         for (pass = 0; dep_files[pass]; pass++) {
136                                 char from_path[4096] = __FILE__;
137                                 char *p1, *p2;
138
139                                 /* dir only */
140                                 p1 = strrchr(from_path, '/');
141                                 p2 = strrchr(from_path, '\\');
142                                 strcpy((p1 > p2 ? p1 : p2)+1, dep_files[pass]);
143                                 /* account for build deps, if makesrna.c (this file) is newer */
144                                 if (file_older(orgfile, from_path)) {
145                                         REN_IF_DIFF;
146                                 }
147                         }
148                 }
149         }
150         /* XXX end dep trick */
151
152
153         fp_new = fopen(tmpfile, "rb");
154
155         if (fp_new == NULL) {
156                 /* shouldn't happen, just to be safe */
157                 fprintf(stderr, "%s:%d, open error: \"%s\"\n", __FILE__, __LINE__, tmpfile);
158                 fclose(fp_org);
159                 return -1;
160         }
161
162         fseek(fp_new, 0L, SEEK_END); len_new = ftell(fp_new); fseek(fp_new, 0L, SEEK_SET);
163         fseek(fp_org, 0L, SEEK_END); len_org = ftell(fp_org); fseek(fp_org, 0L, SEEK_SET);
164
165
166         if (len_new != len_org) {
167                 fclose(fp_new);
168                 fclose(fp_org);
169                 REN_IF_DIFF;
170         }
171
172         /* now compare the files... */
173         arr_new = MEM_mallocN(sizeof(char)*len_new, "rna_cmp_file_new");
174         arr_org = MEM_mallocN(sizeof(char)*len_org, "rna_cmp_file_org");
175
176         if (fread(arr_new, sizeof(char), len_new, fp_new) != len_new)
177                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, tmpfile);
178         if (fread(arr_org, sizeof(char), len_org, fp_org) != len_org)
179                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, orgfile);
180
181         fclose(fp_new);
182         fclose(fp_org);
183
184         cmp = memcmp(arr_new, arr_org, len_new);
185
186         MEM_freeN(arr_new);
187         MEM_freeN(arr_org);
188
189         if (cmp) {
190                 REN_IF_DIFF;
191         }
192         else {
193                 remove(tmpfile);
194                 return 0;
195         }
196
197 #undef REN_IF_DIFF
198 }
199
200 /* Helper to solve keyword problems with C/C++ */
201
202 static const char *rna_safe_id(const char *id)
203 {
204         if (strcmp(id, "default") == 0)
205                 return "default_value";
206         else if (strcmp(id, "operator") == 0)
207                 return "operator_value";
208
209         return id;
210 }
211
212 /* Sorting */
213
214 static int cmp_struct(const void *a, const void *b)
215 {
216         const StructRNA *structa = *(const StructRNA**)a;
217         const StructRNA *structb = *(const StructRNA**)b;
218
219         return strcmp(structa->identifier, structb->identifier);
220 }
221
222 static int cmp_property(const void *a, const void *b)
223 {
224         const PropertyRNA *propa = *(const PropertyRNA**)a;
225         const PropertyRNA *propb = *(const PropertyRNA**)b;
226
227         if (strcmp(propa->identifier, "rna_type") == 0) return -1;
228         else if (strcmp(propb->identifier, "rna_type") == 0) return 1;
229
230         if (strcmp(propa->identifier, "name") == 0) return -1;
231         else if (strcmp(propb->identifier, "name") == 0) return 1;
232
233         return strcmp(propa->name, propb->name);
234 }
235
236 static int cmp_def_struct(const void *a, const void *b)
237 {
238         const StructDefRNA *dsa = *(const StructDefRNA**)a;
239         const StructDefRNA *dsb = *(const StructDefRNA**)b;
240
241         return cmp_struct(&dsa->srna, &dsb->srna);
242 }
243
244 static int cmp_def_property(const void *a, const void *b)
245 {
246         const PropertyDefRNA *dpa = *(const PropertyDefRNA**)a;
247         const PropertyDefRNA *dpb = *(const PropertyDefRNA**)b;
248
249         return cmp_property(&dpa->prop, &dpb->prop);
250 }
251
252 static void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
253 {
254         Link *link;
255         void **array;
256         int a, size;
257         
258         if (listbase->first == listbase->last)
259                 return;
260
261         for (size = 0, link = listbase->first; link; link = link->next)
262                 size++;
263
264         array = MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
265         for (a = 0, link = listbase->first; link; link = link->next, a++)
266                 array[a] = link;
267
268         qsort(array, size, sizeof(void*), cmp);
269
270         listbase->first = listbase->last = NULL;
271         for (a = 0; a<size; a++) {
272                 link = array[a];
273                 link->next = link->prev = NULL;
274                 rna_addtail(listbase, link);
275         }
276
277         MEM_freeN(array);
278 }
279
280 /* Preprocessing */
281
282 static void rna_print_c_string(FILE *f, const char *str)
283 {
284         static const char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
285         int i, j;
286
287         if (!str) {
288                 fprintf(f, "NULL");
289                 return;
290         }
291
292         fprintf(f, "\"");
293         for (i = 0; str[i]; i++) {
294                 for (j = 0; escape[j]; j++)
295                         if (str[i] == escape[j][0])
296                                 break;
297
298                 if (escape[j]) fprintf(f, "\\%c", escape[j][1]);
299                 else fprintf(f, "%c", str[i]);
300         }
301         fprintf(f, "\"");
302 }
303
304 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
305 {
306         if (dp->dnastructfromname && dp->dnastructfromprop)
307                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
308         else
309                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
310 }
311
312 static void rna_print_id_get(FILE *f, PropertyDefRNA *dp)
313 {
314         fprintf(f, "    ID *id= ptr->id.data;\n");
315 }
316
317 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
318 {
319         AllocDefRNA *alloc;
320         char buffer[2048];
321         char *result;
322
323         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
324         result = MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
325         strcpy(result, buffer);
326
327         alloc = MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
328         alloc->mem = result;
329         rna_addtail(&DefRNA.allocs, alloc);
330
331         return result;
332 }
333
334 static StructRNA *rna_find_struct(const char *identifier)
335 {
336         StructDefRNA *ds;
337
338         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
339                 if (strcmp(ds->srna->identifier, identifier) == 0)
340                         return ds->srna;
341
342         return NULL;
343 }
344
345 static const char *rna_find_type(const char *type)
346 {
347         StructDefRNA *ds;
348
349         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
350                 if (ds->dnaname && strcmp(ds->dnaname, type) == 0)
351                         return ds->srna->identifier;
352
353         return NULL;
354 }
355
356 static const char *rna_find_dna_type(const char *type)
357 {
358         StructDefRNA *ds;
359
360         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
361                 if (strcmp(ds->srna->identifier, type) == 0)
362                         return ds->dnaname;
363
364         return NULL;
365 }
366
367 static const char *rna_type_type_name(PropertyRNA *prop)
368 {
369         switch (prop->type) {
370                 case PROP_BOOLEAN:
371                 case PROP_INT:
372                 case PROP_ENUM:
373                         return "int";
374                 case PROP_FLOAT:
375                         return "float";
376                 case PROP_STRING:
377                         if (prop->flag & PROP_THICK_WRAP) {
378                                 return "char*";
379                         }
380                         else {
381                                 return "const char*";
382                         }
383                 default:
384                         return NULL;
385         }
386 }
387
388 static const char *rna_type_type(PropertyRNA *prop)
389 {
390         const char *type;
391
392         type = rna_type_type_name(prop);
393
394         if (type)
395                 return type;
396
397         return "PointerRNA";
398 }
399
400 static const char *rna_type_struct(PropertyRNA *prop)
401 {
402         const char *type;
403
404         type = rna_type_type_name(prop);
405
406         if (type)
407                 return "";
408
409         return "struct ";
410 }
411
412 static const char *rna_parameter_type_name(PropertyRNA *parm)
413 {
414         const char *type;
415
416         type = rna_type_type_name(parm);
417
418         if (type)
419                 return type;
420
421         switch (parm->type) {
422                 case PROP_POINTER:  {
423                         PointerPropertyRNA *pparm = (PointerPropertyRNA*)parm;
424
425                         if (parm->flag & PROP_RNAPTR)
426                                 return "PointerRNA";
427                         else
428                                 return rna_find_dna_type((const char *)pparm->type);
429                 }
430                 case PROP_COLLECTION: {
431                         return "ListBase";
432                 }
433                 default:
434                         return "<error, no type specified>";
435         }
436 }
437
438 static int rna_enum_bitmask(PropertyRNA *prop)
439 {
440         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
441         int a, mask = 0;
442
443         if (eprop->item) {
444                 for (a = 0; a<eprop->totitem; a++)
445                         if (eprop->item[a].identifier[0])
446                                 mask |= eprop->item[a].value;
447         }
448         
449         return mask;
450 }
451
452 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
453 {
454         return ( (prop->type == PROP_FLOAT) &&
455                  (prop->subtype == PROP_COLOR || prop->subtype == PROP_COLOR_GAMMA) &&
456                  (IS_DNATYPE_FLOAT_COMPAT(dp->dnatype) == 0) );
457 }
458
459 static const char *rna_function_string(void *func)
460 {
461         return (func)? (const char*)func: "NULL";
462 }
463
464 static void rna_float_print(FILE *f, float num)
465 {
466         if (num == -FLT_MAX) fprintf(f, "-FLT_MAX");
467         else if (num == FLT_MAX) fprintf(f, "FLT_MAX");
468         else if ((int)num == num) fprintf(f, "%.1ff", num);
469         else fprintf(f, "%.10ff", num);
470 }
471
472 static void rna_int_print(FILE *f, int num)
473 {
474         if (num == INT_MIN) fprintf(f, "INT_MIN");
475         else if (num == INT_MAX) fprintf(f, "INT_MAX");
476         else fprintf(f, "%d", num);
477 }
478
479 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
480 {
481         char *func;
482
483         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
484                 return NULL;
485
486         if (!manualfunc) {
487                 if (!dp->dnastructname || !dp->dnaname) {
488                         fprintf(stderr, "%s (0): %s.%s has no valid dna info.\n",
489                                 __func__, srna->identifier, prop->identifier);
490                         DefRNA.error = 1;
491                         return NULL;
492                 }
493
494                 /* typecheck,  */
495                 if (dp->dnatype && *dp->dnatype) {
496
497                         if (prop->type == PROP_FLOAT) {
498                                 if (IS_DNATYPE_FLOAT_COMPAT(dp->dnatype) == 0) {
499                                         if (prop->subtype != PROP_COLOR_GAMMA) { /* colors are an exception. these get translated */
500                                                 fprintf(stderr, "%s (1): %s.%s is a '%s' but wrapped as type '%s'.\n",
501                                                         __func__, srna->identifier, prop->identifier, dp->dnatype, RNA_property_typename(prop->type));
502                                                 DefRNA.error = 1;
503                                                 return NULL;
504                                         }
505                                 }
506                         }
507                         else if (prop->type == PROP_INT || prop->type == PROP_BOOLEAN || prop->type == PROP_ENUM) {
508                                 if (IS_DNATYPE_INT_COMPAT(dp->dnatype) == 0) {
509                                         fprintf(stderr, "%s (2): %s.%s is a '%s' but wrapped as type '%s'.\n",
510                                                 __func__, srna->identifier, prop->identifier, dp->dnatype, RNA_property_typename(prop->type));
511                                         DefRNA.error = 1;
512                                         return NULL;
513                                 }
514                         }
515                 }
516
517         }
518
519         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
520
521         switch (prop->type) {
522                 case PROP_STRING: {
523                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
524                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
525                         fprintf(f, "{\n");
526                         if (manualfunc) {
527                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
528                         }
529                         else {
530                                 const PropertySubType subtype = prop->subtype;
531                                 const char *string_copy_func = (subtype == PROP_FILEPATH ||
532                                                                subtype == PROP_DIRPATH  ||
533                                                                subtype == PROP_FILENAME ||
534                                                                subtype == PROP_BYTESTRING) ?
535                                             "BLI_strncpy" : "BLI_strncpy_utf8";
536
537                                 rna_print_data_get(f, dp);
538                                 if (sprop->maxlength)
539                                         fprintf(f, "    %s(value, data->%s, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
540                                 else
541                                         fprintf(f, "    %s(value, data->%s, sizeof(data->%s));\n", string_copy_func, dp->dnaname, dp->dnaname);
542                         }
543                         fprintf(f, "}\n\n");
544                         break;
545                 }
546                 case PROP_POINTER: {
547                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
548                         fprintf(f, "{\n");
549                         if (manualfunc) {
550                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
551                         }
552                         else {
553                                 PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
554                                 rna_print_data_get(f, dp);
555                                 if (dp->dnapointerlevel == 0)
556                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (const char*)pprop->type, dp->dnaname);
557                                 else
558                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (const char*)pprop->type, dp->dnaname);
559                         }
560                         fprintf(f, "}\n\n");
561                         break;
562                 }
563                 case PROP_COLLECTION: {
564                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
565
566                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
567                         fprintf(f, "{\n");
568                         if (manualfunc) {
569                                 if (strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
570                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
571                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
572                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->item_type)? (const char*)cprop->item_type: "UnknownType", manualfunc);
573                                 else
574                                         fprintf(f, "    return %s(iter);\n", manualfunc);
575                         }
576                         fprintf(f, "}\n\n");
577                         break;
578                 }
579                 default:
580                         if (prop->arraydimension) {
581                                 if (prop->flag & PROP_DYNAMIC)
582                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[])\n", func, rna_type_type(prop));
583                                 else
584                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[%u])\n", func, rna_type_type(prop), prop->totarraylength);
585                                 fprintf(f, "{\n");
586
587                                 if (manualfunc) {
588                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
589                                 }
590                                 else {
591                                         rna_print_data_get(f, dp);
592
593                                         if (prop->flag & PROP_DYNAMIC) {
594                                                 char *lenfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get_length");
595                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
596                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
597                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
598                                                 MEM_freeN(lenfunc);
599                                         }
600                                         else {
601                                                 fprintf(f, "    int i;\n\n");
602                                                 fprintf(f, "    for(i=0; i<%u; i++) {\n", prop->totarraylength);
603                                         }
604
605                                         if (dp->dnaarraylength == 1) {
606                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit)
607                                                         fprintf(f, "            values[i]= %s((data->%s & (%d<<i)) != 0);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
608                                                 else
609                                                         fprintf(f, "            values[i]= (%s)%s((&data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
610                                         }
611                                         else {
612                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
613                                                         fprintf(f, "            values[i]= %s((data->%s[i] & ", (dp->booleannegative)? "!": "", dp->dnaname);
614                                                         rna_int_print(f, dp->booleanbit);
615                                                         fprintf(f, ") != 0);\n");
616                                                 }
617                                                 else if (rna_color_quantize(prop, dp))
618                                                         fprintf(f, "            values[i]= (%s)(data->%s[i]*(1.0f/255.0f));\n", rna_type_type(prop), dp->dnaname);
619                                                 else if (dp->dnatype)
620                                                         fprintf(f, "            values[i]= (%s)%s(((%s*)data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnatype, dp->dnaname);
621                                                 else
622                                                         fprintf(f, "            values[i]= (%s)%s((data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
623                                         }
624                                         fprintf(f, "    }\n");
625                                 }
626                                 fprintf(f, "}\n\n");
627                         }
628                         else {
629                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
630                                 fprintf(f, "{\n");
631
632                                 if (manualfunc) {
633                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
634                                 }
635                                 else {
636                                         rna_print_data_get(f, dp);
637                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
638                                                 fprintf(f, "    return %s(((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
639                                                 rna_int_print(f, dp->booleanbit);
640                                                 fprintf(f, ") != 0);\n");
641                                         }
642                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
643                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
644                                                 rna_int_print(f, rna_enum_bitmask(prop));
645                                                 fprintf(f, ");\n");
646                                         }
647                                         else
648                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
649                                 }
650
651                                 fprintf(f, "}\n\n");
652                         }
653                         break;
654         }
655
656         return func;
657 }
658
659 /* defined min/max variables to be used by rna_clamp_value() */
660 static void rna_clamp_value_range(FILE *f, PropertyRNA *prop)
661 {
662         if (prop->type == PROP_FLOAT) {
663                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
664                 if (fprop->range) {
665                         fprintf(f, "    float prop_clamp_min, prop_clamp_max;\n");
666                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max);\n", rna_function_string(fprop->range));
667                 }
668         }
669         else if (prop->type == PROP_INT) {
670                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
671                 if (iprop->range) {
672                         fprintf(f, "    int prop_clamp_min, prop_clamp_max;\n");
673                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max);\n", rna_function_string(iprop->range));
674                 }
675         }
676 }
677
678 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array)
679 {
680         if (prop->type == PROP_INT) {
681                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
682
683                 if (iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
684                         if (array) fprintf(f, "CLAMPIS(values[i], ");
685                         else fprintf(f, "CLAMPIS(value, ");
686                         if (iprop->range) {
687                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
688                         }
689                         else {
690                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
691                                 rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
692                         }
693                         return;
694                 }
695         }
696         else if (prop->type == PROP_FLOAT) {
697                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
698
699                 if (fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
700                         if (array) fprintf(f, "CLAMPIS(values[i], ");
701                         else fprintf(f, "CLAMPIS(value, ");
702                         if (fprop->range) {
703                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
704                         }
705                         else {
706                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
707                                 rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
708                         }
709                         return;
710                 }
711         }
712
713         if (array)
714                 fprintf(f, "values[i];\n");
715         else
716                 fprintf(f, "value;\n");
717 }
718
719 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
720 {
721         char *func;
722
723         if (!(prop->flag & PROP_EDITABLE))
724                 return NULL;
725         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
726                 return NULL;
727
728         if (!manualfunc) {
729                 if (!dp->dnastructname || !dp->dnaname) {
730                         if (prop->flag & PROP_EDITABLE) {
731                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
732                                         __func__, srna->identifier, prop->identifier);
733                                 DefRNA.error = 1;
734                         }
735                         return NULL;
736                 }
737         }
738
739         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "set");
740
741         switch (prop->type) {
742                 case PROP_STRING: {
743                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
744                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
745                         fprintf(f, "{\n");
746                         if (manualfunc) {
747                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
748                         }
749                         else {
750                                 const PropertySubType subtype = prop->subtype;
751                                 const char *string_copy_func = (subtype == PROP_FILEPATH ||
752                                                                subtype == PROP_DIRPATH  ||
753                                                                subtype == PROP_FILENAME ||
754                                                                subtype == PROP_BYTESTRING) ?
755                                             "BLI_strncpy" : "BLI_strncpy_utf8";
756
757                                 rna_print_data_get(f, dp);
758                                 if (sprop->maxlength)
759                                         fprintf(f, "    %s(data->%s, value, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
760                                 else
761                                         fprintf(f, "    %s(data->%s, value, sizeof(data->%s));\n", string_copy_func, dp->dnaname, dp->dnaname);
762                         }
763                         fprintf(f, "}\n\n");
764                         break;
765                 }
766                 case PROP_POINTER: {
767                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
768                         fprintf(f, "{\n");
769                         if (manualfunc) {
770                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
771                         }
772                         else {
773                                 rna_print_data_get(f, dp);
774
775                                 if (prop->flag & PROP_ID_SELF_CHECK) {
776                                         rna_print_id_get(f, dp);
777                                         fprintf(f, "    if(id==value.data) return;\n\n");
778                                 }
779
780                                 if (prop->flag & PROP_ID_REFCOUNT) {
781                                         fprintf(f, "\n  if(data->%s)\n", dp->dnaname);
782                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
783                                         fprintf(f, "    if(value.data)\n");
784                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
785                                 }
786                                 else {
787                                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
788                                         StructRNA *type = rna_find_struct((const char*)pprop->type);
789                                         if (type && (type->flag & STRUCT_ID)) {
790                                                 fprintf(f, "    if(value.data)\n");
791                                                 fprintf(f, "            id_lib_extern((ID*)value.data);\n\n");
792                                         }
793                                 }
794
795                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
796
797                         }
798                         fprintf(f, "}\n\n");
799                         break;
800                 }
801                 default:
802                         if (prop->arraydimension) {
803                                 if (prop->flag & PROP_DYNAMIC)
804                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[])\n", func, rna_type_type(prop));
805                                 else
806                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[%u])\n", func, rna_type_type(prop), prop->totarraylength);
807                                 fprintf(f, "{\n");
808
809                                 if (manualfunc) {
810                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
811                                 }
812                                 else {
813                                         rna_print_data_get(f, dp);
814
815                                         if (prop->flag & PROP_DYNAMIC) {
816                                                 char *lenfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "set_length");
817                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
818                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
819                                                 rna_clamp_value_range(f, prop);
820                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
821                                                 MEM_freeN(lenfunc);
822                                         }
823                                         else {
824                                                 fprintf(f, "    int i;\n\n");
825                                                 rna_clamp_value_range(f, prop);
826                                                 fprintf(f, "    for(i=0; i<%u; i++) {\n", prop->totarraylength);
827                                         }
828
829                                         if (dp->dnaarraylength == 1) {
830                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
831                                                         fprintf(f, "            if(%svalues[i]) data->%s |= (%d<<i);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
832                                                         fprintf(f, "            else data->%s &= ~(%d<<i);\n", dp->dnaname, dp->booleanbit);
833                                                 }
834                                                 else {
835                                                         fprintf(f, "            (&data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
836                                                         rna_clamp_value(f, prop, 1);
837                                                 }
838                                         }
839                                         else {
840                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
841                                                         fprintf(f, "            if(%svalues[i]) data->%s[i] |= ", (dp->booleannegative)? "!": "", dp->dnaname);
842                                                         rna_int_print(f, dp->booleanbit);
843                                                         fprintf(f, ";\n");
844                                                         fprintf(f, "            else data->%s[i] &= ~", dp->dnaname);
845                                                         rna_int_print(f, dp->booleanbit);
846                                                         fprintf(f, ";\n");
847                                                 }
848                                                 else if (rna_color_quantize(prop, dp)) {
849                                                         fprintf(f, "            data->%s[i]= FTOCHAR(values[i]);\n", dp->dnaname);
850                                                 }
851                                                 else {
852                                                         if (dp->dnatype)
853                                                                 fprintf(f, "            ((%s*)data->%s)[i]= %s", dp->dnatype, dp->dnaname, (dp->booleannegative)? "!": "");
854                                                         else
855                                                                 fprintf(f, "            (data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
856                                                         rna_clamp_value(f, prop, 1);
857                                                 }
858                                         }
859                                         fprintf(f, "    }\n");
860                                 }
861                                 fprintf(f, "}\n\n");
862                         }
863                         else {
864                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
865                                 fprintf(f, "{\n");
866
867                                 if (manualfunc) {
868                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
869                                 }
870                                 else {
871                                         rna_print_data_get(f, dp);
872                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
873                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
874                                                 rna_int_print(f, dp->booleanbit);
875                                                 fprintf(f, ";\n");
876                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
877                                                 rna_int_print(f, dp->booleanbit);
878                                                 fprintf(f, ";\n");
879                                         }
880                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
881                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
882                                                 rna_int_print(f, rna_enum_bitmask(prop));
883                                                 fprintf(f, ";\n");
884                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
885                                         }
886                                         else {
887                                                 rna_clamp_value_range(f, prop);
888                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
889                                                 rna_clamp_value(f, prop, 0);
890                                         }
891                                 }
892                                 fprintf(f, "}\n\n");
893                         }
894                         break;
895         }
896
897         return func;
898 }
899
900 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
901 {
902         char *func = NULL;
903
904         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
905                 return NULL;
906
907         if (prop->type == PROP_STRING) {
908                 if (!manualfunc) {
909                         if (!dp->dnastructname || !dp->dnaname) {
910                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
911                                         __func__, srna->identifier, prop->identifier);
912                                 DefRNA.error = 1;
913                                 return NULL;
914                         }
915                 }
916
917                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
918
919                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
920                 fprintf(f, "{\n");
921                 if (manualfunc) {
922                         fprintf(f, "    return %s(ptr);\n", manualfunc);
923                 }
924                 else {
925                         rna_print_data_get(f, dp);
926                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
927                 }
928                 fprintf(f, "}\n\n");
929         }
930         else if (prop->type == PROP_COLLECTION) {
931                 if (!manualfunc) {
932                         if (prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
933                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
934                                         __func__, srna->identifier, prop->identifier);
935                                 DefRNA.error = 1;
936                                 return NULL;
937                         }
938                 }
939
940                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
941
942                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
943                 fprintf(f, "{\n");
944                 if (manualfunc) {
945                         fprintf(f, "    return %s(ptr);\n", manualfunc);
946                 }
947                 else {
948                         rna_print_data_get(f, dp);
949                         if (dp->dnalengthname)
950                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
951                         else
952                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
953                 }
954                 fprintf(f, "}\n\n");
955         }
956
957         return func;
958 }
959
960 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
961 {
962         char *func, *getfunc;
963
964         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
965                 return NULL;
966
967         if (!manualfunc) {
968                 if (!dp->dnastructname || !dp->dnaname) {
969                         fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
970                                 __func__, srna->identifier, prop->identifier);
971                         DefRNA.error = 1;
972                         return NULL;
973                 }
974         }
975
976         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "begin");
977
978         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
979         fprintf(f, "{\n");
980
981         if (!manualfunc)
982                 rna_print_data_get(f, dp);
983
984         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
985         fprintf(f, "    iter->parent= *ptr;\n");
986         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
987
988         if (dp->dnalengthname || dp->dnalengthfixed) {
989                 if (manualfunc) {
990                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
991                 }
992                 else {
993                         if (dp->dnalengthname)
994                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
995                         else
996                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
997                 }
998         }
999         else {
1000                 if (manualfunc)
1001                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
1002                 else if (dp->dnapointerlevel == 0)
1003                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
1004                 else
1005                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
1006         }
1007
1008         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1009
1010         fprintf(f, "\n  if(iter->valid)\n");
1011         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1012
1013         fprintf(f, "}\n\n");
1014
1015
1016         return func;
1017 }
1018
1019 static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc, const char *nextfunc)
1020 {
1021         /* note on indices, this is for external functions and ignores skipped values.
1022          * so the the index can only be checked against the length when there is no 'skip' funcion. */
1023         char *func;
1024
1025         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1026                 return NULL;
1027
1028         if (!manualfunc) {
1029                 if (!dp->dnastructname || !dp->dnaname)
1030                         return NULL;
1031
1032                 /* only supported in case of standard next functions */
1033                 if (strcmp(nextfunc, "rna_iterator_array_next") == 0);
1034                 else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
1035                 else return NULL;
1036         }
1037
1038         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "lookup_int");
1039
1040         fprintf(f, "int %s(PointerRNA *ptr, int index, PointerRNA *r_ptr)\n", func);
1041         fprintf(f, "{\n");
1042
1043         if (manualfunc) {
1044                 fprintf(f, "\n  return %s(ptr, index, r_ptr);\n", manualfunc);
1045                 fprintf(f, "}\n\n");
1046                 return func;
1047         }
1048
1049         fprintf(f, "    int found= 0;\n");
1050         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
1051
1052         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1053         fprintf(f, "    if(iter.valid){\n");
1054
1055         if (strcmp(nextfunc, "rna_iterator_array_next") == 0) {
1056                 fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
1057                 fprintf(f, "            if(index < 0 || index >= internal->length) {\n");
1058                 fprintf(f, "#ifdef __GNUC__\n");
1059                 fprintf(f, "                    printf(\"Array iterator out of range: %%s (index %%d)\\n\", __func__, index);\n");
1060                 fprintf(f, "#else\n");
1061                 fprintf(f, "                    printf(\"Array iterator out of range: (index %%d)\\n\", index);\n");
1062                 fprintf(f, "#endif\n");
1063                 fprintf(f, "            }\n");
1064                 fprintf(f, "            else if(internal->skip) {\n");
1065                 fprintf(f, "                    while(index-- > 0 && iter.valid) {\n");
1066                 fprintf(f, "                            rna_iterator_array_next(&iter);\n");
1067                 fprintf(f, "                    }\n");
1068                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1069                 fprintf(f, "            }\n");
1070                 fprintf(f, "            else {\n");
1071                 fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
1072                 fprintf(f, "                    found= 1;\n");
1073                 fprintf(f, "            }\n");
1074         }
1075         else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
1076                 fprintf(f, "            ListBaseIterator *internal= iter.internal;\n");
1077                 fprintf(f, "            if(internal->skip) {\n");
1078                 fprintf(f, "                    while(index-- > 0 && iter.valid) {\n");
1079                 fprintf(f, "                            rna_iterator_listbase_next(&iter);\n");
1080                 fprintf(f, "                    }\n");
1081                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1082                 fprintf(f, "            }\n");
1083                 fprintf(f, "            else {\n");
1084                 fprintf(f, "                    while(index-- > 0 && internal->link)\n");
1085                 fprintf(f, "                            internal->link= internal->link->next;\n");
1086                 fprintf(f, "                    found= (index == -1 && internal->link);\n");
1087                 fprintf(f, "            }\n");
1088         }
1089
1090         fprintf(f, "            if(found) *r_ptr = %s_%s_get(&iter);\n", srna->identifier, rna_safe_id(prop->identifier));
1091         fprintf(f, "    }\n\n");
1092         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1093
1094         fprintf(f, "    return found;\n");
1095
1096 #if 0
1097         rna_print_data_get(f, dp);
1098         item_type = (cprop->item_type)? (const char*)cprop->item_type: "UnknownType";
1099
1100         if (dp->dnalengthname || dp->dnalengthfixed) {
1101                 if (dp->dnalengthname)
1102                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
1103                 else
1104                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
1105         }
1106         else {
1107                 if (dp->dnapointerlevel == 0)
1108                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n", item_type, dp->dnaname);
1109                 else
1110                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
1111         }
1112 #endif
1113
1114         fprintf(f, "}\n\n");
1115
1116         return func;
1117 }
1118
1119 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
1120 {
1121         char *func, *getfunc;
1122
1123         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1124                 return NULL;
1125
1126         if (!manualfunc)
1127                 return NULL;
1128
1129         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "next");
1130
1131         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1132         fprintf(f, "{\n");
1133         fprintf(f, "    %s(iter);\n", manualfunc);
1134
1135         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1136
1137         fprintf(f, "\n  if(iter->valid)\n");
1138         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1139
1140         fprintf(f, "}\n\n");
1141
1142         return func;
1143 }
1144
1145 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
1146 {
1147         char *func;
1148
1149         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1150                 return NULL;
1151
1152         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "end");
1153
1154         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1155         fprintf(f, "{\n");
1156         if (manualfunc)
1157                 fprintf(f, "    %s(iter);\n", manualfunc);
1158         fprintf(f, "}\n\n");
1159
1160         return func;
1161 }
1162
1163 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
1164 {
1165         if (dp->dnapointerlevel != 0)
1166                 return;
1167         if (!dp->dnatype || !dp->dnaname || !dp->dnastructname)
1168                 return;
1169         
1170         if (strcmp(dp->dnatype, "char") == 0) {
1171                 prop->rawtype = PROP_RAW_CHAR;
1172                 prop->flag |= PROP_RAW_ACCESS;
1173         }
1174         else if (strcmp(dp->dnatype, "short") == 0) {
1175                 prop->rawtype = PROP_RAW_SHORT;
1176                 prop->flag |= PROP_RAW_ACCESS;
1177         }
1178         else if (strcmp(dp->dnatype, "int") == 0) {
1179                 prop->rawtype = PROP_RAW_INT;
1180                 prop->flag |= PROP_RAW_ACCESS;
1181         }
1182         else if (strcmp(dp->dnatype, "float") == 0) {
1183                 prop->rawtype = PROP_RAW_FLOAT;
1184                 prop->flag |= PROP_RAW_ACCESS;
1185         }
1186         else if (strcmp(dp->dnatype, "double") == 0) {
1187                 prop->rawtype = PROP_RAW_DOUBLE;
1188                 prop->flag |= PROP_RAW_ACCESS;
1189         }
1190 }
1191
1192 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1193 {
1194         PropertyDefRNA *dp = rna_find_struct_property_def(srna, prop);
1195
1196         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1197 }
1198
1199 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1200 {
1201         PropertyRNA *prop;
1202
1203         prop = dp->prop;
1204
1205         switch (prop->type) {
1206                 case PROP_BOOLEAN: {
1207                         BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
1208
1209                         if (!prop->arraydimension) {
1210                                 if (!bprop->get && !bprop->set && !dp->booleanbit)
1211                                         rna_set_raw_property(dp, prop);
1212
1213                                 bprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)bprop->get);
1214                                 bprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)bprop->set);
1215                         }
1216                         else {
1217                                 bprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)bprop->getarray);
1218                                 bprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)bprop->setarray);
1219                         }
1220                         break;
1221                 }
1222                 case PROP_INT: {
1223                         IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
1224
1225                         if (!prop->arraydimension) {
1226                                 if (!iprop->get && !iprop->set)
1227                                         rna_set_raw_property(dp, prop);
1228
1229                                 iprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)iprop->get);
1230                                 iprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)iprop->set);
1231                         }
1232                         else {
1233                                 if (!iprop->getarray && !iprop->setarray)
1234                                         rna_set_raw_property(dp, prop);
1235
1236                                 iprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)iprop->getarray);
1237                                 iprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)iprop->setarray);
1238                         }
1239                         break;
1240                 }
1241                 case PROP_FLOAT: {
1242                         FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
1243
1244                         if (!prop->arraydimension) {
1245                                 if (!fprop->get && !fprop->set)
1246                                         rna_set_raw_property(dp, prop);
1247
1248                                 fprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)fprop->get);
1249                                 fprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)fprop->set);
1250                         }
1251                         else {
1252                                 if (!fprop->getarray && !fprop->setarray)
1253                                         rna_set_raw_property(dp, prop);
1254
1255                                 fprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)fprop->getarray);
1256                                 fprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)fprop->setarray);
1257                         }
1258                         break;
1259                 }
1260                 case PROP_ENUM: {
1261                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1262
1263                         eprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)eprop->get);
1264                         eprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)eprop->set);
1265                         break;
1266                 }
1267                 case PROP_STRING: {
1268                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
1269
1270                         sprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)sprop->get);
1271                         sprop->length = (void*)rna_def_property_length_func(f, srna, prop, dp, (const char*)sprop->length);
1272                         sprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)sprop->set);
1273                         break;
1274                 }
1275                 case PROP_POINTER: {
1276                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
1277
1278                         pprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)pprop->get);
1279                         pprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)pprop->set);
1280                         if (!pprop->type) {
1281                                 fprintf(stderr, "%s: %s.%s, pointer must have a struct type.\n",
1282                                         __func__, srna->identifier, prop->identifier);
1283                                 DefRNA.error = 1;
1284                         }
1285                         break;
1286                 }
1287                 case PROP_COLLECTION: {
1288                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
1289                         const char *nextfunc = (const char*)cprop->next;
1290
1291                         if (dp->dnatype && strcmp(dp->dnatype, "ListBase") == 0);
1292                         else if (dp->dnalengthname || dp->dnalengthfixed)
1293                                 cprop->length = (void*)rna_def_property_length_func(f, srna, prop, dp, (const char*)cprop->length);
1294
1295                         /* test if we can allow raw array access, if it is using our standard
1296                          * array get/next function, we can be sure it is an actual array */
1297                         if (cprop->next && cprop->get)
1298                                 if (strcmp((const char*)cprop->next, "rna_iterator_array_next") == 0 &&
1299                                    strcmp((const char*)cprop->get, "rna_iterator_array_get") == 0)
1300                                         prop->flag |= PROP_RAW_ARRAY;
1301
1302                         cprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)cprop->get);
1303                         cprop->begin = (void*)rna_def_property_begin_func(f, srna, prop, dp, (const char*)cprop->begin);
1304                         cprop->next = (void*)rna_def_property_next_func(f, srna, prop, dp, (const char*)cprop->next);
1305                         cprop->end = (void*)rna_def_property_end_func(f, srna, prop, dp, (const char*)cprop->end);
1306                         cprop->lookupint = (void*)rna_def_property_lookup_int_func(f, srna, prop, dp, (const char*)cprop->lookupint, nextfunc);
1307
1308                         if (!(prop->flag & PROP_IDPROPERTY)) {
1309                                 if (!cprop->begin) {
1310                                         fprintf(stderr, "%s: %s.%s, collection must have a begin function.\n",
1311                                                 __func__, srna->identifier, prop->identifier);
1312                                         DefRNA.error = 1;
1313                                 }
1314                                 if (!cprop->next) {
1315                                         fprintf(stderr, "%s: %s.%s, collection must have a next function.\n",
1316                                                 __func__, srna->identifier, prop->identifier);
1317                                         DefRNA.error = 1;
1318                                 }
1319                                 if (!cprop->get) {
1320                                         fprintf(stderr, "%s: %s.%s, collection must have a get function.\n",
1321                                                 __func__, srna->identifier, prop->identifier);
1322                                         DefRNA.error = 1;
1323                                 }
1324                         }
1325                         if (!cprop->item_type) {
1326                                 fprintf(stderr, "%s: %s.%s, collection must have a struct type.\n",
1327                                         __func__, srna->identifier, prop->identifier);
1328                                 DefRNA.error = 1;
1329                         }
1330                         break;
1331                 }
1332         }
1333 }
1334
1335 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1336 {
1337         PropertyRNA *prop;
1338         char *func;
1339
1340         prop = dp->prop;
1341
1342         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1343                 return;
1344
1345         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "");
1346
1347         switch (prop->type) {
1348                 case PROP_BOOLEAN:
1349                 case PROP_INT: {
1350                         if (!prop->arraydimension) {
1351                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1352                                 /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1353                         }
1354                         else if (prop->arraydimension && prop->totarraylength) {
1355                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%u]);\n", func, prop->totarraylength);
1356                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength); */
1357                         }
1358                         else {
1359                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[]);\n", func);
1360                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[]);\n", func); */
1361                         }
1362                         break;
1363                 }
1364                 case PROP_FLOAT: {
1365                         if (!prop->arraydimension) {
1366                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1367                                 /*fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func); */
1368                         }
1369                         else if (prop->arraydimension && prop->totarraylength) {
1370                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%u]);\n", func, prop->totarraylength);
1371                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength); */
1372                         }
1373                         else {
1374                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[]);\n", func);
1375                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[]);\n", func); */
1376                         }
1377                         break;
1378                 }
1379                 case PROP_ENUM: {
1380                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1381                         int i;
1382
1383                         if (eprop->item) {
1384                                 fprintf(f, "enum {\n");
1385
1386                                 for (i = 0; i<eprop->totitem; i++)
1387                                         if (eprop->item[i].identifier[0])
1388                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1389
1390                                 fprintf(f, "};\n\n");
1391                         }
1392
1393                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1394                         /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1395
1396                         break;
1397                 }
1398                 case PROP_STRING: {
1399                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
1400
1401                         if (sprop->maxlength) {
1402                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1403                         }
1404                         
1405                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1406                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1407                         /*fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func); */
1408
1409                         break;
1410                 }
1411                 case PROP_POINTER: {
1412                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1413                         /*fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func); */
1414                         break;
1415                 }
1416                 case PROP_COLLECTION: {
1417                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1418                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1419                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1420                         /*fprintf(f, "int %slength(PointerRNA *ptr);\n", func); */
1421                         /*fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func); */
1422                         /*fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func); */
1423                         break;
1424                 }
1425         }
1426
1427         fprintf(f, "\n");
1428 }
1429
1430 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1431 {
1432         PropertyRNA *prop;
1433
1434         prop = dp->prop;
1435
1436         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1437                 return;
1438         
1439         if (prop->name && prop->description && prop->description[0] != '\0')
1440                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1441         else if (prop->name)
1442                 fprintf(f, "\t/* %s */\n", prop->name);
1443         else
1444                 fprintf(f, "\t/* */\n");
1445
1446         switch (prop->type) {
1447                 case PROP_BOOLEAN: {
1448                         if (!prop->arraydimension)
1449                                 fprintf(f, "\tinline bool %s(void);", rna_safe_id(prop->identifier));
1450                         else if (prop->totarraylength)
1451                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1452                         break;
1453                 }
1454                 case PROP_INT: {
1455                         if (!prop->arraydimension)
1456                                 fprintf(f, "\tinline int %s(void);", rna_safe_id(prop->identifier));
1457                         else if (prop->totarraylength)
1458                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1459                         break;
1460                 }
1461                 case PROP_FLOAT: {
1462                         if (!prop->arraydimension)
1463                                 fprintf(f, "\tinline float %s(void);", rna_safe_id(prop->identifier));
1464                         else if (prop->totarraylength)
1465                                 fprintf(f, "\tinline Array<float, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1466                         break;
1467                 }
1468                 case PROP_ENUM: {
1469                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1470                         int i;
1471
1472                         if (eprop->item) {
1473                                 fprintf(f, "\tenum %s_enum {\n", rna_safe_id(prop->identifier));
1474
1475                                 for (i = 0; i<eprop->totitem; i++)
1476                                         if (eprop->item[i].identifier[0])
1477                                                 fprintf(f, "\t\t%s_%s = %d,\n", rna_safe_id(prop->identifier), eprop->item[i].identifier, eprop->item[i].value);
1478
1479                                 fprintf(f, "\t};\n");
1480                         }
1481
1482                         fprintf(f, "\tinline %s_enum %s(void);", rna_safe_id(prop->identifier), rna_safe_id(prop->identifier));
1483                         break;
1484                 }
1485                 case PROP_STRING: {
1486                         fprintf(f, "\tinline std::string %s(void);", rna_safe_id(prop->identifier));
1487                         break;
1488                 }
1489                 case PROP_POINTER: {
1490                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1491
1492                         if (pprop->type)
1493                                 fprintf(f, "\tinline %s %s(void);", (const char*)pprop->type, rna_safe_id(prop->identifier));
1494                         else
1495                                 fprintf(f, "\tinline %s %s(void);", "UnknownType", rna_safe_id(prop->identifier));
1496                         break;
1497                 }
1498                 case PROP_COLLECTION: {
1499                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1500
1501                         if (cprop->item_type)
1502                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char*)cprop->item_type, srna->identifier, rna_safe_id(prop->identifier));
1503                         else
1504                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, rna_safe_id(prop->identifier));
1505                         break;
1506                 }
1507         }
1508
1509         fprintf(f, "\n");
1510 }
1511
1512 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1513 {
1514         PropertyRNA *prop;
1515
1516         prop = dp->prop;
1517
1518         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1519                 return;
1520
1521         switch (prop->type) {
1522                 case PROP_BOOLEAN: {
1523                         if (!prop->arraydimension)
1524                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1525                         else if (prop->totarraylength)
1526                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength, rna_safe_id(prop->identifier));
1527                         break;
1528                 }
1529                 case PROP_INT: {
1530                         if (!prop->arraydimension)
1531                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1532                         else if (prop->totarraylength)
1533                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength, rna_safe_id(prop->identifier));
1534                         break;
1535                 }
1536                 case PROP_FLOAT: {
1537                         if (!prop->arraydimension)
1538                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1539                         else if (prop->totarraylength)
1540                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength, rna_safe_id(prop->identifier));
1541                         break;
1542                 }
1543                 case PROP_ENUM: {
1544                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", rna_safe_id(prop->identifier), srna->identifier, rna_safe_id(prop->identifier));
1545
1546                         break;
1547                 }
1548                 case PROP_STRING: {
1549                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1550                         break;
1551                 }
1552                 case PROP_POINTER: {
1553                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1554
1555                         if (pprop->type)
1556                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (const char*)pprop->type, srna->identifier, rna_safe_id(prop->identifier));
1557                         else
1558                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, rna_safe_id(prop->identifier));
1559                         break;
1560                 }
1561                 case PROP_COLLECTION: {
1562                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1563
1564                         if(cprop->type)
1565                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char*)cprop->type, srna->identifier, prop->identifier);
1566                         else
1567                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1568                         break;
1569                 }
1570         }
1571
1572         fprintf(f, "\n");
1573 }
1574
1575 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1576 {
1577         StructRNA *srna;
1578         FunctionRNA *func;
1579         PropertyDefRNA *dparm;
1580         PropertyType type;
1581         const char *funcname, *valstr;
1582         const char *ptrstr;
1583         const short has_data = (dfunc->cont.properties.first != NULL);
1584         int flag, pout, cptr, first;
1585
1586         srna = dsrna->srna;
1587         func = dfunc->func;
1588
1589         if (!dfunc->call)
1590                 return;
1591
1592         funcname = rna_alloc_function_name(srna->identifier, func->identifier, "call");
1593
1594         /* function definition */
1595         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1596         fprintf(f, "\n{\n");
1597
1598         /* variable definitions */
1599         
1600         if (func->flag & FUNC_USE_SELF_ID) {
1601                 fprintf(f, "\tstruct ID *_selfid;\n");
1602         }
1603
1604         if ((func->flag & FUNC_NO_SELF) == 0) {
1605                 if (dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1606                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1607         }
1608
1609         dparm = dfunc->cont.properties.first;
1610         for (; dparm; dparm = dparm->next) {
1611                 type = dparm->prop->type;
1612                 flag = dparm->prop->flag;
1613                 pout = (flag & PROP_OUTPUT);
1614                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1615
1616                 if (dparm->prop == func->c_ret)
1617                         ptrstr = cptr || dparm->prop->arraydimension ? "*" : "";
1618                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1619                 else if (cptr || (flag & PROP_DYNAMIC))
1620                         ptrstr = pout ? "**" : "*";
1621                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1622                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1623                         ptrstr = "*";
1624                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack, but type name for string props is already char*, so leave empty */
1625                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1626                         ptrstr = "";
1627                 else
1628                         ptrstr = pout ? "*" : "";
1629
1630                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1631                 if (flag & PROP_DYNAMIC)
1632                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1633                 
1634                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1635         }
1636
1637         if (has_data) {
1638                 fprintf(f, "\tchar *_data");
1639                 if (func->c_ret) fprintf(f, ", *_retdata");
1640                 fprintf(f, ";\n");
1641                 fprintf(f, "\t\n");
1642         }
1643
1644         /* assign self */
1645         if (func->flag & FUNC_USE_SELF_ID) {
1646                 fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1647         }
1648         
1649         if ((func->flag & FUNC_NO_SELF) == 0) {
1650                 if (dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1651                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1652         }
1653
1654         if (has_data) {
1655                 fprintf(f, "\t_data= (char *)_parms->data;\n");
1656         }
1657
1658         dparm = dfunc->cont.properties.first;
1659         for (; dparm; dparm = dparm->next) {
1660                 type = dparm->prop->type;
1661                 flag = dparm->prop->flag;
1662                 pout = (flag & PROP_OUTPUT);
1663                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1664
1665                 if (dparm->prop == func->c_ret)
1666                         fprintf(f, "\t_retdata= _data;\n");
1667                 else  {
1668                         const char *data_str;
1669                         if (cptr || (flag & PROP_DYNAMIC)) {
1670                                 ptrstr = "**";
1671                                 valstr = "*";
1672                         }
1673                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1674                                 ptrstr = "*";
1675                                 valstr = "";
1676                         }
1677                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1678                                 ptrstr = "";
1679                                 valstr = "";
1680                         }
1681                         else {
1682                                 ptrstr = "*";
1683                                 valstr = "*";
1684                         }
1685
1686                         /* this must be kept in sync with RNA_parameter_length_get_data, we could just call the function directly, but this is faster */
1687                         if (flag & PROP_DYNAMIC) {
1688                                 fprintf(f, "\t%s_len= %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
1689                                 data_str = "(&(((char *)_data)[sizeof(void *)]))";
1690                         }
1691                         else {
1692                                 data_str = "_data";
1693                         }
1694                         fprintf(f, "\t%s= ", dparm->prop->identifier);
1695
1696                         if (!pout)
1697                                 fprintf(f, "%s", valstr);
1698
1699                         fprintf(f, "((%s%s%s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, data_str);
1700                 }
1701
1702                 if (dparm->next)
1703                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size_alloc(dparm->prop));
1704         }
1705
1706         if (dfunc->call) {
1707                 fprintf(f, "\t\n");
1708                 fprintf(f, "\t");
1709                 if (func->c_ret) fprintf(f, "%s= ", func->c_ret->identifier);
1710                 fprintf(f, "%s(", dfunc->call);
1711
1712                 first = 1;
1713
1714                 if (func->flag & FUNC_USE_SELF_ID) {
1715                         fprintf(f, "_selfid");
1716                         first = 0;
1717                 }
1718
1719                 if ((func->flag & FUNC_NO_SELF) == 0) {
1720                         if (!first) fprintf(f, ", ");
1721                         fprintf(f, "_self");
1722                         first = 0;
1723                 }
1724
1725                 if (func->flag & FUNC_USE_MAIN) {
1726                         if (!first) fprintf(f, ", ");
1727                         first = 0;
1728                         fprintf(f, "CTX_data_main(C)"); /* may have direct access later */
1729                 }
1730
1731                 if (func->flag & FUNC_USE_CONTEXT) {
1732                         if (!first) fprintf(f, ", ");
1733                         first = 0;
1734                         fprintf(f, "C");
1735                 }
1736
1737                 if (func->flag & FUNC_USE_REPORTS) {
1738                         if (!first) fprintf(f, ", ");
1739                         first = 0;
1740                         fprintf(f, "reports");
1741                 }
1742
1743                 dparm = dfunc->cont.properties.first;
1744                 for (; dparm; dparm = dparm->next) {
1745                         if (dparm->prop == func->c_ret)
1746                                 continue;
1747
1748                         if (!first) fprintf(f, ", ");
1749                         first = 0;
1750
1751                         if (dparm->prop->flag & PROP_DYNAMIC)
1752                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
1753                         else
1754                                 fprintf(f, "%s", dparm->prop->identifier);
1755                 }
1756
1757                 fprintf(f, ");\n");
1758
1759                 if (func->c_ret) {
1760                         dparm = rna_find_parameter_def(func->c_ret);
1761                         ptrstr = (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) || (dparm->prop->arraydimension))? "*": "";
1762                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
1763                 }
1764         }
1765
1766         fprintf(f, "}\n\n");
1767
1768         dfunc->gencall = funcname;
1769 }
1770
1771 static void rna_auto_types(void)
1772 {
1773         StructDefRNA *ds;
1774         PropertyDefRNA *dp;
1775
1776         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
1777                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1778                 if (ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1779                         ds->dnaname = "bScreen";
1780
1781                 for (dp = ds->cont.properties.first; dp; dp = dp->next) {
1782                         if (dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1783                                 dp->dnastructname = "bScreen";
1784
1785                         if (dp->dnatype) {
1786                                 if (dp->prop->type == PROP_POINTER) {
1787                                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1788                                         StructRNA *type;
1789
1790                                         if (!pprop->type && !pprop->get)
1791                                                 pprop->type = (StructRNA*)rna_find_type(dp->dnatype);
1792
1793                                         if (pprop->type) {
1794                                                 type = rna_find_struct((const char*)pprop->type);
1795                                                 if (type && (type->flag & STRUCT_ID_REFCOUNT))
1796                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1797                                         }
1798                                 }
1799                                 else if (dp->prop->type == PROP_COLLECTION) {
1800                                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1801
1802                                         if (!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase") == 0)
1803                                                 cprop->item_type = (StructRNA*)rna_find_type(dp->dnatype);
1804                                 }
1805                         }
1806                 }
1807         }
1808 }
1809
1810 static void rna_sort(BlenderRNA *brna)
1811 {
1812         StructDefRNA *ds;
1813         StructRNA *srna;
1814
1815         rna_sortlist(&brna->structs, cmp_struct);
1816         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1817
1818         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1819                 rna_sortlist(&srna->cont.properties, cmp_property);
1820
1821         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
1822                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1823 }
1824
1825 static const char *rna_property_structname(PropertyType type)
1826 {
1827         switch (type) {
1828                 case PROP_BOOLEAN: return "BoolPropertyRNA";
1829                 case PROP_INT: return "IntPropertyRNA";
1830                 case PROP_FLOAT: return "FloatPropertyRNA";
1831                 case PROP_STRING: return "StringPropertyRNA";
1832                 case PROP_ENUM: return "EnumPropertyRNA";
1833                 case PROP_POINTER: return "PointerPropertyRNA";
1834                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1835                 default: return "UnknownPropertyRNA";
1836         }
1837 }
1838
1839 static const char *rna_property_subtypename(PropertySubType type)
1840 {
1841         switch (type) {
1842                 case PROP_NONE: return "PROP_NONE";
1843                 case PROP_FILEPATH: return "PROP_FILEPATH";
1844                 case PROP_FILENAME: return "PROP_FILENAME";
1845                 case PROP_DIRPATH: return "PROP_DIRPATH";
1846                 case PROP_BYTESTRING: return "PROP_BYTESTRING";
1847                 case PROP_TRANSLATE: return "PROP_TRANSLATE";
1848                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1849                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1850                 case PROP_FACTOR: return "PROP_FACTOR";
1851                 case PROP_ANGLE: return "PROP_ANGLE";
1852                 case PROP_TIME: return "PROP_TIME";
1853                 case PROP_DISTANCE: return "PROP_DISTANCE";
1854                 case PROP_COLOR: return "PROP_COLOR";
1855                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1856                 case PROP_DIRECTION: return "PROP_DIRECTION";
1857                 case PROP_MATRIX: return "PROP_MATRIX";
1858                 case PROP_EULER: return "PROP_EULER";
1859                 case PROP_QUATERNION: return "PROP_QUATERNION";
1860                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1861                 case PROP_VELOCITY: return "PROP_VELOCITY";
1862                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1863                 case PROP_XYZ: return "PROP_XYZ";
1864                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1865                 case PROP_COORDS: return "PROP_COORDS";
1866                 case PROP_LAYER: return "PROP_LAYER";
1867                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1868                 default: {
1869                         /* in case we dont have a type preset that includes the subtype */
1870                         if (RNA_SUBTYPE_UNIT(type)) {
1871                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1872                         }
1873                         else {
1874                                 return "PROP_SUBTYPE_UNKNOWN";
1875                         }
1876                 }
1877         }
1878 }
1879
1880 static const char *rna_property_subtype_unit(PropertySubType type)
1881 {
1882         switch (RNA_SUBTYPE_UNIT(type)) {
1883                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1884                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1885                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1886                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1887                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1888                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1889                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1890                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1891                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1892                 default:                                        return "PROP_UNIT_UNKNOWN";
1893         }
1894 }
1895
1896 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1897 {
1898         StructRNA *srna;
1899
1900         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1901                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1902         fprintf(f, "\n");
1903 }
1904
1905 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1906 {
1907         StructRNA *srna;
1908
1909         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1910
1911         srna = brna->structs.first;
1912         if (srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1913         else fprintf(f, "{NULL, ");
1914
1915         srna = brna->structs.last;
1916         if (srna) fprintf(f, "&RNA_%s}", srna->identifier);
1917         else fprintf(f, "NULL}");
1918
1919         fprintf(f, "};\n\n");
1920 }
1921
1922 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1923 {
1924         PropertyRNA *prop;
1925         StructRNA *base;
1926
1927         base = srna->base;
1928         while (base) {
1929                 fprintf(f, "\n");
1930                 for (prop = base->cont.properties.first; prop; prop = prop->next)
1931                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1932                 base = base->base;
1933         }
1934
1935         if (srna->cont.properties.first)
1936                 fprintf(f, "\n");
1937
1938         for (prop = srna->cont.properties.first; prop; prop = prop->next)
1939                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1940         fprintf(f, "\n");
1941 }
1942
1943 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1944 {
1945         PropertyRNA *parm;
1946
1947         for (parm = func->cont.properties.first; parm; parm = parm->next)
1948                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1949
1950         if (func->cont.properties.first)
1951                 fprintf(f, "\n");
1952 }
1953
1954 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1955 {
1956         FunctionRNA *func;
1957         StructRNA *base;
1958
1959         base = srna->base;
1960         while (base) {
1961                 for (func = base->functions.first; func; func = func->cont.next) {
1962                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1963                         rna_generate_parameter_prototypes(brna, base, func, f);
1964                 }
1965
1966                 if (base->functions.first)
1967                         fprintf(f, "\n");
1968
1969                 base = base->base;
1970         }
1971
1972         for (func = srna->functions.first; func; func = func->cont.next) {
1973                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1974                 rna_generate_parameter_prototypes(brna, srna, func, f);
1975         }
1976
1977         if (srna->functions.first)
1978                 fprintf(f, "\n");
1979 }
1980
1981 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1982 {
1983         FunctionRNA *func;
1984         PropertyDefRNA *dparm;
1985         StructDefRNA *dsrna;
1986         PropertyType type;
1987         int flag, pout, cptr, first;
1988         const char *ptrstr;
1989
1990         dsrna = rna_find_struct_def(srna);
1991         func = dfunc->func;
1992
1993         /* return type */
1994         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
1995                 if (dparm->prop == func->c_ret) {
1996                         if (dparm->prop->arraydimension)
1997                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1998                         else if (dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
1999                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2000                         else
2001                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2002
2003                         break;
2004                 }
2005         }
2006
2007         /* void if nothing to return */
2008         if (!dparm)
2009                 fprintf(f, "void ");
2010
2011         /* function name */
2012         fprintf(f, "%s(", dfunc->call);
2013
2014         first = 1;
2015
2016         /* self, context and reports parameters */
2017         if (func->flag & FUNC_USE_SELF_ID) {
2018                 fprintf(f, "struct ID *_selfid");
2019                 first = 0;              
2020         }
2021         
2022         if ((func->flag & FUNC_NO_SELF) == 0) {
2023                 if (!first) fprintf(f, ", ");
2024                 if (dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
2025                 else fprintf(f, "struct %s *_self", srna->identifier);
2026                 first = 0;
2027         }
2028
2029         if (func->flag & FUNC_USE_MAIN) {
2030                 if (!first) fprintf(f, ", ");
2031                 first = 0;
2032                 fprintf(f, "Main *bmain");
2033         }
2034
2035         if (func->flag & FUNC_USE_CONTEXT) {
2036                 if (!first) fprintf(f, ", ");
2037                 first = 0;
2038                 fprintf(f, "bContext *C");
2039         }
2040
2041         if (func->flag & FUNC_USE_REPORTS) {
2042                 if (!first) fprintf(f, ", ");
2043                 first = 0;
2044                 fprintf(f, "ReportList *reports");
2045         }
2046
2047         /* defined parameters */
2048         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2049                 type = dparm->prop->type;
2050                 flag = dparm->prop->flag;
2051                 pout = (flag & PROP_OUTPUT);
2052                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2053
2054                 if (dparm->prop == func->c_ret)
2055                         continue;
2056
2057                 if (cptr || (flag & PROP_DYNAMIC))
2058                         ptrstr = pout ? "**" : "*";
2059                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
2060                         ptrstr = "*";
2061                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
2062                         ptrstr = "";
2063                 else
2064                         ptrstr = pout ? "*" : "";
2065
2066                 if (!first) fprintf(f, ", ");
2067                 first = 0;
2068
2069                 if (flag & PROP_DYNAMIC)
2070                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
2071
2072                 if (!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
2073                         fprintf(f, "%s%s %s[%u]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->totarraylength);
2074                 else
2075                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
2076
2077         }
2078
2079         fprintf(f, ");\n");
2080 }
2081
2082 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2083 {
2084         FunctionRNA *func;
2085         FunctionDefRNA *dfunc;
2086         int first = 1;
2087
2088         for (func = srna->functions.first; func; func = func->cont.next) {
2089                 dfunc = rna_find_function_def(func);
2090
2091                 if (dfunc->call) {
2092                         if (first) {
2093                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
2094                                 first = 0;
2095                         }
2096
2097                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
2098                 }
2099         }
2100
2101         fprintf(f, "\n");
2102 }
2103
2104 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
2105 {
2106         char *strnest = "", *errnest = "";
2107         int len, freenest = 0;
2108         
2109         if (nest != NULL) {
2110                 len = strlen(nest);
2111
2112                 strnest = MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
2113                 errnest = MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
2114
2115                 strcpy(strnest, "_"); strcat(strnest, nest);
2116                 strcpy(errnest, "."); strcat(errnest, nest);
2117
2118                 freenest = 1;
2119         }
2120
2121         switch (prop->type) {
2122                         case PROP_ENUM: {
2123                                 EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
2124                                 int i, defaultfound = 0, totflag = 0;
2125
2126                                 if (eprop->item) {
2127                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
2128
2129                                         for (i = 0; i<eprop->totitem; i++) {
2130                                                 fprintf(f, "{%d, ", eprop->item[i].value);
2131                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
2132                                                 fprintf(f, "%d, ", eprop->item[i].icon);
2133                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
2134                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
2135
2136                                                 if (eprop->item[i].identifier[0]) {
2137                                                         if (prop->flag & PROP_ENUM_FLAG) {
2138                                                                 totflag |= eprop->item[i].value;
2139                                                         }
2140                                                         else {
2141                                                                 if (eprop->defaultvalue == eprop->item[i].value) {
2142                                                                         defaultfound = 1;
2143                                                                 }
2144                                                         }
2145                                                 }
2146                                         }
2147
2148                                         fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
2149
2150                                         if (prop->flag & PROP_ENUM_FLAG) {
2151                                                 if (eprop->defaultvalue & ~totflag) {
2152                                                         fprintf(stderr, "%s: %s%s.%s, enum default includes unused bits (%d).\n",
2153                                                                 __func__, srna->identifier, errnest, prop->identifier, eprop->defaultvalue & ~totflag);
2154                                                         DefRNA.error = 1;
2155                                                 }
2156                                         }
2157                                         else {
2158                                                 if (!defaultfound) {
2159                                                         fprintf(stderr, "%s: %s%s.%s, enum default is not in items.\n",
2160                                                                 __func__, srna->identifier, errnest, prop->identifier);
2161                                                         DefRNA.error = 1;
2162                                                 }
2163                                         }
2164                                 }
2165                                 else {
2166                                         fprintf(stderr, "%s: %s%s.%s, enum must have items defined.\n",
2167                                                 __func__, srna->identifier, errnest, prop->identifier);
2168                                         DefRNA.error = 1;
2169                                 }
2170                                 break;
2171                         }
2172                         case PROP_BOOLEAN: {
2173                                 BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
2174                                 unsigned int i;
2175
2176                                 if (prop->arraydimension && prop->totarraylength) {
2177                                         fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2178
2179                                         for (i = 0; i<prop->totarraylength; i++) {
2180                                                 if (bprop->defaultarray)
2181                                                         fprintf(f, "%d", bprop->defaultarray[i]);
2182                                                 else
2183                                                         fprintf(f, "%d", bprop->defaultvalue);
2184                                                 if (i != prop->totarraylength-1)
2185                                                         fprintf(f, ",\n\t");
2186                                         }
2187
2188                                         fprintf(f, "\n};\n\n");
2189                                 }
2190                                 break;
2191                         }
2192                         case PROP_INT: {
2193                                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
2194                                 unsigned int i;
2195
2196                                 if (prop->arraydimension && prop->totarraylength) {
2197                                         fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2198
2199                                         for (i = 0; i<prop->totarraylength; i++) {
2200                                                 if (iprop->defaultarray)
2201                                                         fprintf(f, "%d", iprop->defaultarray[i]);
2202                                                 else
2203                                                         fprintf(f, "%d", iprop->defaultvalue);
2204                                                 if (i != prop->totarraylength-1)
2205                                                         fprintf(f, ",\n\t");
2206                                         }
2207
2208                                         fprintf(f, "\n};\n\n");
2209                                 }
2210                                 break;
2211                         }
2212                         case PROP_FLOAT: {
2213                                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
2214                                 unsigned int i;
2215
2216                                 if (prop->arraydimension && prop->totarraylength) {
2217                                         fprintf(f, "static float rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2218
2219                                         for (i = 0; i<prop->totarraylength; i++) {
2220                                                 if (fprop->defaultarray)
2221                                                         rna_float_print(f, fprop->defaultarray[i]);
2222                                                 else
2223                                                         rna_float_print(f, fprop->defaultvalue);
2224                                                 if (i != prop->totarraylength-1)
2225                                                         fprintf(f, ",\n\t");
2226                                         }
2227
2228                                         fprintf(f, "\n};\n\n");
2229                                 }
2230                                 break;
2231                         }
2232                         default:
2233                                 break;
2234         }
2235
2236         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
2237
2238         if (prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2239         else fprintf(f, "\t{NULL, ");
2240         if (prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2241         else fprintf(f, "NULL,\n");
2242         fprintf(f, "\t%d, ", prop->magic);
2243         rna_print_c_string(f, prop->identifier);
2244         fprintf(f, ", %d, ", prop->flag);
2245         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2246         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2247         fprintf(f, "%d,\n", prop->icon);
2248         rna_print_c_string(f, prop->translation_context); fprintf(f, ",\n\t");
2249         fprintf(f, "\t%s, %s|%s, %s, %u, {%u, %u, %u}, %u,\n", RNA_property_typename(prop->type), rna_property_subtypename(prop->subtype), rna_property_subtype_unit(prop->subtype), rna_function_string(prop->getlength), prop->arraydimension, prop->arraylength[0], prop->arraylength[1], prop->arraylength[2], prop->totarraylength);
2250         fprintf(f, "\t%s%s, %d, %s, %s,\n", (prop->flag & PROP_CONTEXT_UPDATE)? "(UpdateFunc)": "", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable), rna_function_string(prop->itemeditable));
2251
2252         if (prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2253         else fprintf(f, "\t0, -1");
2254
2255         /* our own type - collections/arrays only */
2256         if (prop->srna) fprintf(f, ", &RNA_%s", (const char*)prop->srna);
2257         else fprintf(f, ", NULL");
2258
2259         fprintf(f, "},\n");
2260
2261         switch (prop->type) {
2262                         case PROP_BOOLEAN: {
2263                                 BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
2264                                 fprintf(f, "\t%s, %s, %s, %s, %d, ", rna_function_string(bprop->get), rna_function_string(bprop->set), rna_function_string(bprop->getarray), rna_function_string(bprop->setarray), bprop->defaultvalue);
2265                                 if (prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2266                                 else fprintf(f, "NULL\n");
2267                                 break;
2268                         }
2269                         case PROP_INT: {
2270                                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
2271                                 fprintf(f, "\t%s, %s, %s, %s, %s,\n\t", rna_function_string(iprop->get), rna_function_string(iprop->set), rna_function_string(iprop->getarray), rna_function_string(iprop->setarray), rna_function_string(iprop->range));
2272                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2273                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2274                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2275                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2276                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
2277                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
2278                                 if (prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2279                                 else fprintf(f, "NULL\n");
2280                                 break;
2281                         }
2282                         case PROP_FLOAT: {
2283                                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
2284                                 fprintf(f, "\t%s, %s, %s, %s, %s, ", rna_function_string(fprop->get), rna_function_string(fprop->set), rna_function_string(fprop->getarray), rna_function_string(fprop->setarray), rna_function_string(fprop->range));
2285                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2286                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2287                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2288                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2289                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
2290                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2291                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2292                                 if (prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2293                                 else fprintf(f, "NULL\n");
2294                                 break;
2295                         }
2296                         case PROP_STRING: {
2297                                 StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
2298                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
2299                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2300                                 break;
2301                         }
2302                         case PROP_ENUM: {
2303                                 EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
2304                                 fprintf(f, "\t%s, %s, %s, NULL, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
2305                                 if (eprop->item)
2306                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2307                                 else
2308                                         fprintf(f, "NULL, ");
2309                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2310                                 break;
2311                         }
2312                         case PROP_POINTER: {
2313                                 PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
2314                                 fprintf(f, "\t%s, %s, %s, %s,", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef), rna_function_string(pprop->poll));
2315                                 if (pprop->type) fprintf(f, "&RNA_%s\n", (const char*)pprop->type);
2316                                 else fprintf(f, "NULL\n");
2317                                 break;
2318                         }
2319                         case PROP_COLLECTION: {
2320                                 CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
2321                                 fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, ", rna_function_string(cprop->begin), rna_function_string(cprop->next), rna_function_string(cprop->end), rna_function_string(cprop->get), rna_function_string(cprop->length), rna_function_string(cprop->lookupint), rna_function_string(cprop->lookupstring), rna_function_string(cprop->assignint));
2322                                 if (cprop->item_type) fprintf(f, "&RNA_%s\n", (const char*)cprop->item_type);
2323                                 else fprintf(f, "NULL\n");
2324                                 break;
2325                         }
2326         }
2327
2328         fprintf(f, "};\n\n");
2329
2330         if (freenest) {
2331                 MEM_freeN(strnest);
2332                 MEM_freeN(errnest);
2333         }
2334 }
2335
2336 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
2337 {
2338         FunctionRNA *func;
2339         FunctionDefRNA *dfunc;
2340         PropertyRNA *prop, *parm;
2341         StructRNA *base;
2342
2343         fprintf(f, "/* %s */\n", srna->name);
2344
2345         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2346                 rna_generate_property(f, srna, NULL, prop);
2347
2348         for (func = srna->functions.first; func; func = func->cont.next) {
2349                 for (parm = func->cont.properties.first; parm; parm = parm->next)
2350                         rna_generate_property(f, srna, func->identifier, parm);
2351
2352                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2353
2354                 if (func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
2355                 else fprintf(f, "\t{NULL, ");
2356                 if (func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
2357                 else fprintf(f, "NULL,\n");
2358
2359                 fprintf(f, "\tNULL,\n");
2360
2361                 parm = func->cont.properties.first;
2362                 if (parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2363                 else fprintf(f, "\t{NULL, ");
2364
2365                 parm = func->cont.properties.last;
2366                 if (parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2367                 else fprintf(f, "NULL}},\n");
2368
2369                 fprintf(f, "\t");
2370                 rna_print_c_string(f, func->identifier);
2371                 fprintf(f, ", %d, ", func->flag);
2372                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2373
2374                 dfunc = rna_find_function_def(func);
2375                 if (dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2376                 else fprintf(f, "\tNULL,\n");
2377
2378                 if (func->c_ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2379                 else fprintf(f, "\tNULL\n");
2380
2381                 fprintf(f, "};\n");
2382                 fprintf(f, "\n");
2383         }
2384
2385         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2386
2387         if (srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
2388         else fprintf(f, "\t{NULL, ");
2389         if (srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
2390         else fprintf(f, "NULL,\n");
2391
2392         fprintf(f, "\tNULL,\n");
2393
2394         prop = srna->cont.properties.first;
2395         if (prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2396         else fprintf(f, "\t{NULL, ");
2397
2398         prop = srna->cont.properties.last;
2399         if (prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2400         else fprintf(f, "NULL}},\n");
2401         fprintf(f, "\t");
2402         rna_print_c_string(f, srna->identifier);
2403         fprintf(f, "\t, NULL,NULL\n"); /* PyType - Cant initialize here */
2404         fprintf(f, ", %d, ", srna->flag);
2405         rna_print_c_string(f, srna->name);
2406         fprintf(f, ", ");
2407         rna_print_c_string(f, srna->description);
2408         fprintf(f, ",\n\t%d,\n", srna->icon);
2409
2410         prop = srna->nameproperty;
2411         if (prop) {
2412                 base = srna;
2413                 while (base->base && base->base->nameproperty == prop)
2414                         base = base->base;
2415
2416                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2417         }
2418         else fprintf(f, "\tNULL, ");
2419
2420         prop = srna->iteratorproperty;
2421         base = srna;
2422         while (base->base && base->base->iteratorproperty == prop)
2423                 base = base->base;
2424         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2425
2426         if (srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2427         else fprintf(f, "\tNULL,\n");
2428
2429         if (srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2430         else fprintf(f, "\tNULL,\n");
2431
2432         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2433         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2434         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2435         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2436         fprintf(f, "\t%s,\n", rna_function_string(srna->instance));
2437         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2438
2439         if (srna->reg && !srna->refine) {
2440                 fprintf(stderr, "%s: %s has a register function, must also have refine function.\n",
2441                         __func__, srna->identifier);
2442                 DefRNA.error = 1;
2443         }
2444
2445         func = srna->functions.first;
2446         if (func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2447         else fprintf(f, "\t{NULL, ");
2448
2449         func = srna->functions.last;
2450         if (func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2451         else fprintf(f, "NULL}\n");
2452
2453         fprintf(f, "};\n");
2454
2455         fprintf(f, "\n");
2456 }
2457
2458 typedef struct RNAProcessItem {
2459         const char *filename;
2460         const char *api_filename;
2461         void (*define)(BlenderRNA *brna);
2462 } RNAProcessItem;
2463
2464 static RNAProcessItem PROCESS_ITEMS[] = {
2465         {"rna_rna.c", NULL, RNA_def_rna},
2466         {"rna_ID.c", NULL, RNA_def_ID},
2467         {"rna_texture.c", "rna_texture_api.c", RNA_def_texture},
2468         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2469         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2470         {"rna_animviz.c", NULL, RNA_def_animviz},
2471         {"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
2472         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2473         {"rna_boid.c", NULL, RNA_def_boid},
2474         {"rna_brush.c", NULL, RNA_def_brush},
2475         {"rna_camera.c", "rna_camera_api.c", RNA_def_camera},
2476         {"rna_cloth.c", NULL, RNA_def_cloth},
2477         {"rna_color.c", NULL, RNA_def_color},
2478         {"rna_constraint.c", NULL, RNA_def_constraint},
2479         {"rna_context.c", NULL, RNA_def_context},
2480         {"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
2481         {"rna_curve.c", NULL, RNA_def_curve},
2482         {"rna_dynamicpaint.c", NULL, RNA_def_dynamic_paint},
2483         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2484         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2485         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2486         {"rna_group.c", NULL, RNA_def_group},
2487         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2488         {"rna_key.c", NULL, RNA_def_key},
2489         {"rna_lamp.c", NULL, RNA_def_lamp},
2490         {"rna_lattice.c", NULL, RNA_def_lattice},
2491         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2492         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2493         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2494         {"rna_meta.c", NULL, RNA_def_meta},
2495         {"rna_modifier.c", NULL, RNA_def_modifier},
2496         {"rna_nla.c", NULL, RNA_def_nla},
2497         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2498         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2499         {"rna_object_force.c", NULL, RNA_def_object_force},
2500         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2501         {"rna_particle.c", NULL, RNA_def_particle},
2502         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2503         {"rna_property.c", NULL, RNA_def_gameproperty},
2504         {"rna_render.c", NULL, RNA_def_render},
2505         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2506         {"rna_screen.c", NULL, RNA_def_screen},
2507         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2508         {"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
2509         {"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
2510         {"rna_smoke.c", NULL, RNA_def_smoke},
2511         {"rna_space.c", NULL, RNA_def_space},
2512         {"rna_speaker.c", NULL, RNA_def_speaker},
2513         {"rna_test.c", NULL, RNA_def_test},
2514         {"rna_text.c", "rna_text_api.c", RNA_def_text},
2515         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2516         {"rna_sound.c", NULL, RNA_def_sound},
2517         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2518         {"rna_userdef.c", NULL, RNA_def_userdef},
2519         {"rna_vfont.c", NULL, RNA_def_vfont},
2520         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2521         {"rna_world.c", NULL, RNA_def_world},   
2522         {"rna_movieclip.c", NULL, RNA_def_movieclip},
2523         {"rna_tracking.c", NULL, RNA_def_tracking},
2524         {NULL, NULL}};
2525
2526 static void rna_generate(BlenderRNA *brna, FILE *f, const char *filename, const char *api_filename)
2527 {
2528         StructDefRNA *ds;
2529         PropertyDefRNA *dp;
2530         FunctionDefRNA *dfunc;
2531         
2532         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
2533                                  "   Do not edit manually, changes will be overwritten.           */\n\n"
2534                                   "#define RNA_RUNTIME\n\n");
2535
2536         fprintf(f, "#include <float.h>\n");
2537         fprintf(f, "#include <stdio.h>\n");
2538         fprintf(f, "#include <limits.h>\n");
2539         fprintf(f, "#include <string.h>\n\n");
2540         fprintf(f, "#include <stddef.h>\n\n");
2541
2542         fprintf(f, "#include \"DNA_ID.h\"\n");
2543         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2544
2545         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2546         fprintf(f, "#include \"BLI_utildefines.h\"\n\n");
2547
2548         fprintf(f, "#include \"BKE_context.h\"\n");
2549         fprintf(f, "#include \"BKE_library.h\"\n");
2550         fprintf(f, "#include \"BKE_main.h\"\n");
2551         fprintf(f, "#include \"BKE_report.h\"\n");
2552
2553         fprintf(f, "#include \"RNA_define.h\"\n");
2554         fprintf(f, "#include \"RNA_types.h\"\n");
2555         fprintf(f, "#include \"rna_internal.h\"\n\n");
2556
2557         rna_generate_prototypes(brna, f);
2558
2559         fprintf(f, "#include \"%s\"\n", filename);
2560         if (api_filename)
2561                 fprintf(f, "#include \"%s\"\n", api_filename);
2562         fprintf(f, "\n");
2563
2564         fprintf(f, "/* Autogenerated Functions */\n\n");
2565
2566         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2567                 if (!filename || ds->filename == filename) {
2568                         rna_generate_property_prototypes(brna, ds->srna, f);
2569                         rna_generate_function_prototypes(brna, ds->srna, f);
2570                 }
2571         }
2572
2573         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2574                 if (!filename || ds->filename == filename)
2575                         for (dp = ds->cont.properties.first; dp; dp = dp->next)
2576                                 rna_def_property_funcs(f, ds->srna, dp);
2577
2578         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2579                 if (!filename || ds->filename == filename) {
2580                         for (dfunc = ds->functions.first; dfunc; dfunc = dfunc->cont.next)
2581                                 rna_def_function_funcs(f, ds, dfunc);
2582
2583                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2584                 }
2585         }
2586
2587         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2588                 if (!filename || ds->filename == filename)
2589                         rna_generate_struct(brna, ds->srna, f);
2590
2591         if (strcmp(filename, "rna_ID.c") == 0) {
2592                 /* this is ugly, but we cannot have c files compiled for both
2593                  * makesrna and blender with some build systems at the moment */
2594                 fprintf(f, "#include \"rna_define.c\"\n\n");
2595
2596                 rna_generate_blender(brna, f);
2597         }
2598 }
2599
2600 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2601 {
2602         StructDefRNA *ds;
2603         PropertyDefRNA *dp;
2604         StructRNA *srna;
2605
2606         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2607         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2608
2609         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2610                                  "   Do not edit manually, changes will be overwritten.              */\n\n");
2611
2612         fprintf(f, "#include \"RNA_types.h\"\n\n");
2613
2614         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2615
2616         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2617         fprintf(f, "    { \\\n");
2618         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2619         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2620         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2621
2622         fprintf(f, "#define FOREACH_END(property) \\\n");
2623         fprintf(f, "            } \\\n");
2624         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2625         fprintf(f, "    }\n\n");
2626
2627         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2628                 srna = ds->srna;
2629
2630                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2631
2632                 while (srna) {
2633                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2634                         srna = srna->base;
2635                 }
2636                 fprintf(f, "\n");
2637
2638                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2639                         rna_def_property_funcs_header(f, ds->srna, dp);
2640         }
2641
2642         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2643
2644         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2645 }
2646
2647 static const char *cpp_classes = ""
2648 "\n"
2649 "#include <string>\n"
2650 "#include <string.h> /* for memcpy */\n"
2651 "\n"
2652 "namespace BL {\n"
2653 "\n"
2654 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2655 "       inline bool sname::identifier(void) { return sname##_##identifier##_get(&ptr)? true: false; }\n"
2656 "\n"
2657 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2658 "       inline Array<int,size> sname::identifier(void) \\\n"
2659 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2660 "\n"
2661 "#define INT_PROPERTY(sname, identifier) \\\n"
2662 "       inline int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2663 "\n"
2664 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2665 "       inline Array<int,size> sname::identifier(void) \\\n"
2666 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2667 "\n"
2668 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2669 "       inline float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2670 "\n"
2671 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2672 "       inline Array<float,size> sname::identifier(void) \\\n"
2673 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2674 "\n"
2675 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2676 "       inline sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2677 "\n"
2678 "#define STRING_PROPERTY(sname, identifier) \\\n"
2679 "       inline std::string sname::identifier(void) { \\\n"
2680 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2681 "               std::string str; str.resize(len); \\\n"
2682 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2683 "\n"
2684 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2685 "       inline type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2686 "\n"
2687 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2688 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2689 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2690 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2691 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2692 "\n"
2693 "class Pointer {\n"
2694 "public:\n"
2695 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2696 "       operator const PointerRNA&() { return ptr; }\n"
2697 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(ptr.type, type)? true: false; }\n"
2698 "       operator void*() { return ptr.data; }\n"
2699 "       operator bool() { return ptr.data != NULL; }\n"
2700 "\n"
2701 "       PointerRNA ptr;\n"
2702 "};\n"
2703 "\n"
2704 "\n"
2705 "template<typename T, int Tsize>\n"
2706 "class Array {\n"
2707 "public:\n"
2708 "       T data[Tsize];\n"
2709 "\n"
2710 "   Array() {}\n"
2711 "       Array(const Array<T, Tsize>& other) { memcpy(data, other.data, sizeof(T)*Tsize); }\n"
2712 "       const Array<T, Tsize>& operator=(const Array<T, Tsize>& other) { memcpy(data, other.data, sizeof(T)*Tsize); return *this; }\n"
2713 "\n"
2714 "       operator T*() { return data; }\n"
2715 "};\n"
2716 "\n"
2717 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2718 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2719 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2720 "\n"
2721 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2722 "class CollectionIterator {\n"
2723 "public:\n"
2724 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2725 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2726 "\n"
2727 "       operator bool(void)\n"
2728 "       { return iter.valid != 0; }\n"
2729 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2730 "\n"
2731 "       T& operator*(void) { return t; }\n"
2732 "       T* operator->(void) { return &t; }\n"
2733 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2734 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2735 "\n"
2736 "       void begin(const Pointer& ptr)\n"
2737 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2738 "\n"
2739 "private:\n"
2740 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy) {}\n"
2741 ""
2742 "       CollectionPropertyIterator iter;\n"
2743 "       T t;\n"
2744 "       bool init;\n"
2745 "};\n"
2746 "\n"
2747 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2748 "class Collection {\n"
2749 "public:\n"
2750 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2751 "\n"
2752 "       void begin(CollectionIterator<T, Tbegin, Tnext, Tend>& iter)\n"
2753 "       { iter.begin(ptr); }\n"
2754 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2755 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2756 "\n"
2757 "private:\n"
2758 "       PointerRNA ptr;\n"
2759 "};\n"
2760 "\n";
2761
2762 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2763 {
2764         StructDefRNA *ds;
2765         PropertyDefRNA *dp;
2766         StructRNA *srna;
2767
2768         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2769         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2770
2771         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2772                                  "   Do not edit manually, changes will be overwritten. */\n\n");
2773         
2774         fprintf(f, "#include \"RNA_blender.h\"\n");
2775         fprintf(f, "#include \"RNA_types.h\"\n");
2776         fprintf(f, "#include \"RNA_access.h\"\n");
2777
2778         fprintf(f, "%s", cpp_classes);
2779
2780         fprintf(f, "/**************** Declarations ****************/\n\n");
2781
2782         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2783                 fprintf(f, "class %s;\n", ds->srna->identifier);
2784         fprintf(f, "\n");
2785
2786         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2787                 srna = ds->srna;
2788
2789                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2790
2791                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2792                 fprintf(f, "public:\n");
2793                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2794                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2795                         if (!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2796                                 if (dp->prop->type == PROP_COLLECTION)
2797                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2798                 fprintf(f, "\n\t\t{}\n\n");
2799
2800                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2801                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2802                 fprintf(f, "};\n\n");
2803         }
2804
2805
2806         fprintf(f, "/**************** Implementation ****************/\n");
2807
2808         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2809                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2810                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2811
2812                 fprintf(f, "\n");
2813         }
2814
2815         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2816 }
2817
2818 static void make_bad_file(const char *file, int line)
2819 {
2820         FILE *fp = fopen(file, "w");
2821         fprintf(fp, "#error \"Error! can't make correct RNA file from %s:%d, STUPID!\"\n", __FILE__, line);
2822         fclose(fp);
2823 }
2824
2825 static int rna_preprocess(const char *outfile)
2826 {
2827         BlenderRNA *brna;
2828         StructDefRNA *ds;
2829         FILE *file;
2830         char deffile[4096];
2831         int i, status;
2832         const char *deps[3]; /* expand as needed */
2833
2834         /* define rna */
2835         brna = RNA_create();
2836
2837         for (i = 0; PROCESS_ITEMS[i].filename; i++) {
2838                 if (PROCESS_ITEMS[i].define) {
2839                         PROCESS_ITEMS[i].define(brna);
2840
2841                         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2842                                 if (!ds->filename)
2843                                         ds->filename = PROCESS_ITEMS[i].filename;
2844                 }
2845         }
2846
2847         rna_auto_types();
2848
2849
2850         /* create RNA_blender_cpp.h */
2851         strcpy(deffile, outfile);
2852         strcat(deffile, "RNA_blender_cpp.h" TMP_EXT);
2853
2854         status = (DefRNA.error != 0);
2855
2856         if (status) {
2857                 make_bad_file(deffile, __LINE__);
2858         }
2859         else {
2860                 file = fopen(deffile, "w");
2861
2862                 if (!file) {
2863                         fprintf(stderr, "Unable to open file: %s\n", deffile);
2864                         status = 1;
2865                 }
2866                 else {
2867                         rna_generate_header_cpp(brna, file);
2868                         fclose(file);
2869                         status = (DefRNA.error != 0);
2870                 }
2871         }
2872
2873         replace_if_different(deffile, NULL);
2874
2875         rna_sort(brna);
2876
2877         /* create rna_gen_*.c files */
2878         for (i = 0; PROCESS_ITEMS[i].filename; i++) {
2879                 strcpy(deffile, outfile);
2880                 strcat(deffile, PROCESS_ITEMS[i].filename);
2881                 deffile[strlen(deffile)-2] = '\0';
2882                 strcat(deffile, "_gen.c" TMP_EXT);
2883
2884                 if (status) {
2885                         make_bad_file(deffile, __LINE__);
2886                 }
2887                 else {
2888                         file = fopen(deffile, "w");
2889
2890                         if (!file) {
2891                                 fprintf(stderr, "Unable to open file: %s\n", deffile);
2892                                 status = 1;
2893                         }
2894                         else {
2895                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2896                                 fclose(file);
2897                                 status = (DefRNA.error != 0);
2898                         }
2899                 }
2900
2901                 /* avoid unneeded rebuilds */
2902                 deps[0] = PROCESS_ITEMS[i].filename;
2903                 deps[1] = PROCESS_ITEMS[i].api_filename;
2904