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