merge with/from trunk at r35190
[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         /* note on indices, this is for external functions and ignores skipped values.
952          * so the the index can only be checked against the length when there is no 'skip' funcion. */
953         char *func;
954
955         if(prop->flag & PROP_IDPROPERTY && manualfunc==NULL)
956                 return NULL;
957
958         if(!manualfunc) {
959                 if(!dp->dnastructname || !dp->dnaname)
960                         return NULL;
961
962                 /* only supported in case of standard next functions */
963                 if(strcmp(nextfunc, "rna_iterator_array_next") == 0);
964                 else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
965                 else return NULL;
966         }
967
968         func= rna_alloc_function_name(srna->identifier, prop->identifier, "lookup_int");
969
970         fprintf(f, "int %s(PointerRNA *ptr, int index, PointerRNA *r_ptr)\n", func);
971         fprintf(f, "{\n");
972
973         if(manualfunc) {
974                 fprintf(f, "\n  return %s(ptr, index, r_ptr);\n", manualfunc);
975                 fprintf(f, "}\n\n");
976                 return func;
977         }
978
979         fprintf(f, "    int found= 0;\n");
980         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
981
982         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, prop->identifier);
983         fprintf(f, "    if(iter.valid){\n");
984
985         if(strcmp(nextfunc, "rna_iterator_array_next") == 0) {
986                 fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
987                 fprintf(f, "            if(index < 0 || index >= internal->length) {\n");
988                 fprintf(f, "#ifdef __GNUC__\n");
989                 fprintf(f, "                    printf(\"Array iterator out of range: %%s (index %%d)\\n\", __func__, index);\n");
990                 fprintf(f, "#else\n");
991                 fprintf(f, "                    printf(\"Array iterator out of range: (index %%d)\\n\", index);\n");
992                 fprintf(f, "#endif\n");
993                 fprintf(f, "            }\n");
994                 fprintf(f, "            else if(internal->skip) {\n");
995                 fprintf(f, "                    while(index-- > 0 && iter.valid) {\n");
996                 fprintf(f, "                            rna_iterator_array_next(&iter);\n");
997                 fprintf(f, "                    }\n");
998                 fprintf(f, "                    found= (index == -1 && iter.valid);\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 && iter.valid) {\n");
1009                 fprintf(f, "                            rna_iterator_listbase_next(&iter);\n");
1010                 fprintf(f, "                    }\n");
1011                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1012                 fprintf(f, "            }\n");
1013                 fprintf(f, "            else {\n");
1014                 fprintf(f, "                    while(index-- > 0 && internal->link)\n");
1015                 fprintf(f, "                            internal->link= internal->link->next;\n");
1016                 fprintf(f, "                    found= (index == -1 && internal->link);\n");
1017                 fprintf(f, "            }\n");
1018         }
1019
1020         fprintf(f, "            if(found) *r_ptr = %s_%s_get(&iter);\n", srna->identifier, prop->identifier);
1021         fprintf(f, "    }\n\n");
1022         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, prop->identifier);
1023
1024         fprintf(f, "    return found;\n");
1025
1026 #if 0
1027         rna_print_data_get(f, dp);
1028         item_type= (cprop->item_type)? (char*)cprop->item_type: "UnknownType";
1029
1030         if(dp->dnalengthname || dp->dnalengthfixed) {
1031                 if(dp->dnalengthname)
1032                         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);
1033                 else
1034                         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);
1035         }
1036         else {
1037                 if(dp->dnapointerlevel == 0)
1038                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n", item_type, dp->dnaname);
1039                 else
1040                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
1041         }
1042 #endif
1043
1044         fprintf(f, "}\n\n");
1045
1046         return func;
1047 }
1048
1049 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
1050 {
1051         char *func, *getfunc;
1052
1053         if(prop->flag & PROP_IDPROPERTY && manualfunc==NULL)
1054                 return NULL;
1055
1056         if(!manualfunc)
1057                 return NULL;
1058
1059         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
1060
1061         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1062         fprintf(f, "{\n");
1063         fprintf(f, "    %s(iter);\n", manualfunc);
1064
1065         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
1066
1067         fprintf(f, "\n  if(iter->valid)\n");
1068         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1069
1070         fprintf(f, "}\n\n");
1071
1072         return func;
1073 }
1074
1075 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
1076 {
1077         char *func;
1078
1079         if(prop->flag & PROP_IDPROPERTY && manualfunc==NULL)
1080                 return NULL;
1081
1082         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
1083
1084         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1085         fprintf(f, "{\n");
1086         if(manualfunc)
1087                 fprintf(f, "    %s(iter);\n", manualfunc);
1088         fprintf(f, "}\n\n");
1089
1090         return func;
1091 }
1092
1093 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
1094 {
1095         if(dp->dnapointerlevel != 0)
1096                 return;
1097         if(!dp->dnatype || !dp->dnaname || !dp->dnastructname)
1098                 return;
1099         
1100         if(strcmp(dp->dnatype, "char") == 0) {
1101                 prop->rawtype= PROP_RAW_CHAR;
1102                 prop->flag |= PROP_RAW_ACCESS;
1103         }
1104         else if(strcmp(dp->dnatype, "short") == 0) {
1105                 prop->rawtype= PROP_RAW_SHORT;
1106                 prop->flag |= PROP_RAW_ACCESS;
1107         }
1108         else if(strcmp(dp->dnatype, "int") == 0) {
1109                 prop->rawtype= PROP_RAW_INT;
1110                 prop->flag |= PROP_RAW_ACCESS;
1111         }
1112         else if(strcmp(dp->dnatype, "float") == 0) {
1113                 prop->rawtype= PROP_RAW_FLOAT;
1114                 prop->flag |= PROP_RAW_ACCESS;
1115         }
1116         else if(strcmp(dp->dnatype, "double") == 0) {
1117                 prop->rawtype= PROP_RAW_DOUBLE;
1118                 prop->flag |= PROP_RAW_ACCESS;
1119         }
1120 }
1121
1122 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1123 {
1124         PropertyDefRNA *dp= rna_find_struct_property_def(srna, prop);
1125
1126         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1127 }
1128
1129 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1130 {
1131         PropertyRNA *prop;
1132
1133         prop= dp->prop;
1134
1135         switch(prop->type) {
1136                 case PROP_BOOLEAN: {
1137                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1138
1139                         if(!prop->arraydimension) {
1140                                 if(!bprop->get && !bprop->set && !dp->booleanbit)
1141                                         rna_set_raw_property(dp, prop);
1142
1143                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
1144                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
1145                         }
1146                         else {
1147                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
1148                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
1149                         }
1150                         break;
1151                 }
1152                 case PROP_INT: {
1153                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1154
1155                         if(!prop->arraydimension) {
1156                                 if(!iprop->get && !iprop->set)
1157                                         rna_set_raw_property(dp, prop);
1158
1159                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
1160                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
1161                         }
1162                         else {
1163                                 if(!iprop->getarray && !iprop->setarray)
1164                                         rna_set_raw_property(dp, prop);
1165
1166                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
1167                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
1168                         }
1169                         break;
1170                 }
1171                 case PROP_FLOAT: {
1172                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1173
1174                         if(!prop->arraydimension) {
1175                                 if(!fprop->get && !fprop->set)
1176                                         rna_set_raw_property(dp, prop);
1177
1178                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
1179                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
1180                         }
1181                         else {
1182                                 if(!fprop->getarray && !fprop->setarray)
1183                                         rna_set_raw_property(dp, prop);
1184
1185                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
1186                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
1187                         }
1188                         break;
1189                 }
1190                 case PROP_ENUM: {
1191                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1192
1193                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
1194                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
1195                         break;
1196                 }
1197                 case PROP_STRING: {
1198                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1199
1200                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
1201                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
1202                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
1203                         break;
1204                 }
1205                 case PROP_POINTER: {
1206                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1207
1208                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
1209                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
1210                         if(!pprop->type) {
1211                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have a struct type.\n", srna->identifier, prop->identifier);
1212                                 DefRNA.error= 1;
1213                         }
1214                         break;
1215                 }
1216                 case PROP_COLLECTION: {
1217                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1218                         char *nextfunc= (char*)cprop->next;
1219
1220                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
1221                         else if(dp->dnalengthname || dp->dnalengthfixed)
1222                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
1223
1224                         /* test if we can allow raw array access, if it is using our standard
1225                          * array get/next function, we can be sure it is an actual array */
1226                         if(cprop->next && cprop->get)
1227                                 if(strcmp((char*)cprop->next, "rna_iterator_array_next") == 0 &&
1228                                    strcmp((char*)cprop->get, "rna_iterator_array_get") == 0)
1229                                         prop->flag |= PROP_RAW_ARRAY;
1230
1231                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
1232                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
1233                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
1234                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
1235                         cprop->lookupint= (void*)rna_def_property_lookup_int_func(f, srna, prop, dp, (char*)cprop->lookupint, nextfunc);
1236
1237                         if(!(prop->flag & PROP_IDPROPERTY)) {
1238                                 if(!cprop->begin) {
1239                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
1240                                         DefRNA.error= 1;
1241                                 }
1242                                 if(!cprop->next) {
1243                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
1244                                         DefRNA.error= 1;
1245                                 }
1246                                 if(!cprop->get) {
1247                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
1248                                         DefRNA.error= 1;
1249                                 }
1250                         }
1251                         if(!cprop->item_type) {
1252                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a struct type.\n", srna->identifier, prop->identifier);
1253                                 DefRNA.error= 1;
1254                         }
1255                         break;
1256                 }
1257         }
1258 }
1259
1260 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1261 {
1262         PropertyRNA *prop;
1263         char *func;
1264
1265         prop= dp->prop;
1266
1267         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1268                 return;
1269
1270         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
1271
1272         switch(prop->type) {
1273                 case PROP_BOOLEAN:
1274                 case PROP_INT: {
1275                         if(!prop->arraydimension) {
1276                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1277                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1278                         }
1279                         else {
1280                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->totarraylength);
1281                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
1282                         }
1283                         break;
1284                 }
1285                 case PROP_FLOAT: {
1286                         if(!prop->arraydimension) {
1287                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1288                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
1289                         }
1290                         else {
1291                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->totarraylength);
1292                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
1293                         }
1294                         break;
1295                 }
1296                 case PROP_ENUM: {
1297                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1298                         int i;
1299
1300                         if(eprop->item) {
1301                                 fprintf(f, "enum {\n");
1302
1303                                 for(i=0; i<eprop->totitem; i++)
1304                                         if(eprop->item[i].identifier[0])
1305                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1306
1307                                 fprintf(f, "};\n\n");
1308                         }
1309
1310                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1311                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1312
1313                         break;
1314                 }
1315                 case PROP_STRING: {
1316                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1317
1318                         if(sprop->maxlength) {
1319                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1320                         }
1321                         
1322                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1323                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1324                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
1325
1326                         break;
1327                 }
1328                 case PROP_POINTER: {
1329                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1330                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
1331                         break;
1332                 }
1333                 case PROP_COLLECTION: {
1334                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1335                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1336                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1337                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1338                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
1339                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
1340                         break;
1341                 }
1342         }
1343
1344         fprintf(f, "\n");
1345 }
1346
1347 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1348 {
1349         PropertyRNA *prop;
1350
1351         prop= dp->prop;
1352
1353         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1354                 return;
1355         
1356         if(prop->name && prop->description && strcmp(prop->description, "") != 0)
1357                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1358         else if(prop->name)
1359                 fprintf(f, "\t/* %s */\n", prop->name);
1360         else
1361                 fprintf(f, "\t/* */\n");
1362
1363         switch(prop->type) {
1364                 case PROP_BOOLEAN: {
1365                         if(!prop->arraydimension)
1366                                 fprintf(f, "\tbool %s(void);", prop->identifier);
1367                         else
1368                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->totarraylength, prop->identifier);
1369                         break;
1370                 }
1371                 case PROP_INT: {
1372                         if(!prop->arraydimension)
1373                                 fprintf(f, "\tint %s(void);", prop->identifier);
1374                         else
1375                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->totarraylength, prop->identifier);
1376                         break;
1377                 }
1378                 case PROP_FLOAT: {
1379                         if(!prop->arraydimension)
1380                                 fprintf(f, "\tfloat %s(void);", prop->identifier);
1381                         else
1382                                 fprintf(f, "\tArray<float, %d> %s(void);", prop->totarraylength, prop->identifier);
1383                         break;
1384                 }
1385                 case PROP_ENUM: {
1386                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1387                         int i;
1388
1389                         if(eprop->item) {
1390                                 fprintf(f, "\tenum %s_enum {\n", prop->identifier);
1391
1392                                 for(i=0; i<eprop->totitem; i++)
1393                                         if(eprop->item[i].identifier[0])
1394                                                 fprintf(f, "\t\t%s_%s = %d,\n", prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1395
1396                                 fprintf(f, "\t};\n");
1397                         }
1398
1399                         fprintf(f, "\t%s_enum %s(void);", prop->identifier, prop->identifier);
1400                         break;
1401                 }
1402                 case PROP_STRING: {
1403                         fprintf(f, "\tstd::string %s(void);", prop->identifier);
1404                         break;
1405                 }
1406                 case PROP_POINTER: {
1407                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1408
1409                         if(pprop->type)
1410                                 fprintf(f, "\t%s %s(void);", (char*)pprop->type, prop->identifier);
1411                         else
1412                                 fprintf(f, "\t%s %s(void);", "UnknownType", prop->identifier);
1413                         break;
1414                 }
1415                 case PROP_COLLECTION: {
1416                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1417
1418                         if(cprop->item_type)
1419                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->item_type, srna->identifier, prop->identifier);
1420                         else
1421                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1422                         break;
1423                 }
1424         }
1425
1426         fprintf(f, "\n");
1427 }
1428
1429 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1430 {
1431         PropertyRNA *prop;
1432
1433         prop= dp->prop;
1434
1435         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1436                 return;
1437
1438         switch(prop->type) {
1439                 case PROP_BOOLEAN: {
1440                         if(!prop->arraydimension)
1441                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1442                         else
1443                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1444                         break;
1445                 }
1446                 case PROP_INT: {
1447                         if(!prop->arraydimension)
1448                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1449                         else
1450                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1451                         break;
1452                 }
1453                 case PROP_FLOAT: {
1454                         if(!prop->arraydimension)
1455                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1456                         else
1457                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1458                         break;
1459                 }
1460                 case PROP_ENUM: {
1461                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", prop->identifier, srna->identifier, prop->identifier);
1462
1463                         break;
1464                 }
1465                 case PROP_STRING: {
1466                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1467                         break;
1468                 }
1469                 case PROP_POINTER: {
1470                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1471
1472                         if(pprop->type)
1473                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (char*)pprop->type, srna->identifier, prop->identifier);
1474                         else
1475                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1476                         break;
1477                 }
1478                 case PROP_COLLECTION: {
1479                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1480
1481                         if(cprop->type)
1482                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1483                         else
1484                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1485                         break;
1486                 }
1487         }
1488
1489         fprintf(f, "\n");
1490 }
1491
1492 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1493 {
1494         StructRNA *srna;
1495         FunctionRNA *func;
1496         PropertyDefRNA *dparm;
1497         PropertyType type;
1498         const char *funcname, *valstr;
1499         const char *ptrstr;
1500         const short has_data= (dfunc->cont.properties.first != NULL);
1501         int flag, pout, cptr, first;
1502
1503         srna= dsrna->srna;
1504         func= dfunc->func;
1505
1506         if(!dfunc->call)
1507                 return;
1508
1509         funcname= rna_alloc_function_name(srna->identifier, func->identifier, "call");
1510
1511         /* function definition */
1512         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1513         fprintf(f, "\n{\n");
1514
1515         /* variable definitions */
1516         
1517         if(func->flag & FUNC_USE_SELF_ID) {
1518                 fprintf(f, "\tstruct ID *_selfid;\n");
1519         }
1520
1521         if((func->flag & FUNC_NO_SELF)==0) {
1522                 if(dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1523                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1524         }
1525
1526         dparm= dfunc->cont.properties.first;
1527         for(; dparm; dparm= dparm->next) {
1528                 type = dparm->prop->type;
1529                 flag = dparm->prop->flag;
1530                 pout = (flag & PROP_OUTPUT);
1531                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1532
1533                 if(dparm->prop==func->c_ret)
1534                         ptrstr= cptr || dparm->prop->arraydimension ? "*" : "";
1535                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1536                 else if (cptr || (flag & PROP_DYNAMIC))
1537                         ptrstr= pout ? "**" : "*";
1538                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1539                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1540                         ptrstr= "*";
1541                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack, but type name for string props is already char*, so leave empty */
1542                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1543                         ptrstr= "";
1544                 else
1545                         ptrstr= pout ? "*" : "";
1546
1547                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1548                 if (flag & PROP_DYNAMIC)
1549                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1550                 
1551                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1552         }
1553
1554         if(has_data) {
1555                 fprintf(f, "\tchar *_data");
1556                 if(func->c_ret) fprintf(f, ", *_retdata");
1557                 fprintf(f, ";\n");
1558                 fprintf(f, "\t\n");
1559         }
1560
1561         /* assign self */
1562         if(func->flag & FUNC_USE_SELF_ID) {
1563                 fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1564         }
1565         
1566         if((func->flag & FUNC_NO_SELF)==0) {
1567                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1568                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1569         }
1570
1571         if(has_data) {
1572                 fprintf(f, "\t_data= (char *)_parms->data;\n");
1573         }
1574
1575         dparm= dfunc->cont.properties.first;
1576         for(; dparm; dparm= dparm->next) {
1577                 type = dparm->prop->type;
1578                 flag = dparm->prop->flag;
1579                 pout = (flag & PROP_OUTPUT);
1580                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1581
1582                 if(dparm->prop==func->c_ret)
1583                         fprintf(f, "\t_retdata= _data;\n");
1584                 else  {
1585                         const char *data_str;
1586
1587                         if (cptr || (flag & PROP_DYNAMIC)) {
1588                                 ptrstr= "**";
1589                                 valstr= "*";
1590                         }
1591                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1592                                 ptrstr= "*";
1593                                 valstr= "";
1594                         }
1595                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1596                                 ptrstr= "";
1597                                 valstr= "";
1598                         }
1599                         else {
1600                                 ptrstr= "*";
1601                                 valstr= "*";
1602                         }
1603
1604                         /* this must be kept in sync with RNA_parameter_length_get_data, we could just call the function directly, but this is faster */
1605                         if (flag & PROP_DYNAMIC) {
1606                                 fprintf(f, "\t%s_len= %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
1607                                 data_str= "(&(((char *)_data)[sizeof(void *)]))";
1608                         }
1609                         else {
1610                                 data_str= "_data";
1611                         }
1612                         fprintf(f, "\t%s= ", dparm->prop->identifier);
1613
1614                         if (!pout)
1615                                 fprintf(f, "%s", valstr);
1616
1617                         fprintf(f, "((%s%s%s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, data_str);
1618                 }
1619
1620                 if(dparm->next)
1621                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size_alloc(dparm->prop));
1622         }
1623
1624         if(dfunc->call) {
1625                 fprintf(f, "\t\n");
1626                 fprintf(f, "\t");
1627                 if(func->c_ret) fprintf(f, "%s= ", func->c_ret->identifier);
1628                 fprintf(f, "%s(", dfunc->call);
1629
1630                 first= 1;
1631
1632                 if(func->flag & FUNC_USE_SELF_ID) {
1633                         fprintf(f, "_selfid");
1634                         first= 0;
1635                 }
1636
1637                 if((func->flag & FUNC_NO_SELF)==0) {
1638                         if(!first) fprintf(f, ", ");
1639                         fprintf(f, "_self");
1640                         first= 0;
1641                 }
1642
1643                 if(func->flag & FUNC_USE_CONTEXT) {
1644                         if(!first) fprintf(f, ", ");
1645                         first= 0;
1646                         fprintf(f, "C");
1647                 }
1648
1649                 if(func->flag & FUNC_USE_REPORTS) {
1650                         if(!first) fprintf(f, ", ");
1651                         first= 0;
1652                         fprintf(f, "reports");
1653                 }
1654
1655                 dparm= dfunc->cont.properties.first;
1656                 for(; dparm; dparm= dparm->next) {
1657                         if(dparm->prop==func->c_ret)
1658                                 continue;
1659
1660                         if(!first) fprintf(f, ", ");
1661                         first= 0;
1662
1663                         if (dparm->prop->flag & PROP_DYNAMIC)
1664                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
1665                         else
1666                                 fprintf(f, "%s", dparm->prop->identifier);
1667                 }
1668
1669                 fprintf(f, ");\n");
1670
1671                 if(func->c_ret) {
1672                         dparm= rna_find_parameter_def(func->c_ret);
1673                         ptrstr= (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) || (dparm->prop->arraydimension))? "*": "";
1674                         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);
1675                 }
1676         }
1677
1678         fprintf(f, "}\n\n");
1679
1680         dfunc->gencall= funcname;
1681 }
1682
1683 static void rna_auto_types(void)
1684 {
1685         StructDefRNA *ds;
1686         PropertyDefRNA *dp;
1687
1688         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1689                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1690                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1691                         ds->dnaname= "bScreen";
1692
1693                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1694                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1695                                 dp->dnastructname= "bScreen";
1696
1697                         if(dp->dnatype) {
1698                                 if(dp->prop->type == PROP_POINTER) {
1699                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1700                                         StructRNA *type;
1701
1702                                         if(!pprop->type && !pprop->get)
1703                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1704
1705                                         if(pprop->type) {
1706                                                 type= rna_find_struct((char*)pprop->type);
1707                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1708                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1709                                         }
1710                                 }
1711                                 else if(dp->prop->type== PROP_COLLECTION) {
1712                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1713
1714                                         if(!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1715                                                 cprop->item_type= (StructRNA*)rna_find_type(dp->dnatype);
1716                                 }
1717                         }
1718                 }
1719         }
1720 }
1721
1722 static void rna_sort(BlenderRNA *brna)
1723 {
1724         StructDefRNA *ds;
1725         StructRNA *srna;
1726
1727         rna_sortlist(&brna->structs, cmp_struct);
1728         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1729
1730         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1731                 rna_sortlist(&srna->cont.properties, cmp_property);
1732
1733         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1734                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1735 }
1736
1737 static const char *rna_property_structname(PropertyType type)
1738 {
1739         switch(type) {
1740                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1741                 case PROP_INT: return "IntPropertyRNA";
1742                 case PROP_FLOAT: return "FloatPropertyRNA";
1743                 case PROP_STRING: return "StringPropertyRNA";
1744                 case PROP_ENUM: return "EnumPropertyRNA";
1745                 case PROP_POINTER: return "PointerPropertyRNA";
1746                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1747                 default: return "UnknownPropertyRNA";
1748         }
1749 }
1750
1751 static const char *rna_property_subtypename(PropertySubType type)
1752 {
1753         switch(type) {
1754                 case PROP_NONE: return "PROP_NONE";
1755                 case PROP_FILEPATH: return "PROP_FILEPATH";
1756                 case PROP_FILENAME: return "PROP_FILENAME";
1757                 case PROP_DIRPATH: return "PROP_DIRPATH";
1758                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1759                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1760                 case PROP_FACTOR: return "PROP_FACTOR";
1761                 case PROP_ANGLE: return "PROP_ANGLE";
1762                 case PROP_TIME: return "PROP_TIME";
1763                 case PROP_DISTANCE: return "PROP_DISTANCE";
1764                 case PROP_COLOR: return "PROP_COLOR";
1765                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1766                 case PROP_DIRECTION: return "PROP_DIRECTION";
1767                 case PROP_MATRIX: return "PROP_MATRIX";
1768                 case PROP_EULER: return "PROP_EULER";
1769                 case PROP_QUATERNION: return "PROP_QUATERNION";
1770                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1771                 case PROP_VELOCITY: return "PROP_VELOCITY";
1772                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1773                 case PROP_XYZ: return "PROP_XYZ";
1774                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1775                 case PROP_LAYER: return "PROP_LAYER";
1776                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1777                 default: {
1778                         /* incase we dont have a type preset that includes the subtype */
1779                         if(RNA_SUBTYPE_UNIT(type)) {
1780                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1781                         }
1782                         else {
1783                                 return "PROP_SUBTYPE_UNKNOWN";
1784                         }
1785                 }
1786         }
1787 }
1788
1789 static const char *rna_property_subtype_unit(PropertyType type)
1790 {
1791         switch(RNA_SUBTYPE_UNIT(type)) {
1792                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1793                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1794                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1795                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1796                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1797                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1798                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1799                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1800                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1801                 default:                                        return "PROP_UNIT_UNKNOWN";
1802         }
1803 }
1804
1805 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1806 {
1807         StructRNA *srna;
1808
1809         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1810                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1811         fprintf(f, "\n");
1812 }
1813
1814 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1815 {
1816         StructRNA *srna;
1817
1818         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1819
1820         srna= brna->structs.first;
1821         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1822         else fprintf(f, "{NULL, ");
1823
1824         srna= brna->structs.last;
1825         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1826         else fprintf(f, "NULL}");
1827
1828         fprintf(f, "};\n\n");
1829 }
1830
1831 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1832 {
1833         PropertyRNA *prop;
1834         StructRNA *base;
1835
1836         base= srna->base;
1837         while (base) {
1838                 fprintf(f, "\n");
1839                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1840                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1841                 base= base->base;
1842         }
1843
1844         if(srna->cont.properties.first)
1845                 fprintf(f, "\n");
1846
1847         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1848                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1849         fprintf(f, "\n");
1850 }
1851
1852 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1853 {
1854         PropertyRNA *parm;
1855
1856         for(parm= func->cont.properties.first; parm; parm= parm->next)
1857                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1858
1859         if(func->cont.properties.first)
1860                 fprintf(f, "\n");
1861 }
1862
1863 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1864 {
1865         FunctionRNA *func;
1866         StructRNA *base;
1867
1868         base= srna->base;
1869         while (base) {
1870                 for(func= base->functions.first; func; func= func->cont.next) {
1871                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1872                         rna_generate_parameter_prototypes(brna, base, func, f);
1873                 }
1874
1875                 if(base->functions.first)
1876                         fprintf(f, "\n");
1877
1878                 base= base->base;
1879         }
1880
1881         for(func= srna->functions.first; func; func= func->cont.next) {
1882                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1883                 rna_generate_parameter_prototypes(brna, srna, func, f);
1884         }
1885
1886         if(srna->functions.first)
1887                 fprintf(f, "\n");
1888 }
1889
1890 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1891 {
1892         FunctionRNA *func;
1893         PropertyDefRNA *dparm;
1894         StructDefRNA *dsrna;
1895         PropertyType type;
1896         int flag, pout, cptr, first;
1897         const char *ptrstr;
1898
1899         dsrna= rna_find_struct_def(srna);
1900         func= dfunc->func;
1901
1902         /* return type */
1903         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1904                 if(dparm->prop==func->c_ret) {
1905                         if(dparm->prop->arraydimension)
1906                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1907                         else if(dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
1908                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1909                         else
1910                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1911
1912                         break;
1913                 }
1914         }
1915
1916         /* void if nothing to return */
1917         if(!dparm)
1918                 fprintf(f, "void ");
1919
1920         /* function name */
1921         fprintf(f, "%s(", dfunc->call);
1922
1923         first= 1;
1924
1925         /* self, context and reports parameters */
1926         if(func->flag & FUNC_USE_SELF_ID) {
1927                 fprintf(f, "struct ID *_selfid");
1928                 first= 0;               
1929         }
1930         
1931         if((func->flag & FUNC_NO_SELF)==0) {
1932                 if(!first) fprintf(f, ", ");
1933                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1934                 else fprintf(f, "struct %s *_self", srna->identifier);
1935                 first= 0;
1936         }
1937
1938         if(func->flag & FUNC_USE_CONTEXT) {
1939                 if(!first) fprintf(f, ", ");
1940                 first= 0;
1941                 fprintf(f, "bContext *C");
1942         }
1943
1944         if(func->flag & FUNC_USE_REPORTS) {
1945                 if(!first) fprintf(f, ", ");
1946                 first= 0;
1947                 fprintf(f, "ReportList *reports");
1948         }
1949
1950         /* defined parameters */
1951         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1952                 type = dparm->prop->type;
1953                 flag = dparm->prop->flag;
1954                 pout = (flag & PROP_OUTPUT);
1955                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1956
1957                 if(dparm->prop==func->c_ret)
1958                         continue;
1959
1960                 if (cptr || (flag & PROP_DYNAMIC))
1961                         ptrstr= pout ? "**" : "*";
1962                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1963                         ptrstr= "*";
1964                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1965                         ptrstr= "";
1966                 else
1967                         ptrstr= pout ? "*" : "";
1968
1969                 if(!first) fprintf(f, ", ");
1970                 first= 0;
1971
1972                 if (flag & PROP_DYNAMIC)
1973                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
1974
1975                 if(!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
1976                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->totarraylength);
1977                 else
1978                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1979
1980         }
1981
1982         fprintf(f, ");\n");
1983 }
1984
1985 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1986 {
1987         FunctionRNA *func;
1988         FunctionDefRNA *dfunc;
1989         int first= 1;
1990
1991         for(func= srna->functions.first; func; func= func->cont.next) {
1992                 dfunc= rna_find_function_def(func);
1993
1994                 if(dfunc->call) {
1995                         if(first) {
1996                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1997                                 first= 0;
1998                         }
1999
2000                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
2001                 }
2002         }
2003
2004         fprintf(f, "\n");
2005 }
2006
2007 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
2008 {
2009         char *strnest= "", *errnest= "";
2010         int len, freenest= 0;
2011         
2012         if(nest != NULL) {
2013                 len= strlen(nest);
2014
2015                 strnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
2016                 errnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
2017
2018                 strcpy(strnest, "_"); strcat(strnest, nest);
2019                 strcpy(errnest, "."); strcat(errnest, nest);
2020
2021                 freenest= 1;
2022         }
2023
2024         switch(prop->type) {
2025                         case PROP_ENUM: {
2026                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
2027                                 int i, defaultfound= 0, totflag= 0;
2028
2029                                 if(eprop->item) {
2030                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
2031
2032                                         for(i=0; i<eprop->totitem; i++) {
2033                                                 fprintf(f, "{%d, ", eprop->item[i].value);
2034                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
2035                                                 fprintf(f, "%d, ", eprop->item[i].icon);
2036                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
2037                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
2038
2039                                                 if(eprop->item[i].identifier[0]) {
2040                                                         if(prop->flag & PROP_ENUM_FLAG) {
2041                                                                 totflag |= eprop->item[i].value;
2042                                                         }
2043                                                         else {
2044                                                                 if(eprop->defaultvalue == eprop->item[i].value) {
2045                                                                         defaultfound= 1;
2046                                                                 }
2047                                                         }
2048                                                 }
2049                                         }
2050
2051                                         fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
2052
2053                                         if(prop->flag & PROP_ENUM_FLAG) {
2054                                                 if(eprop->defaultvalue & ~totflag) {
2055                                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default includes unused bits (%d).\n", srna->identifier, errnest, prop->identifier, eprop->defaultvalue & ~totflag);
2056                                                         DefRNA.error= 1;
2057                                                 }
2058                                         }
2059                                         else {
2060                                                 if(!defaultfound) {
2061                                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
2062                                                         DefRNA.error= 1;
2063                                                 }
2064                                         }
2065                                 }
2066                                 else {
2067                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
2068                                         DefRNA.error= 1;
2069                                 }
2070                                 break;
2071                         }
2072                         case PROP_BOOLEAN: {
2073                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
2074                                 unsigned int i;
2075
2076                                 if(prop->arraydimension && prop->totarraylength) {
2077                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2078
2079                                         for(i=0; i<prop->totarraylength; i++) {
2080                                                 if(bprop->defaultarray)
2081                                                         fprintf(f, "%d", bprop->defaultarray[i]);
2082                                                 else
2083                                                         fprintf(f, "%d", bprop->defaultvalue);
2084                                                 if(i != prop->totarraylength-1)
2085                                                         fprintf(f, ",\n\t");
2086                                         }
2087
2088                                         fprintf(f, "\n};\n\n");
2089                                 }
2090                                 break;
2091                         }
2092                         case PROP_INT: {
2093                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
2094                                 unsigned int i;
2095
2096                                 if(prop->arraydimension && prop->totarraylength) {
2097                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2098
2099                                         for(i=0; i<prop->totarraylength; i++) {
2100                                                 if(iprop->defaultarray)
2101                                                         fprintf(f, "%d", iprop->defaultarray[i]);
2102                                                 else
2103                                                         fprintf(f, "%d", iprop->defaultvalue);
2104                                                 if(i != prop->totarraylength-1)
2105                                                         fprintf(f, ",\n\t");
2106                                         }
2107
2108                                         fprintf(f, "\n};\n\n");
2109                                 }
2110                                 break;
2111                         }
2112                         case PROP_FLOAT: {
2113                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
2114                                 unsigned int i;
2115
2116                                 if(prop->arraydimension && prop->totarraylength) {
2117                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2118
2119                                         for(i=0; i<prop->totarraylength; i++) {
2120                                                 if(fprop->defaultarray)
2121                                                         rna_float_print(f, fprop->defaultarray[i]);
2122                                                 else
2123                                                         rna_float_print(f, fprop->defaultvalue);
2124                                                 if(i != prop->totarraylength-1)
2125                                                         fprintf(f, ",\n\t");
2126                                         }
2127
2128                                         fprintf(f, "\n};\n\n");
2129                                 }
2130                                 break;
2131                         }
2132                         default:
2133                                 break;
2134         }
2135
2136         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
2137
2138         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2139         else fprintf(f, "\t{NULL, ");
2140         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2141         else fprintf(f, "NULL,\n");
2142         fprintf(f, "\t%d, ", prop->magic);
2143         rna_print_c_string(f, prop->identifier);
2144         fprintf(f, ", %d, ", prop->flag);
2145         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2146         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2147         fprintf(f, "%d,\n", prop->icon);
2148         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);
2149         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));
2150
2151         if(prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2152         else fprintf(f, "\t0, -1");
2153
2154         /* our own type - collections/arrays only */
2155         if(prop->srna) fprintf(f, ", &RNA_%s", (char*)prop->srna);
2156         else fprintf(f, ", NULL");
2157
2158         fprintf(f, "},\n");
2159
2160         switch(prop->type) {
2161                         case PROP_BOOLEAN: {
2162                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
2163                                 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);
2164                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2165                                 else fprintf(f, "NULL\n");
2166                                 break;
2167                         }
2168                         case PROP_INT: {
2169                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
2170                                 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));
2171                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2172                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2173                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2174                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2175                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
2176                                 rna_int_print(f, iprop->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_FLOAT: {
2182                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
2183                                 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));
2184                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2185                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2186                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2187                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2188                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
2189                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2190                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2191                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2192                                 else fprintf(f, "NULL\n");
2193                                 break;
2194                          }
2195                         case PROP_STRING: {
2196                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
2197                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
2198                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2199                                 break;
2200                         }
2201                         case PROP_ENUM: {
2202                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
2203                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
2204                                 if(eprop->item)
2205                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2206                                 else
2207                                         fprintf(f, "NULL, ");
2208                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2209                                 break;
2210                         }
2211                         case PROP_POINTER: {
2212                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
2213                                 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));
2214                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
2215                                 else fprintf(f, "NULL\n");
2216                                 break;
2217                          }
2218                         case PROP_COLLECTION: {
2219                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
2220                                 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));
2221                                 if(cprop->item_type) fprintf(f, "&RNA_%s\n", (char*)cprop->item_type);
2222                                 else fprintf(f, "NULL\n");
2223                                 break;
2224                         }
2225         }
2226
2227         fprintf(f, "};\n\n");
2228
2229         if(freenest) {
2230                 MEM_freeN(strnest);
2231                 MEM_freeN(errnest);
2232         }
2233 }
2234
2235 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
2236 {
2237         FunctionRNA *func;
2238         FunctionDefRNA *dfunc;
2239         PropertyRNA *prop, *parm;
2240         StructRNA *base;
2241
2242         fprintf(f, "/* %s */\n", srna->name);
2243
2244         for(prop= srna->cont.properties.first; prop; prop= prop->next)
2245                 rna_generate_property(f, srna, NULL, prop);
2246
2247         for(func= srna->functions.first; func; func= func->cont.next) {
2248                 for(parm= func->cont.properties.first; parm; parm= parm->next)
2249                         rna_generate_property(f, srna, func->identifier, parm);
2250
2251                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2252
2253                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
2254                 else fprintf(f, "\t{NULL, ");
2255                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
2256                 else fprintf(f, "NULL,\n");
2257
2258                 fprintf(f, "\tNULL,\n");
2259
2260                 parm= func->cont.properties.first;
2261                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2262                 else fprintf(f, "\t{NULL, ");
2263
2264                 parm= func->cont.properties.last;
2265                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2266                 else fprintf(f, "NULL}},\n");
2267
2268                 fprintf(f, "\t");
2269                 rna_print_c_string(f, func->identifier);
2270                 fprintf(f, ", %d, ", func->flag);
2271                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2272
2273                 dfunc= rna_find_function_def(func);
2274                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2275                 else fprintf(f, "\tNULL,\n");
2276
2277                 if(func->c_ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2278                 else fprintf(f, "\tNULL\n");
2279
2280                 fprintf(f, "};\n");
2281                 fprintf(f, "\n");
2282         }
2283
2284         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2285
2286         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
2287         else fprintf(f, "\t{NULL, ");
2288         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
2289         else fprintf(f, "NULL,\n");
2290
2291         fprintf(f, "\tNULL,\n");
2292
2293         prop= srna->cont.properties.first;
2294         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2295         else fprintf(f, "\t{NULL, ");
2296
2297         prop= srna->cont.properties.last;
2298         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2299         else fprintf(f, "NULL}},\n");
2300         fprintf(f, "\t");
2301         rna_print_c_string(f, srna->identifier);
2302         fprintf(f, "\t, NULL,NULL\n"); /* PyType - Cant initialize here */
2303         fprintf(f, ", %d, ", srna->flag);
2304         rna_print_c_string(f, srna->name);
2305         fprintf(f, ", ");
2306         rna_print_c_string(f, srna->description);
2307         fprintf(f, ",\n\t%d,\n", srna->icon);
2308
2309         prop= srna->nameproperty;
2310         if(prop) {
2311                 base= srna;
2312                 while (base->base && base->base->nameproperty==prop)
2313                         base= base->base;
2314
2315                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2316         }
2317         else fprintf(f, "\tNULL, ");
2318
2319         prop= srna->iteratorproperty;
2320         base= srna;
2321         while (base->base && base->base->iteratorproperty==prop)
2322                 base= base->base;
2323         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2324
2325         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2326         else fprintf(f, "\tNULL,\n");
2327
2328         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2329         else fprintf(f, "\tNULL,\n");
2330
2331         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2332         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2333         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2334         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2335         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2336
2337         if(srna->reg && !srna->refine) {
2338                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
2339                 DefRNA.error= 1;
2340         }
2341
2342         func= srna->functions.first;
2343         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2344         else fprintf(f, "\t{NULL, ");
2345
2346         func= srna->functions.last;
2347         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2348         else fprintf(f, "NULL}\n");
2349
2350         fprintf(f, "};\n");
2351
2352         fprintf(f, "\n");
2353 }
2354
2355 typedef struct RNAProcessItem {
2356         const char *filename;
2357         const char *api_filename;
2358         void (*define)(BlenderRNA *brna);
2359 } RNAProcessItem;
2360
2361 static RNAProcessItem PROCESS_ITEMS[]= {
2362         {"rna_rna.c", NULL, RNA_def_rna},
2363         {"rna_ID.c", NULL, RNA_def_ID},
2364         {"rna_texture.c", NULL, RNA_def_texture},
2365         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2366         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2367         {"rna_animviz.c", NULL, RNA_def_animviz},
2368         {"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
2369         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2370         {"rna_boid.c", NULL, RNA_def_boid},
2371         {"rna_brush.c", NULL, RNA_def_brush},
2372         {"rna_camera.c", NULL, RNA_def_camera},
2373         {"rna_cloth.c", NULL, RNA_def_cloth},
2374         {"rna_color.c", NULL, RNA_def_color},
2375         {"rna_constraint.c", NULL, RNA_def_constraint},
2376         {"rna_context.c", NULL, RNA_def_context},
2377         {"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
2378         {"rna_curve.c", NULL, RNA_def_curve},
2379         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2380         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2381         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2382         {"rna_group.c", NULL, RNA_def_group},
2383         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2384         {"rna_key.c", NULL, RNA_def_key},
2385         {"rna_lamp.c", NULL, RNA_def_lamp},
2386         {"rna_lattice.c", NULL, RNA_def_lattice},
2387         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2388         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2389         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2390         {"rna_meta.c", NULL, RNA_def_meta},
2391         {"rna_modifier.c", NULL, RNA_def_modifier},
2392         {"rna_nla.c", NULL, RNA_def_nla},
2393         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2394         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2395         {"rna_object_force.c", NULL, RNA_def_object_force},
2396         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2397         {"rna_particle.c", NULL, RNA_def_particle},
2398         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2399         {"rna_property.c", NULL, RNA_def_gameproperty},
2400         {"rna_render.c", NULL, RNA_def_render},
2401         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2402         {"rna_screen.c", NULL, RNA_def_screen},
2403         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2404         {"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
2405         {"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
2406         {"rna_smoke.c", NULL, RNA_def_smoke},
2407         {"rna_space.c", NULL, RNA_def_space},
2408         {"rna_test.c", NULL, RNA_def_test},
2409         {"rna_text.c", NULL, RNA_def_text},
2410         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2411         {"rna_sound.c", NULL, RNA_def_sound},
2412         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2413         {"rna_userdef.c", NULL, RNA_def_userdef},
2414         {"rna_vfont.c", NULL, RNA_def_vfont},
2415         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2416         {"rna_world.c", NULL, RNA_def_world},   
2417         {NULL, NULL}};
2418
2419 static void rna_generate(BlenderRNA *brna, FILE *f, const char *filename, const char *api_filename)
2420 {
2421         StructDefRNA *ds;
2422         PropertyDefRNA *dp;
2423         FunctionDefRNA *dfunc;
2424         
2425         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
2426                                  "   Do not edit manually, changes will be overwritten.           */\n\n"
2427                                   "#define RNA_RUNTIME\n\n");
2428
2429         fprintf(f, "#include <float.h>\n");
2430         fprintf(f, "#include <stdio.h>\n");
2431         fprintf(f, "#include <limits.h>\n");
2432         fprintf(f, "#include <string.h>\n\n");
2433         fprintf(f, "#include <stddef.h>\n\n");
2434
2435         fprintf(f, "#include \"DNA_ID.h\"\n");
2436         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2437
2438         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2439         fprintf(f, "#include \"BLI_utildefines.h\"\n\n");
2440
2441         fprintf(f, "#include \"BKE_context.h\"\n");
2442         fprintf(f, "#include \"BKE_library.h\"\n");
2443         fprintf(f, "#include \"BKE_main.h\"\n");
2444         fprintf(f, "#include \"BKE_report.h\"\n");
2445
2446         fprintf(f, "#include \"RNA_define.h\"\n");
2447         fprintf(f, "#include \"RNA_types.h\"\n");
2448         fprintf(f, "#include \"rna_internal.h\"\n\n");
2449
2450         rna_generate_prototypes(brna, f);
2451
2452         fprintf(f, "#include \"%s\"\n", filename);
2453         if(api_filename)
2454                 fprintf(f, "#include \"%s\"\n", api_filename);
2455         fprintf(f, "\n");
2456
2457         fprintf(f, "/* Autogenerated Functions */\n\n");
2458
2459         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2460                 if(!filename || ds->filename == filename) {
2461                         rna_generate_property_prototypes(brna, ds->srna, f);
2462                         rna_generate_function_prototypes(brna, ds->srna, f);
2463                 }
2464         }
2465
2466         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2467                 if(!filename || ds->filename == filename)
2468                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
2469                                 rna_def_property_funcs(f, ds->srna, dp);
2470
2471         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2472                 if(!filename || ds->filename == filename) {
2473                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
2474                                 rna_def_function_funcs(f, ds, dfunc);
2475
2476                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2477                 }
2478         }
2479
2480         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2481                 if(!filename || ds->filename == filename)
2482                         rna_generate_struct(brna, ds->srna, f);
2483
2484         if(strcmp(filename, "rna_ID.c") == 0) {
2485                 /* this is ugly, but we cannot have c files compiled for both
2486                  * makesrna and blender with some build systems at the moment */
2487                 fprintf(f, "#include \"rna_define.c\"\n\n");
2488
2489                 rna_generate_blender(brna, f);
2490         }
2491 }
2492
2493 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2494 {
2495         StructDefRNA *ds;
2496         PropertyDefRNA *dp;
2497         StructRNA *srna;
2498
2499         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2500         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2501
2502         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2503                                  "   Do not edit manually, changes will be overwritten.              */\n\n");
2504
2505         fprintf(f, "#include \"RNA_types.h\"\n\n");
2506
2507         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2508
2509         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2510         fprintf(f, "    { \\\n");
2511         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2512         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2513         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2514
2515         fprintf(f, "#define FOREACH_END(property) \\\n");
2516         fprintf(f, "            } \\\n");
2517         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2518         fprintf(f, "    }\n\n");
2519
2520         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2521                 srna= ds->srna;
2522
2523                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2524
2525                 while(srna) {
2526                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2527                         srna= srna->base;
2528                 }
2529                 fprintf(f, "\n");
2530
2531                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2532                         rna_def_property_funcs_header(f, ds->srna, dp);
2533         }
2534
2535         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2536
2537         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2538 }
2539
2540 static const char *cpp_classes = ""
2541 "\n"
2542 "#include <string>\n"
2543 "\n"
2544 "namespace RNA {\n"
2545 "\n"
2546 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2547 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
2548 "\n"
2549 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2550 "       Array<int,size> sname::identifier(void) \\\n"
2551 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2552 "\n"
2553 "#define INT_PROPERTY(sname, identifier) \\\n"
2554 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2555 "\n"
2556 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2557 "       Array<int,size> sname::identifier(void) \\\n"
2558 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2559 "\n"
2560 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2561 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2562 "\n"
2563 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2564 "       Array<float,size> sname::identifier(void) \\\n"
2565 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2566 "\n"
2567 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2568 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2569 "\n"
2570 "#define STRING_PROPERTY(sname, identifier) \\\n"
2571 "       std::string sname::identifier(void) { \\\n"
2572 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2573 "               std::string str; str.resize(len); \\\n"
2574 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2575 "\n"
2576 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2577 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2578 "\n"
2579 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2580 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2581 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2582 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2583 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2584 "\n"
2585 "class Pointer {\n"
2586 "public:\n"
2587 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2588 "       operator const PointerRNA&() { return ptr; }\n"
2589 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2590 "       operator void*() { return ptr.data; }\n"
2591 "       operator bool() { return ptr.data != NULL; }\n"
2592 "\n"
2593 "       PointerRNA ptr;\n"
2594 "};\n"
2595 "\n"
2596 "\n"
2597 "template<typename T, int Tsize>\n"
2598 "class Array {\n"
2599 "public:\n"
2600 "       T data[Tsize];\n"
2601 "       operator T*() { return data; }\n"
2602 "};\n"
2603 "\n"
2604 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2605 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2606 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2607 "\n"
2608 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2609 "class CollectionIterator {\n"
2610 "public:\n"
2611 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2612 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2613 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2614 "       { 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"
2615 "\n"
2616 "       operator bool(void)\n"
2617 "       { return iter.valid != 0; }\n"
2618 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2619 "       T& operator*(void) { return t; }\n"
2620 "       T* operator->(void) { return &t; }\n"
2621 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2622 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2623 "\n"
2624 "       void begin(const Pointer& ptr)\n"
2625 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2626 "\n"
2627 "private:\n"
2628 "       CollectionPropertyIterator iter;\n"
2629 "       T t;\n"
2630 "       bool init;\n"
2631 "};\n"
2632 "\n"
2633 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2634 "class Collection {\n"
2635 "public:\n"
2636 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2637 "\n"
2638 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2639 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2640 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2641 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2642 "\n"
2643 "private:\n"
2644 "       PointerRNA ptr;\n"
2645 "};\n"
2646 "\n";
2647
2648 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2649 {
2650         StructDefRNA *ds;
2651         PropertyDefRNA *dp;
2652         StructRNA *srna;
2653
2654         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2655         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2656
2657         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2658                                  "   Do not edit manually, changes will be overwritten. */\n\n");
2659
2660         fprintf(f, "#include \"RNA_blender.h\"\n");
2661         fprintf(f, "#include \"RNA_types.h\"\n");
2662
2663         fprintf(f, "%s", cpp_classes);
2664
2665         fprintf(f, "/**************** Declarations ****************/\n\n");
2666
2667         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2668                 fprintf(f, "class %s;\n", ds->srna->identifier);
2669         fprintf(f, "\n");
2670
2671         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2672                 srna= ds->srna;
2673
2674                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2675
2676                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2677                 fprintf(f, "public:\n");
2678                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2679                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2680                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2681                                 if(dp->prop->type == PROP_COLLECTION)
2682                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2683                 fprintf(f, "\n\t\t{}\n\n");
2684
2685                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2686                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2687                 fprintf(f, "};\n\n");
2688         }
2689
2690
2691         fprintf(f, "/**************** Implementation ****************/\n");
2692
2693         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2694                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2695                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2696
2697                 fprintf(f, "\n");
2698         }
2699
2700         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2701 }
2702
2703 static void make_bad_file(char *file, int line)
2704 {
2705         FILE *fp= fopen(file, "w");
2706         fprintf(fp, "#error \"Error! can't make correct RNA file from %s:%d, STUPID!\"\n", __FILE__, line);
2707         fclose(fp);
2708 }
2709
2710 static int rna_preprocess(char *outfile)
2711 {
2712         BlenderRNA *brna;
2713         StructDefRNA *ds;
2714         FILE *file;
2715         char deffile[4096];
2716         int i, status;
2717         const char *deps[3]; /* expand as needed */
2718
2719         /* define rna */
2720         brna= RNA_create();
2721
2722         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2723                 if(PROCESS_ITEMS[i].define) {
2724                         PROCESS_ITEMS[i].define(brna);
2725
2726                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2727                                 if(!ds->filename)
2728                                         ds->filename= PROCESS_ITEMS[i].filename;
2729                 }
2730         }
2731
2732         rna_auto_types();
2733
2734
2735         /* create RNA_blender_cpp.h */
2736         strcpy(deffile, outfile);
2737         strcat(deffile, "RNA_blender_cpp.h" TMP_EXT);
2738
2739         status= (DefRNA.error != 0);
2740
2741         if(status) {
2742                 make_bad_file(deffile, __LINE__);
2743         }
2744         else {
2745                 file = fopen(deffile, "w");
2746
2747                 if(!file) {
2748                         printf ("Unable to open file: %s\n", deffile);
2749                         status = 1;
2750                 }
2751                 else {
2752                         rna_generate_header_cpp(brna, file);
2753                         fclose(file);
2754                         status= (DefRNA.error != 0);
2755                 }
2756         }
2757
2758         replace_if_different(deffile, NULL);
2759
2760         rna_sort(brna);
2761
2762         /* create rna_gen_*.c files */
2763         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2764                 strcpy(deffile, outfile);
2765                 strcat(deffile, PROCESS_ITEMS[i].filename);
2766                 deffile[strlen(deffile)-2] = '\0';
2767                 strcat(deffile, "_gen.c" TMP_EXT);
2768
2769                 if(status) {
2770                         make_bad_file(deffile, __LINE__);
2771                 }
2772                 else {
2773                         file = fopen(deffile, "w");
2774
2775                         if(!file) {
2776                                 printf ("Unable to open file: %s\n", deffile);
2777                                 status = 1;
2778                         }
2779                         else {
2780                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2781                                 fclose(file);
2782                                 status= (DefRNA.error != 0);
2783                         }
2784                 }
2785
2786                 /* avoid unneeded rebuilds */
2787                 deps[0]= PROCESS_ITEMS[i].filename;
2788                 deps[1]= PROCESS_ITEMS[i].api_filename;
2789                 deps[2]= NULL;
2790
2791                 replace_if_different(deffile, deps);
2792         }
2793
2794         /* create RNA_blender.h */
2795         strcpy(deffile, outfile);
2796         strcat(deffile, "RNA_blender.h" TMP_EXT);
2797
2798         if(status) {
2799                 make_bad_file(deffile, __LINE__);
2800         }
2801         else {
2802                 file = fopen(deffile, "w");
2803
2804                 if(!file) {
2805                         printf ("Unable to open file: %s\n", deffile);
2806                         status = 1;
2807                 }
2808                 else {
2809                         rna_generate_header(brna, file);
2810                         fclose(file);
2811                         status= (DefRNA.error != 0);
2812                 }
2813         }
2814
2815         replace_if_different(deffile, NULL);
2816
2817         /* free RNA */
2818         RNA_define_free(brna);
2819         RNA_free(brna);
2820
2821         return status;
2822 }
2823
2824 static void mem_error_cb(const char *errorStr)
2825 {
2826         fprintf(stderr, "%s", errorStr);
2827         fflush(stderr);
2828 }
2829
2830 int main(int argc, char **argv)
2831 {
2832         int totblock, return_status = 0;
2833
2834         if(argc<2) {
2835                 printf("Usage: %s outdirectory/\n", argv[0]);
2836                 return_status = 1;
2837         }
2838         else {
2839                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2840                 makesrna_path= argv[0];
2841                 return_status= rna_preprocess(argv[1]);
2842         }
2843
2844         totblock= MEM_get_memory_blocks_in_use();
2845         if(totblock!=0) {
2846                 printf("Error Totblock: %d\n",totblock);
2847                 MEM_set_error_callback(mem_error_cb);
2848                 MEM_printmemlist();
2849         }
2850
2851         return return_status;
2852 }
2853
2854