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