Merge with -r 22620:23107.
[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         if(func->ret && (func->ret->flag & PROP_DYNAMIC)) fprintf(f, "\tint _ret_array_length;\n");
1241         fprintf(f, "\t\n");
1242
1243         /* assign self */
1244         if((func->flag & FUNC_NO_SELF)==0) {
1245                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1246                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1247         }
1248
1249         fprintf(f, "\t_data= (char *)_parms->data;\n");
1250
1251         dparm= dfunc->cont.properties.first;
1252         for(; dparm; dparm= dparm->next) {
1253                 if(dparm->prop==func->ret) 
1254                         fprintf(f, "\t_retdata= _data;\n");
1255                 else if(dparm->prop->arraydimension)
1256                         fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1257                 else if(dparm->prop->type == PROP_POINTER) {
1258                         if(dparm->prop->flag & PROP_RNAPTR)
1259                                 fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1260                         else
1261                                 fprintf(f, "\t%s= *((%s%s**)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1262                 }
1263                 else
1264                         fprintf(f, "\t%s= *((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1265
1266                 if(dparm->next)
1267                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size(dparm->prop));
1268         }
1269
1270         if(dfunc->call) {
1271                 fprintf(f, "\t\n");
1272                 fprintf(f, "\t");
1273                 if(func->ret) fprintf(f, "%s= ", func->ret->identifier);
1274                 fprintf(f, "%s(", dfunc->call);
1275
1276                 first= 1;
1277
1278                 if((func->flag & FUNC_NO_SELF)==0) {
1279                         fprintf(f, "_self");
1280                         first= 0;
1281                 }
1282
1283                 if(func->flag & FUNC_USE_CONTEXT) {
1284                         if(!first) fprintf(f, ", ");
1285                         first= 0;
1286                         fprintf(f, "C");
1287                 }
1288
1289                 if(func->flag & FUNC_USE_REPORTS) {
1290                         if(!first) fprintf(f, ", ");
1291                         first= 0;
1292                         fprintf(f, "reports");
1293                 }
1294
1295                 if(func->ret && (func->ret->flag & PROP_DYNAMIC)) {
1296                         if(!first) fprintf(f, ", ");
1297                         first= 0;
1298                         fprintf(f, "&_ret_array_length");
1299                 }
1300
1301                 dparm= dfunc->cont.properties.first;
1302                 for(; dparm; dparm= dparm->next) {
1303                         if(dparm->prop==func->ret)
1304                                 continue;
1305
1306                         if(!first) fprintf(f, ", ");
1307                         first= 0;
1308
1309                         fprintf(f, "%s", dparm->prop->identifier);
1310                 }
1311
1312                 fprintf(f, ");\n");
1313
1314                 if(func->ret) {
1315                         dparm= rna_find_parameter_def(func->ret);
1316                         ptrstr= (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) || (dparm->prop->arraydimension))? "*": "";
1317                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->ret->identifier);
1318
1319                         if(func->ret && (func->ret->flag & PROP_DYNAMIC)) {
1320                                 fprintf(f, "\t_parms->func->ret->arraylength[0]= _ret_array_length;\n");
1321                                 fprintf(f, "\t_parms->func->ret->totarraylength= _ret_array_length;\n");
1322                                 fprintf(f, "\t_parms->func->ret->arraydimension= 1;\n");
1323                         }
1324                 }
1325         }
1326
1327         fprintf(f, "}\n\n");
1328
1329         dfunc->gencall= funcname;
1330 }
1331
1332 static void rna_auto_types()
1333 {
1334         StructDefRNA *ds;
1335         PropertyDefRNA *dp;
1336
1337         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1338                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1339                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1340                         ds->dnaname= "bScreen";
1341
1342                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1343                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1344                                 dp->dnastructname= "bScreen";
1345
1346                         if(dp->dnatype) {
1347                                 if(dp->prop->type == PROP_POINTER) {
1348                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1349                                         StructRNA *type;
1350
1351                                         if(!pprop->type && !pprop->get)
1352                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1353
1354                                         if(pprop->type) {
1355                                                 type= rna_find_struct((char*)pprop->type);
1356                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1357                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1358                                         }
1359                                 }
1360                                 else if(dp->prop->type== PROP_COLLECTION) {
1361                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1362
1363                                         if(!cprop->type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1364                                                 cprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1365                                 }
1366                         }
1367                 }
1368         }
1369 }
1370
1371 static void rna_sort(BlenderRNA *brna)
1372 {
1373         StructDefRNA *ds;
1374         StructRNA *srna;
1375
1376         rna_sortlist(&brna->structs, cmp_struct);
1377         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1378
1379         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1380                 rna_sortlist(&srna->cont.properties, cmp_property);
1381
1382         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1383                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1384 }
1385
1386 static const char *rna_property_structname(PropertyType type)
1387 {
1388         switch(type) {
1389                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1390                 case PROP_INT: return "IntPropertyRNA";
1391                 case PROP_FLOAT: return "FloatPropertyRNA";
1392                 case PROP_STRING: return "StringPropertyRNA";
1393                 case PROP_ENUM: return "EnumPropertyRNA";
1394                 case PROP_POINTER: return "PointerPropertyRNA";
1395                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1396                 default: return "UnknownPropertyRNA";
1397         }
1398 }
1399
1400 static const char *rna_property_typename(PropertyType type)
1401 {
1402         switch(type) {
1403                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1404                 case PROP_INT: return "PROP_INT";
1405                 case PROP_FLOAT: return "PROP_FLOAT";
1406                 case PROP_STRING: return "PROP_STRING";
1407                 case PROP_ENUM: return "PROP_ENUM";
1408                 case PROP_POINTER: return "PROP_POINTER";
1409                 case PROP_COLLECTION: return "PROP_COLLECTION";
1410                 default: return "PROP_UNKNOWN";
1411         }
1412 }
1413
1414 static const char *rna_property_subtypename(PropertyType type)
1415 {
1416         switch(type) {
1417                 case PROP_NONE: return "PROP_NONE";
1418                 case PROP_FILEPATH: return "PROP_FILEPATH";
1419                 case PROP_DIRPATH: return "PROP_DIRPATH";
1420                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1421                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1422                 case PROP_ANGLE: return "PROP_ANGLE";
1423                 case PROP_TIME: return "PROP_TIME";
1424                 case PROP_DISTANCE: return "PROP_DISTANCE";
1425                 case PROP_COLOR: return "PROP_COLOR";
1426                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1427                 case PROP_DIRECTION: return "PROP_DIRECTION";
1428                 case PROP_MATRIX: return "PROP_MATRIX";
1429                 case PROP_EULER: return "PROP_EULER";
1430                 case PROP_QUATERNION: return "PROP_QUATERNION";
1431                 case PROP_VELOCITY: return "PROP_VELOCITY";
1432                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1433                 case PROP_XYZ: return "PROP_XYZ";
1434                 case PROP_RGB: return "PROP_RGB";
1435                 case PROP_NEVER_NULL: return "PROP_NEVER_NULL";
1436                 case PROP_LAYER: return "PROP_LAYER";
1437                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1438                 default: {
1439                         /* incase we dont have a type preset that includes the subtype */
1440                         if(RNA_SUBTYPE_UNIT(type)) {
1441                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1442                         }
1443                         else {
1444                                 return "PROP_SUBTYPE_UNKNOWN";
1445                         }
1446                 }
1447         }
1448 }
1449
1450 static const char *rna_property_subtype_unit(PropertyType type)
1451 {
1452         switch(RNA_SUBTYPE_UNIT(type)) {
1453                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1454                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1455                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1456                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1457                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1458                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1459                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1460                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1461                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1462                 default: return "PROP_UNKNOWN";
1463         }
1464 }
1465
1466 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1467 {
1468         StructRNA *srna;
1469
1470         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1471                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1472         fprintf(f, "\n");
1473 }
1474
1475 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1476 {
1477         StructRNA *srna;
1478
1479         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1480
1481         srna= brna->structs.first;
1482         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1483         else fprintf(f, "{NULL, ");
1484
1485         srna= brna->structs.last;
1486         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1487         else fprintf(f, "NULL}");
1488
1489         fprintf(f, "};\n\n");
1490 }
1491
1492 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1493 {
1494         PropertyRNA *prop;
1495         StructRNA *base;
1496
1497         base= srna->base;
1498         while (base) {
1499                 fprintf(f, "\n");
1500                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1501                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1502                 base= base->base;
1503         }
1504
1505         if(srna->cont.properties.first)
1506                 fprintf(f, "\n");
1507
1508         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1509                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1510         fprintf(f, "\n");
1511 }
1512
1513 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1514 {
1515         PropertyRNA *parm;
1516
1517         for(parm= func->cont.properties.first; parm; parm= parm->next)
1518                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1519
1520         if(func->cont.properties.first)
1521                 fprintf(f, "\n");
1522 }
1523
1524 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1525 {
1526         FunctionRNA *func;
1527         StructRNA *base;
1528
1529         base= srna->base;
1530         while (base) {
1531                 for(func= base->functions.first; func; func= func->cont.next) {
1532                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1533                         rna_generate_parameter_prototypes(brna, base, func, f);
1534                 }
1535
1536                 if(base->functions.first)
1537                         fprintf(f, "\n");
1538
1539                 base= base->base;
1540         }
1541
1542         for(func= srna->functions.first; func; func= func->cont.next) {
1543                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1544                 rna_generate_parameter_prototypes(brna, srna, func, f);
1545         }
1546
1547         if(srna->functions.first)
1548                 fprintf(f, "\n");
1549 }
1550
1551 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1552 {
1553         FunctionRNA *func;
1554         PropertyDefRNA *dparm;
1555         StructDefRNA *dsrna;
1556         int first;
1557
1558         dsrna= rna_find_struct_def(srna);
1559         func= dfunc->func;
1560
1561         /* return type */
1562         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1563                 if(dparm->prop==func->ret) {
1564                         if(dparm->prop->arraydimension)
1565                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1566                         else if(dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
1567                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1568                         else
1569                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1570
1571                         break;
1572                 }
1573         }
1574
1575         /* void if nothing to return */
1576         if(!dparm)
1577                 fprintf(f, "void ");
1578
1579         /* function name */
1580         fprintf(f, "%s(", dfunc->call);
1581
1582         first= 1;
1583
1584         /* self, context and reports parameters */
1585         if((func->flag & FUNC_NO_SELF)==0) {
1586                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1587                 else fprintf(f, "struct %s *_self", srna->identifier);
1588                 first= 0;
1589         }
1590
1591         if(func->flag & FUNC_USE_CONTEXT) {
1592                 if(!first) fprintf(f, ", ");
1593                 first= 0;
1594                 fprintf(f, "bContext *C");
1595         }
1596
1597         if(func->flag & FUNC_USE_REPORTS) {
1598                 if(!first) fprintf(f, ", ");
1599                 first= 0;
1600                 fprintf(f, "ReportList *reports");
1601         }
1602
1603         /* dynamic array length paramter */
1604         if(func->ret && (func->ret->flag & PROP_DYNAMIC)) {
1605                 if(!first) fprintf(f, ", ");
1606                 first= 0;
1607                 fprintf(f, "int *array_length");
1608         }
1609
1610         /* defined parameters */
1611         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1612                 if(dparm->prop==func->ret)
1613                         continue;
1614
1615                 if(!first) fprintf(f, ", ");
1616                 first= 0;
1617
1618                 if(dparm->prop->arraydimension)
1619                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->totarraylength);
1620                 else if(dparm->prop->type == PROP_POINTER)
1621                         fprintf(f, "%s%s *%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1622                 else
1623                         fprintf(f, "%s%s %s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1624         }
1625
1626         fprintf(f, ");\n");
1627 }
1628
1629 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1630 {
1631         FunctionRNA *func;
1632         FunctionDefRNA *dfunc;
1633
1634         fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1635
1636         for(func= srna->functions.first; func; func= func->cont.next) {
1637                 if(func->flag & FUNC_REGISTER)
1638                         continue;
1639
1640                 dfunc= rna_find_function_def(func);
1641                 if(dfunc->call)
1642                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1643         }
1644
1645         fprintf(f, "\n");
1646 }
1647
1648 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1649 {
1650         char *strnest= "", *errnest= "";
1651         int len, freenest= 0;
1652         
1653         if(nest != NULL) {
1654                 len= strlen(nest);
1655
1656                 strnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
1657                 errnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
1658
1659                 strcpy(strnest, "_"); strcat(strnest, nest);
1660                 strcpy(errnest, "."); strcat(errnest, nest);
1661
1662                 freenest= 1;
1663         }
1664
1665         switch(prop->type) {
1666                         case PROP_ENUM: {
1667                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1668                                 int i, defaultfound= 0;
1669
1670                                 if(eprop->item) {
1671                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
1672
1673                                         for(i=0; i<eprop->totitem; i++) {
1674                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1675                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1676                                                 fprintf(f, "%d, ", eprop->item[i].icon);
1677                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1678                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}, ");
1679
1680                                                 if(eprop->item[i].identifier[0])
1681                                                         if(eprop->defaultvalue == eprop->item[i].value)
1682                                                                 defaultfound= 1;
1683                                         }
1684
1685                                         fprintf(f, "{0, NULL, 0, NULL, NULL}};\n\n");
1686
1687                                         if(!defaultfound) {
1688                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1689                                                 DefRNA.error= 1;
1690                                         }
1691                                 }
1692                                 else {
1693                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1694                                         DefRNA.error= 1;
1695                                 }
1696                                 break;
1697                         }
1698                         case PROP_BOOLEAN: {
1699                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)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(bprop->defaultarray)
1707                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1708                                                 else
1709                                                         fprintf(f, "%d", bprop->defaultvalue);
1710                                                 if(i != prop->totarraylength-1)
1711                                                         fprintf(f, ", ");
1712                                         }
1713
1714                                         fprintf(f, "};\n\n");
1715                                 }
1716                                 break;
1717                         }
1718                         case PROP_INT: {
1719                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1720                                 unsigned int i;
1721
1722                                 if(prop->arraydimension && prop->totarraylength) {
1723                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1724
1725                                         for(i=0; i<prop->totarraylength; i++) {
1726                                                 if(iprop->defaultarray)
1727                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1728                                                 else
1729                                                         fprintf(f, "%d", iprop->defaultvalue);
1730                                                 if(i != prop->totarraylength-1)
1731                                                         fprintf(f, ", ");
1732                                         }
1733
1734                                         fprintf(f, "};\n\n");
1735                                 }
1736                                 break;
1737                         }
1738                         case PROP_FLOAT: {
1739                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1740                                 unsigned int i;
1741
1742                                 if(prop->arraydimension && prop->totarraylength) {
1743                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1744
1745                                         for(i=0; i<prop->totarraylength; i++) {
1746                                                 if(fprop->defaultarray)
1747                                                         rna_float_print(f, fprop->defaultarray[i]);
1748                                                 else
1749                                                         rna_float_print(f, fprop->defaultvalue);
1750                                                 if(i != prop->totarraylength-1)
1751                                                         fprintf(f, ", ");
1752                                         }
1753
1754                                         fprintf(f, "};\n\n");
1755                                 }
1756                                 break;
1757                         }
1758                         default:
1759                                 break;
1760         }
1761
1762         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
1763
1764         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
1765         else fprintf(f, "\t{NULL, ");
1766         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
1767         else fprintf(f, "NULL,\n");
1768         fprintf(f, "\t%d, ", prop->magic);
1769         rna_print_c_string(f, prop->identifier);
1770         fprintf(f, ", %d, ", prop->flag);
1771         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
1772         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
1773         fprintf(f, "%d,\n", prop->icon);
1774         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);
1775         fprintf(f, "\t%s, %d, %s,\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
1776
1777         if(prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
1778         else fprintf(f, "\t0, 0");
1779         fprintf(f, "},\n");
1780
1781         switch(prop->type) {
1782                         case PROP_BOOLEAN: {
1783                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1784                                 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);
1785                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1786                                 else fprintf(f, "NULL\n");
1787                                 break;
1788                         }
1789                         case PROP_INT: {
1790                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1791                                 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));
1792                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
1793                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
1794                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
1795                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
1796                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
1797                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
1798                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1799                                 else fprintf(f, "NULL\n");
1800                                 break;
1801                          }
1802                         case PROP_FLOAT: {
1803                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1804                                 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));
1805                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
1806                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
1807                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
1808                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
1809                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
1810                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
1811                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
1812                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1813                                 else fprintf(f, "NULL\n");
1814                                 break;
1815                          }
1816                         case PROP_STRING: {
1817                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1818                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
1819                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
1820                                 break;
1821                         }
1822                         case PROP_ENUM: {
1823                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1824                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
1825                                 if(eprop->item)
1826                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
1827                                 else
1828                                         fprintf(f, "NULL, ");
1829                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
1830                                 break;
1831                         }
1832                         case PROP_POINTER: {
1833                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1834                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef));
1835                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
1836                                 else fprintf(f, "NULL\n");
1837                                 break;
1838                          }
1839                         case PROP_COLLECTION: {
1840                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1841                                 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));
1842                                 if(cprop->add) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->add);
1843                                 else fprintf(f, "NULL, ");
1844                                 if(cprop->remove) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->remove);
1845                                 else fprintf(f, "NULL, ");
1846                                 if(cprop->type) fprintf(f, "&RNA_%s\n", (char*)cprop->type);
1847                                 else fprintf(f, "NULL\n");
1848                                 break;
1849                         }
1850         }
1851
1852         fprintf(f, "};\n\n");
1853
1854         if(freenest) {
1855                 MEM_freeN(strnest);
1856                 MEM_freeN(errnest);
1857         }
1858 }
1859
1860 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
1861 {
1862         FunctionRNA *func;
1863         FunctionDefRNA *dfunc;
1864         PropertyRNA *prop, *parm;
1865         StructRNA *base;
1866
1867         fprintf(f, "/* %s */\n", srna->name);
1868
1869         for(prop= srna->cont.properties.first; prop; prop= prop->next)
1870                 rna_generate_property(f, srna, NULL, prop);
1871
1872         for(func= srna->functions.first; func; func= func->cont.next) {
1873                 for(parm= func->cont.properties.first; parm; parm= parm->next)
1874                         rna_generate_property(f, srna, func->identifier, parm);
1875
1876                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
1877
1878                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
1879                 else fprintf(f, "\t{NULL, ");
1880                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
1881                 else fprintf(f, "NULL,\n");
1882
1883                 fprintf(f, "\tNULL,\n");
1884
1885                 parm= func->cont.properties.first;
1886                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
1887                 else fprintf(f, "\t{NULL, ");
1888
1889                 parm= func->cont.properties.last;
1890                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
1891                 else fprintf(f, "NULL}},\n");
1892
1893                 fprintf(f, "\t");
1894                 rna_print_c_string(f, func->identifier);
1895                 fprintf(f, ", %d, ", func->flag);
1896                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
1897
1898                 dfunc= rna_find_function_def(func);
1899                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
1900                 else fprintf(f, "\tNULL,\n");
1901         
1902                 if(func->ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->ret->identifier);
1903                 else fprintf(f, "\tNULL\n");
1904
1905                 fprintf(f, "};\n");
1906                 fprintf(f, "\n");
1907         }
1908
1909         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
1910
1911         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
1912         else fprintf(f, "\t{NULL, ");
1913         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
1914         else fprintf(f, "NULL,\n");
1915
1916         fprintf(f, "\tNULL,\n");
1917
1918         prop= srna->cont.properties.first;
1919         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
1920         else fprintf(f, "\t{NULL, ");
1921
1922         prop= srna->cont.properties.last;
1923         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
1924         else fprintf(f, "NULL}},\n");
1925
1926         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
1927         
1928         fprintf(f, "\t");
1929         rna_print_c_string(f, srna->identifier);
1930         fprintf(f, ", %d, ", srna->flag);
1931         rna_print_c_string(f, srna->name);
1932         fprintf(f, ", ");
1933         rna_print_c_string(f, srna->description);
1934         fprintf(f, ",\n %d,\n", srna->icon);
1935
1936         prop= srna->nameproperty;
1937         if(prop) {
1938                 base= srna;
1939                 while (base->base && base->base->nameproperty==prop)
1940                         base= base->base;
1941
1942                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
1943         }
1944         else fprintf(f, "\tNULL, ");
1945
1946         prop= srna->iteratorproperty;
1947         base= srna;
1948         while (base->base && base->base->iteratorproperty==prop)
1949                 base= base->base;
1950         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
1951
1952         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
1953         else fprintf(f, "\tNULL,\n");
1954
1955         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
1956         else fprintf(f, "\tNULL,\n");
1957
1958         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
1959         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
1960         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
1961         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
1962         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
1963
1964         if(srna->reg && !srna->refine) {
1965                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
1966                 DefRNA.error= 1;
1967         }
1968
1969         func= srna->functions.first;
1970         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
1971         else fprintf(f, "\t{NULL, ");
1972
1973         func= srna->functions.last;
1974         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
1975         else fprintf(f, "NULL}\n");
1976
1977         fprintf(f, "};\n");
1978
1979         fprintf(f, "\n");
1980 }
1981
1982 typedef struct RNAProcessItem {
1983         char *filename;
1984         char *api_filename;
1985         void (*define)(BlenderRNA *brna);
1986 } RNAProcessItem;
1987
1988 RNAProcessItem PROCESS_ITEMS[]= {
1989         {"rna_rna.c", NULL, RNA_def_rna},
1990         {"rna_ID.c", NULL, RNA_def_ID},
1991         {"rna_texture.c", NULL, RNA_def_texture},
1992         {"rna_action.c", "rna_action_api.c", RNA_def_action},
1993         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
1994         {"rna_actuator.c", NULL, RNA_def_actuator},
1995         {"rna_armature.c", NULL, RNA_def_armature},
1996         {"rna_boid.c", NULL, RNA_def_boid},
1997         {"rna_brush.c", NULL, RNA_def_brush},
1998         {"rna_camera.c", NULL, RNA_def_camera},
1999         {"rna_cloth.c", NULL, RNA_def_cloth},
2000         {"rna_color.c", NULL, RNA_def_color},
2001         {"rna_constraint.c", NULL, RNA_def_constraint},
2002         {"rna_context.c", NULL, RNA_def_context},
2003         {"rna_controller.c", NULL, RNA_def_controller},
2004         {"rna_curve.c", NULL, RNA_def_curve},
2005         {"rna_fcurve.c", NULL, RNA_def_fcurve},
2006         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2007         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2008         {"rna_group.c", NULL, RNA_def_group},
2009         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2010         {"rna_key.c", NULL, RNA_def_key},
2011         {"rna_lamp.c", NULL, RNA_def_lamp},
2012         {"rna_lattice.c", NULL, RNA_def_lattice},
2013         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2014         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2015         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2016         {"rna_meta.c", NULL, RNA_def_meta},
2017         {"rna_modifier.c", NULL, RNA_def_modifier},
2018         {"rna_nla.c", NULL, RNA_def_nla},
2019         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2020         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2021         {"rna_object_force.c", NULL, RNA_def_object_force},
2022         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2023         {"rna_particle.c", NULL, RNA_def_particle},
2024         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2025         {"rna_property.c", NULL, RNA_def_gameproperty},
2026         {"rna_render.c", NULL, RNA_def_render},
2027         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2028         {"rna_screen.c", NULL, RNA_def_screen},
2029         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2030         {"rna_sensor.c", NULL, RNA_def_sensor},
2031         {"rna_sequence.c", NULL, RNA_def_sequence},
2032         {"rna_smoke.c", NULL, RNA_def_smoke},
2033         {"rna_space.c", NULL, RNA_def_space},
2034         {"rna_test.c", NULL, RNA_def_test},
2035         {"rna_text.c", NULL, RNA_def_text},
2036         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2037         {"rna_sound.c", NULL, RNA_def_sound},
2038         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2039         {"rna_userdef.c", NULL, RNA_def_userdef},
2040         {"rna_vfont.c", NULL, RNA_def_vfont},
2041         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2042         {"rna_world.c", NULL, RNA_def_world},   
2043         {NULL, NULL}};
2044
2045 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename, char *api_filename)
2046 {
2047         StructDefRNA *ds;
2048         PropertyDefRNA *dp;
2049         FunctionDefRNA *dfunc;
2050         
2051         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
2052                      "   Do not edit manually, changes will be overwritten.           */\n\n"
2053                       "#define RNA_RUNTIME\n\n");
2054
2055         fprintf(f, "#include <float.h>\n");
2056         fprintf(f, "#include <limits.h>\n");
2057         fprintf(f, "#include <string.h>\n\n");
2058         fprintf(f, "#include <stddef.h>\n\n");
2059
2060         fprintf(f, "#include \"DNA_ID.h\"\n");
2061
2062         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2063
2064         fprintf(f, "#include \"BKE_context.h\"\n");
2065         fprintf(f, "#include \"BKE_library.h\"\n");
2066         fprintf(f, "#include \"BKE_report.h\"\n");
2067         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
2068
2069         fprintf(f, "#include \"RNA_define.h\"\n");
2070         fprintf(f, "#include \"RNA_types.h\"\n");
2071         fprintf(f, "#include \"rna_internal.h\"\n\n");
2072
2073         rna_generate_prototypes(brna, f);
2074
2075         fprintf(f, "#include \"%s\"\n", filename);
2076         if(api_filename)
2077                 fprintf(f, "#include \"%s\"\n", api_filename);
2078         fprintf(f, "\n");
2079
2080         fprintf(f, "/* Autogenerated Functions */\n\n");
2081
2082         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2083                 if(!filename || ds->filename == filename) {
2084                         rna_generate_property_prototypes(brna, ds->srna, f);
2085                         rna_generate_function_prototypes(brna, ds->srna, f);
2086                 }
2087         }
2088
2089         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2090                 if(!filename || ds->filename == filename)
2091                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
2092                                 rna_def_property_funcs(f, ds->srna, dp);
2093
2094         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2095                 if(!filename || ds->filename == filename) {
2096                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
2097                                 rna_def_function_funcs(f, ds, dfunc);
2098
2099                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2100                 }
2101         }
2102
2103         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2104                 if(!filename || ds->filename == filename)
2105                         rna_generate_struct(brna, ds->srna, f);
2106
2107         if(strcmp(filename, "rna_ID.c") == 0) {
2108                 /* this is ugly, but we cannot have c files compiled for both
2109                  * makesrna and blender with some build systems at the moment */
2110                 fprintf(f, "#include \"rna_define.c\"\n\n");
2111
2112                 rna_generate_blender(brna, f);
2113         }
2114 }
2115
2116 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2117 {
2118         StructDefRNA *ds;
2119         PropertyDefRNA *dp;
2120         StructRNA *srna;
2121
2122         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2123         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2124
2125         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2126                      "   Do not edit manually, changes will be overwritten.              */\n\n");
2127
2128         fprintf(f, "#include \"RNA_types.h\"\n\n");
2129
2130         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2131
2132         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2133         fprintf(f, "    { \\\n");
2134         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2135         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2136         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2137
2138         fprintf(f, "#define FOREACH_END(property) \\\n");
2139         fprintf(f, "            } \\\n");
2140         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2141         fprintf(f, "    }\n\n");
2142
2143         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2144                 srna= ds->srna;
2145
2146                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2147
2148                 while(srna) {
2149                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2150                         srna= srna->base;
2151                 }
2152                 fprintf(f, "\n");
2153
2154                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2155                         rna_def_property_funcs_header(f, ds->srna, dp);
2156         }
2157
2158         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2159
2160         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2161 }
2162
2163 static const char *cpp_classes = ""
2164 "\n"
2165 "#include <string>\n"
2166 "\n"
2167 "namespace RNA {\n"
2168 "\n"
2169 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2170 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
2171 "\n"
2172 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2173 "       Array<int,size> sname::identifier(void) \\\n"
2174 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2175 "\n"
2176 "#define INT_PROPERTY(sname, identifier) \\\n"
2177 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2178 "\n"
2179 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2180 "       Array<int,size> sname::identifier(void) \\\n"
2181 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2182 "\n"
2183 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2184 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2185 "\n"
2186 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2187 "       Array<float,size> sname::identifier(void) \\\n"
2188 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2189 "\n"
2190 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2191 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2192 "\n"
2193 "#define STRING_PROPERTY(sname, identifier) \\\n"
2194 "       std::string sname::identifier(void) { \\\n"
2195 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2196 "               std::string str; str.resize(len); \\\n"
2197 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2198 "\n"
2199 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2200 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2201 "\n"
2202 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2203 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2204 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2205 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2206 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2207 "\n"
2208 "class Pointer {\n"
2209 "public:\n"
2210 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2211 "       operator const PointerRNA&() { return ptr; }\n"
2212 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2213 "       operator void*() { return ptr.data; }\n"
2214 "       operator bool() { return ptr.data != NULL; }\n"
2215 "\n"
2216 "       PointerRNA ptr;\n"
2217 "};\n"
2218 "\n"
2219 "\n"
2220 "template<typename T, int Tsize>\n"
2221 "class Array {\n"
2222 "public:\n"
2223 "       T data[Tsize];\n"
2224 "       operator T*() { return data; }\n"
2225 "};\n"
2226 "\n"
2227 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2228 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2229 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2230 "\n"
2231 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2232 "class CollectionIterator {\n"
2233 "public:\n"
2234 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2235 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2236 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2237 "       { 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"
2238 "\n"
2239 "       operator bool(void)\n"
2240 "       { return iter.valid != 0; }\n"
2241 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2242 "       T& operator*(void) { return t; }\n"
2243 "       T* operator->(void) { return &t; }\n"
2244 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2245 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2246 "\n"
2247 "       void begin(const Pointer& ptr)\n"
2248 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2249 "\n"
2250 "private:\n"
2251 "       CollectionPropertyIterator iter;\n"
2252 "       T t;\n"
2253 "       bool init;\n"
2254 "};\n"
2255 "\n"
2256 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2257 "class Collection {\n"
2258 "public:\n"
2259 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2260 "\n"
2261 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2262 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2263 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2264 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2265 "\n"
2266 "private:\n"
2267 "       PointerRNA ptr;\n"
2268 "};\n"
2269 "\n";
2270
2271 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2272 {
2273         StructDefRNA *ds;
2274         PropertyDefRNA *dp;
2275         StructRNA *srna;
2276
2277         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2278         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2279
2280         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2281                      "   Do not edit manually, changes will be overwritten. */\n\n");
2282
2283         fprintf(f, "#include \"RNA_blender.h\"\n");
2284         fprintf(f, "#include \"RNA_types.h\"\n");
2285
2286         fprintf(f, "%s", cpp_classes);
2287
2288         fprintf(f, "/**************** Declarations ****************/\n\n");
2289
2290         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2291                 fprintf(f, "class %s;\n", ds->srna->identifier);
2292         fprintf(f, "\n");
2293
2294         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2295                 srna= ds->srna;
2296
2297                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2298
2299                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2300                 fprintf(f, "public:\n");
2301                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2302                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2303                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2304                                 if(dp->prop->type == PROP_COLLECTION)
2305                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2306                 fprintf(f, "\n\t\t{}\n\n");
2307
2308                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2309                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2310                 fprintf(f, "};\n\n");
2311         }
2312
2313
2314         fprintf(f, "/**************** Implementation ****************/\n");
2315
2316         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2317                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2318                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2319
2320                 fprintf(f, "\n");
2321         }
2322
2323         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2324 }
2325
2326 static void make_bad_file(char *file)
2327 {
2328         FILE *fp= fopen(file, "w");
2329         fprintf(fp, "ERROR! Cannot make correct RNA file, STUPID!\n");
2330         fclose(fp);
2331 }
2332
2333 static int rna_preprocess(char *outfile)
2334 {
2335         BlenderRNA *brna;
2336         StructDefRNA *ds;
2337         FILE *file;
2338         char deffile[4096];
2339         int i, status;
2340
2341         /* define rna */
2342         brna= RNA_create();
2343
2344         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2345                 if(PROCESS_ITEMS[i].define) {
2346                         PROCESS_ITEMS[i].define(brna);
2347
2348                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2349                                 if(!ds->filename)
2350                                         ds->filename= PROCESS_ITEMS[i].filename;
2351                 }
2352         }
2353
2354         rna_auto_types();
2355
2356
2357         /* create RNA_blender_cpp.h */
2358         strcpy(deffile, outfile);
2359         strcat(deffile, "RNA_blender_cpp.h");
2360
2361         status= (DefRNA.error != 0);
2362
2363         if(status) {
2364                 make_bad_file(deffile);
2365         }
2366         else {
2367                 file = fopen(deffile, "w");
2368
2369                 if(!file) {
2370                         printf ("Unable to open file: %s\n", deffile);
2371                         status = 1;
2372                 }
2373                 else {
2374                         rna_generate_header_cpp(brna, file);
2375                         fclose(file);
2376
2377                         status= (DefRNA.error != 0);
2378                 }
2379         }
2380
2381         rna_sort(brna);
2382
2383         /* create rna_gen_*.c files */
2384         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2385                 strcpy(deffile, outfile);
2386                 strcat(deffile, PROCESS_ITEMS[i].filename);
2387                 deffile[strlen(deffile)-2] = '\0';
2388                 strcat(deffile, "_gen.c");
2389
2390                 if(status) {
2391                         make_bad_file(deffile);
2392                 }
2393                 else {
2394                         file = fopen(deffile, "w");
2395
2396                         if(!file) {
2397                                 printf ("Unable to open file: %s\n", deffile);
2398                                 status = 1;
2399                         }
2400                         else {
2401                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2402                                 fclose(file);
2403
2404                                 status= (DefRNA.error != 0);
2405                         }
2406                 }
2407         }
2408
2409         /* create RNA_blender.h */
2410         strcpy(deffile, outfile);
2411         strcat(deffile, "RNA_blender.h");
2412
2413         if(status) {
2414                 make_bad_file(deffile);
2415         }
2416         else {
2417                 file = fopen(deffile, "w");
2418
2419                 if(!file) {
2420                         printf ("Unable to open file: %s\n", deffile);
2421                         status = 1;
2422                 }
2423                 else {
2424                         rna_generate_header(brna, file);
2425                         fclose(file);
2426
2427                         status= (DefRNA.error != 0);
2428                 }
2429         }
2430
2431         /* free RNA */
2432         RNA_define_free(brna);
2433         RNA_free(brna);
2434
2435         return status;
2436 }
2437
2438 static void mem_error_cb(char *errorStr)
2439 {
2440         fprintf(stderr, "%s", errorStr);
2441         fflush(stderr);
2442 }
2443
2444 int main(int argc, char **argv)
2445 {
2446         int totblock, return_status = 0;
2447
2448         if(argc<2) {
2449                 printf("Usage: %s outdirectory/\n", argv[0]);
2450                 return_status = 1;
2451         }
2452         else {
2453                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2454                 return_status= rna_preprocess(argv[1]);
2455         }
2456
2457         totblock= MEM_get_memory_blocks_in_use();
2458         if(totblock!=0) {
2459                 printf("Error Totblock: %d\n",totblock);
2460                 MEM_set_error_callback(mem_error_cb);
2461                 MEM_printmemlist();
2462         }
2463
2464         return return_status;
2465 }
2466
2467