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