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' funcion. */
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                         else if (dp->dnalengthname || dp->dnalengthfixed)
1377                                 cprop->length = (void *)rna_def_property_length_func(f, srna, prop, dp, (const char *)cprop->length);
1378
1379                         /* test if we can allow raw array access, if it is using our standard
1380                          * array get/next function, we can be sure it is an actual array */
1381                         if (cprop->next && cprop->get)
1382                                 if (strcmp((const char *)cprop->next, "rna_iterator_array_next") == 0 &&
1383                                     strcmp((const char *)cprop->get, "rna_iterator_array_get") == 0)
1384                                 {
1385                                         prop->flag |= PROP_RAW_ARRAY;
1386                                 }
1387
1388                         cprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)cprop->get);
1389                         cprop->begin = (void *)rna_def_property_begin_func(f, srna, prop, dp, (const char *)cprop->begin);
1390                         cprop->next = (void *)rna_def_property_next_func(f, srna, prop, dp, (const char *)cprop->next);
1391                         cprop->end = (void *)rna_def_property_end_func(f, srna, prop, dp, (const char *)cprop->end);
1392                         cprop->lookupint = (void *)rna_def_property_lookup_int_func(f, srna, prop, dp,
1393                                                                                     (const char *)cprop->lookupint, nextfunc);
1394
1395                         if (!(prop->flag & PROP_IDPROPERTY)) {
1396                                 if (!cprop->begin) {
1397                                         fprintf(stderr, "%s: %s.%s, collection must have a begin function.\n",
1398                                                 __func__, srna->identifier, prop->identifier);
1399                                         DefRNA.error = 1;
1400                                 }
1401                                 if (!cprop->next) {
1402                                         fprintf(stderr, "%s: %s.%s, collection must have a next function.\n",
1403                                                 __func__, srna->identifier, prop->identifier);
1404                                         DefRNA.error = 1;
1405                                 }
1406                                 if (!cprop->get) {
1407                                         fprintf(stderr, "%s: %s.%s, collection must have a get function.\n",
1408                                                 __func__, srna->identifier, prop->identifier);
1409                                         DefRNA.error = 1;
1410                                 }
1411                         }
1412                         if (!cprop->item_type) {
1413                                 fprintf(stderr, "%s: %s.%s, collection must have a struct type.\n",
1414                                         __func__, srna->identifier, prop->identifier);
1415                                 DefRNA.error = 1;
1416                         }
1417                         break;
1418                 }
1419         }
1420 }
1421
1422 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1423 {
1424         PropertyRNA *prop;
1425         char *func;
1426
1427         prop = dp->prop;
1428
1429         if (prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN))
1430                 return;
1431
1432         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "");
1433
1434         switch (prop->type) {
1435                 case PROP_BOOLEAN:
1436                 case PROP_INT:
1437                 {
1438                         if (!prop->arraydimension) {
1439                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1440                                 /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1441                         }
1442                         else if (prop->arraydimension && prop->totarraylength) {
1443                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%u]);\n", func, prop->totarraylength);
1444                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength); */
1445                         }
1446                         else {
1447                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[]);\n", func);
1448                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[]);\n", func); */
1449                         }
1450                         break;
1451                 }
1452                 case PROP_FLOAT:
1453                 {
1454                         if (!prop->arraydimension) {
1455                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1456                                 /*fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func); */
1457                         }
1458                         else if (prop->arraydimension && prop->totarraylength) {
1459                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%u]);\n", func, prop->totarraylength);
1460                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength); */
1461                         }
1462                         else {
1463                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[]);\n", func);
1464                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[]);\n", func); */
1465                         }
1466                         break;
1467                 }
1468                 case PROP_ENUM:
1469                 {
1470                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
1471                         int i;
1472
1473                         if (eprop->item) {
1474                                 fprintf(f, "enum {\n");
1475
1476                                 for (i = 0; i < eprop->totitem; i++)
1477                                         if (eprop->item[i].identifier[0])
1478                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier,
1479                                                         eprop->item[i].identifier, eprop->item[i].value);
1480
1481                                 fprintf(f, "};\n\n");
1482                         }
1483
1484                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1485                         /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1486
1487                         break;
1488                 }
1489                 case PROP_STRING:
1490                 {
1491                         StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
1492
1493                         if (sprop->maxlength) {
1494                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1495                         }
1496                         
1497                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1498                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1499                         /*fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func); */
1500
1501                         break;
1502                 }
1503                 case PROP_POINTER:
1504                 {
1505                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1506                         /*fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func); */
1507                         break;
1508                 }
1509                 case PROP_COLLECTION:
1510                 {
1511                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1512                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1513                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1514                         /*fprintf(f, "int %slength(PointerRNA *ptr);\n", func); */
1515                         /*fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func); */
1516                         /*fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func); */
1517                         break;
1518                 }
1519         }
1520
1521         fprintf(f, "\n");
1522 }
1523
1524 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1525 {
1526         PropertyRNA *prop;
1527
1528         prop = dp->prop;
1529
1530         if (prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN))
1531                 return;
1532         
1533         if (prop->name && prop->description && prop->description[0] != '\0')
1534                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1535         else if (prop->name)
1536                 fprintf(f, "\t/* %s */\n", prop->name);
1537         else
1538                 fprintf(f, "\t/* */\n");
1539
1540         switch (prop->type) {
1541                 case PROP_BOOLEAN:
1542                 {
1543                         if (!prop->arraydimension)
1544                                 fprintf(f, "\tinline bool %s(void);", rna_safe_id(prop->identifier));
1545                         else if (prop->totarraylength)
1546                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1547                         break;
1548                 }
1549                 case PROP_INT:
1550                 {
1551                         if (!prop->arraydimension)
1552                                 fprintf(f, "\tinline int %s(void);", rna_safe_id(prop->identifier));
1553                         else if (prop->totarraylength)
1554                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1555                         break;
1556                 }
1557                 case PROP_FLOAT:
1558                 {
1559                         if (!prop->arraydimension)
1560                                 fprintf(f, "\tinline float %s(void);", rna_safe_id(prop->identifier));
1561                         else if (prop->totarraylength)
1562                                 fprintf(f, "\tinline Array<float, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1563                         break;
1564                 }
1565                 case PROP_ENUM:
1566                 {
1567                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
1568                         int i;
1569
1570                         if (eprop->item) {
1571                                 fprintf(f, "\tenum %s_enum {\n", rna_safe_id(prop->identifier));
1572
1573                                 for (i = 0; i < eprop->totitem; i++)
1574                                         if (eprop->item[i].identifier[0])
1575                                                 fprintf(f, "\t\t%s_%s = %d,\n", rna_safe_id(prop->identifier), eprop->item[i].identifier,
1576                                                         eprop->item[i].value);
1577
1578                                 fprintf(f, "\t};\n");
1579                         }
1580
1581                         fprintf(f, "\tinline %s_enum %s(void);", rna_safe_id(prop->identifier), rna_safe_id(prop->identifier));
1582                         break;
1583                 }
1584                 case PROP_STRING:
1585                 {
1586                         fprintf(f, "\tinline std::string %s(void);", rna_safe_id(prop->identifier));
1587                         break;
1588                 }
1589                 case PROP_POINTER:
1590                 {
1591                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
1592
1593                         if (pprop->type)
1594                                 fprintf(f, "\tinline %s %s(void);", (const char *)pprop->type, rna_safe_id(prop->identifier));
1595                         else
1596                                 fprintf(f, "\tinline %s %s(void);", "UnknownType", rna_safe_id(prop->identifier));
1597                         break;
1598                 }
1599                 case PROP_COLLECTION:
1600                 {
1601                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)dp->prop;
1602
1603                         if (cprop->item_type)
1604                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char *)cprop->item_type, srna->identifier,
1605                                         rna_safe_id(prop->identifier));
1606                         else
1607                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier,
1608                                         rna_safe_id(prop->identifier));
1609                         break;
1610                 }
1611         }
1612
1613         fprintf(f, "\n");
1614 }
1615
1616 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1617 {
1618         PropertyRNA *prop;
1619
1620         prop = dp->prop;
1621
1622         if (prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN))
1623                 return;
1624
1625         switch (prop->type) {
1626                 case PROP_BOOLEAN:
1627                 {
1628                         if (!prop->arraydimension)
1629                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1630                         else if (prop->totarraylength)
1631                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1632                                         rna_safe_id(prop->identifier));
1633                         break;
1634                 }
1635                 case PROP_INT:
1636                 {
1637                         if (!prop->arraydimension)
1638                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1639                         else if (prop->totarraylength)
1640                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1641                                         rna_safe_id(prop->identifier));
1642                         break;
1643                 }
1644                 case PROP_FLOAT:
1645                 {
1646                         if (!prop->arraydimension)
1647                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1648                         else if (prop->totarraylength)
1649                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1650                                         rna_safe_id(prop->identifier));
1651                         break;
1652                 }
1653                 case PROP_ENUM:
1654                 {
1655                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", rna_safe_id(prop->identifier), srna->identifier,
1656                                 rna_safe_id(prop->identifier));
1657
1658                         break;
1659                 }
1660                 case PROP_STRING:
1661                 {
1662                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1663                         break;
1664                 }
1665                 case PROP_POINTER:
1666                 {
1667                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
1668
1669                         if (pprop->type)
1670                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (const char *)pprop->type, srna->identifier,
1671                                         rna_safe_id(prop->identifier));
1672                         else
1673                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier,
1674                                         rna_safe_id(prop->identifier));
1675                         break;
1676                 }
1677                 case PROP_COLLECTION:
1678                 {
1679 #if 0
1680                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)dp->prop;
1681
1682                         if (cprop->type)
1683                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char *)cprop->type, srna->identifier,
1684                                         prop->identifier);
1685                         else
1686                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1687 #endif
1688                         break;
1689                 }
1690         }
1691
1692         fprintf(f, "\n");
1693 }
1694
1695 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1696 {
1697         StructRNA *srna;
1698         FunctionRNA *func;
1699         PropertyDefRNA *dparm;
1700         PropertyType type;
1701         const char *funcname, *valstr;
1702         const char *ptrstr;
1703         const short has_data = (dfunc->cont.properties.first != NULL);
1704         int flag, pout, cptr, first;
1705
1706         srna = dsrna->srna;
1707         func = dfunc->func;
1708
1709         if (!dfunc->call)
1710                 return;
1711
1712         funcname = rna_alloc_function_name(srna->identifier, func->identifier, "call");
1713
1714         /* function definition */
1715         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1716         fprintf(f, "\n{\n");
1717
1718         /* variable definitions */
1719         
1720         if (func->flag & FUNC_USE_SELF_ID) {
1721                 fprintf(f, "\tstruct ID *_selfid;\n");
1722         }
1723
1724         if ((func->flag & FUNC_NO_SELF) == 0) {
1725                 if (dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1726                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1727         }
1728
1729         dparm = dfunc->cont.properties.first;
1730         for (; dparm; dparm = dparm->next) {
1731                 type = dparm->prop->type;
1732                 flag = dparm->prop->flag;
1733                 pout = (flag & PROP_OUTPUT);
1734                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1735
1736                 if (dparm->prop == func->c_ret)
1737                         ptrstr = cptr || dparm->prop->arraydimension ? "*" : "";
1738                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1739                 else if (cptr || (flag & PROP_DYNAMIC))
1740                         ptrstr = pout ? "**" : "*";
1741                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1742                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1743                         ptrstr = "*";
1744                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack,
1745                  * but type name for string props is already char*, so leave empty */
1746                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1747                         ptrstr = "";
1748                 else
1749                         ptrstr = pout ? "*" : "";
1750
1751                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1752                 if (flag & PROP_DYNAMIC)
1753                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1754                 
1755                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
1756                         ptrstr, dparm->prop->identifier);
1757         }
1758
1759         if (has_data) {
1760                 fprintf(f, "\tchar *_data");
1761                 if (func->c_ret) fprintf(f, ", *_retdata");
1762                 fprintf(f, ";\n");
1763                 fprintf(f, "\t\n");
1764         }
1765
1766         /* assign self */
1767         if (func->flag & FUNC_USE_SELF_ID) {
1768                 fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1769         }
1770         
1771         if ((func->flag & FUNC_NO_SELF) == 0) {
1772                 if (dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1773                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1774         }
1775
1776         if (has_data) {
1777                 fprintf(f, "\t_data= (char *)_parms->data;\n");
1778         }
1779
1780         dparm = dfunc->cont.properties.first;
1781         for (; dparm; dparm = dparm->next) {
1782                 type = dparm->prop->type;
1783                 flag = dparm->prop->flag;
1784                 pout = (flag & PROP_OUTPUT);
1785                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1786
1787                 if (dparm->prop == func->c_ret)
1788                         fprintf(f, "\t_retdata= _data;\n");
1789                 else {
1790                         const char *data_str;
1791                         if (cptr || (flag & PROP_DYNAMIC)) {
1792                                 ptrstr = "**";
1793                                 valstr = "*";
1794                         }
1795                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1796                                 ptrstr = "*";
1797                                 valstr = "";
1798                         }
1799                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1800                                 ptrstr = "";
1801                                 valstr = "";
1802                         }
1803                         else {
1804                                 ptrstr = "*";
1805                                 valstr = "*";
1806                         }
1807
1808                         /* this must be kept in sync with RNA_parameter_length_get_data,
1809                          * we could just call the function directly, but this is faster */
1810                         if (flag & PROP_DYNAMIC) {
1811                                 fprintf(f, "\t%s_len= %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
1812                                 data_str = "(&(((char *)_data)[sizeof(void *)]))";
1813                         }
1814                         else {
1815                                 data_str = "_data";
1816                         }
1817                         fprintf(f, "\t%s = ", dparm->prop->identifier);
1818
1819                         if (!pout)
1820                                 fprintf(f, "%s", valstr);
1821
1822                         fprintf(f, "((%s%s%s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
1823                                 ptrstr, data_str);
1824                 }
1825
1826                 if (dparm->next)
1827                         fprintf(f, "\t_data += %d;\n", rna_parameter_size_alloc(dparm->prop));
1828         }
1829
1830         if (dfunc->call) {
1831                 fprintf(f, "\t\n");
1832                 fprintf(f, "\t");
1833                 if (func->c_ret) fprintf(f, "%s = ", func->c_ret->identifier);
1834                 fprintf(f, "%s(", dfunc->call);
1835
1836                 first = 1;
1837
1838                 if (func->flag & FUNC_USE_SELF_ID) {
1839                         fprintf(f, "_selfid");
1840                         first = 0;
1841                 }
1842
1843                 if ((func->flag & FUNC_NO_SELF) == 0) {
1844                         if (!first) fprintf(f, ", ");
1845                         fprintf(f, "_self");
1846                         first = 0;
1847                 }
1848
1849                 if (func->flag & FUNC_USE_MAIN) {
1850                         if (!first) fprintf(f, ", ");
1851                         first = 0;
1852                         fprintf(f, "CTX_data_main(C)"); /* may have direct access later */
1853                 }
1854
1855                 if (func->flag & FUNC_USE_CONTEXT) {
1856                         if (!first) fprintf(f, ", ");
1857                         first = 0;
1858                         fprintf(f, "C");
1859                 }
1860
1861                 if (func->flag & FUNC_USE_REPORTS) {
1862                         if (!first) fprintf(f, ", ");
1863                         first = 0;
1864                         fprintf(f, "reports");
1865                 }
1866
1867                 dparm = dfunc->cont.properties.first;
1868                 for (; dparm; dparm = dparm->next) {
1869                         if (dparm->prop == func->c_ret)
1870                                 continue;
1871
1872                         if (!first) fprintf(f, ", ");
1873                         first = 0;
1874
1875                         if (dparm->prop->flag & PROP_DYNAMIC)
1876                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
1877                         else
1878                                 fprintf(f, "%s", dparm->prop->identifier);
1879                 }
1880
1881                 fprintf(f, ");\n");
1882
1883                 if (func->c_ret) {
1884                         dparm = rna_find_parameter_def(func->c_ret);
1885                         ptrstr = (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) ||
1886                                   (dparm->prop->arraydimension)) ? "*" : "";
1887                         fprintf(f, "\t*((%s%s%s*)_retdata) = %s;\n", rna_type_struct(dparm->prop),
1888                                 rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
1889                 }
1890         }
1891
1892         fprintf(f, "}\n\n");
1893
1894         dfunc->gencall = funcname;
1895 }
1896
1897 static void rna_auto_types(void)
1898 {
1899         StructDefRNA *ds;
1900         PropertyDefRNA *dp;
1901
1902         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
1903                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1904                 if (ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1905                         ds->dnaname = "bScreen";
1906
1907                 for (dp = ds->cont.properties.first; dp; dp = dp->next) {
1908                         if (dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1909                                 dp->dnastructname = "bScreen";
1910
1911                         if (dp->dnatype) {
1912                                 if (dp->prop->type == PROP_POINTER) {
1913                                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
1914                                         StructRNA *type;
1915
1916                                         if (!pprop->type && !pprop->get)
1917                                                 pprop->type = (StructRNA *)rna_find_type(dp->dnatype);
1918
1919                                         if (pprop->type) {
1920                                                 type = rna_find_struct((const char *)pprop->type);
1921                                                 if (type && (type->flag & STRUCT_ID_REFCOUNT))
1922                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1923                                         }
1924                                 }
1925                                 else if (dp->prop->type == PROP_COLLECTION) {
1926                                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)dp->prop;
1927
1928                                         if (!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase") == 0)
1929                                                 cprop->item_type = (StructRNA *)rna_find_type(dp->dnatype);
1930                                 }
1931                         }
1932                 }
1933         }
1934 }
1935
1936 static void rna_sort(BlenderRNA *brna)
1937 {
1938         StructDefRNA *ds;
1939         StructRNA *srna;
1940
1941         rna_sortlist(&brna->structs, cmp_struct);
1942         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1943
1944         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1945                 rna_sortlist(&srna->cont.properties, cmp_property);
1946
1947         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
1948                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1949 }
1950
1951 static const char *rna_property_structname(PropertyType type)
1952 {
1953         switch (type) {
1954                 case PROP_BOOLEAN: return "BoolPropertyRNA";
1955                 case PROP_INT: return "IntPropertyRNA";
1956                 case PROP_FLOAT: return "FloatPropertyRNA";
1957                 case PROP_STRING: return "StringPropertyRNA";
1958                 case PROP_ENUM: return "EnumPropertyRNA";
1959                 case PROP_POINTER: return "PointerPropertyRNA";
1960                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1961                 default: return "UnknownPropertyRNA";
1962         }
1963 }
1964
1965 static const char *rna_property_subtypename(PropertySubType type)
1966 {
1967         switch (type) {
1968                 case PROP_NONE: return "PROP_NONE";
1969                 case PROP_FILEPATH: return "PROP_FILEPATH";
1970                 case PROP_FILENAME: return "PROP_FILENAME";
1971                 case PROP_DIRPATH: return "PROP_DIRPATH";
1972                 case PROP_BYTESTRING: return "PROP_BYTESTRING";
1973                 case PROP_TRANSLATE: return "PROP_TRANSLATE";
1974                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1975                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1976                 case PROP_FACTOR: return "PROP_FACTOR";
1977                 case PROP_ANGLE: return "PROP_ANGLE";
1978                 case PROP_TIME: return "PROP_TIME";
1979                 case PROP_DISTANCE: return "PROP_DISTANCE";
1980                 case PROP_COLOR: return "PROP_COLOR";
1981                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1982                 case PROP_DIRECTION: return "PROP_DIRECTION";
1983                 case PROP_MATRIX: return "PROP_MATRIX";
1984                 case PROP_EULER: return "PROP_EULER";
1985                 case PROP_QUATERNION: return "PROP_QUATERNION";
1986                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1987                 case PROP_VELOCITY: return "PROP_VELOCITY";
1988                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1989                 case PROP_XYZ: return "PROP_XYZ";
1990                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1991                 case PROP_COORDS: return "PROP_COORDS";
1992                 case PROP_LAYER: return "PROP_LAYER";
1993                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1994                 default: {
1995                         /* in case we don't have a type preset that includes the subtype */
1996                         if (RNA_SUBTYPE_UNIT(type)) {
1997                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1998                         }
1999                         else {
2000                                 return "PROP_SUBTYPE_UNKNOWN";
2001                         }
2002                 }
2003         }
2004 }
2005
2006 static const char *rna_property_subtype_unit(PropertySubType type)
2007 {
2008         switch (RNA_SUBTYPE_UNIT(type)) {
2009                 case PROP_UNIT_NONE:         return "PROP_UNIT_NONE";
2010                 case PROP_UNIT_LENGTH:       return "PROP_UNIT_LENGTH";
2011                 case PROP_UNIT_AREA:         return "PROP_UNIT_AREA";
2012                 case PROP_UNIT_VOLUME:       return "PROP_UNIT_VOLUME";
2013                 case PROP_UNIT_MASS:         return "PROP_UNIT_MASS";
2014                 case PROP_UNIT_ROTATION:     return "PROP_UNIT_ROTATION";
2015                 case PROP_UNIT_TIME:         return "PROP_UNIT_TIME";
2016                 case PROP_UNIT_VELOCITY:     return "PROP_UNIT_VELOCITY";
2017                 case PROP_UNIT_ACCELERATION: return "PROP_UNIT_ACCELERATION";
2018                 default:                     return "PROP_UNIT_UNKNOWN";
2019         }
2020 }
2021
2022 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
2023 {
2024         StructRNA *srna;
2025
2026         for (srna = brna->structs.first; srna; srna = srna->cont.next)
2027                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2028         fprintf(f, "\n");
2029 }
2030
2031 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
2032 {
2033         StructRNA *srna;
2034
2035         fprintf(f, "BlenderRNA BLENDER_RNA = {");
2036
2037         srna = brna->structs.first;
2038         if (srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
2039         else fprintf(f, "{NULL, ");
2040
2041         srna = brna->structs.last;
2042         if (srna) fprintf(f, "&RNA_%s}", srna->identifier);
2043         else fprintf(f, "NULL}");
2044
2045         fprintf(f, "};\n\n");
2046 }
2047
2048 static void rna_generate_property_prototypes(BlenderRNA *UNUSED(brna), StructRNA *srna, FILE *f)
2049 {
2050         PropertyRNA *prop;
2051         StructRNA *base;
2052
2053         base = srna->base;
2054         while (base) {
2055                 fprintf(f, "\n");
2056                 for (prop = base->cont.properties.first; prop; prop = prop->next)
2057                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type),
2058                                 base->identifier, prop->identifier);
2059                 base = base->base;
2060         }
2061
2062         if (srna->cont.properties.first)
2063                 fprintf(f, "\n");
2064
2065         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2066                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT) ? "" : "", rna_property_structname(prop->type),
2067                         srna->identifier, prop->identifier);
2068         fprintf(f, "\n");
2069 }
2070
2071 static void rna_generate_parameter_prototypes(BlenderRNA *UNUSED(brna), StructRNA *srna, FunctionRNA *func, FILE *f)
2072 {
2073         PropertyRNA *parm;
2074
2075         for (parm = func->cont.properties.first; parm; parm = parm->next)
2076                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier,
2077                         func->identifier, parm->identifier);
2078
2079         if (func->cont.properties.first)
2080                 fprintf(f, "\n");
2081 }
2082
2083 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2084 {
2085         FunctionRNA *func;
2086         StructRNA *base;
2087
2088         base = srna->base;
2089         while (base) {
2090                 for (func = base->functions.first; func; func = func->cont.next) {
2091                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
2092                         rna_generate_parameter_prototypes(brna, base, func, f);
2093                 }
2094
2095                 if (base->functions.first)
2096                         fprintf(f, "\n");
2097
2098                 base = base->base;
2099         }
2100
2101         for (func = srna->functions.first; func; func = func->cont.next) {
2102                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
2103                 rna_generate_parameter_prototypes(brna, srna, func, f);
2104         }
2105
2106         if (srna->functions.first)
2107                 fprintf(f, "\n");
2108 }
2109
2110 static void rna_generate_static_parameter_prototypes(BlenderRNA *UNUSED(brna), StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
2111 {
2112         FunctionRNA *func;
2113         PropertyDefRNA *dparm;
2114         StructDefRNA *dsrna;
2115         PropertyType type;
2116         int flag, pout, cptr, first;
2117         const char *ptrstr;
2118
2119         dsrna = rna_find_struct_def(srna);
2120         func = dfunc->func;
2121
2122         /* return type */
2123         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2124                 if (dparm->prop == func->c_ret) {
2125                         if (dparm->prop->arraydimension)
2126                                 fprintf(f, "XXX no array return types yet");  /* XXX not supported */
2127                         else if (dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
2128                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2129                         else
2130                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2131
2132                         break;
2133                 }
2134         }
2135
2136         /* void if nothing to return */
2137         if (!dparm)
2138                 fprintf(f, "void ");
2139
2140         /* function name */
2141         fprintf(f, "%s(", dfunc->call);
2142
2143         first = 1;
2144
2145         /* self, context and reports parameters */
2146         if (func->flag & FUNC_USE_SELF_ID) {
2147                 fprintf(f, "struct ID *_selfid");
2148                 first = 0;
2149         }
2150         
2151         if ((func->flag & FUNC_NO_SELF) == 0) {
2152                 if (!first) fprintf(f, ", ");
2153                 if (dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
2154                 else fprintf(f, "struct %s *_self", srna->identifier);
2155                 first = 0;
2156         }
2157
2158         if (func->flag & FUNC_USE_MAIN) {
2159                 if (!first) fprintf(f, ", ");
2160                 first = 0;
2161                 fprintf(f, "Main *bmain");
2162         }
2163
2164         if (func->flag & FUNC_USE_CONTEXT) {
2165                 if (!first) fprintf(f, ", ");
2166                 first = 0;
2167                 fprintf(f, "bContext *C");
2168         }
2169
2170         if (func->flag & FUNC_USE_REPORTS) {
2171                 if (!first) fprintf(f, ", ");
2172                 first = 0;
2173                 fprintf(f, "ReportList *reports");
2174         }
2175
2176         /* defined parameters */
2177         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2178                 type = dparm->prop->type;
2179                 flag = dparm->prop->flag;
2180                 pout = (flag & PROP_OUTPUT);
2181                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2182
2183                 if (dparm->prop == func->c_ret)
2184                         continue;
2185
2186                 if (cptr || (flag & PROP_DYNAMIC))
2187                         ptrstr = pout ? "**" : "*";
2188                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
2189                         ptrstr = "*";
2190                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
2191                         ptrstr = "";
2192                 else
2193                         ptrstr = pout ? "*" : "";
2194
2195                 if (!first) fprintf(f, ", ");
2196                 first = 0;
2197
2198                 if (flag & PROP_DYNAMIC)
2199                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
2200
2201                 if (!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
2202                         fprintf(f, "%s%s %s[%u]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2203                                 dparm->prop->identifier, dparm->prop->totarraylength);
2204                 else
2205                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2206                                 ptrstr, dparm->prop->identifier);
2207
2208         }
2209
2210         fprintf(f, ");\n");
2211 }
2212
2213 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2214 {
2215         FunctionRNA *func;
2216         FunctionDefRNA *dfunc;
2217         int first = 1;
2218
2219         for (func = srna->functions.first; func; func = func->cont.next) {
2220                 dfunc = rna_find_function_def(func);
2221
2222                 if (dfunc->call) {
2223                         if (first) {
2224                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
2225                                 first = 0;
2226                         }
2227
2228                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
2229                 }
2230         }
2231
2232         fprintf(f, "\n");
2233 }
2234
2235 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop)
2236 {
2237         char *strnest = "", *errnest = "";
2238         int len, freenest = 0;
2239         
2240         if (nest != NULL) {
2241                 len = strlen(nest);
2242
2243                 strnest = MEM_mallocN(sizeof(char) * (len + 2), "rna_generate_property -> strnest");
2244                 errnest = MEM_mallocN(sizeof(char) * (len + 2), "rna_generate_property -> errnest");
2245
2246                 strcpy(strnest, "_"); strcat(strnest, nest);
2247                 strcpy(errnest, "."); strcat(errnest, nest);
2248
2249                 freenest = 1;
2250         }
2251
2252         switch (prop->type) {
2253                 case PROP_ENUM:
2254                 {
2255                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
2256                         int i, defaultfound = 0, totflag = 0;
2257
2258                         if (eprop->item) {
2259                                 fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier,
2260                                         strnest, prop->identifier, eprop->totitem + 1);
2261
2262                                 for (i = 0; i < eprop->totitem; i++) {
2263                                         fprintf(f, "{%d, ", eprop->item[i].value);
2264                                         rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
2265                                         fprintf(f, "%d, ", eprop->item[i].icon);
2266                                         rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
2267                                         rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
2268
2269                                         if (eprop->item[i].identifier[0]) {
2270                                                 if (prop->flag & PROP_ENUM_FLAG) {
2271                                                         totflag |= eprop->item[i].value;
2272                                                 }
2273                                                 else {
2274                                                         if (eprop->defaultvalue == eprop->item[i].value) {
2275                                                                 defaultfound = 1;
2276                                                         }
2277                                                 }
2278                                         }
2279                                 }
2280
2281                                 fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
2282
2283                                 if (prop->flag & PROP_ENUM_FLAG) {
2284                                         if (eprop->defaultvalue & ~totflag) {
2285                                                 fprintf(stderr, "%s: %s%s.%s, enum default includes unused bits (%d).\n",
2286                                                         __func__, srna->identifier, errnest, prop->identifier,
2287                                                         eprop->defaultvalue & ~totflag);
2288                                                 DefRNA.error = 1;
2289                                         }
2290                                 }
2291                                 else {
2292                                         if (!defaultfound) {
2293                                                 fprintf(stderr, "%s: %s%s.%s, enum default is not in items.\n",
2294                                                         __func__, srna->identifier, errnest, prop->identifier);
2295                                                 DefRNA.error = 1;
2296                                         }
2297                                 }
2298                         }
2299                         else {
2300                                 fprintf(stderr, "%s: %s%s.%s, enum must have items defined.\n",
2301                                         __func__, srna->identifier, errnest, prop->identifier);
2302                                 DefRNA.error = 1;
2303                         }
2304                         break;
2305                 }
2306                 case PROP_BOOLEAN:
2307                 {
2308                         BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
2309                         unsigned int i;
2310
2311                         if (prop->arraydimension && prop->totarraylength) {
2312                                 fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2313                                         prop->identifier, prop->totarraylength);
2314
2315                                 for (i = 0; i < prop->totarraylength; i++) {
2316                                         if (bprop->defaultarray)
2317                                                 fprintf(f, "%d", bprop->defaultarray[i]);
2318                                         else
2319                                                 fprintf(f, "%d", bprop->defaultvalue);
2320                                         if (i != prop->totarraylength - 1)
2321                                                 fprintf(f, ",\n\t");
2322                                 }
2323
2324                                 fprintf(f, "\n};\n\n");
2325                         }
2326                         break;
2327                 }
2328                 case PROP_INT:
2329                 {
2330                         IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
2331                         unsigned int i;
2332
2333                         if (prop->arraydimension && prop->totarraylength) {
2334                                 fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2335                                         prop->identifier, prop->totarraylength);
2336
2337                                 for (i = 0; i < prop->totarraylength; i++) {
2338                                         if (iprop->defaultarray)
2339                                                 fprintf(f, "%d", iprop->defaultarray[i]);
2340                                         else
2341                                                 fprintf(f, "%d", iprop->defaultvalue);
2342                                         if (i != prop->totarraylength - 1)
2343                                                 fprintf(f, ",\n\t");
2344                                 }
2345
2346                                 fprintf(f, "\n};\n\n");
2347                         }
2348                         break;
2349                 }
2350                 case PROP_FLOAT:
2351                 {
2352                         FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
2353                         unsigned int i;
2354
2355                         if (prop->arraydimension && prop->totarraylength) {
2356                                 fprintf(f, "static float rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2357                                         prop->identifier, prop->totarraylength);
2358
2359                                 for (i = 0; i < prop->totarraylength; i++) {
2360                                         if (fprop->defaultarray)
2361                                                 rna_float_print(f, fprop->defaultarray[i]);
2362                                         else
2363                                                 rna_float_print(f, fprop->defaultvalue);
2364                                         if (i != prop->totarraylength - 1)
2365                                                 fprintf(f, ",\n\t");
2366                                 }
2367
2368                                 fprintf(f, "\n};\n\n");
2369                         }
2370                         break;
2371                 }
2372                 default:
2373                         break;
2374         }
2375
2376         fprintf(f, "%s%s rna_%s%s_%s = {\n",
2377                 (prop->flag & PROP_EXPORT) ? "" : "",
2378                 rna_property_structname(prop->type),
2379                 srna->identifier, strnest, prop->identifier);
2380
2381         if (prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2382         else fprintf(f, "\t{NULL, ");
2383         if (prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2384         else fprintf(f, "NULL,\n");
2385         fprintf(f, "\t%d, ", prop->magic);
2386         rna_print_c_string(f, prop->identifier);
2387         fprintf(f, ", %d, ", prop->flag);
2388         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2389         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2390         fprintf(f, "%d,\n", prop->icon);
2391         rna_print_c_string(f, prop->translation_context); fprintf(f, ",\n\t");
2392         fprintf(f, "\t%s, %s|%s, %s, %u, {%u, %u, %u}, %u,\n",
2393                 RNA_property_typename(prop->type),
2394                 rna_property_subtypename(prop->subtype),
2395                 rna_property_subtype_unit(prop->subtype),
2396                 rna_function_string(prop->getlength),
2397                 prop->arraydimension,
2398                 prop->arraylength[0],
2399                 prop->arraylength[1],
2400                 prop->arraylength[2],
2401                 prop->totarraylength);
2402         fprintf(f, "\t%s%s, %d, %s, %s,\n",
2403                 (prop->flag & PROP_CONTEXT_UPDATE) ? "(UpdateFunc)" : "",
2404                 rna_function_string(prop->update),
2405                 prop->noteflag,
2406                 rna_function_string(prop->editable),
2407                 rna_function_string(prop->itemeditable));
2408
2409         if (prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2410         else fprintf(f, "\t0, -1");
2411
2412         /* our own type - collections/arrays only */
2413         if (prop->srna) fprintf(f, ", &RNA_%s", (const char *)prop->srna);
2414         else fprintf(f, ", NULL");
2415
2416         fprintf(f, "},\n");
2417
2418         switch (prop->type) {
2419                 case PROP_BOOLEAN:
2420                 {
2421                         BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
2422                         fprintf(f, "\t%s, %s, %s, %s, %d, ",
2423                                 rna_function_string(bprop->get),
2424                                 rna_function_string(bprop->set),
2425                                 rna_function_string(bprop->getarray),
2426                                 rna_function_string(bprop->setarray),
2427                                 bprop->defaultvalue);
2428                         if (prop->arraydimension && prop->totarraylength)
2429                                 fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2430                         else fprintf(f, "NULL\n");
2431                         break;
2432                 }
2433                 case PROP_INT:
2434                 {
2435                         IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
2436                         fprintf(f, "\t%s, %s, %s, %s, %s,\n\t",
2437                                 rna_function_string(iprop->get),
2438                                 rna_function_string(iprop->set),
2439                                 rna_function_string(iprop->getarray),
2440                                 rna_function_string(iprop->setarray),
2441                                 rna_function_string(iprop->range));
2442                         rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2443                         rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2444                         rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2445                         rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2446                         rna_int_print(f, iprop->step); fprintf(f, ", ");
2447                         rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
2448                         if (prop->arraydimension && prop->totarraylength)
2449                                 fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2450                         else fprintf(f, "NULL\n");
2451                         break;
2452                 }
2453                 case PROP_FLOAT:
2454                 {
2455                         FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
2456                         fprintf(f, "\t%s, %s, %s, %s, %s, ",
2457                                 rna_function_string(fprop->get),
2458                                 rna_function_string(fprop->set),
2459                                 rna_function_string(fprop->getarray),
2460                                 rna_function_string(fprop->setarray),
2461                                 rna_function_string(fprop->range));
2462                         rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2463                         rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2464                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2465                         rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2466                         rna_float_print(f, fprop->step); fprintf(f, ", ");
2467                         rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2468                         rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2469                         if (prop->arraydimension && prop->totarraylength)
2470                                 fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2471                         else fprintf(f, "NULL\n");
2472                         break;
2473                 }
2474                 case PROP_STRING:
2475                 {
2476                         StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
2477                         fprintf(f, "\t%s, %s, %s, %d, ",
2478                                 rna_function_string(sprop->get),
2479                                 rna_function_string(sprop->length),
2480                                 rna_function_string(sprop->set),
2481                                 sprop->maxlength);
2482                         rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2483                         break;
2484                 }
2485                 case PROP_ENUM:
2486                 {
2487                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
2488                         fprintf(f, "\t%s, %s, %s, NULL, ",
2489                                 rna_function_string(eprop->get),
2490                                 rna_function_string(eprop->set),
2491                                 rna_function_string(eprop->itemf));
2492                         if (eprop->item)
2493                                 fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2494                         else
2495                                 fprintf(f, "NULL, ");
2496                         fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2497                         break;
2498                 }
2499                 case PROP_POINTER:
2500                 {
2501                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)prop;
2502                         fprintf(f, "\t%s, %s, %s, %s,", rna_function_string(pprop->get),
2503                                 rna_function_string(pprop->set),
2504                                 rna_function_string(pprop->typef),
2505                                 rna_function_string(pprop->poll));
2506                         if (pprop->type) fprintf(f, "&RNA_%s\n", (const char *)pprop->type);
2507                         else fprintf(f, "NULL\n");
2508                         break;
2509                 }
2510                 case PROP_COLLECTION:
2511                 {
2512                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)prop;
2513                         fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, ",
2514                                 rna_function_string(cprop->begin),
2515                                 rna_function_string(cprop->next),
2516                                 rna_function_string(cprop->end),
2517                                 rna_function_string(cprop->get),
2518                                 rna_function_string(cprop->length),
2519                                 rna_function_string(cprop->lookupint),
2520                                 rna_function_string(cprop->lookupstring),
2521                                 rna_function_string(cprop->assignint));
2522                         if (cprop->item_type) fprintf(f, "&RNA_%s\n", (const char *)cprop->item_type);
2523                         else fprintf(f, "NULL\n");
2524                         break;
2525                 }
2526         }
2527
2528         fprintf(f, "};\n\n");
2529
2530         if (freenest) {
2531                 MEM_freeN(strnest);
2532                 MEM_freeN(errnest);
2533         }
2534 }
2535
2536 static void rna_generate_struct(BlenderRNA *UNUSED(brna), StructRNA *srna, FILE *f)
2537 {
2538         FunctionRNA *func;
2539         FunctionDefRNA *dfunc;
2540         PropertyRNA *prop, *parm;
2541         StructRNA *base;
2542
2543         fprintf(f, "/* %s */\n", srna->name);
2544
2545         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2546                 rna_generate_property(f, srna, NULL, prop);
2547
2548         for (func = srna->functions.first; func; func = func->cont.next) {
2549                 for (parm = func->cont.properties.first; parm; parm = parm->next)
2550                         rna_generate_property(f, srna, func->identifier, parm);
2551
2552                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2553
2554                 if (func->cont.next)
2555                         fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier,
2556                                 ((FunctionRNA *)func->cont.next)->identifier);
2557                 else
2558                         fprintf(f, "\t{NULL, ");
2559                 if (func->cont.prev)
2560                         fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier,
2561                                 ((FunctionRNA *)func->cont.prev)->identifier);
2562                 else
2563                         fprintf(f, "NULL,\n");
2564
2565                 fprintf(f, "\tNULL,\n");
2566
2567                 parm = func->cont.properties.first;
2568                 if (parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2569                 else fprintf(f, "\t{NULL, ");
2570
2571                 parm = func->cont.properties.last;
2572                 if (parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2573                 else fprintf(f, "NULL}},\n");
2574
2575                 fprintf(f, "\t");
2576                 rna_print_c_string(f, func->identifier);
2577                 fprintf(f, ", %d, ", func->flag);
2578                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2579
2580                 dfunc = rna_find_function_def(func);
2581                 if (dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2582                 else fprintf(f, "\tNULL,\n");
2583
2584                 if (func->c_ret)
2585                         fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2586                 else
2587                         fprintf(f, "\tNULL\n");
2588
2589                 fprintf(f, "};\n");
2590                 fprintf(f, "\n");
2591         }
2592
2593         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2594
2595         if (srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA *)srna->cont.next)->identifier);
2596         else fprintf(f, "\t{NULL, ");
2597         if (srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA *)srna->cont.prev)->identifier);
2598         else fprintf(f, "NULL,\n");
2599
2600         fprintf(f, "\tNULL,\n");
2601
2602         prop = srna->cont.properties.first;
2603         if (prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2604         else fprintf(f, "\t{NULL, ");
2605
2606         prop = srna->cont.properties.last;
2607         if (prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2608         else fprintf(f, "NULL}},\n");
2609         fprintf(f, "\t");
2610         rna_print_c_string(f, srna->identifier);
2611         fprintf(f, "\t, NULL,NULL\n"); /* PyType - Cant initialize here */
2612         fprintf(f, ", %d, ", srna->flag);
2613         rna_print_c_string(f, srna->name);
2614         fprintf(f, ", ");
2615         rna_print_c_string(f, srna->description);
2616         fprintf(f, ", ");
2617         rna_print_c_string(f, srna->translation_context);
2618         fprintf(f, ",\n\t%d,\n", srna->icon);
2619
2620         prop = srna->nameproperty;
2621         if (prop) {
2622                 base = srna;
2623                 while (base->base && base->base->nameproperty == prop)
2624                         base = base->base;
2625
2626                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2627         }
2628         else fprintf(f, "\tNULL, ");
2629
2630         prop = srna->iteratorproperty;
2631         base = srna;
2632         while (base->base && base->base->iteratorproperty == prop)
2633                 base = base->base;
2634         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2635
2636         if (srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2637         else fprintf(f, "\tNULL,\n");
2638
2639         if (srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2640         else fprintf(f, "\tNULL,\n");
2641
2642         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2643         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2644         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2645         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2646         fprintf(f, "\t%s,\n", rna_function_string(srna->instance));
2647         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2648
2649         if (srna->reg && !srna->refine) {
2650                 fprintf(stderr, "%s: %s has a register function, must also have refine function.\n",
2651                         __func__, srna->identifier);
2652                 DefRNA.error = 1;
2653         }
2654
2655         func = srna->functions.first;
2656         if (func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2657         else fprintf(f, "\t{NULL, ");
2658
2659         func = srna->functions.last;
2660         if (func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2661         else fprintf(f, "NULL}\n");
2662
2663         fprintf(f, "};\n");
2664
2665         fprintf(f, "\n");
2666 }
2667
2668 typedef struct RNAProcessItem {
2669         const char *filename;
2670         const char *api_filename;
2671         void (*define)(BlenderRNA *brna);
2672 } RNAProcessItem;
2673
2674 static RNAProcessItem PROCESS_ITEMS[] = {
2675         {"rna_rna.c", NULL, RNA_def_rna},
2676         {"rna_ID.c", NULL, RNA_def_ID},
2677         {"rna_texture.c", "rna_texture_api.c", RNA_def_texture},
2678         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2679         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2680         {"rna_animviz.c", NULL, RNA_def_animviz},
2681         {"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
2682         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2683         {"rna_boid.c", NULL, RNA_def_boid},
2684         {"rna_brush.c", NULL, RNA_def_brush},
2685         {"rna_camera.c", "rna_camera_api.c", RNA_def_camera},
2686         {"rna_cloth.c", NULL, RNA_def_cloth},
2687         {"rna_color.c", NULL, RNA_def_color},
2688         {"rna_constraint.c", NULL, RNA_def_constraint},
2689         {"rna_context.c", NULL, RNA_def_context},
2690         {"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
2691         {"rna_curve.c", NULL, RNA_def_curve},
2692         {"rna_dynamicpaint.c", NULL, RNA_def_dynamic_paint},
2693         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2694         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2695         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2696         {"rna_group.c", NULL, RNA_def_group},
2697         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2698         {"rna_key.c", NULL, RNA_def_key},
2699         {"rna_lamp.c", NULL, RNA_def_lamp},
2700         {"rna_lattice.c", NULL, RNA_def_lattice},
2701         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2702         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2703         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2704         {"rna_meta.c", NULL, RNA_def_meta},
2705         {"rna_modifier.c", NULL, RNA_def_modifier},
2706         {"rna_nla.c", NULL, RNA_def_nla},
2707         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2708         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2709         {"rna_object_force.c", NULL, RNA_def_object_force},
2710         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2711         {"rna_particle.c", NULL, RNA_def_particle},
2712         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2713         {"rna_property.c", NULL, RNA_def_gameproperty},
2714         {"rna_render.c", NULL, RNA_def_render},
2715         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2716         {"rna_screen.c", NULL, RNA_def_screen},
2717         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2718         {"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
2719         {"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
2720         {"rna_smoke.c", NULL, RNA_def_smoke},
2721         {"rna_space.c", NULL, RNA_def_space},
2722         {"rna_speaker.c", NULL, RNA_def_speaker},
2723         {"rna_test.c", NULL, RNA_def_test},
2724         {"rna_text.c", "rna_text_api.c", RNA_def_text},
2725         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2726         {"rna_sound.c", NULL, RNA_def_sound},
2727         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2728         {"rna_userdef.c", NULL, RNA_def_userdef},
2729         {"rna_vfont.c", NULL, RNA_def_vfont},
2730         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2731         {"rna_world.c", NULL, RNA_def_world},
2732         {"rna_movieclip.c", NULL, RNA_def_movieclip},
2733         {"rna_tracking.c", NULL, RNA_def_tracking},
2734         {"rna_mask.c", NULL, RNA_def_mask},
2735         {NULL, NULL}
2736 };
2737
2738 static void rna_generate(BlenderRNA *brna, FILE *f, const char *filename, const char *api_filename)
2739 {
2740         StructDefRNA *ds;
2741         PropertyDefRNA *dp;
2742         FunctionDefRNA *dfunc;
2743         
2744         fprintf(f,
2745                 "\n"
2746                 "/* Automatically generated struct definitions for the Data API.\n"
2747                 " * Do not edit manually, changes will be overwritten.           */\n\n"
2748                 "#define RNA_RUNTIME\n\n");
2749
2750         fprintf(f, "#include <float.h>\n");
2751         fprintf(f, "#include <stdio.h>\n");
2752         fprintf(f, "#include <limits.h>\n");
2753         fprintf(f, "#include <string.h>\n\n");
2754         fprintf(f, "#include <stddef.h>\n\n");
2755
2756         fprintf(f, "#include \"DNA_ID.h\"\n");
2757         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2758
2759         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2760         fprintf(f, "#include \"BLI_utildefines.h\"\n\n");
2761
2762         fprintf(f, "#include \"BKE_context.h\"\n");
2763         fprintf(f, "#include \"BKE_library.h\"\n");
2764         fprintf(f, "#include \"BKE_main.h\"\n");
2765         fprintf(f, "#include \"BKE_report.h\"\n");
2766
2767         fprintf(f, "#include \"RNA_define.h\"\n");
2768         fprintf(f, "#include \"RNA_types.h\"\n");
2769         fprintf(f, "#include \"rna_internal.h\"\n\n");
2770
2771         rna_generate_prototypes(brna, f);
2772
2773         fprintf(f, "#include \"%s\"\n", filename);
2774         if (api_filename)
2775                 fprintf(f, "#include \"%s\"\n", api_filename);
2776         fprintf(f, "\n");
2777
2778         fprintf(f, "/* Autogenerated Functions */\n\n");
2779
2780         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2781                 if (!filename || ds->filename == filename) {
2782                         rna_generate_property_prototypes(brna, ds->srna, f);
2783                         rna_generate_function_prototypes(brna, ds->srna, f);
2784                 }
2785         }
2786
2787         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2788                 if (!filename || ds->filename == filename)
2789                         for (dp = ds->cont.properties.first; dp; dp = dp->next)
2790                                 rna_def_property_funcs(f, ds->srna, dp);
2791
2792         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2793                 if (!filename || ds->filename == filename) {
2794                         for (dfunc = ds->functions.first; dfunc; dfunc = dfunc->cont.next)
2795                                 rna_def_function_funcs(f, ds, dfunc);
2796
2797                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2798                 }
2799         }
2800
2801         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2802                 if (!filename || ds->filename == filename)
2803                         rna_generate_struct(brna, ds->srna, f);
2804
2805         if (strcmp(filename, "rna_ID.c") == 0) {
2806                 /* this is ugly, but we cannot have c files compiled for both
2807                  * makesrna and blender with some build systems at the moment */
2808                 fprintf(f, "#include \"rna_define.c\"\n\n");
2809
2810                 rna_generate_blender(brna, f);
2811         }
2812 }
2813
2814 static void rna_generate_header(BlenderRNA *UNUSED(brna), FILE *f)
2815 {
2816         StructDefRNA *ds;
2817         PropertyDefRNA *dp;
2818         StructRNA *srna;
2819
2820         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2821         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2822
2823         fprintf(f,
2824                 "/* Automatically generated function declarations for the Data API.\n"
2825                 " * Do not edit manually, changes will be overwritten.              */\n\n");
2826
2827         fprintf(f, "#include \"RNA_types.h\"\n\n");
2828
2829         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2830
2831         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2832         fprintf(f, "    { \\\n");
2833         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2834         fprintf(f, "            for (property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; "
2835                 "property##_next(&rna_macro_iter)) { \\\n");
2836         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2837
2838         fprintf(f, "#define FOREACH_END(property) \\\n");
2839         fprintf(f, "            } \\\n");
2840         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2841         fprintf(f, "    }\n\n");
2842
2843         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2844                 srna = ds->srna;
2845
2846                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2847
2848                 while (srna) {
2849                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2850                         srna = srna->base;
2851                 }
2852                 fprintf(f, "\n");
2853
2854                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2855                         rna_def_property_funcs_header(f, ds->srna, dp);
2856         }
2857
2858         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2859
2860         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2861 }
2862
2863 static const char *cpp_classes = ""
2864 "\n"
2865 "#include <string>\n"
2866 "#include <string.h> /* for memcpy */\n"
2867 "\n"
2868 "namespace BL {\n"
2869 "\n"
2870 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2871 "       inline bool sname::identifier(void) { return sname##_##identifier##_get(&ptr)? true: false; }\n"