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