Merge from 2.5 r20991 through r21037
[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 if(dp->dnapointerlevel == 0)
706                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
707                 else
708                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
709         }
710
711         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
712
713         fprintf(f, "\n  if(iter->valid)\n");
714         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
715
716         fprintf(f, "}\n\n");
717
718
719         return func;
720 }
721
722 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
723 {
724         char *func, *getfunc;
725
726         if(prop->flag & PROP_IDPROPERTY)
727                 return NULL;
728
729         if(!manualfunc)
730                 return NULL;
731
732         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
733
734         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
735         fprintf(f, "{\n");
736         fprintf(f, "    %s(iter);\n", manualfunc);
737
738         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
739
740         fprintf(f, "\n  if(iter->valid)\n");
741         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
742
743         fprintf(f, "}\n\n");
744
745         return func;
746 }
747
748 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
749 {
750         char *func;
751
752         if(prop->flag & PROP_IDPROPERTY)
753                 return NULL;
754
755         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
756
757         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
758         fprintf(f, "{\n");
759         if(manualfunc)
760                 fprintf(f, "    %s(iter);\n", manualfunc);
761         fprintf(f, "}\n\n");
762
763         return func;
764 }
765
766 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
767 {
768         PropertyRNA *prop;
769
770         prop= dp->prop;
771
772         switch(prop->type) {
773                 case PROP_BOOLEAN: {
774                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
775
776                         if(!prop->arraylength) {
777                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
778                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
779                         }
780                         else {
781                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
782                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
783                         }
784                         break;
785                 }
786                 case PROP_INT: {
787                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
788
789                         if(!prop->arraylength) {
790                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
791                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
792                         }
793                         else {
794                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
795                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
796                         }
797                         break;
798                 }
799                 case PROP_FLOAT: {
800                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
801
802                         if(!prop->arraylength) {
803                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
804                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
805                         }
806                         else {
807                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
808                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
809                         }
810                         break;
811                 }
812                 case PROP_ENUM: {
813                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
814
815                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
816                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
817                         break;
818                 }
819                 case PROP_STRING: {
820                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
821
822                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
823                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
824                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
825                         break;
826                 }
827                 case PROP_POINTER: {
828                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
829
830                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
831                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
832                         if(!pprop->type) {
833                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have a struct type.\n", srna->identifier, prop->identifier);
834                                 DefRNA.error= 1;
835                         }
836                         break;
837                 }
838                 case PROP_COLLECTION: {
839                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
840
841                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
842                         else if(dp->dnalengthname || dp->dnalengthfixed)
843                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
844
845                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
846                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
847                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
848                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
849
850                         if(!(prop->flag & PROP_IDPROPERTY)) {
851                                 if(!cprop->begin) {
852                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
853                                         DefRNA.error= 1;
854                                 }
855                                 if(!cprop->next) {
856                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
857                                         DefRNA.error= 1;
858                                 }
859                                 if(!cprop->get) {
860                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
861                                         DefRNA.error= 1;
862                                 }
863                         }
864                         if(!cprop->type) {
865                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a struct type.\n", srna->identifier, prop->identifier);
866                                 DefRNA.error= 1;
867                         }
868                         break;
869                 }
870         }
871 }
872
873 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
874 {
875         PropertyRNA *prop;
876         char *func;
877
878         prop= dp->prop;
879
880         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
881                 return;
882
883         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
884
885         switch(prop->type) {
886                 case PROP_BOOLEAN:
887                 case PROP_INT: {
888                         if(!prop->arraylength) {
889                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
890                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
891                         }
892                         else {
893                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->arraylength);
894                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
895                         }
896                         break;
897                 }
898                 case PROP_FLOAT: {
899                         if(!prop->arraylength) {
900                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
901                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
902                         }
903                         else {
904                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->arraylength);
905                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
906                         }
907                         break;
908                 }
909                 case PROP_ENUM: {
910                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
911                         int i;
912
913                         if(eprop->item) {
914                                 fprintf(f, "enum {\n");
915
916                                 for(i=0; i<eprop->totitem; i++)
917                                         fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
918
919                                 fprintf(f, "};\n\n");
920                         }
921
922                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
923                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
924
925                         break;
926                 }
927                 case PROP_STRING: {
928                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
929
930                         if(sprop->maxlength) {
931                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
932                         }
933                         
934                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
935                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
936                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
937
938                         break;
939                 }
940                 case PROP_POINTER: {
941                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
942                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
943                         break;
944                 }
945                 case PROP_COLLECTION: {
946                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
947                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
948                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
949                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
950                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
951                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
952                         break;
953                 }
954         }
955
956         fprintf(f, "\n");
957 }
958
959 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
960 {
961         PropertyRNA *prop;
962
963         prop= dp->prop;
964
965         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
966                 return;
967         
968         if(prop->name && prop->description && strcmp(prop->description, "") != 0)
969                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
970         else if(prop->name)
971                 fprintf(f, "\t/* %s */\n", prop->name);
972         else
973                 fprintf(f, "\t/* */\n");
974
975         switch(prop->type) {
976                 case PROP_BOOLEAN: {
977                         if(!prop->arraylength)
978                                 fprintf(f, "\tbool %s(void);", prop->identifier);
979                         else
980                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->arraylength, prop->identifier);
981                         break;
982                 }
983                 case PROP_INT: {
984                         if(!prop->arraylength)
985                                 fprintf(f, "\tint %s(void);", prop->identifier);
986                         else
987                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->arraylength, prop->identifier);
988                         break;
989                 }
990                 case PROP_FLOAT: {
991                         if(!prop->arraylength)
992                                 fprintf(f, "\tfloat %s(void);", prop->identifier);
993                         else
994                                 fprintf(f, "\tArray<float, %d> %s(void);", prop->arraylength, prop->identifier);
995                         break;
996                 }
997                 case PROP_ENUM: {
998                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
999                         int i;
1000
1001                         if(eprop->item) {
1002                                 fprintf(f, "\tenum %s_enum {\n", prop->identifier);
1003
1004                                 for(i=0; i<eprop->totitem; i++)
1005                                         fprintf(f, "\t\t%s_%s = %d,\n", prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1006
1007                                 fprintf(f, "\t};\n");
1008                         }
1009
1010                         fprintf(f, "\t%s_enum %s(void);", prop->identifier, prop->identifier);
1011                         break;
1012                 }
1013                 case PROP_STRING: {
1014                         fprintf(f, "\tstd::string %s(void);", prop->identifier);
1015                         break;
1016                 }
1017                 case PROP_POINTER: {
1018                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1019
1020                         if(pprop->type)
1021                                 fprintf(f, "\t%s %s(void);", (char*)pprop->type, prop->identifier);
1022                         else
1023                                 fprintf(f, "\t%s %s(void);", "UnknownType", prop->identifier);
1024                         break;
1025                 }
1026                 case PROP_COLLECTION: {
1027                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1028
1029                         if(cprop->type)
1030                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1031                         else
1032                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1033                         break;
1034                 }
1035         }
1036
1037         fprintf(f, "\n");
1038 }
1039
1040 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1041 {
1042         PropertyRNA *prop;
1043
1044         prop= dp->prop;
1045
1046         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1047                 return;
1048
1049         switch(prop->type) {
1050                 case PROP_BOOLEAN: {
1051                         if(!prop->arraylength)
1052                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1053                         else
1054                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1055                         break;
1056                 }
1057                 case PROP_INT: {
1058                         if(!prop->arraylength)
1059                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1060                         else
1061                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1062                         break;
1063                 }
1064                 case PROP_FLOAT: {
1065                         if(!prop->arraylength)
1066                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1067                         else
1068                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1069                         break;
1070                 }
1071                 case PROP_ENUM: {
1072                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", prop->identifier, srna->identifier, prop->identifier);
1073
1074                         break;
1075                 }
1076                 case PROP_STRING: {
1077                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1078                         break;
1079                 }
1080                 case PROP_POINTER: {
1081                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1082
1083                         if(pprop->type)
1084                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (char*)pprop->type, srna->identifier, prop->identifier);
1085                         else
1086                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1087                         break;
1088                 }
1089                 case PROP_COLLECTION: {
1090                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1091
1092                         if(cprop->type)
1093                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1094                         else
1095                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1096                         break;
1097                 }
1098         }
1099
1100         fprintf(f, "\n");
1101 }
1102
1103 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1104 {
1105         StructRNA *srna;
1106         FunctionRNA *func;
1107         PropertyDefRNA *dparm;
1108         char *funcname, *ptrstr;
1109         int first;
1110
1111         srna= dsrna->srna;
1112         func= dfunc->func;
1113
1114         if(func->flag & FUNC_REGISTER)
1115                 return;
1116
1117         funcname= rna_alloc_function_name(srna->identifier, func->identifier, "call");
1118
1119         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1120         fprintf(f, "\n{\n");
1121
1122         if((func->flag & FUNC_NO_SELF)==0) {
1123                 if(dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1124                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1125         }
1126
1127         dparm= dfunc->cont.properties.first;
1128         for(; dparm; dparm= dparm->next) {
1129                 ptrstr= (dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0)? "*" : "";
1130                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1131         }
1132
1133         fprintf(f, "\tchar *_data");
1134         if(func->ret) fprintf(f, ", *_retdata");
1135         fprintf(f, ";\n");
1136         fprintf(f, "\t\n");
1137
1138         if((func->flag & FUNC_NO_SELF)==0) {
1139                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1140                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1141         }
1142
1143         fprintf(f, "\t_data= (char *)_parms->data;\n");
1144
1145         dparm= dfunc->cont.properties.first;
1146         for(; dparm; dparm= dparm->next) {
1147                 if(dparm->prop==func->ret) 
1148                         fprintf(f, "\t_retdata= _data;\n");
1149                 else if(dparm->prop->arraylength)
1150                         fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1151                 else if(dparm->prop->type == PROP_POINTER) {
1152                         if(dparm->prop->flag & PROP_RNAPTR)
1153                                 fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1154                         else
1155                                 fprintf(f, "\t%s= *((%s%s**)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1156                 }
1157                 else
1158                         fprintf(f, "\t%s= *((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1159
1160                 if(dparm->next)
1161                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size(dparm->prop));
1162         }
1163
1164         if(dfunc->call) {
1165                 fprintf(f, "\t\n");
1166                 fprintf(f, "\t");
1167                 if(func->ret) fprintf(f, "%s= ", func->ret->identifier);
1168                 fprintf(f, "%s(", dfunc->call);
1169
1170                 first= 1;
1171
1172                 if((func->flag & FUNC_NO_SELF)==0) {
1173                         fprintf(f, "_self");
1174                         first= 0;
1175                 }
1176
1177                 if(func->flag & FUNC_USE_CONTEXT) {
1178                         if(!first) fprintf(f, ", ");
1179                         first= 0;
1180                         fprintf(f, "C");
1181                 }
1182
1183                 if(func->flag & FUNC_USE_REPORTS) {
1184                         if(!first) fprintf(f, ", ");
1185                         first= 0;
1186                         fprintf(f, "reports");
1187                 }
1188
1189                 dparm= dfunc->cont.properties.first;
1190                 for(; dparm; dparm= dparm->next) {
1191                         if(dparm->prop==func->ret)
1192                                 continue;
1193
1194                         if(!first) fprintf(f, ", ");
1195                         first= 0;
1196
1197                         fprintf(f, "%s", dparm->prop->identifier);
1198                 }
1199
1200                 fprintf(f, ");\n");
1201
1202                 if(func->ret) {
1203                         dparm= rna_find_parameter_def(func->ret);
1204                         ptrstr= dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0 ? "*" : "";
1205                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->ret->identifier);
1206                 }
1207         }
1208
1209         fprintf(f, "}\n\n");
1210
1211         dfunc->gencall= funcname;
1212 }
1213
1214 static void rna_auto_types()
1215 {
1216         StructDefRNA *ds;
1217         PropertyDefRNA *dp;
1218
1219         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1220                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1221                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1222                         ds->dnaname= "bScreen";
1223
1224                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1225                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1226                                 dp->dnastructname= "bScreen";
1227
1228                         if(dp->dnatype) {
1229                                 if(dp->prop->type == PROP_POINTER) {
1230                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1231                                         StructRNA *type;
1232
1233                                         if(!pprop->type && !pprop->get)
1234                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1235
1236                                         if(pprop->type) {
1237                                                 type= rna_find_struct((char*)pprop->type);
1238                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1239                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1240                                         }
1241                                 }
1242                                 else if(dp->prop->type== PROP_COLLECTION) {
1243                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1244
1245                                         if(!cprop->type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1246                                                 cprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1247                                 }
1248                         }
1249                 }
1250         }
1251 }
1252
1253 static void rna_sort(BlenderRNA *brna)
1254 {
1255         StructDefRNA *ds;
1256         StructRNA *srna;
1257
1258         rna_sortlist(&brna->structs, cmp_struct);
1259         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1260
1261         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1262                 rna_sortlist(&srna->cont.properties, cmp_property);
1263
1264         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1265                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1266 }
1267
1268 static const char *rna_property_structname(PropertyType type)
1269 {
1270         switch(type) {
1271                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1272                 case PROP_INT: return "IntPropertyRNA";
1273                 case PROP_FLOAT: return "FloatPropertyRNA";
1274                 case PROP_STRING: return "StringPropertyRNA";
1275                 case PROP_ENUM: return "EnumPropertyRNA";
1276                 case PROP_POINTER: return "PointerPropertyRNA";
1277                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1278                 default: return "UnknownPropertyRNA";
1279         }
1280 }
1281
1282 static const char *rna_property_typename(PropertyType type)
1283 {
1284         switch(type) {
1285                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1286                 case PROP_INT: return "PROP_INT";
1287                 case PROP_FLOAT: return "PROP_FLOAT";
1288                 case PROP_STRING: return "PROP_STRING";
1289                 case PROP_ENUM: return "PROP_ENUM";
1290                 case PROP_POINTER: return "PROP_POINTER";
1291                 case PROP_COLLECTION: return "PROP_COLLECTION";
1292                 default: return "PROP_UNKNOWN";
1293         }
1294 }
1295
1296 static const char *rna_property_subtypename(PropertyType type)
1297 {
1298         switch(type) {
1299                 case PROP_NONE: return "PROP_NONE";
1300                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1301                 case PROP_FILEPATH: return "PROP_FILEPATH";
1302                 case PROP_DIRPATH: return "PROP_DIRPATH";
1303                 case PROP_COLOR: return "PROP_COLOR";
1304                 case PROP_VECTOR: return "PROP_VECTOR";
1305                 case PROP_MATRIX: return "PROP_MATRIX";
1306                 case PROP_ROTATION: return "PROP_ROTATION";
1307                 case PROP_NEVER_NULL: return "PROP_NEVER_NULL";
1308                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1309                 default: return "PROP_UNKNOWN";
1310         }
1311 }
1312
1313 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1314 {
1315         StructRNA *srna;
1316
1317         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1318                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1319         fprintf(f, "\n");
1320 }
1321
1322 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1323 {
1324         StructRNA *srna;
1325
1326         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1327
1328         srna= brna->structs.first;
1329         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1330         else fprintf(f, "{NULL, ");
1331
1332         srna= brna->structs.last;
1333         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1334         else fprintf(f, "NULL}");
1335
1336         fprintf(f, "};\n\n");
1337 }
1338
1339 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1340 {
1341         PropertyRNA *prop;
1342         StructRNA *base;
1343
1344         base= srna->base;
1345         while (base) {
1346                 fprintf(f, "\n");
1347                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1348                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1349                 base= base->base;
1350         }
1351
1352         if(srna->cont.properties.first)
1353                 fprintf(f, "\n");
1354
1355         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1356                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1357         fprintf(f, "\n");
1358 }
1359
1360 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1361 {
1362         PropertyRNA *parm;
1363
1364         for(parm= func->cont.properties.first; parm; parm= parm->next)
1365                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1366
1367         if(func->cont.properties.first)
1368                 fprintf(f, "\n");
1369 }
1370
1371 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1372 {
1373         FunctionRNA *func;
1374         StructRNA *base;
1375
1376         base= srna->base;
1377         while (base) {
1378                 for(func= base->functions.first; func; func= func->cont.next) {
1379                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1380                         rna_generate_parameter_prototypes(brna, base, func, f);
1381                 }
1382
1383                 if(base->functions.first)
1384                         fprintf(f, "\n");
1385
1386                 base= base->base;
1387         }
1388
1389         for(func= srna->functions.first; func; func= func->cont.next) {
1390                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1391                 rna_generate_parameter_prototypes(brna, srna, func, f);
1392         }
1393
1394         if(srna->functions.first)
1395                 fprintf(f, "\n");
1396 }
1397
1398 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1399 {
1400         FunctionRNA *func;
1401         PropertyDefRNA *dparm;
1402         StructDefRNA *dsrna;
1403         int first;
1404
1405         dsrna= rna_find_struct_def(srna);
1406         func= dfunc->func;
1407
1408         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1409                 if(dparm->prop==func->ret) {
1410                         if(dparm->prop->arraylength)
1411                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1412                         else if(dparm->prop->type == PROP_POINTER)
1413                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1414                         else
1415                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1416
1417                         break;
1418                 }
1419         }
1420
1421         if(!dparm)
1422                 fprintf(f, "void ");
1423
1424         fprintf(f, "%s(", dfunc->call);
1425
1426         first= 1;
1427
1428         if((func->flag & FUNC_NO_SELF)==0) {
1429                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1430                 else fprintf(f, "struct %s *_self", srna->identifier);
1431                 first= 0;
1432         }
1433
1434         if(func->flag & FUNC_USE_CONTEXT) {
1435                 if(!first) fprintf(f, ", ");
1436                 first= 0;
1437                 fprintf(f, "bContext *C");
1438         }
1439
1440         if(func->flag & FUNC_USE_REPORTS) {
1441                 if(!first) fprintf(f, ", ");
1442                 first= 0;
1443                 fprintf(f, "ReportList *reports");
1444         }
1445
1446         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1447                 if(dparm->prop==func->ret)
1448                         continue;
1449
1450                 if(!first) fprintf(f, ", ");
1451                 first= 0;
1452
1453                 if(dparm->prop->arraylength)
1454                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->arraylength);
1455                 else if(dparm->prop->type == PROP_POINTER)
1456                         fprintf(f, "%s%s *%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1457                 else
1458                         fprintf(f, "%s%s %s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1459         }
1460
1461         fprintf(f, ");\n");
1462 }
1463
1464 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1465 {
1466         FunctionRNA *func;
1467         FunctionDefRNA *dfunc;
1468
1469         fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1470
1471         for(func= srna->functions.first; func; func= func->cont.next) {
1472                 if(func->flag & FUNC_REGISTER)
1473                         continue;
1474
1475                 dfunc= rna_find_function_def(func);
1476                 if(dfunc->call)
1477                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1478         }
1479
1480         fprintf(f, "\n");
1481 }
1482
1483 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1484 {
1485         char *strnest= "", *errnest= "";
1486         int len, freenest= 0;
1487         
1488         if(nest != NULL) {
1489                 len= strlen(nest);
1490
1491                 strnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
1492                 errnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
1493
1494                 strcpy(strnest, "_"); strcat(strnest, nest);
1495                 strcpy(errnest, "."); strcat(errnest, nest);
1496
1497                 freenest= 1;
1498         }
1499
1500         switch(prop->type) {
1501                         case PROP_ENUM: {
1502                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1503                                 int i, defaultfound= 0;
1504
1505                                 if(eprop->item) {
1506                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {", srna->identifier, strnest, prop->identifier, eprop->totitem);
1507
1508                                         for(i=0; i<eprop->totitem; i++) {
1509                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1510                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1511                                                 fprintf(f, "%d, ", eprop->item[i].icon);
1512                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1513                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
1514                                                 if(i != eprop->totitem-1)
1515                                                         fprintf(f, ", ");
1516
1517                                                 if(eprop->defaultvalue == eprop->item[i].value)
1518                                                         defaultfound= 1;
1519                                         }
1520
1521                                         fprintf(f, "};\n\n");
1522
1523                                         if(!defaultfound) {
1524                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1525                                                 DefRNA.error= 1;
1526                                         }
1527                                 }
1528                                 else if(eprop->itemf);
1529                                 else {
1530                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1531                                         DefRNA.error= 1;
1532                                 }
1533                                 break;
1534                                                                                         }
1535                         case PROP_BOOLEAN: {
1536                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1537                                 unsigned int i;
1538
1539                                 if(prop->arraylength) {
1540                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1541
1542                                         for(i=0; i<prop->arraylength; i++) {
1543                                                 if(bprop->defaultarray)
1544                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1545                                                 else
1546                                                         fprintf(f, "%d", bprop->defaultvalue);
1547                                                 if(i != prop->arraylength-1)
1548                                                         fprintf(f, ", ");
1549                                         }
1550
1551                                         fprintf(f, "};\n\n");
1552                                 }
1553                                 break;
1554                                                                                                  }
1555                         case PROP_INT: {
1556                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1557                                 unsigned int i;
1558
1559                                 if(prop->arraylength) {
1560                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1561
1562                                         for(i=0; i<prop->arraylength; i++) {
1563                                                 if(iprop->defaultarray)
1564                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1565                                                 else
1566                                                         fprintf(f, "%d", iprop->defaultvalue);
1567                                                 if(i != prop->arraylength-1)
1568                                                         fprintf(f, ", ");
1569                                         }
1570
1571                                         fprintf(f, "};\n\n");
1572                                 }
1573                                 break;
1574                                                                                  }
1575                         case PROP_FLOAT: {
1576                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1577                                 unsigned int i;
1578
1579                                 if(prop->arraylength) {
1580                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1581
1582                                         for(i=0; i<prop->arraylength; i++) {
1583                                                 if(fprop->defaultarray)
1584                                                         rna_float_print(f, fprop->defaultarray[i]);
1585                                                 else
1586                                                         rna_float_print(f, fprop->defaultvalue);
1587                                                 if(i != prop->arraylength-1)
1588                                                         fprintf(f, ", ");
1589                                         }
1590
1591                                         fprintf(f, "};\n\n");
1592                                 }
1593                                 break;
1594                                                                                          }
1595                         default:
1596                                 break;
1597         }
1598
1599         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
1600
1601         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
1602         else fprintf(f, "\t{NULL, ");
1603         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
1604         else fprintf(f, "NULL,\n");
1605         fprintf(f, "\t%d, ", prop->magic);
1606         rna_print_c_string(f, prop->identifier);
1607         fprintf(f, ", %d, ", prop->flag);
1608         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
1609         rna_print_c_string(f, prop->description); fprintf(f, ",\n");
1610         fprintf(f, "%d, ", prop->icon);
1611         fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
1612         fprintf(f, "\t%s, %d, %s},\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
1613
1614         switch(prop->type) {
1615                         case PROP_BOOLEAN: {
1616                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1617                                 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);
1618                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1619                                 else fprintf(f, "NULL\n");
1620                                 break;
1621                                                                                                  }
1622                         case PROP_INT: {
1623                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1624                                 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));
1625                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
1626                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
1627                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
1628                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
1629                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
1630                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
1631                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1632                                 else fprintf(f, "NULL\n");
1633                                 break;
1634                                                                                  }
1635                         case PROP_FLOAT: {
1636                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1637                                 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));
1638                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
1639                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
1640                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
1641                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
1642                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
1643                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
1644                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
1645                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1646                                 else fprintf(f, "NULL\n");
1647                                 break;
1648                                                                                          }
1649                         case PROP_STRING: {
1650                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1651                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
1652                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
1653                                 break;
1654                                                                                                 }
1655                         case PROP_ENUM: {
1656                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1657                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
1658                                 if(eprop->item)
1659                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
1660                                 else
1661                                         fprintf(f, "NULL, ");
1662                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
1663                                 break;
1664                                                                                         }
1665                         case PROP_POINTER: {
1666                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1667                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef));
1668                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
1669                                 else fprintf(f, "NULL\n");
1670                                 break;
1671                                                                                                  }
1672                         case PROP_COLLECTION: {
1673                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1674                                 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));
1675                                 if(cprop->add) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->add);
1676                                 else fprintf(f, "NULL, ");
1677                                 if(cprop->remove) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->remove);
1678                                 else fprintf(f, "NULL, ");
1679                                 if(cprop->type) fprintf(f, "&RNA_%s\n", (char*)cprop->type);
1680                                 else fprintf(f, "NULL\n");
1681                                 break;
1682                         }
1683         }
1684
1685         fprintf(f, "};\n\n");
1686
1687         if(freenest) {
1688                 MEM_freeN(strnest);
1689                 MEM_freeN(errnest);
1690         }
1691 }
1692
1693 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
1694 {
1695         FunctionRNA *func;
1696         FunctionDefRNA *dfunc;
1697         PropertyRNA *prop, *parm;
1698         StructRNA *base;
1699
1700         fprintf(f, "/* %s */\n", srna->name);
1701
1702         for(prop= srna->cont.properties.first; prop; prop= prop->next)
1703                 rna_generate_property(f, srna, NULL, prop);
1704
1705         for(func= srna->functions.first; func; func= func->cont.next) {
1706                 for(parm= func->cont.properties.first; parm; parm= parm->next)
1707                         rna_generate_property(f, srna, func->identifier, parm);
1708
1709                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
1710
1711                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
1712                 else fprintf(f, "\t{NULL, ");
1713                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
1714                 else fprintf(f, "NULL,\n");
1715
1716                 fprintf(f, "\tNULL,\n");
1717
1718                 parm= func->cont.properties.first;
1719                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
1720                 else fprintf(f, "\t{NULL, ");
1721
1722                 parm= func->cont.properties.last;
1723                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
1724                 else fprintf(f, "NULL}},\n");
1725
1726                 fprintf(f, "\t");
1727                 rna_print_c_string(f, func->identifier);
1728                 fprintf(f, ", %d, ", func->flag);
1729                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
1730
1731                 dfunc= rna_find_function_def(func);
1732                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
1733                 else fprintf(f, "\tNULL,\n");
1734         
1735                 if(func->ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->ret->identifier);
1736                 else fprintf(f, "\tNULL\n");
1737
1738                 fprintf(f, "};\n");
1739                 fprintf(f, "\n");
1740         }
1741
1742         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
1743
1744         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
1745         else fprintf(f, "\t{NULL, ");
1746         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
1747         else fprintf(f, "NULL,\n");
1748
1749         fprintf(f, "\tNULL,\n");
1750
1751         prop= srna->cont.properties.first;
1752         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
1753         else fprintf(f, "\t{NULL, ");
1754
1755         prop= srna->cont.properties.last;
1756         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
1757         else fprintf(f, "NULL}},\n");
1758
1759         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
1760         
1761         fprintf(f, "\t");
1762         rna_print_c_string(f, srna->identifier);
1763         fprintf(f, ", %d, ", srna->flag);
1764         rna_print_c_string(f, srna->name);
1765         fprintf(f, ", ");
1766         rna_print_c_string(f, srna->description);
1767         fprintf(f, ",\n %d,\n", srna->icon);
1768
1769         prop= srna->nameproperty;
1770         if(prop) {
1771                 base= srna;
1772                 while (base->base && base->base->nameproperty==prop)
1773                         base= base->base;
1774
1775                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
1776         }
1777         else fprintf(f, "\tNULL, ");
1778
1779         prop= srna->iteratorproperty;
1780         base= srna;
1781         while (base->base && base->base->iteratorproperty==prop)
1782                 base= base->base;
1783         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
1784
1785         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
1786         else fprintf(f, "\tNULL,\n");
1787
1788         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
1789         else fprintf(f, "\tNULL,\n");
1790
1791         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
1792         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
1793         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
1794         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
1795         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
1796
1797         if(srna->reg && !srna->refine) {
1798                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
1799                 DefRNA.error= 1;
1800         }
1801
1802         func= srna->functions.first;
1803         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
1804         else fprintf(f, "\t{NULL, ");
1805
1806         func= srna->functions.last;
1807         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
1808         else fprintf(f, "NULL}\n");
1809
1810         fprintf(f, "};\n");
1811
1812         fprintf(f, "\n");
1813 }
1814
1815 typedef struct RNAProcessItem {
1816         char *filename;
1817         char *api_filename;
1818         void (*define)(BlenderRNA *brna);
1819 } RNAProcessItem;
1820
1821 RNAProcessItem PROCESS_ITEMS[]= {
1822         {"rna_rna.c", NULL, RNA_def_rna},
1823         {"rna_ID.c", NULL, RNA_def_ID},
1824         {"rna_texture.c", NULL, RNA_def_texture},
1825         {"rna_action.c", NULL, RNA_def_action},
1826         {"rna_animation.c", NULL, RNA_def_animation},
1827         {"rna_actuator.c", NULL, RNA_def_actuator},
1828         {"rna_armature.c", NULL, RNA_def_armature},
1829         {"rna_brush.c", NULL, RNA_def_brush},
1830         {"rna_camera.c", NULL, RNA_def_camera},
1831         {"rna_cloth.c", NULL, RNA_def_cloth},
1832         {"rna_color.c", NULL, RNA_def_color},
1833         {"rna_constraint.c", NULL, RNA_def_constraint},
1834         {"rna_context.c", NULL, RNA_def_context},
1835         {"rna_controller.c", NULL, RNA_def_controller},
1836         {"rna_curve.c", NULL, RNA_def_curve},
1837         {"rna_fcurve.c", NULL, RNA_def_fcurve},
1838         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
1839         {"rna_group.c", NULL, RNA_def_group},
1840         {"rna_image.c", NULL, RNA_def_image},
1841         {"rna_key.c", NULL, RNA_def_key},
1842         {"rna_lamp.c", NULL, RNA_def_lamp},
1843         {"rna_lattice.c", NULL, RNA_def_lattice},
1844         {"rna_main.c", "rna_main_api.c", RNA_def_main},
1845         {"rna_material.c", NULL, RNA_def_material},
1846         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
1847         {"rna_meta.c", NULL, RNA_def_meta},
1848         {"rna_modifier.c", NULL, RNA_def_modifier},
1849         {"rna_nodetree.c", NULL, RNA_def_nodetree},
1850         {"rna_object.c", "rna_object_api.c", RNA_def_object},
1851         {"rna_object_force.c", NULL, RNA_def_object_force},
1852         {"rna_packedfile.c", NULL, RNA_def_packedfile},
1853         {"rna_particle.c", NULL, RNA_def_particle},
1854         {"rna_pose.c", NULL, RNA_def_pose},
1855         {"rna_property.c", NULL, RNA_def_gameproperty},
1856         {"rna_radio.c", NULL, RNA_def_radio},
1857         {"rna_scene.c", NULL, RNA_def_scene},
1858         {"rna_screen.c", NULL, RNA_def_screen},
1859         {"rna_scriptlink.c", NULL, RNA_def_scriptlink},
1860         {"rna_sensor.c", NULL, RNA_def_sensor},
1861         {"rna_sequence.c", NULL, RNA_def_sequence},
1862         {"rna_space.c", NULL, RNA_def_space},
1863         {"rna_text.c", NULL, RNA_def_text},
1864         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
1865         {"rna_sound.c", NULL, RNA_def_sound},
1866         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
1867         {"rna_userdef.c", NULL, RNA_def_userdef},
1868         {"rna_vfont.c", NULL, RNA_def_vfont},
1869         {"rna_vpaint.c", NULL, RNA_def_vpaint},
1870         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
1871         {"rna_world.c", NULL, RNA_def_world},   
1872         {NULL, NULL}};
1873
1874 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename, char *api_filename)
1875 {
1876         StructDefRNA *ds;
1877         PropertyDefRNA *dp;
1878         FunctionDefRNA *dfunc;
1879         
1880         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
1881                      "   Do not edit manually, changes will be overwritten.           */\n\n"
1882                       "#define RNA_RUNTIME\n\n");
1883
1884         fprintf(f, "#include <float.h>\n");
1885         fprintf(f, "#include <limits.h>\n");
1886         fprintf(f, "#include <string.h>\n\n");
1887
1888         fprintf(f, "#include \"DNA_ID.h\"\n");
1889
1890         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
1891
1892         fprintf(f, "#include \"BKE_context.h\"\n");
1893         fprintf(f, "#include \"BKE_library.h\"\n");
1894         fprintf(f, "#include \"BKE_report.h\"\n");
1895         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
1896
1897         fprintf(f, "#include \"RNA_define.h\"\n");
1898         fprintf(f, "#include \"RNA_types.h\"\n");
1899         fprintf(f, "#include \"rna_internal.h\"\n\n");
1900
1901         rna_generate_prototypes(brna, f);
1902
1903         fprintf(f, "#include \"%s\"\n", filename);
1904         if(api_filename)
1905                 fprintf(f, "#include \"%s\"\n", api_filename);
1906         fprintf(f, "\n");
1907
1908         fprintf(f, "/* Autogenerated Functions */\n\n");
1909
1910         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1911                 if(!filename || ds->filename == filename) {
1912                         rna_generate_property_prototypes(brna, ds->srna, f);
1913                         rna_generate_function_prototypes(brna, ds->srna, f);
1914                 }
1915         }
1916
1917         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1918                 if(!filename || ds->filename == filename)
1919                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
1920                                 rna_def_property_funcs(f, ds->srna, dp);
1921
1922         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1923                 if(!filename || ds->filename == filename) {
1924                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
1925                                 rna_def_function_funcs(f, ds, dfunc);
1926
1927                         rna_generate_static_function_prototypes(brna, ds->srna, f);
1928                 }
1929         }
1930
1931         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1932                 if(!filename || ds->filename == filename)
1933                         rna_generate_struct(brna, ds->srna, f);
1934
1935         if(strcmp(filename, "rna_ID.c") == 0) {
1936                 /* this is ugly, but we cannot have c files compiled for both
1937                  * makesrna and blender with some build systems at the moment */
1938                 fprintf(f, "#include \"rna_define.c\"\n\n");
1939
1940                 rna_generate_blender(brna, f);
1941         }
1942 }
1943
1944 static void rna_generate_header(BlenderRNA *brna, FILE *f)
1945 {
1946         StructDefRNA *ds;
1947         PropertyDefRNA *dp;
1948         StructRNA *srna;
1949
1950         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
1951         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
1952
1953         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
1954                      "   Do not edit manually, changes will be overwritten.              */\n\n");
1955
1956         fprintf(f, "#include \"RNA_types.h\"\n\n");
1957
1958         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
1959
1960         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
1961         fprintf(f, "    { \\\n");
1962         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
1963         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
1964         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
1965
1966         fprintf(f, "#define FOREACH_END(property) \\\n");
1967         fprintf(f, "            } \\\n");
1968         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
1969         fprintf(f, "    }\n\n");
1970
1971         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1972                 srna= ds->srna;
1973
1974                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
1975
1976                 while(srna) {
1977                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1978                         srna= srna->base;
1979                 }
1980                 fprintf(f, "\n");
1981
1982                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
1983                         rna_def_property_funcs_header(f, ds->srna, dp);
1984         }
1985
1986         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
1987
1988         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
1989 }
1990
1991 static const char *cpp_classes = ""
1992 "\n"
1993 "#include <string>\n"
1994 "\n"
1995 "namespace RNA {\n"
1996 "\n"
1997 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
1998 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
1999 "\n"
2000 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2001 "       Array<int,size> sname::identifier(void) \\\n"
2002 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2003 "\n"
2004 "#define INT_PROPERTY(sname, identifier) \\\n"
2005 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2006 "\n"
2007 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2008 "       Array<int,size> sname::identifier(void) \\\n"
2009 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2010 "\n"
2011 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2012 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2013 "\n"
2014 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2015 "       Array<float,size> sname::identifier(void) \\\n"
2016 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2017 "\n"
2018 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2019 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2020 "\n"
2021 "#define STRING_PROPERTY(sname, identifier) \\\n"
2022 "       std::string sname::identifier(void) { \\\n"
2023 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2024 "               std::string str; str.resize(len); \\\n"
2025 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2026 "\n"
2027 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2028 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2029 "\n"
2030 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2031 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2032 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2033 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2034 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2035 "\n"
2036 "class Pointer {\n"
2037 "public:\n"
2038 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2039 "       operator const PointerRNA&() { return ptr; }\n"
2040 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2041 "       operator void*() { return ptr.data; }\n"
2042 "       operator bool() { return ptr.data != NULL; }\n"
2043 "\n"
2044 "       PointerRNA ptr;\n"
2045 "};\n"
2046 "\n"
2047 "\n"
2048 "template<typename T, int Tsize>\n"
2049 "class Array {\n"
2050 "public:\n"
2051 "       T data[Tsize];\n"
2052 "       operator T*() { return data; }\n"
2053 "};\n"
2054 "\n"
2055 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2056 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2057 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2058 "\n"
2059 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2060 "class CollectionIterator {\n"
2061 "public:\n"
2062 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2063 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2064 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2065 "       { 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"
2066 "\n"
2067 "       operator bool(void)\n"
2068 "       { return iter.valid != 0; }\n"
2069 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2070 "       T& operator*(void) { return t; }\n"
2071 "       T* operator->(void) { return &t; }\n"
2072 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2073 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2074 "\n"
2075 "       void begin(const Pointer& ptr)\n"
2076 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2077 "\n"
2078 "private:\n"
2079 "       CollectionPropertyIterator iter;\n"
2080 "       T t;\n"
2081 "       bool init;\n"
2082 "};\n"
2083 "\n"
2084 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2085 "class Collection {\n"
2086 "public:\n"
2087 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2088 "\n"
2089 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2090 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2091 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2092 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2093 "\n"
2094 "private:\n"
2095 "       PointerRNA ptr;\n"
2096 "};\n"
2097 "\n";
2098
2099 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2100 {
2101         StructDefRNA *ds;
2102         PropertyDefRNA *dp;
2103         StructRNA *srna;
2104
2105         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2106         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2107
2108         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2109                      "   Do not edit manually, changes will be overwritten. */\n\n");
2110
2111         fprintf(f, "#include \"RNA_blender.h\"\n");
2112         fprintf(f, "#include \"RNA_types.h\"\n");
2113
2114         fprintf(f, cpp_classes);
2115
2116         fprintf(f, "/**************** Declarations ****************/\n\n");
2117
2118         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2119                 fprintf(f, "class %s;\n", ds->srna->identifier);
2120         fprintf(f, "\n");
2121
2122         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2123                 srna= ds->srna;
2124
2125                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2126
2127                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2128                 fprintf(f, "public:\n");
2129                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2130                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2131                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2132                                 if(dp->prop->type == PROP_COLLECTION)
2133                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2134                 fprintf(f, "\n\t\t{}\n\n");
2135
2136                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2137                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2138                 fprintf(f, "};\n\n");
2139         }
2140
2141
2142         fprintf(f, "/**************** Implementation ****************/\n");
2143
2144         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2145                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2146                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2147
2148                 fprintf(f, "\n");
2149         }
2150
2151         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2152 }
2153
2154 static void make_bad_file(char *file)
2155 {
2156         FILE *fp= fopen(file, "w");
2157         fprintf(fp, "ERROR! Cannot make correct RNA file, STUPID!\n");
2158         fclose(fp);
2159 }
2160
2161 static int rna_preprocess(char *outfile)
2162 {
2163         BlenderRNA *brna;
2164         StructDefRNA *ds;
2165         FILE *file;
2166         char deffile[4096];
2167         int i, status;
2168
2169         /* define rna */
2170         brna= RNA_create();
2171
2172         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2173                 if(PROCESS_ITEMS[i].define) {
2174                         PROCESS_ITEMS[i].define(brna);
2175
2176                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2177                                 if(!ds->filename)
2178                                         ds->filename= PROCESS_ITEMS[i].filename;
2179                 }
2180         }
2181
2182         rna_auto_types();
2183
2184
2185         /* create RNA_blender_cpp.h */
2186         strcpy(deffile, outfile);
2187         strcat(deffile, "RNA_blender_cpp.h");
2188
2189         status= (DefRNA.error != 0);
2190
2191         if(status) {
2192                 make_bad_file(deffile);
2193         }
2194         else {
2195                 file = fopen(deffile, "w");
2196
2197                 if(!file) {
2198                         printf ("Unable to open file: %s\n", deffile);
2199                         status = 1;
2200                 }
2201                 else {
2202                         rna_generate_header_cpp(brna, file);
2203                         fclose(file);
2204
2205                         status= (DefRNA.error != 0);
2206                 }
2207         }
2208
2209         rna_sort(brna);
2210
2211         /* create rna_gen_*.c files */
2212         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2213                 strcpy(deffile, outfile);
2214                 strcat(deffile, PROCESS_ITEMS[i].filename);
2215                 deffile[strlen(deffile)-2] = '\0';
2216                 strcat(deffile, "_gen.c");
2217
2218                 if(status) {
2219                         make_bad_file(deffile);
2220                 }
2221                 else {
2222                         file = fopen(deffile, "w");
2223
2224                         if(!file) {
2225                                 printf ("Unable to open file: %s\n", deffile);
2226                                 status = 1;
2227                         }
2228                         else {
2229                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2230                                 fclose(file);
2231
2232                                 status= (DefRNA.error != 0);
2233                         }
2234                 }
2235         }
2236
2237         /* create RNA_blender.h */
2238         strcpy(deffile, outfile);
2239         strcat(deffile, "RNA_blender.h");
2240
2241         if(status) {
2242                 make_bad_file(deffile);
2243         }
2244         else {
2245                 file = fopen(deffile, "w");
2246
2247                 if(!file) {
2248                         printf ("Unable to open file: %s\n", deffile);
2249                         status = 1;
2250                 }
2251                 else {
2252                         rna_generate_header(brna, file);
2253                         fclose(file);
2254
2255                         status= (DefRNA.error != 0);
2256                 }
2257         }
2258
2259         /* free RNA */
2260         RNA_define_free(brna);
2261         RNA_free(brna);
2262
2263         return status;
2264 }
2265
2266 static void mem_error_cb(char *errorStr)
2267 {
2268         fprintf(stderr, "%s", errorStr);
2269         fflush(stderr);
2270 }
2271
2272 int main(int argc, char **argv)
2273 {
2274         int totblock, return_status = 0;
2275
2276         if(argc<2) {
2277                 printf("Usage: %s outdirectory/\n", argv[0]);
2278                 return_status = 1;
2279         }
2280         else {
2281                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2282                 return_status= rna_preprocess(argv[1]);
2283         }
2284
2285         totblock= MEM_get_memory_blocks_in_use();
2286         if(totblock!=0) {
2287                 printf("Error Totblock: %d\n",totblock);
2288                 MEM_set_error_callback(mem_error_cb);
2289                 MEM_printmemlist();
2290         }
2291
2292         return return_status;
2293 }
2294
2295