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