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