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