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