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