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