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