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