RNA
[blender.git] / source / blender / makesrna / intern / makesrna.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
31 #include "MEM_guardedalloc.h"
32
33 #include "RNA_access.h"
34 #include "RNA_define.h"
35 #include "RNA_types.h"
36
37 #include "rna_internal.h"
38
39 #define RNA_VERSION_DATE "$Id$"
40
41 #ifdef _WIN32
42 #ifndef snprintf
43 #define snprintf _snprintf
44 #endif
45 #endif
46
47 /* Sorting */
48
49 static int cmp_struct(const void *a, const void *b)
50 {
51         const StructRNA *structa= *(const StructRNA**)a;
52         const StructRNA *structb= *(const StructRNA**)b;
53
54         return strcmp(structa->identifier, structb->identifier);
55 }
56
57 static int cmp_property(const void *a, const void *b)
58 {
59         const PropertyRNA *propa= *(const PropertyRNA**)a;
60         const PropertyRNA *propb= *(const PropertyRNA**)b;
61
62         if(strcmp(propa->identifier, "rna_type") == 0) return -1;
63         else if(strcmp(propb->identifier, "rna_type") == 0) return 1;
64
65         if(strcmp(propa->identifier, "name") == 0) return -1;
66         else if(strcmp(propb->identifier, "name") == 0) return 1;
67
68         return strcmp(propa->name, propb->name);
69 }
70
71 static int cmp_def_struct(const void *a, const void *b)
72 {
73         const StructDefRNA *dsa= *(const StructDefRNA**)a;
74         const StructDefRNA *dsb= *(const StructDefRNA**)b;
75
76         return cmp_struct(&dsa->srna, &dsb->srna);
77 }
78
79 static int cmp_def_property(const void *a, const void *b)
80 {
81         const PropertyDefRNA *dpa= *(const PropertyDefRNA**)a;
82         const PropertyDefRNA *dpb= *(const PropertyDefRNA**)b;
83
84         return cmp_property(&dpa->prop, &dpb->prop);
85 }
86
87 static void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
88 {
89         Link *link;
90         void **array;
91         int a, size;
92         
93         if(listbase->first == listbase->last)
94                 return;
95
96         for(size=0, link=listbase->first; link; link=link->next)
97                 size++;
98
99         array= MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
100         for(a=0, link=listbase->first; link; link=link->next, a++)
101                 array[a]= link;
102
103         qsort(array, size, sizeof(void*), cmp);
104
105         listbase->first= listbase->last= NULL;
106         for(a=0; a<size; a++) {
107                 link= array[a];
108                 link->next= link->prev= NULL;
109                 rna_addtail(listbase, link);
110         }
111
112         MEM_freeN(array);
113 }
114
115 /* Preprocessing */
116
117 static void rna_print_c_string(FILE *f, const char *str)
118 {
119         static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
120         int i, j;
121
122         fprintf(f, "\"");
123         for(i=0; str[i]; i++) {
124                 for(j=0; escape[j]; j++)
125                         if(str[i] == escape[j][0])
126                                 break;
127
128                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
129                 else fprintf(f, "%c", str[i]);
130         }
131         fprintf(f, "\"");
132 }
133
134 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
135 {
136         if(dp->dnastructfromname && dp->dnastructfromprop)
137                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
138         else
139                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
140 }
141
142 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
143 {
144         AllocDefRNA *alloc;
145         char buffer[2048];
146         char *result;
147
148         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
149         result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
150         strcpy(result, buffer);
151
152         alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
153         alloc->mem= result;
154         rna_addtail(&DefRNA.allocs, alloc);
155
156         return result;
157 }
158
159 static StructRNA *rna_find_struct(const char *identifier)
160 {
161         StructDefRNA *ds;
162
163         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
164                 if(strcmp(ds->srna->identifier, identifier)==0)
165                         return ds->srna;
166
167         return NULL;
168 }
169
170 static const char *rna_find_type(const char *type)
171 {
172         StructDefRNA *ds;
173
174         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
175                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
176                         return ds->srna->identifier;
177
178         return NULL;
179 }
180
181 static const char *rna_find_dna_type(const char *type)
182 {
183         StructDefRNA *ds;
184
185         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
186                 if(strcmp(ds->srna->identifier, type)==0)
187                         return ds->dnaname;
188
189         return NULL;
190 }
191
192 static const char *rna_type_type_name(PropertyRNA *prop)
193 {
194         switch(prop->type) {
195                 case PROP_BOOLEAN:
196                 case PROP_INT:
197                 case PROP_ENUM:
198                         return "int";
199                 case PROP_FLOAT:
200                         return "float";
201                 case PROP_STRING:
202                         return "char*";
203                 default:
204                         return NULL;
205         }
206 }
207
208 static const char *rna_type_type(PropertyRNA *prop)
209 {
210         const char *type;
211
212         type= rna_type_type_name(prop);
213
214         if(type)
215                 return type;
216
217         return "PointerRNA";
218 }
219
220 static const char *rna_type_struct(PropertyRNA *prop)
221 {
222         const char *type;
223
224         type= rna_type_type_name(prop);
225
226         if(type)
227                 return "";
228
229         return "struct ";
230 }
231
232 static const char *rna_parameter_type_name(PropertyRNA *parm)
233 {
234         const char *type;
235
236         type= rna_type_type_name(parm);
237
238         if(type)
239                 return type;
240
241         switch(parm->type) {
242                 case PROP_POINTER:  {
243                         PointerPropertyRNA *pparm= (PointerPropertyRNA*)parm;
244
245                         if(parm->flag & PROP_RNAPTR)
246                                 return "PointerRNA";
247                         else
248                                 return rna_find_dna_type((const char *)pparm->type);
249                 }
250                 case PROP_COLLECTION: {
251                         CollectionPropertyRNA *cparm= (CollectionPropertyRNA*)parm;
252                         return rna_find_dna_type((const char *)cparm->type);
253                 }
254                 default:
255                         return "<error, no type specified>";
256         }
257 }
258
259 static int rna_enum_bitmask(PropertyRNA *prop)
260 {
261         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
262         int a, mask= 0;
263
264         if(eprop->item) {
265                 for(a=0; a<eprop->totitem; a++)
266                         mask |= eprop->item[a].value;
267         }
268         
269         return mask;
270 }
271
272 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
273 {
274         if(prop->type == PROP_FLOAT && prop->subtype == PROP_COLOR)
275                 if(strcmp(dp->dnatype, "float") != 0 && strcmp(dp->dnatype, "double") != 0)
276                         return 1;
277         
278         return 0;
279 }
280
281 static const char *rna_function_string(void *func)
282 {
283         return (func)? (const char*)func: "NULL";
284 }
285
286 static void rna_float_print(FILE *f, float num)
287 {
288         if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
289         else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
290         else if((int)num == num) fprintf(f, "%.1ff", num);
291         else fprintf(f, "%.10ff", num);
292 }
293
294 static void rna_int_print(FILE *f, int num)
295 {
296         if(num == INT_MIN) fprintf(f, "INT_MIN");
297         else if(num == INT_MAX) fprintf(f, "INT_MAX");
298         else fprintf(f, "%d", num);
299 }
300
301 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
302 {
303         char *func;
304         int i;
305
306         if(prop->flag & PROP_IDPROPERTY)
307                 return NULL;
308         
309         if(!manualfunc) {
310                 if(!dp->dnastructname || !dp->dnaname) {
311                         fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
312                         DefRNA.error= 1;
313                         return NULL;
314                 }
315         }
316
317         func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
318
319         switch(prop->type) {
320                 case PROP_STRING: {
321                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
322                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
323                         fprintf(f, "{\n");
324                         if(manualfunc) {
325                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
326                         }
327                         else {
328                                 rna_print_data_get(f, dp);
329                                 if(sprop->maxlength)
330                                         fprintf(f, "    BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
331                                 else
332                                         fprintf(f, "    BLI_strncpy(value, data->%s, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
333                         }
334                         fprintf(f, "}\n\n");
335                         break;
336                 }
337                 case PROP_POINTER: {
338                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
339                         fprintf(f, "{\n");
340                         if(manualfunc) {
341                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
342                         }
343                         else {
344                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
345                                 rna_print_data_get(f, dp);
346                                 if(dp->dnapointerlevel == 0)
347                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (char*)pprop->type, dp->dnaname);
348                                 else
349                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (char*)pprop->type, dp->dnaname);
350                         }
351                         fprintf(f, "}\n\n");
352                         break;
353                 }
354                 case PROP_COLLECTION: {
355                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
356
357                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
358                         fprintf(f, "{\n");
359                         if(manualfunc) {
360                                 if(strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
361                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
362                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
363                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->type)? (char*)cprop->type: "UnknownType", manualfunc);
364                                 else
365                                         fprintf(f, "    return %s(iter);\n", manualfunc);
366                         }
367                         fprintf(f, "}\n\n");
368                         break;
369                 }
370                 default:
371                         if(prop->arraylength) {
372                                 fprintf(f, "void %s(PointerRNA *ptr, %s values[%d])\n", func, rna_type_type(prop), prop->arraylength);
373                                 fprintf(f, "{\n");
374
375                                 if(manualfunc) {
376                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
377                                 }
378                                 else {
379                                         rna_print_data_get(f, dp);
380
381                                         for(i=0; i<prop->arraylength; i++) {
382                                                 if(dp->dnaarraylength == 1) {
383                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit)
384                                                                 fprintf(f, "    values[%d]= (%s(data->%s & (%d<<%d)) != 0);\n", i, (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit, i);
385                                                         else
386                                                                 fprintf(f, "    values[%d]= (%s)%s((&data->%s)[%d]);\n", i, rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname, i);
387                                                 }
388                                                 else {
389                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
390                                                                 fprintf(f, "    values[%d]= (%s(data->%s[%d] & ", i, (dp->booleannegative)? "!": "", dp->dnaname, i);
391                                                                 rna_int_print(f, dp->booleanbit);
392                                                                 fprintf(f, ") != 0);\n");
393                                                         }
394                                                         else if(rna_color_quantize(prop, dp))
395                                                                 fprintf(f, "    values[%d]= (%s)(data->%s[%d]*(1.0f/255.0f));\n", i, rna_type_type(prop), dp->dnaname, i);
396                                                         else
397                                                                 fprintf(f, "    values[%d]= (%s)%s(((%s*)data->%s)[%d]);\n", i, rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnatype, dp->dnaname, i);
398                                                 }
399                                         }
400                                 }
401                                 fprintf(f, "}\n\n");
402                         }
403                         else {
404                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
405                                 fprintf(f, "{\n");
406
407                                 if(manualfunc) {
408                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
409                                 }
410                                 else {
411                                         rna_print_data_get(f, dp);
412                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
413                                                 fprintf(f, "    return (%s((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
414                                                 rna_int_print(f, dp->booleanbit);
415                                                 fprintf(f, ") != 0);\n");
416                                         }
417                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
418                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
419                                                 rna_int_print(f, rna_enum_bitmask(prop));
420                                                 fprintf(f, ");\n");
421                                         }
422                                         else
423                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
424                                 }
425
426                                 fprintf(f, "}\n\n");
427                         }
428                         break;
429         }
430
431         return func;
432 }
433
434 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array, int i)
435 {
436         if(prop->type == PROP_INT) {
437                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
438
439                 if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
440                         if(array) fprintf(f, "CLAMPIS(values[%d], ", i);
441                         else fprintf(f, "CLAMPIS(value, ");
442                         rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
443                         rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
444                         return;
445                 }
446         }
447         else if(prop->type == PROP_FLOAT) {
448                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
449
450                 if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
451                         if(array) fprintf(f, "CLAMPIS(values[%d], ", i);
452                         else fprintf(f, "CLAMPIS(value, ");
453                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
454                         rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
455                         return;
456                 }
457         }
458
459         if(array)
460                 fprintf(f, "values[%d];\n", i);
461         else
462                 fprintf(f, "value;\n");
463 }
464
465 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
466 {
467         char *func;
468         int i;
469
470         if(!(prop->flag & PROP_EDITABLE))
471                 return NULL;
472         if(prop->flag & PROP_IDPROPERTY)
473                 return NULL;
474
475         if(!manualfunc) {
476                 if(!dp->dnastructname || !dp->dnaname) {
477                         if(prop->flag & PROP_EDITABLE) {
478                                 fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
479                                 DefRNA.error= 1;
480                         }
481                         return NULL;
482                 }
483         }
484
485         func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");
486
487         switch(prop->type) {
488                 case PROP_STRING: {
489                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
490                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
491                         fprintf(f, "{\n");
492                         if(manualfunc) {
493                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
494                         }
495                         else {
496                                 rna_print_data_get(f, dp);
497                                 if(sprop->maxlength)
498                                         fprintf(f, "    BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
499                                 else
500                                         fprintf(f, "    BLI_strncpy(data->%s, value, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
501                         }
502                         fprintf(f, "}\n\n");
503                         break;
504                 }
505                 case PROP_POINTER: {
506                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
507                         fprintf(f, "{\n");
508                         if(manualfunc) {
509                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
510                         }
511                         else {
512                                 rna_print_data_get(f, dp);
513
514                                 if(prop->flag & PROP_ID_REFCOUNT) {
515                                         fprintf(f, "\n  if(data->%s)\n", dp->dnaname);
516                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
517                                         fprintf(f, "    if(value.data)\n");
518                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
519                                 }
520
521                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
522
523                         }
524                         fprintf(f, "}\n\n");
525                         break;
526                 }
527                 default:
528                         if(prop->arraylength) {
529                                 fprintf(f, "void %s(PointerRNA *ptr, const %s values[%d])\n", func, rna_type_type(prop), prop->arraylength);
530                                 fprintf(f, "{\n");
531
532                                 if(manualfunc) {
533                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
534                                 }
535                                 else {
536                                         rna_print_data_get(f, dp);
537
538                                         for(i=0; i<prop->arraylength; i++) {
539                                                 if(dp->dnaarraylength == 1) {
540                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
541                                                                 fprintf(f, "    if(%svalues[%d]) data->%s |= (%d<<%d);\n", (dp->booleannegative)? "!": "", i, dp->dnaname, dp->booleanbit, i);
542                                                                 fprintf(f, "    else data->%s &= ~(%d<<%d);\n", dp->dnaname, dp->booleanbit, i);
543                                                         }
544                                                         else {
545                                                                 fprintf(f, "    (&data->%s)[%d]= %s", dp->dnaname, i, (dp->booleannegative)? "!": "");
546                                                                 rna_clamp_value(f, prop, 1, i);
547                                                         }
548                                                 }
549                                                 else {
550                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
551                                                                 fprintf(f, "    if(%svalues[%d]) data->%s[%d] |= ", (dp->booleannegative)? "!": "", i, dp->dnaname, i);
552                                                                 rna_int_print(f, dp->booleanbit);
553                                                                 fprintf(f, ";\n");
554                                                                 fprintf(f, "    else data->%s[%d] &= ~", dp->dnaname, i);
555                                                                 rna_int_print(f, dp->booleanbit);
556                                                                 fprintf(f, ";\n");
557                                                         }
558                                                         else if(rna_color_quantize(prop, dp)) {
559                                                                 fprintf(f, "    data->%s[%d]= FTOCHAR(values[%d]);\n", dp->dnaname, i, i);
560                                                         }
561                                                         else {
562                                                                 fprintf(f, "    ((%s*)data->%s)[%d]= %s", dp->dnatype, dp->dnaname, i, (dp->booleannegative)? "!": "");
563                                                                 rna_clamp_value(f, prop, 1, i);
564                                                         }
565                                                 }
566                                         }
567                                 }
568                                 fprintf(f, "}\n\n");
569                         }
570                         else {
571                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
572                                 fprintf(f, "{\n");
573
574                                 if(manualfunc) {
575                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
576                                 }
577                                 else {
578                                         rna_print_data_get(f, dp);
579                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
580                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
581                                                 rna_int_print(f, dp->booleanbit);
582                                                 fprintf(f, ";\n");
583                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
584                                                 rna_int_print(f, dp->booleanbit);
585                                                 fprintf(f, ";\n");
586                                         }
587                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
588                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
589                                                 rna_int_print(f, rna_enum_bitmask(prop));
590                                                 fprintf(f, ";\n");
591                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
592                                         }
593                                         else {
594                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
595                                                 rna_clamp_value(f, prop, 0, 0);
596                                         }
597                                 }
598                                 fprintf(f, "}\n\n");
599                         }
600                         break;
601         }
602
603         return func;
604 }
605
606 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
607 {
608         char *func= NULL;
609
610         if(prop->flag & PROP_IDPROPERTY)
611                 return NULL;
612
613         if(prop->type == PROP_STRING) {
614                 if(!manualfunc) {
615                         if(!dp->dnastructname || !dp->dnaname) {
616                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
617                                 DefRNA.error= 1;
618                                 return NULL;
619                         }
620                 }
621
622                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
623
624                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
625                 fprintf(f, "{\n");
626                 if(manualfunc) {
627                         fprintf(f, "    return %s(ptr);\n", manualfunc);
628                 }
629                 else {
630                         rna_print_data_get(f, dp);
631                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
632                 }
633                 fprintf(f, "}\n\n");
634         }
635         else if(prop->type == PROP_COLLECTION) {
636                 if(!manualfunc) {
637                         if(prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
638                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
639                                 DefRNA.error= 1;
640                                 return NULL;
641                         }
642                 }
643
644                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
645
646                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
647                 fprintf(f, "{\n");
648                 if(manualfunc) {
649                         fprintf(f, "    return %s(ptr);\n", manualfunc);
650                 }
651                 else {
652                         rna_print_data_get(f, dp);
653                         if(dp->dnalengthname)
654                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
655                         else
656                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
657                 }
658                 fprintf(f, "}\n\n");
659         }
660
661         return func;
662 }
663
664 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
665 {
666         char *func, *getfunc;
667
668         if(prop->flag & PROP_IDPROPERTY)
669                 return NULL;
670
671         if(!manualfunc) {
672                 if(!dp->dnastructname || !dp->dnaname) {
673                         fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
674                         DefRNA.error= 1;
675                         return NULL;
676                 }
677         }
678
679         func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");
680
681         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
682         fprintf(f, "{\n");
683
684         if(!manualfunc)
685                 rna_print_data_get(f, dp);
686
687         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
688         fprintf(f, "    iter->parent= *ptr;\n");
689         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
690
691         if(dp->dnalengthname || dp->dnalengthfixed) {
692                 if(manualfunc) {
693                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
694                 }
695                 else {
696                         if(dp->dnalengthname)
697                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
698                         else
699                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
700                 }
701         }
702         else {
703                 if(manualfunc)
704                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
705                 else
706                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
707         }
708
709         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
710
711         fprintf(f, "\n  if(iter->valid)\n");
712         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
713
714         fprintf(f, "}\n\n");
715
716
717         return func;
718 }
719
720 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
721 {
722         char *func, *getfunc;
723
724         if(prop->flag & PROP_IDPROPERTY)
725                 return NULL;
726
727         if(!manualfunc)
728                 return NULL;
729
730         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
731
732         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
733         fprintf(f, "{\n");
734         fprintf(f, "    %s(iter);\n", manualfunc);
735
736         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
737
738         fprintf(f, "\n  if(iter->valid)\n");
739         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
740
741         fprintf(f, "}\n\n");
742
743         return func;
744 }
745
746 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
747 {
748         char *func;
749
750         if(prop->flag & PROP_IDPROPERTY)
751                 return NULL;
752
753         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
754
755         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
756         fprintf(f, "{\n");
757         if(manualfunc)
758                 fprintf(f, "    %s(iter);\n", manualfunc);
759         fprintf(f, "}\n\n");
760
761         return func;
762 }
763
764 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
765 {
766         PropertyRNA *prop;
767
768         prop= dp->prop;
769
770         switch(prop->type) {
771                 case PROP_BOOLEAN: {
772                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
773
774                         if(!prop->arraylength) {
775                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
776                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
777                         }
778                         else {
779                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
780                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
781                         }
782                         break;
783                 }
784                 case PROP_INT: {
785                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
786
787                         if(!prop->arraylength) {
788                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
789                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
790                         }
791                         else {
792                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
793                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
794                         }
795                         break;
796                 }
797                 case PROP_FLOAT: {
798                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
799
800                         if(!prop->arraylength) {
801                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
802                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
803                         }
804                         else {
805                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
806                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
807                         }
808                         break;
809                 }
810                 case PROP_ENUM: {
811                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
812
813                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
814                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
815                         break;
816                 }
817                 case PROP_STRING: {
818                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
819
820                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
821                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
822                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
823                         break;
824                 }
825                 case PROP_POINTER: {
826                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
827
828                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
829                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
830                         if(!pprop->type) {
831                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have a struct type.\n", srna->identifier, prop->identifier);
832                                 DefRNA.error= 1;
833                         }
834                         break;
835                 }
836                 case PROP_COLLECTION: {
837                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
838
839                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
840                         else if(dp->dnalengthname || dp->dnalengthfixed)
841                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
842
843                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
844                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
845                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
846                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
847
848                         if(!(prop->flag & PROP_IDPROPERTY)) {
849                                 if(!cprop->begin) {
850                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
851                                         DefRNA.error= 1;
852                                 }
853                                 if(!cprop->next) {
854                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
855                                         DefRNA.error= 1;
856                                 }
857                                 if(!cprop->get) {
858                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
859                                         DefRNA.error= 1;
860                                 }
861                         }
862                         if(!cprop->type) {
863                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a struct type.\n", srna->identifier, prop->identifier);
864                                 DefRNA.error= 1;
865                         }
866                         break;
867                 }
868         }
869 }
870
871 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
872 {
873         PropertyRNA *prop;
874         char *func;
875
876         prop= dp->prop;
877
878         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
879                 return;
880
881         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
882
883         switch(prop->type) {
884                 case PROP_BOOLEAN:
885                 case PROP_INT: {
886                         if(!prop->arraylength) {
887                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
888                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
889                         }
890                         else {
891                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->arraylength);
892                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
893                         }
894                         break;
895                 }
896                 case PROP_FLOAT: {
897                         if(!prop->arraylength) {
898                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
899                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
900                         }
901                         else {
902                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->arraylength);
903                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
904                         }
905                         break;
906                 }
907                 case PROP_ENUM: {
908                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
909                         int i;
910
911                         if(eprop->item) {
912                                 fprintf(f, "enum {\n");
913
914                                 for(i=0; i<eprop->totitem; i++)
915                                         fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
916
917                                 fprintf(f, "};\n\n");
918                         }
919
920                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
921                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
922
923                         break;
924                 }
925                 case PROP_STRING: {
926                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
927
928                         if(sprop->maxlength) {
929                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
930                         }
931                         
932                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
933                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
934                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
935
936                         break;
937                 }
938                 case PROP_POINTER: {
939                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
940                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
941                         break;
942                 }
943                 case PROP_COLLECTION: {
944                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
945                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
946                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
947                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
948                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
949                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
950                         break;
951                 }
952         }
953
954         fprintf(f, "\n");
955 }
956
957 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
958 {
959         PropertyRNA *prop;
960
961         prop= dp->prop;
962
963         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
964                 return;
965         
966         if(prop->name && prop->description && strcmp(prop->description, "") != 0)
967                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
968         else if(prop->name)
969                 fprintf(f, "\t/* %s */\n", prop->name);
970         else
971                 fprintf(f, "\t/* */\n");
972
973         switch(prop->type) {
974                 case PROP_BOOLEAN: {
975                         if(!prop->arraylength)
976                                 fprintf(f, "\tbool %s(void);", prop->identifier);
977                         else
978                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->arraylength, prop->identifier);
979                         break;
980                 }
981                 case PROP_INT: {
982                         if(!prop->arraylength)
983                                 fprintf(f, "\tint %s(void);", prop->identifier);
984                         else
985                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->arraylength, prop->identifier);
986                         break;
987                 }
988                 case PROP_FLOAT: {
989                         if(!prop->arraylength)
990                                 fprintf(f, "\tfloat %s(void);", prop->identifier);
991                         else
992                                 fprintf(f, "\tArray<float, %d> %s(void);", prop->arraylength, prop->identifier);
993                         break;
994                 }
995                 case PROP_ENUM: {
996                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
997                         int i;
998
999                         if(eprop->item) {
1000                                 fprintf(f, "\tenum %s_enum {\n", prop->identifier);
1001
1002                                 for(i=0; i<eprop->totitem; i++)
1003                                         fprintf(f, "\t\t%s_%s = %d,\n", prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1004
1005                                 fprintf(f, "\t};\n");
1006                         }
1007
1008                         fprintf(f, "\t%s_enum %s(void);", prop->identifier, prop->identifier);
1009                         break;
1010                 }
1011                 case PROP_STRING: {
1012                         fprintf(f, "\tstd::string %s(void);", prop->identifier);
1013                         break;
1014                 }
1015                 case PROP_POINTER: {
1016                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1017
1018                         if(pprop->type)
1019                                 fprintf(f, "\t%s %s(void);", (char*)pprop->type, prop->identifier);
1020                         else
1021                                 fprintf(f, "\t%s %s(void);", "UnknownType", prop->identifier);
1022                         break;
1023                 }
1024                 case PROP_COLLECTION: {
1025                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1026
1027                         if(cprop->type)
1028                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1029                         else
1030                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1031                         break;
1032                 }
1033         }
1034
1035         fprintf(f, "\n");
1036 }
1037
1038 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1039 {
1040         PropertyRNA *prop;
1041
1042         prop= dp->prop;
1043
1044         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1045                 return;
1046
1047         switch(prop->type) {
1048                 case PROP_BOOLEAN: {
1049                         if(!prop->arraylength)
1050                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1051                         else
1052                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1053                         break;
1054                 }
1055                 case PROP_INT: {
1056                         if(!prop->arraylength)
1057                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1058                         else
1059                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1060                         break;
1061                 }
1062                 case PROP_FLOAT: {
1063                         if(!prop->arraylength)
1064                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1065                         else
1066                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1067                         break;
1068                 }
1069                 case PROP_ENUM: {
1070                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", prop->identifier, srna->identifier, prop->identifier);
1071
1072                         break;
1073                 }
1074                 case PROP_STRING: {
1075                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1076                         break;
1077                 }
1078                 case PROP_POINTER: {
1079                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1080
1081                         if(pprop->type)
1082                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (char*)pprop->type, srna->identifier, prop->identifier);
1083                         else
1084                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1085                         break;
1086                 }
1087                 case PROP_COLLECTION: {
1088                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1089
1090                         if(cprop->type)
1091                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1092                         else
1093                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1094                         break;
1095                 }
1096         }
1097
1098         fprintf(f, "\n");
1099 }
1100
1101 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1102 {
1103         StructRNA *srna;
1104         FunctionRNA *func;
1105         PropertyDefRNA *dparm;
1106         char *funcname, *ptrstr;
1107         int first;
1108
1109         srna= dsrna->srna;
1110         func= dfunc->func;
1111
1112         if(func->flag & FUNC_REGISTER)
1113                 return;
1114
1115         funcname= rna_alloc_function_name(srna->identifier, func->identifier, "call");
1116
1117         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1118         fprintf(f, "\n{\n");
1119
1120         if((func->flag & FUNC_NO_SELF)==0) {
1121                 if(dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1122                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1123         }
1124
1125         dparm= dfunc->cont.properties.first;
1126         for(; dparm; dparm= dparm->next) {
1127                 ptrstr= (dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0)? "*" : "";
1128                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1129         }
1130
1131         fprintf(f, "\tchar *_data");
1132         if(func->ret) fprintf(f, ", *_retdata");
1133         fprintf(f, ";\n");
1134         fprintf(f, "\t\n");
1135
1136         if((func->flag & FUNC_NO_SELF)==0) {
1137                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1138                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1139         }
1140
1141         fprintf(f, "\t_data= (char *)_parms->data;\n");
1142
1143         dparm= dfunc->cont.properties.first;
1144         for(; dparm; dparm= dparm->next) {
1145                 if(dparm->prop==func->ret) 
1146                         fprintf(f, "\t_retdata= _data;\n");
1147                 else if(dparm->prop->arraylength)
1148                         fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1149                 else if(dparm->prop->type == PROP_POINTER) {
1150                         if(dparm->prop->flag & PROP_RNAPTR)
1151                                 fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1152                         else
1153                                 fprintf(f, "\t%s= *((%s%s**)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1154                 }
1155                 else
1156                         fprintf(f, "\t%s= *((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1157
1158                 if(dparm->next)
1159                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size(dparm->prop));
1160         }
1161
1162         if(dfunc->call) {
1163                 fprintf(f, "\t\n");
1164                 fprintf(f, "\t");
1165                 if(func->ret) fprintf(f, "%s= ", func->ret->identifier);
1166                 fprintf(f, "%s(", dfunc->call);
1167
1168                 first= 1;
1169
1170                 if((func->flag & FUNC_NO_SELF)==0) {
1171                         fprintf(f, "_self");
1172                         first= 0;
1173                 }
1174
1175                 if(func->flag & FUNC_USE_CONTEXT) {
1176                         if(!first) fprintf(f, ", ");
1177                         first= 0;
1178                         fprintf(f, "C");
1179                 }
1180
1181                 if(func->flag & FUNC_USE_REPORTS) {
1182                         if(!first) fprintf(f, ", ");
1183                         first= 0;
1184                         fprintf(f, "reports");
1185                 }
1186
1187                 dparm= dfunc->cont.properties.first;
1188                 for(; dparm; dparm= dparm->next) {
1189                         if(dparm->prop==func->ret)
1190                                 continue;
1191
1192                         if(!first) fprintf(f, ", ");
1193                         first= 0;
1194
1195                         fprintf(f, "%s", dparm->prop->identifier);
1196                 }
1197
1198                 fprintf(f, ");\n");
1199
1200                 if(func->ret) {
1201                         dparm= rna_find_parameter_def(func->ret);
1202                         ptrstr= dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0 ? "*" : "";
1203                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->ret->identifier);
1204                 }
1205         }
1206
1207         fprintf(f, "}\n\n");
1208
1209         dfunc->gencall= funcname;
1210 }
1211
1212 static void rna_auto_types()
1213 {
1214         StructDefRNA *ds;
1215         PropertyDefRNA *dp;
1216
1217         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1218                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1219                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1220                         ds->dnaname= "bScreen";
1221
1222                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1223                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1224                                 dp->dnastructname= "bScreen";
1225
1226                         if(dp->dnatype) {
1227                                 if(dp->prop->type == PROP_POINTER) {
1228                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1229                                         StructRNA *type;
1230
1231                                         if(!pprop->type && !pprop->get)
1232                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1233
1234                                         if(pprop->type) {
1235                                                 type= rna_find_struct((char*)pprop->type);
1236                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1237                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1238                                         }
1239                                 }
1240                                 else if(dp->prop->type== PROP_COLLECTION) {
1241                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1242
1243                                         if(!cprop->type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1244                                                 cprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1245                                 }
1246                         }
1247                 }
1248         }
1249 }
1250
1251 static void rna_sort(BlenderRNA *brna)
1252 {
1253         StructDefRNA *ds;
1254         StructRNA *srna;
1255
1256         rna_sortlist(&brna->structs, cmp_struct);
1257         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1258
1259         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1260                 rna_sortlist(&srna->cont.properties, cmp_property);
1261
1262         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1263                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1264 }
1265
1266 static const char *rna_property_structname(PropertyType type)
1267 {
1268         switch(type) {
1269                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1270                 case PROP_INT: return "IntPropertyRNA";
1271                 case PROP_FLOAT: return "FloatPropertyRNA";
1272                 case PROP_STRING: return "StringPropertyRNA";
1273                 case PROP_ENUM: return "EnumPropertyRNA";
1274                 case PROP_POINTER: return "PointerPropertyRNA";
1275                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1276                 default: return "UnknownPropertyRNA";
1277         }
1278 }
1279
1280 static const char *rna_property_typename(PropertyType type)
1281 {
1282         switch(type) {
1283                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1284                 case PROP_INT: return "PROP_INT";
1285                 case PROP_FLOAT: return "PROP_FLOAT";
1286                 case PROP_STRING: return "PROP_STRING";
1287                 case PROP_ENUM: return "PROP_ENUM";
1288                 case PROP_POINTER: return "PROP_POINTER";
1289                 case PROP_COLLECTION: return "PROP_COLLECTION";
1290                 default: return "PROP_UNKNOWN";
1291         }
1292 }
1293
1294 static const char *rna_property_subtypename(PropertyType type)
1295 {
1296         switch(type) {
1297                 case PROP_NONE: return "PROP_NONE";
1298                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1299                 case PROP_FILEPATH: return "PROP_FILEPATH";
1300                 case PROP_DIRPATH: return "PROP_DIRPATH";
1301                 case PROP_COLOR: return "PROP_COLOR";
1302                 case PROP_VECTOR: return "PROP_VECTOR";
1303                 case PROP_MATRIX: return "PROP_MATRIX";
1304                 case PROP_ROTATION: return "PROP_ROTATION";
1305                 case PROP_NEVER_NULL: return "PROP_NEVER_NULL";
1306                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1307                 default: return "PROP_UNKNOWN";
1308         }
1309 }
1310
1311 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1312 {
1313         StructRNA *srna;
1314
1315         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1316                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1317         fprintf(f, "\n");
1318 }
1319
1320 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1321 {
1322         StructRNA *srna;
1323
1324         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1325
1326         srna= brna->structs.first;
1327         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1328         else fprintf(f, "{NULL, ");
1329
1330         srna= brna->structs.last;
1331         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1332         else fprintf(f, "NULL}");
1333
1334         fprintf(f, "};\n\n");
1335 }
1336
1337 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1338 {
1339         PropertyRNA *prop;
1340         StructRNA *base;
1341
1342         base= srna->base;
1343         while (base) {
1344                 fprintf(f, "\n");
1345                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1346                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1347                 base= base->base;
1348         }
1349
1350         if(srna->cont.properties.first)
1351                 fprintf(f, "\n");
1352
1353         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1354                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1355         fprintf(f, "\n");
1356 }
1357
1358 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1359 {
1360         PropertyRNA *parm;
1361
1362         for(parm= func->cont.properties.first; parm; parm= parm->next)
1363                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1364
1365         if(func->cont.properties.first)
1366                 fprintf(f, "\n");
1367 }
1368
1369 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1370 {
1371         FunctionRNA *func;
1372         StructRNA *base;
1373
1374         base= srna->base;
1375         while (base) {
1376                 for(func= base->functions.first; func; func= func->cont.next) {
1377                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1378                         rna_generate_parameter_prototypes(brna, base, func, f);
1379                 }
1380
1381                 if(base->functions.first)
1382                         fprintf(f, "\n");
1383
1384                 base= base->base;
1385         }
1386
1387         for(func= srna->functions.first; func; func= func->cont.next) {
1388                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1389                 rna_generate_parameter_prototypes(brna, srna, func, f);
1390         }
1391
1392         if(srna->functions.first)
1393                 fprintf(f, "\n");
1394 }
1395
1396 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1397 {
1398         FunctionRNA *func;
1399         PropertyDefRNA *dparm;
1400         StructDefRNA *dsrna;
1401         int first;
1402
1403         dsrna= rna_find_struct_def(srna);
1404         func= dfunc->func;
1405
1406         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1407                 if(dparm->prop==func->ret) {
1408                         if(dparm->prop->arraylength)
1409                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1410                         else if(dparm->prop->type == PROP_POINTER)
1411                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1412                         else
1413                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1414
1415                         break;
1416                 }
1417         }
1418
1419         if(!dparm)
1420                 fprintf(f, "void ");
1421
1422         fprintf(f, "%s(", dfunc->call);
1423
1424         first= 1;
1425
1426         if((func->flag & FUNC_NO_SELF)==0) {
1427                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1428                 else fprintf(f, "struct %s *_self", srna->identifier);
1429                 first= 0;
1430         }
1431
1432         if(func->flag & FUNC_USE_CONTEXT) {
1433                 if(!first) fprintf(f, ", ");
1434                 first= 0;
1435                 fprintf(f, "bContext *C");
1436         }
1437
1438         if(func->flag & FUNC_USE_REPORTS) {
1439                 if(!first) fprintf(f, ", ");
1440                 first= 0;
1441                 fprintf(f, "ReportList *reports");
1442         }
1443
1444         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1445                 if(dparm->prop==func->ret)
1446                         continue;
1447
1448                 if(!first) fprintf(f, ", ");
1449                 first= 0;
1450
1451                 if(dparm->prop->arraylength)
1452                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->arraylength);
1453                 else if(dparm->prop->type == PROP_POINTER)
1454                         fprintf(f, "%s%s *%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1455                 else
1456                         fprintf(f, "%s%s %s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1457         }
1458
1459         fprintf(f, ");\n");
1460 }
1461
1462 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1463 {
1464         FunctionRNA *func;
1465         FunctionDefRNA *dfunc;
1466
1467         fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1468
1469         for(func= srna->functions.first; func; func= func->cont.next) {
1470                 if(func->flag & FUNC_REGISTER)
1471                         continue;
1472
1473                 dfunc= rna_find_function_def(func);
1474                 if(dfunc->call)
1475                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1476         }
1477
1478         fprintf(f, "\n");
1479 }
1480
1481 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1482 {
1483         char *strnest= "", *errnest= "";
1484         int len, freenest= 0;
1485         
1486         if(nest != NULL) {
1487                 len= strlen(nest);
1488
1489                 strnest= MEM_mallocN(sizeof(char)*(len+1), "rna_generate_property -> strnest");
1490                 errnest= MEM_mallocN(sizeof(char)*(len+1), "rna_generate_property -> errnest");
1491
1492                 strcpy(strnest, "_"); strcat(strnest, nest);
1493                 strcpy(errnest, "."); strcat(errnest, nest);
1494
1495                 freenest= 1;
1496         }
1497
1498         switch(prop->type) {
1499                         case PROP_ENUM: {
1500                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1501                                 int i, defaultfound= 0;
1502
1503                                 if(eprop->item) {
1504                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {", srna->identifier, strnest, prop->identifier, eprop->totitem);
1505
1506                                         for(i=0; i<eprop->totitem; i++) {
1507                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1508                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1509                                                 fprintf(f, "%d, ", eprop->item[i].icon);
1510                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1511                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
1512                                                 if(i != eprop->totitem-1)
1513                                                         fprintf(f, ", ");
1514
1515                                                 if(eprop->defaultvalue == eprop->item[i].value)
1516                                                         defaultfound= 1;
1517                                         }
1518
1519                                         fprintf(f, "};\n\n");
1520
1521                                         if(!defaultfound) {
1522                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1523                                                 DefRNA.error= 1;
1524                                         }
1525                                 }
1526                                 else if(eprop->itemf);
1527                                 else {
1528                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1529                                         DefRNA.error= 1;
1530                                 }
1531                                 break;
1532                                                                                         }
1533                         case PROP_BOOLEAN: {
1534                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1535                                 unsigned int i;
1536
1537                                 if(prop->arraylength) {
1538                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1539
1540                                         for(i=0; i<prop->arraylength; i++) {
1541                                                 if(bprop->defaultarray)
1542                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1543                                                 else
1544                                                         fprintf(f, "%d", bprop->defaultvalue);
1545                                                 if(i != prop->arraylength-1)
1546                                                         fprintf(f, ", ");
1547                                         }
1548
1549                                         fprintf(f, "};\n\n");
1550                                 }
1551                                 break;
1552                                                                                                  }
1553                         case PROP_INT: {
1554                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1555                                 unsigned int i;
1556
1557                                 if(prop->arraylength) {
1558                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1559
1560                                         for(i=0; i<prop->arraylength; i++) {
1561                                                 if(iprop->defaultarray)
1562                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1563                                                 else
1564                                                         fprintf(f, "%d", iprop->defaultvalue);
1565                                                 if(i != prop->arraylength-1)
1566                                                         fprintf(f, ", ");
1567                                         }
1568
1569                                         fprintf(f, "};\n\n");
1570                                 }
1571                                 break;
1572                                                                                  }
1573                         case PROP_FLOAT: {
1574                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1575                                 unsigned int i;
1576
1577                                 if(prop->arraylength) {
1578                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1579
1580                                         for(i=0; i<prop->arraylength; i++) {
1581                                                 if(fprop->defaultarray)
1582                                                         rna_float_print(f, fprop->defaultarray[i]);
1583                                                 else
1584                                                         rna_float_print(f, fprop->defaultvalue);
1585                                                 if(i != prop->arraylength-1)
1586                                                         fprintf(f, ", ");
1587                                         }
1588
1589                                         fprintf(f, "};\n\n");
1590                                 }
1591                                 break;
1592                                                                                          }
1593                         default:
1594                                 break;
1595         }
1596
1597         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
1598
1599         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
1600         else fprintf(f, "\t{NULL, ");
1601         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
1602         else fprintf(f, "NULL,\n");
1603         fprintf(f, "\t%d, ", prop->magic);
1604         rna_print_c_string(f, prop->identifier);
1605         fprintf(f, ", %d, ", prop->flag);
1606         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
1607         rna_print_c_string(f, prop->description); fprintf(f, ",\n");
1608         fprintf(f, "%d, ", prop->icon);
1609         fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
1610         fprintf(f, "\t%s, %d, %s},\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
1611
1612         switch(prop->type) {
1613                         case PROP_BOOLEAN: {
1614                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1615                                 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);
1616                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1617                                 else fprintf(f, "NULL\n");
1618                                 break;
1619                                                                                                  }
1620                         case PROP_INT: {
1621                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1622                                 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));
1623                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
1624                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
1625                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
1626                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
1627                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
1628                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
1629                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1630                                 else fprintf(f, "NULL\n");
1631                                 break;
1632                                                                                  }
1633                         case PROP_FLOAT: {
1634                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1635                                 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));
1636                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
1637                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
1638                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
1639                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
1640                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
1641                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
1642                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
1643                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1644                                 else fprintf(f, "NULL\n");
1645                                 break;
1646                                                                                          }
1647                         case PROP_STRING: {
1648                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1649                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
1650                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
1651                                 break;
1652                                                                                                 }
1653                         case PROP_ENUM: {
1654                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1655                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
1656                                 if(eprop->item)
1657                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
1658                                 else
1659                                         fprintf(f, "NULL, ");
1660                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
1661                                 break;
1662                                                                                         }
1663                         case PROP_POINTER: {
1664                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1665                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef));
1666                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
1667                                 else fprintf(f, "NULL\n");
1668                                 break;
1669                                                                                                  }
1670                         case PROP_COLLECTION: {
1671                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1672                                 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));
1673                                 if(cprop->add) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->add);
1674                                 else fprintf(f, "NULL, ");
1675                                 if(cprop->remove) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->remove);
1676                                 else fprintf(f, "NULL, ");
1677                                 if(cprop->type) fprintf(f, "&RNA_%s\n", (char*)cprop->type);
1678                                 else fprintf(f, "NULL\n");
1679                                 break;
1680                         }
1681         }
1682
1683         fprintf(f, "};\n\n");
1684
1685         if(freenest) {
1686                 MEM_freeN(strnest);
1687                 MEM_freeN(errnest);
1688         }
1689 }
1690
1691 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
1692 {
1693         FunctionRNA *func;
1694         FunctionDefRNA *dfunc;
1695         PropertyRNA *prop, *parm;
1696         StructRNA *base;
1697
1698         fprintf(f, "/* %s */\n", srna->name);
1699
1700         for(prop= srna->cont.properties.first; prop; prop= prop->next)
1701                 rna_generate_property(f, srna, NULL, prop);
1702
1703         for(func= srna->functions.first; func; func= func->cont.next) {
1704                 for(parm= func->cont.properties.first; parm; parm= parm->next)
1705                         rna_generate_property(f, srna, func->identifier, parm);
1706
1707                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
1708
1709                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
1710                 else fprintf(f, "\t{NULL, ");
1711                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
1712                 else fprintf(f, "NULL,\n");
1713
1714                 parm= func->cont.properties.first;
1715                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
1716                 else fprintf(f, "\t{NULL, ");
1717
1718                 parm= func->cont.properties.last;
1719                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
1720                 else fprintf(f, "NULL}},\n");
1721
1722                 fprintf(f, "\t");
1723                 rna_print_c_string(f, func->identifier);
1724                 fprintf(f, ", %d, ", func->flag);
1725                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
1726
1727                 dfunc= rna_find_function_def(func);
1728                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
1729                 else fprintf(f, "\tNULL,\n");
1730         
1731                 if(func->ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->ret->identifier);
1732                 else fprintf(f, "\tNULL\n");
1733
1734                 fprintf(f, "};\n");
1735                 fprintf(f, "\n");
1736         }
1737
1738         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
1739
1740         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
1741         else fprintf(f, "\t{NULL, ");
1742         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
1743         else fprintf(f, "NULL,\n");
1744
1745         prop= srna->cont.properties.first;
1746         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
1747         else fprintf(f, "\t{NULL, ");
1748
1749         prop= srna->cont.properties.last;
1750         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
1751         else fprintf(f, "NULL}},\n");
1752
1753         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
1754         
1755         fprintf(f, "\t");
1756         rna_print_c_string(f, srna->identifier);
1757         fprintf(f, ", %d, ", srna->flag);
1758         rna_print_c_string(f, srna->name);
1759         fprintf(f, ", ");
1760         rna_print_c_string(f, srna->description);
1761         fprintf(f, ",\n %d,\n", srna->icon);
1762
1763         prop= srna->nameproperty;
1764         if(prop) {
1765                 base= srna;
1766                 while (base->base && base->base->nameproperty==prop)
1767                         base= base->base;
1768
1769                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
1770         }
1771         else fprintf(f, "\tNULL, ");
1772
1773         prop= srna->iteratorproperty;
1774         base= srna;
1775         while (base->base && base->base->iteratorproperty==prop)
1776                 base= base->base;
1777         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
1778
1779         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
1780         else fprintf(f, "\tNULL,\n");
1781
1782         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
1783         else fprintf(f, "\tNULL,\n");
1784
1785         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
1786         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
1787         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
1788         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
1789         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
1790
1791         if(srna->reg && !srna->refine) {
1792                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
1793                 DefRNA.error= 1;
1794         }
1795
1796         func= srna->functions.first;
1797         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
1798         else fprintf(f, "\t{NULL, ");
1799
1800         func= srna->functions.last;
1801         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
1802         else fprintf(f, "NULL}\n");
1803
1804         fprintf(f, "};\n");
1805
1806         fprintf(f, "\n");
1807 }
1808
1809 typedef struct RNAProcessItem {
1810         char *filename;
1811         char *api_filename;
1812         void (*define)(BlenderRNA *brna);
1813 } RNAProcessItem;
1814
1815 RNAProcessItem PROCESS_ITEMS[]= {
1816         {"rna_rna.c", NULL, RNA_def_rna},
1817         {"rna_ID.c", NULL, RNA_def_ID},
1818         {"rna_texture.c", NULL, RNA_def_texture},
1819         {"rna_action.c", NULL, RNA_def_action},
1820         {"rna_animation.c", NULL, RNA_def_animation},
1821         {"rna_actuator.c", NULL, RNA_def_actuator},
1822         {"rna_armature.c", NULL, RNA_def_armature},
1823         {"rna_brush.c", NULL, RNA_def_brush},
1824         {"rna_camera.c", NULL, RNA_def_camera},
1825         {"rna_cloth.c", NULL, RNA_def_cloth},
1826         {"rna_color.c", NULL, RNA_def_color},
1827         {"rna_constraint.c", NULL, RNA_def_constraint},
1828         {"rna_context.c", NULL, RNA_def_context},
1829         {"rna_controller.c", NULL, RNA_def_controller},
1830         {"rna_curve.c", NULL, RNA_def_curve},
1831         {"rna_fcurve.c", NULL, RNA_def_fcurve},
1832         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
1833         {"rna_group.c", NULL, RNA_def_group},
1834         {"rna_image.c", NULL, RNA_def_image},
1835         {"rna_key.c", NULL, RNA_def_key},
1836         {"rna_lamp.c", NULL, RNA_def_lamp},
1837         {"rna_lattice.c", NULL, RNA_def_lattice},
1838         {"rna_main.c", "rna_main_api.c", RNA_def_main},
1839         {"rna_material.c", NULL, RNA_def_material},
1840         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
1841         {"rna_meta.c", NULL, RNA_def_meta},
1842         {"rna_modifier.c", NULL, RNA_def_modifier},
1843         {"rna_nodetree.c", NULL, RNA_def_nodetree},
1844         {"rna_object.c", "rna_object_api.c", RNA_def_object},
1845         {"rna_object_force.c", NULL, RNA_def_object_force},
1846         {"rna_packedfile.c", NULL, RNA_def_packedfile},
1847         {"rna_particle.c", NULL, RNA_def_particle},
1848         {"rna_pose.c", NULL, RNA_def_pose},
1849         {"rna_property.c", NULL, RNA_def_gameproperty},
1850         {"rna_radio.c", NULL, RNA_def_radio},
1851         {"rna_scene.c", NULL, RNA_def_scene},
1852         {"rna_screen.c", NULL, RNA_def_screen},
1853         {"rna_scriptlink.c", NULL, RNA_def_scriptlink},
1854         {"rna_sensor.c", NULL, RNA_def_sensor},
1855         {"rna_sequence.c", NULL, RNA_def_sequence},
1856         {"rna_space.c", NULL, RNA_def_space},
1857         {"rna_text.c", NULL, RNA_def_text},
1858         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
1859         {"rna_sound.c", NULL, RNA_def_sound},
1860         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
1861         {"rna_userdef.c", NULL, RNA_def_userdef},
1862         {"rna_vfont.c", NULL, RNA_def_vfont},
1863         {"rna_vpaint.c", NULL, RNA_def_vpaint},
1864         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
1865         {"rna_world.c", NULL, RNA_def_world},   
1866         {NULL, NULL}};
1867
1868 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename, char *api_filename)
1869 {
1870         StructDefRNA *ds;
1871         PropertyDefRNA *dp;
1872         FunctionDefRNA *dfunc;
1873         
1874         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
1875                      "   Do not edit manually, changes will be overwritten.           */\n\n"
1876                       "#define RNA_RUNTIME\n\n");
1877
1878         fprintf(f, "#include <float.h>\n");
1879         fprintf(f, "#include <limits.h>\n");
1880         fprintf(f, "#include <string.h>\n\n");
1881
1882         fprintf(f, "#include \"DNA_ID.h\"\n");
1883
1884         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
1885
1886         fprintf(f, "#include \"BKE_context.h\"\n");
1887         fprintf(f, "#include \"BKE_library.h\"\n");
1888         fprintf(f, "#include \"BKE_report.h\"\n");
1889         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
1890
1891         fprintf(f, "#include \"RNA_define.h\"\n");
1892         fprintf(f, "#include \"RNA_types.h\"\n");
1893         fprintf(f, "#include \"rna_internal.h\"\n\n");
1894
1895         rna_generate_prototypes(brna, f);
1896
1897         fprintf(f, "#include \"%s\"\n", filename);
1898         if(api_filename)
1899                 fprintf(f, "#include \"%s\"\n", api_filename);
1900         fprintf(f, "\n");
1901
1902         fprintf(f, "/* Autogenerated Functions */\n\n");
1903
1904         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1905                 if(!filename || ds->filename == filename) {
1906                         rna_generate_property_prototypes(brna, ds->srna, f);
1907                         rna_generate_function_prototypes(brna, ds->srna, f);
1908                 }
1909         }
1910
1911         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1912                 if(!filename || ds->filename == filename)
1913                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
1914                                 rna_def_property_funcs(f, ds->srna, dp);
1915
1916         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1917                 if(!filename || ds->filename == filename) {
1918                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
1919                                 rna_def_function_funcs(f, ds, dfunc);
1920
1921                         rna_generate_static_function_prototypes(brna, ds->srna, f);
1922                 }
1923         }
1924
1925         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1926                 if(!filename || ds->filename == filename)
1927                         rna_generate_struct(brna, ds->srna, f);
1928
1929         if(strcmp(filename, "rna_ID.c") == 0) {
1930                 /* this is ugly, but we cannot have c files compiled for both
1931                  * makesrna and blender with some build systems at the moment */
1932                 fprintf(f, "#include \"rna_define.c\"\n\n");
1933
1934                 rna_generate_blender(brna, f);
1935         }
1936 }
1937
1938 static void rna_generate_header(BlenderRNA *brna, FILE *f)
1939 {
1940         StructDefRNA *ds;
1941         PropertyDefRNA *dp;
1942         StructRNA *srna;
1943
1944         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
1945         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
1946
1947         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
1948                      "   Do not edit manually, changes will be overwritten.              */\n\n");
1949
1950         fprintf(f, "#include \"RNA_types.h\"\n\n");
1951
1952         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
1953
1954         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
1955         fprintf(f, "    { \\\n");
1956         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
1957         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
1958         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
1959
1960         fprintf(f, "#define FOREACH_END(property) \\\n");
1961         fprintf(f, "            } \\\n");
1962         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
1963         fprintf(f, "    }\n\n");
1964
1965         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1966                 srna= ds->srna;
1967
1968                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
1969
1970                 while(srna) {
1971                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1972                         srna= srna->base;
1973                 }
1974                 fprintf(f, "\n");
1975
1976                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
1977                         rna_def_property_funcs_header(f, ds->srna, dp);
1978         }
1979
1980         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
1981
1982         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
1983 }
1984
1985 static const char *cpp_classes = ""
1986 "\n"
1987 "#include <string>\n"
1988 "\n"
1989 "namespace RNA {\n"
1990 "\n"
1991 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
1992 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
1993 "\n"
1994 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
1995 "       Array<int,size> sname::identifier(void) \\\n"
1996 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
1997 "\n"
1998 "#define INT_PROPERTY(sname, identifier) \\\n"
1999 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2000 "\n"
2001 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2002 "       Array<int,size> sname::identifier(void) \\\n"
2003 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2004 "\n"
2005 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2006 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2007 "\n"
2008 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2009 "       Array<float,size> sname::identifier(void) \\\n"
2010 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2011 "\n"
2012 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2013 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2014 "\n"
2015 "#define STRING_PROPERTY(sname, identifier) \\\n"
2016 "       std::string sname::identifier(void) { \\\n"
2017 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2018 "               std::string str; str.resize(len); \\\n"
2019 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2020 "\n"
2021 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2022 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2023 "\n"
2024 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2025 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2026 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2027 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2028 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2029 "\n"
2030 "class Pointer {\n"
2031 "public:\n"
2032 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2033 "       operator const PointerRNA&() { return ptr; }\n"
2034 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2035 "       operator void*() { return ptr.data; }\n"
2036 "       operator bool() { return ptr.data != NULL; }\n"
2037 "\n"
2038 "       PointerRNA ptr;\n"
2039 "};\n"
2040 "\n"
2041 "\n"
2042 "template<typename T, int Tsize>\n"
2043 "class Array {\n"
2044 "public:\n"
2045 "       T data[Tsize];\n"
2046 "       operator T*() { return data; }\n"
2047 "};\n"
2048 "\n"
2049 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2050 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2051 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2052 "\n"
2053 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2054 "class CollectionIterator {\n"
2055 "public:\n"
2056 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2057 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2058 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2059 "       { 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"
2060 "\n"
2061 "       operator bool(void)\n"
2062 "       { return iter.valid != 0; }\n"
2063 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2064 "       T& operator*(void) { return t; }\n"
2065 "       T* operator->(void) { return &t; }\n"
2066 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2067 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2068 "\n"
2069 "       void begin(const Pointer& ptr)\n"
2070 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2071 "\n"
2072 "private:\n"
2073 "       CollectionPropertyIterator iter;\n"
2074 "       T t;\n"
2075 "       bool init;\n"
2076 "};\n"
2077 "\n"
2078 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2079 "class Collection {\n"
2080 "public:\n"
2081 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2082 "\n"
2083 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2084 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2085 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2086 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2087 "\n"
2088 "private:\n"
2089 "       PointerRNA ptr;\n"
2090 "};\n"
2091 "\n";
2092
2093 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2094 {
2095         StructDefRNA *ds;
2096         PropertyDefRNA *dp;
2097         StructRNA *srna;
2098
2099         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2100         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2101
2102         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2103                      "   Do not edit manually, changes will be overwritten. */\n\n");
2104
2105         fprintf(f, "#include \"RNA_blender.h\"\n");
2106         fprintf(f, "#include \"RNA_types.h\"\n");
2107
2108         fprintf(f, cpp_classes);
2109
2110         fprintf(f, "/**************** Declarations ****************/\n\n");
2111
2112         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2113                 fprintf(f, "class %s;\n", ds->srna->identifier);
2114         fprintf(f, "\n");
2115
2116         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2117                 srna= ds->srna;
2118
2119                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2120
2121                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2122                 fprintf(f, "public:\n");
2123                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2124                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2125                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2126                                 if(dp->prop->type == PROP_COLLECTION)
2127                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2128                 fprintf(f, "\n\t\t{}\n\n");
2129
2130                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2131                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2132                 fprintf(f, "};\n\n");
2133         }
2134
2135
2136         fprintf(f, "/**************** Implementation ****************/\n");
2137
2138         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2139                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2140                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2141
2142                 fprintf(f, "\n");
2143         }
2144
2145         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2146 }
2147
2148 static void make_bad_file(char *file)
2149 {
2150         FILE *fp= fopen(file, "w");
2151         fprintf(fp, "ERROR! Cannot make correct RNA file, STUPID!\n");
2152         fclose(fp);
2153 }
2154
2155 static int rna_preprocess(char *outfile)
2156 {
2157         BlenderRNA *brna;
2158         StructDefRNA *ds;
2159         FILE *file;
2160         char deffile[4096];
2161         int i, status;
2162
2163         /* define rna */
2164         brna= RNA_create();
2165
2166         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2167                 if(PROCESS_ITEMS[i].define) {
2168                         PROCESS_ITEMS[i].define(brna);
2169
2170                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2171                                 if(!ds->filename)
2172                                         ds->filename= PROCESS_ITEMS[i].filename;
2173                 }
2174         }
2175
2176         rna_auto_types();
2177
2178
2179         /* create RNA_blender_cpp.h */
2180         strcpy(deffile, outfile);
2181         strcat(deffile, "RNA_blender_cpp.h");
2182
2183         status= (DefRNA.error != 0);
2184
2185         if(status) {
2186                 make_bad_file(deffile);
2187         }
2188         else {
2189                 file = fopen(deffile, "w");
2190
2191                 if(!file) {
2192                         printf ("Unable to open file: %s\n", deffile);
2193                         status = 1;
2194                 }
2195                 else {
2196                         rna_generate_header_cpp(brna, file);
2197                         fclose(file);
2198
2199                         status= (DefRNA.error != 0);
2200                 }
2201         }
2202
2203         rna_sort(brna);
2204
2205         /* create rna_gen_*.c files */
2206         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2207                 strcpy(deffile, outfile);
2208                 strcat(deffile, PROCESS_ITEMS[i].filename);
2209                 deffile[strlen(deffile)-2] = '\0';
2210                 strcat(deffile, "_gen.c");
2211
2212                 if(status) {
2213                         make_bad_file(deffile);
2214                 }
2215                 else {
2216                         file = fopen(deffile, "w");
2217
2218                         if(!file) {
2219                                 printf ("Unable to open file: %s\n", deffile);
2220                                 status = 1;
2221                         }
2222                         else {
2223                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2224                                 fclose(file);
2225
2226                                 status= (DefRNA.error != 0);
2227                         }
2228                 }
2229         }
2230
2231         /* create RNA_blender.h */
2232         strcpy(deffile, outfile);
2233         strcat(deffile, "RNA_blender.h");
2234
2235         if(status) {
2236                 make_bad_file(deffile);
2237         }
2238         else {
2239                 file = fopen(deffile, "w");
2240
2241                 if(!file) {
2242                         printf ("Unable to open file: %s\n", deffile);
2243                         status = 1;
2244                 }
2245                 else {
2246                         rna_generate_header(brna, file);
2247                         fclose(file);
2248
2249                         status= (DefRNA.error != 0);
2250                 }
2251         }
2252
2253         /* free RNA */
2254         RNA_define_free(brna);
2255         RNA_free(brna);
2256
2257         return status;
2258 }
2259
2260 static void mem_error_cb(char *errorStr)
2261 {
2262         fprintf(stderr, "%s", errorStr);
2263         fflush(stderr);
2264 }
2265
2266 int main(int argc, char **argv)
2267 {
2268         int totblock, return_status = 0;
2269
2270         if(argc<2) {
2271                 printf("Usage: %s outdirectory/\n", argv[0]);
2272                 return_status = 1;
2273         }
2274         else {
2275                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2276                 return_status= rna_preprocess(argv[1]);
2277         }
2278
2279         totblock= MEM_get_memory_blocks_in_use();
2280         if(totblock!=0) {
2281                 printf("Error Totblock: %d\n",totblock);
2282                 MEM_set_error_callback(mem_error_cb);
2283                 MEM_printmemlist();
2284         }
2285
2286         return return_status;
2287 }
2288
2289