Merging r44227 through r45619 from trunk into soc-2011-tomato
[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, "    float prop_clamp_min = -FLT_MAX, prop_clamp_max = FLT_MAX, prop_soft_min, prop_soft_max;\n");
699                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max, &prop_soft_min, &prop_soft_max);\n", rna_function_string(fprop->range));
700                 }
701         }
702         else if (prop->type == PROP_INT) {
703                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
704                 if (iprop->range) {
705                         fprintf(f, "    int prop_clamp_min = INT_MIN, prop_clamp_max = INT_MAX, prop_soft_min, prop_soft_max;\n");
706                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max, &prop_soft_min, &prop_soft_max);\n", rna_function_string(iprop->range));
707                 }
708         }
709 }
710
711 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array)
712 {
713         if (prop->type == PROP_INT) {
714                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
715
716                 if (iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
717                         if (array) fprintf(f, "CLAMPIS(values[i], ");
718                         else fprintf(f, "CLAMPIS(value, ");
719                         if (iprop->range) {
720                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
721                         }
722                         else {
723                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
724                                 rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
725                         }
726                         return;
727                 }
728         }
729         else if (prop->type == PROP_FLOAT) {
730                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
731
732                 if (fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
733                         if (array) fprintf(f, "CLAMPIS(values[i], ");
734                         else fprintf(f, "CLAMPIS(value, ");
735                         if (fprop->range) {
736                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
737                         }
738                         else {
739                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
740                                 rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
741                         }
742                         return;
743                 }
744         }
745
746         if (array)
747                 fprintf(f, "values[i];\n");
748         else
749                 fprintf(f, "value;\n");
750 }
751
752 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
753                                        const char *manualfunc)
754 {
755         char *func;
756
757         if (!(prop->flag & PROP_EDITABLE))
758                 return NULL;
759         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
760                 return NULL;
761
762         if (!manualfunc) {
763                 if (!dp->dnastructname || !dp->dnaname) {
764                         if (prop->flag & PROP_EDITABLE) {
765                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
766                                         __func__, srna->identifier, prop->identifier);
767                                 DefRNA.error = 1;
768                         }
769                         return NULL;
770                 }
771         }
772
773         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "set");
774
775         switch (prop->type) {
776                 case PROP_STRING: {
777                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
778                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
779                         fprintf(f, "{\n");
780                         if (manualfunc) {
781                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
782                         }
783                         else {
784                                 const PropertySubType subtype = prop->subtype;
785                                 const char *string_copy_func = (subtype == PROP_FILEPATH ||
786                                                                subtype == PROP_DIRPATH  ||
787                                                                subtype == PROP_FILENAME ||
788                                                                subtype == PROP_BYTESTRING) ?
789                                             "BLI_strncpy" : "BLI_strncpy_utf8";
790
791                                 rna_print_data_get(f, dp);
792
793                                 if (!(prop->flag & PROP_NEVER_NULL)) {
794                                         fprintf(f, "    if (data->%s == NULL) {\n", dp->dnaname);
795                                         fprintf(f, "            return;\n");
796                                         fprintf(f, "    }\n");
797                                 }
798
799                                 if (sprop->maxlength)
800                                         fprintf(f, "    %s(data->%s, value, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
801                                 else
802                                         fprintf(f, "    %s(data->%s, value, sizeof(data->%s));\n", string_copy_func,
803                                                 dp->dnaname, dp->dnaname);
804                         }
805                         fprintf(f, "}\n\n");
806                         break;
807                 }
808                 case PROP_POINTER: {
809                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
810                         fprintf(f, "{\n");
811                         if (manualfunc) {
812                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
813                         }
814                         else {
815                                 rna_print_data_get(f, dp);
816
817                                 if (prop->flag & PROP_ID_SELF_CHECK) {
818                                         rna_print_id_get(f, dp);
819                                         fprintf(f, "    if (id==value.data) return;\n\n");
820                                 }
821
822                                 if (prop->flag & PROP_ID_REFCOUNT) {
823                                         fprintf(f, "\n  if (data->%s)\n", dp->dnaname);
824                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
825                                         fprintf(f, "    if (value.data)\n");
826                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
827                                 }
828                                 else {
829                                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
830                                         StructRNA *type = rna_find_struct((const char*)pprop->type);
831                                         if (type && (type->flag & STRUCT_ID)) {
832                                                 fprintf(f, "    if (value.data)\n");
833                                                 fprintf(f, "            id_lib_extern((ID*)value.data);\n\n");
834                                         }
835                                 }
836
837                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
838
839                         }
840                         fprintf(f, "}\n\n");
841                         break;
842                 }
843                 default:
844                         if (prop->arraydimension) {
845                                 if (prop->flag & PROP_DYNAMIC)
846                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[])\n", func, rna_type_type(prop));
847                                 else
848                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[%u])\n", func,
849                                                 rna_type_type(prop), prop->totarraylength);
850                                 fprintf(f, "{\n");
851
852                                 if (manualfunc) {
853                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
854                                 }
855                                 else {
856                                         rna_print_data_get(f, dp);
857
858                                         if (prop->flag & PROP_DYNAMIC) {
859                                                 char *lenfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier),
860                                                                                         "set_length");
861                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
862                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
863                                                 rna_clamp_value_range(f, prop);
864                                                 fprintf(f, "    for (i=0; i<len; i++) {\n");
865                                                 MEM_freeN(lenfunc);
866                                         }
867                                         else {
868                                                 fprintf(f, "    int i;\n\n");
869                                                 rna_clamp_value_range(f, prop);
870                                                 fprintf(f, "    for (i=0; i<%u; i++) {\n", prop->totarraylength);
871                                         }
872
873                                         if (dp->dnaarraylength == 1) {
874                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
875                                                         fprintf(f, "            if (%svalues[i]) data->%s |= (%d<<i);\n",
876                                                                 (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
877                                                         fprintf(f, "            else data->%s &= ~(%d<<i);\n", dp->dnaname, dp->booleanbit);
878                                                 }
879                                                 else {
880                                                         fprintf(f, "            (&data->%s)[i]= %s", dp->dnaname, (dp->booleannegative) ? "!" : "");
881                                                         rna_clamp_value(f, prop, 1);
882                                                 }
883                                         }
884                                         else {
885                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
886                                                         fprintf(f, "            if (%svalues[i]) data->%s[i] |= ", (dp->booleannegative) ? "!" : "",
887                                                                 dp->dnaname);
888                                                         rna_int_print(f, dp->booleanbit);
889                                                         fprintf(f, ";\n");
890                                                         fprintf(f, "            else data->%s[i] &= ~", dp->dnaname);
891                                                         rna_int_print(f, dp->booleanbit);
892                                                         fprintf(f, ";\n");
893                                                 }
894                                                 else if (rna_color_quantize(prop, dp)) {
895                                                         fprintf(f, "            data->%s[i]= FTOCHAR(values[i]);\n", dp->dnaname);
896                                                 }
897                                                 else {
898                                                         if (dp->dnatype)
899                                                                 fprintf(f, "            ((%s*)data->%s)[i]= %s", dp->dnatype, dp->dnaname,
900                                                                         (dp->booleannegative) ? "!" : "");
901                                                         else
902                                                                 fprintf(f, "            (data->%s)[i]= %s", dp->dnaname, (dp->booleannegative) ? "!" : "");
903                                                         rna_clamp_value(f, prop, 1);
904                                                 }
905                                         }
906                                         fprintf(f, "    }\n");
907                                 }
908                                 fprintf(f, "}\n\n");
909                         }
910                         else {
911                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
912                                 fprintf(f, "{\n");
913
914                                 if (manualfunc) {
915                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
916                                 }
917                                 else {
918                                         rna_print_data_get(f, dp);
919                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
920                                                 fprintf(f, "    if (%svalue) data->%s |= ", (dp->booleannegative) ? "!" : "", dp->dnaname);
921                                                 rna_int_print(f, dp->booleanbit);
922                                                 fprintf(f, ";\n");
923                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
924                                                 rna_int_print(f, dp->booleanbit);
925                                                 fprintf(f, ";\n");
926                                         }
927                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
928                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
929                                                 rna_int_print(f, rna_enum_bitmask(prop));
930                                                 fprintf(f, ";\n");
931                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
932                                         }
933                                         else {
934                                                 rna_clamp_value_range(f, prop);
935                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative) ? "!" : "");
936                                                 rna_clamp_value(f, prop, 0);
937                                         }
938                                 }
939                                 fprintf(f, "}\n\n");
940                         }
941                         break;
942         }
943
944         return func;
945 }
946
947 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
948                                           const char *manualfunc)
949 {
950         char *func = NULL;
951
952         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
953                 return NULL;
954
955         if (prop->type == PROP_STRING) {
956                 if (!manualfunc) {
957                         if (!dp->dnastructname || !dp->dnaname) {
958                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
959                                         __func__, srna->identifier, prop->identifier);
960                                 DefRNA.error = 1;
961                                 return NULL;
962                         }
963                 }
964
965                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
966
967                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
968                 fprintf(f, "{\n");
969                 if (manualfunc) {
970                         fprintf(f, "    return %s(ptr);\n", manualfunc);
971                 }
972                 else {
973                         rna_print_data_get(f, dp);
974                         if (!(prop->flag & PROP_NEVER_NULL)) {
975                                 fprintf(f, "    if (data->%s == NULL) return 0;\n", dp->dnaname);
976                         }
977                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
978                 }
979                 fprintf(f, "}\n\n");
980         }
981         else if (prop->type == PROP_COLLECTION) {
982                 if (!manualfunc) {
983                         if (prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
984                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
985                                         __func__, srna->identifier, prop->identifier);
986                                 DefRNA.error = 1;
987                                 return NULL;
988                         }
989                 }
990
991                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
992
993                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
994                 fprintf(f, "{\n");
995                 if (manualfunc) {
996                         fprintf(f, "    return %s(ptr);\n", manualfunc);
997                 }
998                 else {
999                         rna_print_data_get(f, dp);
1000                         if (dp->dnalengthname)
1001                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
1002                         else
1003                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
1004                 }
1005                 fprintf(f, "}\n\n");
1006         }
1007
1008         return func;
1009 }
1010
1011 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1012                                          const char *manualfunc)
1013 {
1014         char *func, *getfunc;
1015
1016         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1017                 return NULL;
1018
1019         if (!manualfunc) {
1020                 if (!dp->dnastructname || !dp->dnaname) {
1021                         fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
1022                                 __func__, srna->identifier, prop->identifier);
1023                         DefRNA.error = 1;
1024                         return NULL;
1025                 }
1026         }
1027
1028         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "begin");
1029
1030         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
1031         fprintf(f, "{\n");
1032
1033         if (!manualfunc)
1034                 rna_print_data_get(f, dp);
1035
1036         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
1037         fprintf(f, "    iter->parent= *ptr;\n");
1038         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
1039
1040         if (dp->dnalengthname || dp->dnalengthfixed) {
1041                 if (manualfunc) {
1042                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
1043                 }
1044                 else {
1045                         if (dp->dnalengthname)
1046                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, 0, NULL);\n",
1047                                         dp->dnaname, dp->dnaname, dp->dnalengthname);
1048                         else
1049                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, 0, NULL);\n",
1050                                         dp->dnaname, dp->dnaname, dp->dnalengthfixed);
1051                 }
1052         }
1053         else {
1054                 if (manualfunc)
1055                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
1056                 else if (dp->dnapointerlevel == 0)
1057                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
1058                 else
1059                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
1060         }
1061
1062         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1063
1064         fprintf(f, "\n  if (iter->valid)\n");
1065         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1066
1067         fprintf(f, "}\n\n");
1068
1069
1070         return func;
1071 }
1072
1073 static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1074                                               const char *manualfunc, const char *nextfunc)
1075 {
1076         /* note on indices, this is for external functions and ignores skipped values.
1077          * so the the index can only be checked against the length when there is no 'skip' funcion. */
1078         char *func;
1079
1080         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1081                 return NULL;
1082
1083         if (!manualfunc) {
1084                 if (!dp->dnastructname || !dp->dnaname)
1085                         return NULL;
1086
1087                 /* only supported in case of standard next functions */
1088                 if (strcmp(nextfunc, "rna_iterator_array_next") == 0);
1089                 else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
1090                 else return NULL;
1091         }
1092
1093         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "lookup_int");
1094
1095         fprintf(f, "int %s(PointerRNA *ptr, int index, PointerRNA *r_ptr)\n", func);
1096         fprintf(f, "{\n");
1097
1098         if (manualfunc) {
1099                 fprintf(f, "\n  return %s(ptr, index, r_ptr);\n", manualfunc);
1100                 fprintf(f, "}\n\n");
1101                 return func;
1102         }
1103
1104         fprintf(f, "    int found= 0;\n");
1105         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
1106
1107         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1108         fprintf(f, "    if (iter.valid) {\n");
1109
1110         if (strcmp(nextfunc, "rna_iterator_array_next") == 0) {
1111                 fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
1112                 fprintf(f, "            if (index < 0 || index >= internal->length) {\n");
1113                 fprintf(f, "#ifdef __GNUC__\n");
1114                 fprintf(f, "                    printf(\"Array iterator out of range: %%s (index %%d)\\n\", __func__, index);\n");
1115                 fprintf(f, "#else\n");
1116                 fprintf(f, "                    printf(\"Array iterator out of range: (index %%d)\\n\", index);\n");
1117                 fprintf(f, "#endif\n");
1118                 fprintf(f, "            }\n");
1119                 fprintf(f, "            else if (internal->skip) {\n");
1120                 fprintf(f, "                    while (index-- > 0 && iter.valid) {\n");
1121                 fprintf(f, "                            rna_iterator_array_next(&iter);\n");
1122                 fprintf(f, "                    }\n");
1123                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1124                 fprintf(f, "            }\n");
1125                 fprintf(f, "            else {\n");
1126                 fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
1127                 fprintf(f, "                    found= 1;\n");
1128                 fprintf(f, "            }\n");
1129         }
1130         else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
1131                 fprintf(f, "            ListBaseIterator *internal= iter.internal;\n");
1132                 fprintf(f, "            if (internal->skip) {\n");
1133                 fprintf(f, "                    while (index-- > 0 && iter.valid) {\n");
1134                 fprintf(f, "                            rna_iterator_listbase_next(&iter);\n");
1135                 fprintf(f, "                    }\n");
1136                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1137                 fprintf(f, "            }\n");
1138                 fprintf(f, "            else {\n");
1139                 fprintf(f, "                    while (index-- > 0 && internal->link)\n");
1140                 fprintf(f, "                            internal->link= internal->link->next;\n");
1141                 fprintf(f, "                    found= (index == -1 && internal->link);\n");
1142                 fprintf(f, "            }\n");
1143         }
1144
1145         fprintf(f, "            if (found) *r_ptr = %s_%s_get(&iter);\n", srna->identifier, rna_safe_id(prop->identifier));
1146         fprintf(f, "    }\n\n");
1147         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1148
1149         fprintf(f, "    return found;\n");
1150
1151 #if 0
1152         rna_print_data_get(f, dp);
1153         item_type = (cprop->item_type)? (const char*)cprop->item_type: "UnknownType";
1154
1155         if (dp->dnalengthname || dp->dnalengthfixed) {
1156                 if (dp->dnalengthname)
1157                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n",
1158                                 item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
1159                 else
1160                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n",
1161                                 item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
1162         }
1163         else {
1164                 if (dp->dnapointerlevel == 0)
1165                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n",
1166                                 item_type, dp->dnaname);
1167                 else
1168                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
1169         }
1170 #endif
1171
1172         fprintf(f, "}\n\n");
1173
1174         return func;
1175 }
1176
1177 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1178                                         const char *manualfunc)
1179 {
1180         char *func, *getfunc;
1181
1182         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1183                 return NULL;
1184
1185         if (!manualfunc)
1186                 return NULL;
1187
1188         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "next");
1189
1190         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1191         fprintf(f, "{\n");
1192         fprintf(f, "    %s(iter);\n", manualfunc);
1193
1194         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1195
1196         fprintf(f, "\n  if (iter->valid)\n");
1197         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1198
1199         fprintf(f, "}\n\n");
1200
1201         return func;
1202 }
1203
1204 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1205                                        const char *manualfunc)
1206 {
1207         char *func;
1208
1209         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1210                 return NULL;
1211
1212         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "end");
1213
1214         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1215         fprintf(f, "{\n");
1216         if (manualfunc)
1217                 fprintf(f, "    %s(iter);\n", manualfunc);
1218         fprintf(f, "}\n\n");
1219
1220         return func;
1221 }
1222
1223 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
1224 {
1225         if (dp->dnapointerlevel != 0)
1226                 return;
1227         if (!dp->dnatype || !dp->dnaname || !dp->dnastructname)
1228                 return;
1229         
1230         if (strcmp(dp->dnatype, "char") == 0) {
1231                 prop->rawtype = PROP_RAW_CHAR;
1232                 prop->flag |= PROP_RAW_ACCESS;
1233         }
1234         else if (strcmp(dp->dnatype, "short") == 0) {
1235                 prop->rawtype = PROP_RAW_SHORT;
1236                 prop->flag |= PROP_RAW_ACCESS;
1237         }
1238         else if (strcmp(dp->dnatype, "int") == 0) {
1239                 prop->rawtype = PROP_RAW_INT;
1240                 prop->flag |= PROP_RAW_ACCESS;
1241         }
1242         else if (strcmp(dp->dnatype, "float") == 0) {
1243                 prop->rawtype = PROP_RAW_FLOAT;
1244                 prop->flag |= PROP_RAW_ACCESS;
1245         }
1246         else if (strcmp(dp->dnatype, "double") == 0) {
1247                 prop->rawtype = PROP_RAW_DOUBLE;
1248                 prop->flag |= PROP_RAW_ACCESS;
1249         }
1250 }
1251
1252 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1253 {
1254         PropertyDefRNA *dp = rna_find_struct_property_def(srna, prop);
1255
1256         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1257 }
1258
1259 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1260 {
1261         PropertyRNA *prop;
1262
1263         prop = dp->prop;
1264
1265         switch (prop->type) {
1266                 case PROP_BOOLEAN: {
1267                         BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
1268
1269                         if (!prop->arraydimension) {
1270                                 if (!bprop->get && !bprop->set && !dp->booleanbit)
1271                                         rna_set_raw_property(dp, prop);
1272
1273                                 bprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)bprop->get);
1274                                 bprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)bprop->set);
1275                         }
1276                         else {
1277                                 bprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)bprop->getarray);
1278                                 bprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)bprop->setarray);
1279                         }
1280                         break;
1281                 }
1282                 case PROP_INT: {
1283                         IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
1284
1285                         if (!prop->arraydimension) {
1286                                 if (!iprop->get && !iprop->set)
1287                                         rna_set_raw_property(dp, prop);
1288
1289                                 iprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)iprop->get);
1290                                 iprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)iprop->set);
1291                         }
1292                         else {
1293                                 if (!iprop->getarray && !iprop->setarray)
1294                                         rna_set_raw_property(dp, prop);
1295
1296                                 iprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)iprop->getarray);
1297                                 iprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)iprop->setarray);
1298                         }
1299                         break;
1300                 }
1301                 case PROP_FLOAT: {
1302                         FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
1303
1304                         if (!prop->arraydimension) {
1305                                 if (!fprop->get && !fprop->set)
1306                                         rna_set_raw_property(dp, prop);
1307
1308                                 fprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)fprop->get);
1309                                 fprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)fprop->set);
1310                         }
1311                         else {
1312                                 if (!fprop->getarray && !fprop->setarray)
1313                                         rna_set_raw_property(dp, prop);
1314
1315                                 fprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)fprop->getarray);
1316                                 fprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)fprop->setarray);
1317                         }
1318                         break;
1319                 }
1320                 case PROP_ENUM: {
1321                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1322
1323                         eprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)eprop->get);
1324                         eprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)eprop->set);
1325                         break;
1326                 }
1327                 case PROP_STRING: {
1328                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
1329
1330                         sprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)sprop->get);
1331                         sprop->length = (void*)rna_def_property_length_func(f, srna, prop, dp, (const char*)sprop->length);
1332                         sprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)sprop->set);
1333                         break;
1334                 }
1335                 case PROP_POINTER: {
1336                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
1337
1338                         pprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)pprop->get);
1339                         pprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)pprop->set);
1340                         if (!pprop->type) {
1341                                 fprintf(stderr, "%s: %s.%s, pointer must have a struct type.\n",
1342                                         __func__, srna->identifier, prop->identifier);
1343                                 DefRNA.error = 1;
1344                         }
1345                         break;
1346                 }
1347                 case PROP_COLLECTION: {
1348                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
1349                         const char *nextfunc = (const char*)cprop->next;
1350
1351                         if (dp->dnatype && strcmp(dp->dnatype, "ListBase") == 0);
1352                         else if (dp->dnalengthname || dp->dnalengthfixed)
1353                                 cprop->length = (void*)rna_def_property_length_func(f, srna, prop, dp, (const char*)cprop->length);
1354
1355                         /* test if we can allow raw array access, if it is using our standard
1356                          * array get/next function, we can be sure it is an actual array */
1357                         if (cprop->next && cprop->get)
1358                                 if (strcmp((const char*)cprop->next, "rna_iterator_array_next") == 0 &&
1359                                    strcmp((const char*)cprop->get, "rna_iterator_array_get") == 0)
1360                                         prop->flag |= PROP_RAW_ARRAY;
1361
1362                         cprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)cprop->get);
1363                         cprop->begin = (void*)rna_def_property_begin_func(f, srna, prop, dp, (const char*)cprop->begin);
1364                         cprop->next = (void*)rna_def_property_next_func(f, srna, prop, dp, (const char*)cprop->next);
1365                         cprop->end = (void*)rna_def_property_end_func(f, srna, prop, dp, (const char*)cprop->end);
1366                         cprop->lookupint = (void*)rna_def_property_lookup_int_func(f, srna, prop, dp,
1367                                                                                    (const char*)cprop->lookupint, nextfunc);
1368
1369                         if (!(prop->flag & PROP_IDPROPERTY)) {
1370                                 if (!cprop->begin) {
1371                                         fprintf(stderr, "%s: %s.%s, collection must have a begin function.\n",
1372                                                 __func__, srna->identifier, prop->identifier);
1373                                         DefRNA.error = 1;
1374                                 }
1375                                 if (!cprop->next) {
1376                                         fprintf(stderr, "%s: %s.%s, collection must have a next function.\n",
1377                                                 __func__, srna->identifier, prop->identifier);
1378                                         DefRNA.error = 1;
1379                                 }
1380                                 if (!cprop->get) {
1381                                         fprintf(stderr, "%s: %s.%s, collection must have a get function.\n",
1382                                                 __func__, srna->identifier, prop->identifier);
1383                                         DefRNA.error = 1;
1384                                 }
1385                         }
1386                         if (!cprop->item_type) {
1387                                 fprintf(stderr, "%s: %s.%s, collection must have a struct type.\n",
1388                                         __func__, srna->identifier, prop->identifier);
1389                                 DefRNA.error = 1;
1390                         }
1391                         break;
1392                 }
1393         }
1394 }
1395
1396 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1397 {
1398         PropertyRNA *prop;
1399         char *func;
1400
1401         prop = dp->prop;
1402
1403         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1404                 return;
1405
1406         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "");
1407
1408         switch (prop->type) {
1409                 case PROP_BOOLEAN:
1410                 case PROP_INT: {
1411                         if (!prop->arraydimension) {
1412                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1413                                 /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1414                         }
1415                         else if (prop->arraydimension && prop->totarraylength) {
1416                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%u]);\n", func, prop->totarraylength);
1417                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength); */
1418                         }
1419                         else {
1420                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[]);\n", func);
1421                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[]);\n", func); */
1422                         }
1423                         break;
1424                 }
1425                 case PROP_FLOAT: {
1426                         if (!prop->arraydimension) {
1427                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1428                                 /*fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func); */
1429                         }
1430                         else if (prop->arraydimension && prop->totarraylength) {
1431                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%u]);\n", func, prop->totarraylength);
1432                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength); */
1433                         }
1434                         else {
1435                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[]);\n", func);
1436                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[]);\n", func); */
1437                         }
1438                         break;
1439                 }
1440                 case PROP_ENUM: {
1441                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1442                         int i;
1443
1444                         if (eprop->item) {
1445                                 fprintf(f, "enum {\n");
1446
1447                                 for (i = 0; i<eprop->totitem; i++)
1448                                         if (eprop->item[i].identifier[0])
1449                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier,
1450                                                         eprop->item[i].identifier, eprop->item[i].value);
1451
1452                                 fprintf(f, "};\n\n");
1453                         }
1454
1455                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1456                         /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1457
1458                         break;
1459                 }
1460                 case PROP_STRING: {
1461                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
1462
1463                         if (sprop->maxlength) {
1464                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1465                         }
1466                         
1467                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1468                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1469                         /*fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func); */
1470
1471                         break;
1472                 }
1473                 case PROP_POINTER: {
1474                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1475                         /*fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func); */
1476                         break;
1477                 }
1478                 case PROP_COLLECTION: {
1479                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1480                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1481                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1482                         /*fprintf(f, "int %slength(PointerRNA *ptr);\n", func); */
1483                         /*fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func); */
1484                         /*fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func); */
1485                         break;
1486                 }
1487         }
1488
1489         fprintf(f, "\n");
1490 }
1491
1492 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1493 {
1494         PropertyRNA *prop;
1495
1496         prop = dp->prop;
1497
1498         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1499                 return;
1500         
1501         if (prop->name && prop->description && prop->description[0] != '\0')
1502                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1503         else if (prop->name)
1504                 fprintf(f, "\t/* %s */\n", prop->name);
1505         else
1506                 fprintf(f, "\t/* */\n");
1507
1508         switch (prop->type) {
1509                 case PROP_BOOLEAN: {
1510                         if (!prop->arraydimension)
1511                                 fprintf(f, "\tinline bool %s(void);", rna_safe_id(prop->identifier));
1512                         else if (prop->totarraylength)
1513                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1514                         break;
1515                 }
1516                 case PROP_INT: {
1517                         if (!prop->arraydimension)
1518                                 fprintf(f, "\tinline int %s(void);", rna_safe_id(prop->identifier));
1519                         else if (prop->totarraylength)
1520                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1521                         break;
1522                 }
1523                 case PROP_FLOAT: {
1524                         if (!prop->arraydimension)
1525                                 fprintf(f, "\tinline float %s(void);", rna_safe_id(prop->identifier));
1526                         else if (prop->totarraylength)
1527                                 fprintf(f, "\tinline Array<float, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1528                         break;
1529                 }
1530                 case PROP_ENUM: {
1531                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1532                         int i;
1533
1534                         if (eprop->item) {
1535                                 fprintf(f, "\tenum %s_enum {\n", rna_safe_id(prop->identifier));
1536
1537                                 for (i = 0; i<eprop->totitem; i++)
1538                                         if (eprop->item[i].identifier[0])
1539                                                 fprintf(f, "\t\t%s_%s = %d,\n", rna_safe_id(prop->identifier), eprop->item[i].identifier,
1540                                                         eprop->item[i].value);
1541
1542                                 fprintf(f, "\t};\n");
1543                         }
1544
1545                         fprintf(f, "\tinline %s_enum %s(void);", rna_safe_id(prop->identifier), rna_safe_id(prop->identifier));
1546                         break;
1547                 }
1548                 case PROP_STRING: {
1549                         fprintf(f, "\tinline std::string %s(void);", rna_safe_id(prop->identifier));
1550                         break;
1551                 }
1552                 case PROP_POINTER: {
1553                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1554
1555                         if (pprop->type)
1556                                 fprintf(f, "\tinline %s %s(void);", (const char*)pprop->type, rna_safe_id(prop->identifier));
1557                         else
1558                                 fprintf(f, "\tinline %s %s(void);", "UnknownType", rna_safe_id(prop->identifier));
1559                         break;
1560                 }
1561                 case PROP_COLLECTION: {
1562                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1563
1564                         if (cprop->item_type)
1565                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char*)cprop->item_type, srna->identifier,
1566                                         rna_safe_id(prop->identifier));
1567                         else
1568                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier,
1569                                         rna_safe_id(prop->identifier));
1570                         break;
1571                 }
1572         }
1573
1574         fprintf(f, "\n");
1575 }
1576
1577 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1578 {
1579         PropertyRNA *prop;
1580
1581         prop = dp->prop;
1582
1583         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1584                 return;
1585
1586         switch (prop->type) {
1587                 case PROP_BOOLEAN: {
1588                         if (!prop->arraydimension)
1589                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1590                         else if (prop->totarraylength)
1591                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1592                                         rna_safe_id(prop->identifier));
1593                         break;
1594                 }
1595                 case PROP_INT: {
1596                         if (!prop->arraydimension)
1597                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1598                         else if (prop->totarraylength)
1599                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1600                                         rna_safe_id(prop->identifier));
1601                         break;
1602                 }
1603                 case PROP_FLOAT: {
1604                         if (!prop->arraydimension)
1605                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1606                         else if (prop->totarraylength)
1607                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1608                                         rna_safe_id(prop->identifier));
1609                         break;
1610                 }
1611                 case PROP_ENUM: {
1612                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", rna_safe_id(prop->identifier), srna->identifier,
1613                                 rna_safe_id(prop->identifier));
1614
1615                         break;
1616                 }
1617                 case PROP_STRING: {
1618                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1619                         break;
1620                 }
1621                 case PROP_POINTER: {
1622                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1623
1624                         if (pprop->type)
1625                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (const char*)pprop->type, srna->identifier,
1626                                         rna_safe_id(prop->identifier));
1627                         else
1628                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier,
1629                                         rna_safe_id(prop->identifier));
1630                         break;
1631                 }
1632                 case PROP_COLLECTION: {
1633 #if 0
1634                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1635
1636                         if (cprop->type)
1637                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char*)cprop->type, srna->identifier,
1638                                         prop->identifier);
1639                         else
1640                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1641 #endif
1642                         break;
1643                 }
1644         }
1645
1646         fprintf(f, "\n");
1647 }
1648
1649 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1650 {
1651         StructRNA *srna;
1652         FunctionRNA *func;
1653         PropertyDefRNA *dparm;
1654         PropertyType type;
1655         const char *funcname, *valstr;
1656         const char *ptrstr;
1657         const short has_data = (dfunc->cont.properties.first != NULL);
1658         int flag, pout, cptr, first;
1659
1660         srna = dsrna->srna;
1661         func = dfunc->func;
1662
1663         if (!dfunc->call)
1664                 return;
1665
1666         funcname = rna_alloc_function_name(srna->identifier, func->identifier, "call");
1667
1668         /* function definition */
1669         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1670         fprintf(f, "\n{\n");
1671
1672         /* variable definitions */
1673         
1674         if (func->flag & FUNC_USE_SELF_ID) {
1675                 fprintf(f, "\tstruct ID *_selfid;\n");
1676         }
1677
1678         if ((func->flag & FUNC_NO_SELF) == 0) {
1679                 if (dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1680                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1681         }
1682
1683         dparm = dfunc->cont.properties.first;
1684         for (; dparm; dparm = dparm->next) {
1685                 type = dparm->prop->type;
1686                 flag = dparm->prop->flag;
1687                 pout = (flag & PROP_OUTPUT);
1688                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1689
1690                 if (dparm->prop == func->c_ret)
1691                         ptrstr = cptr || dparm->prop->arraydimension ? "*" : "";
1692                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1693                 else if (cptr || (flag & PROP_DYNAMIC))
1694                         ptrstr = pout ? "**" : "*";
1695                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1696                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1697                         ptrstr = "*";
1698                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack,
1699                  * but type name for string props is already char*, so leave empty */
1700                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1701                         ptrstr = "";
1702                 else
1703                         ptrstr = pout ? "*" : "";
1704
1705                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1706                 if (flag & PROP_DYNAMIC)
1707                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1708                 
1709                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
1710                         ptrstr, dparm->prop->identifier);
1711         }
1712
1713         if (has_data) {
1714                 fprintf(f, "\tchar *_data");
1715                 if (func->c_ret) fprintf(f, ", *_retdata");
1716                 fprintf(f, ";\n");
1717                 fprintf(f, "\t\n");
1718         }
1719
1720         /* assign self */
1721         if (func->flag & FUNC_USE_SELF_ID) {
1722                 fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1723         }
1724         
1725         if ((func->flag & FUNC_NO_SELF) == 0) {
1726                 if (dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1727                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1728         }
1729
1730         if (has_data) {
1731                 fprintf(f, "\t_data= (char *)_parms->data;\n");
1732         }
1733
1734         dparm = dfunc->cont.properties.first;
1735         for (; dparm; dparm = dparm->next) {
1736                 type = dparm->prop->type;
1737                 flag = dparm->prop->flag;
1738                 pout = (flag & PROP_OUTPUT);
1739                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1740
1741                 if (dparm->prop == func->c_ret)
1742                         fprintf(f, "\t_retdata= _data;\n");
1743                 else  {
1744                         const char *data_str;
1745                         if (cptr || (flag & PROP_DYNAMIC)) {
1746                                 ptrstr = "**";
1747                                 valstr = "*";
1748                         }
1749                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1750                                 ptrstr = "*";
1751                                 valstr = "";
1752                         }
1753                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1754                                 ptrstr = "";
1755                                 valstr = "";
1756                         }
1757                         else {
1758                                 ptrstr = "*";
1759                                 valstr = "*";
1760                         }
1761
1762                         /* this must be kept in sync with RNA_parameter_length_get_data,
1763                          * we could just call the function directly, but this is faster */
1764                         if (flag & PROP_DYNAMIC) {
1765                                 fprintf(f, "\t%s_len= %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
1766                                 data_str = "(&(((char *)_data)[sizeof(void *)]))";
1767                         }
1768                         else {
1769                                 data_str = "_data";
1770                         }
1771                         fprintf(f, "\t%s= ", dparm->prop->identifier);
1772
1773                         if (!pout)
1774                                 fprintf(f, "%s", valstr);
1775
1776                         fprintf(f, "((%s%s%s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
1777                                 ptrstr, data_str);
1778                 }
1779
1780                 if (dparm->next)
1781                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size_alloc(dparm->prop));
1782         }
1783
1784         if (dfunc->call) {
1785                 fprintf(f, "\t\n");
1786                 fprintf(f, "\t");
1787                 if (func->c_ret) fprintf(f, "%s= ", func->c_ret->identifier);
1788                 fprintf(f, "%s(", dfunc->call);
1789
1790                 first = 1;
1791
1792                 if (func->flag & FUNC_USE_SELF_ID) {
1793                         fprintf(f, "_selfid");
1794                         first = 0;
1795                 }
1796
1797                 if ((func->flag & FUNC_NO_SELF) == 0) {
1798                         if (!first) fprintf(f, ", ");
1799                         fprintf(f, "_self");
1800                         first = 0;
1801                 }
1802
1803                 if (func->flag & FUNC_USE_MAIN) {
1804                         if (!first) fprintf(f, ", ");
1805                         first = 0;
1806                         fprintf(f, "CTX_data_main(C)"); /* may have direct access later */
1807                 }
1808
1809                 if (func->flag & FUNC_USE_CONTEXT) {
1810                         if (!first) fprintf(f, ", ");
1811                         first = 0;
1812                         fprintf(f, "C");
1813                 }
1814
1815                 if (func->flag & FUNC_USE_REPORTS) {
1816                         if (!first) fprintf(f, ", ");
1817                         first = 0;
1818                         fprintf(f, "reports");
1819                 }
1820
1821                 dparm = dfunc->cont.properties.first;
1822                 for (; dparm; dparm = dparm->next) {
1823                         if (dparm->prop == func->c_ret)
1824                                 continue;
1825
1826                         if (!first) fprintf(f, ", ");
1827                         first = 0;
1828
1829                         if (dparm->prop->flag & PROP_DYNAMIC)
1830                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
1831                         else
1832                                 fprintf(f, "%s", dparm->prop->identifier);
1833                 }
1834
1835                 fprintf(f, ");\n");
1836
1837                 if (func->c_ret) {
1838                         dparm = rna_find_parameter_def(func->c_ret);
1839                         ptrstr = (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) ||
1840                                   (dparm->prop->arraydimension)) ? "*" : "";
1841                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop),
1842                                 rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
1843                 }
1844         }
1845
1846         fprintf(f, "}\n\n");
1847
1848         dfunc->gencall = funcname;
1849 }
1850
1851 static void rna_auto_types(void)
1852 {
1853         StructDefRNA *ds;
1854         PropertyDefRNA *dp;
1855
1856         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
1857                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1858                 if (ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1859                         ds->dnaname = "bScreen";
1860
1861                 for (dp = ds->cont.properties.first; dp; dp = dp->next) {
1862                         if (dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1863                                 dp->dnastructname = "bScreen";
1864
1865                         if (dp->dnatype) {
1866                                 if (dp->prop->type == PROP_POINTER) {
1867                                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1868                                         StructRNA *type;
1869
1870                                         if (!pprop->type && !pprop->get)
1871                                                 pprop->type = (StructRNA*)rna_find_type(dp->dnatype);
1872
1873                                         if (pprop->type) {
1874                                                 type = rna_find_struct((const char*)pprop->type);
1875                                                 if (type && (type->flag & STRUCT_ID_REFCOUNT))
1876                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1877                                         }
1878                                 }
1879                                 else if (dp->prop->type == PROP_COLLECTION) {
1880                                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1881
1882                                         if (!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase") == 0)
1883                                                 cprop->item_type = (StructRNA*)rna_find_type(dp->dnatype);
1884                                 }
1885                         }
1886                 }
1887         }
1888 }
1889
1890 static void rna_sort(BlenderRNA *brna)
1891 {
1892         StructDefRNA *ds;
1893         StructRNA *srna;
1894
1895         rna_sortlist(&brna->structs, cmp_struct);
1896         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1897
1898         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1899                 rna_sortlist(&srna->cont.properties, cmp_property);
1900
1901         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
1902                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1903 }
1904
1905 static const char *rna_property_structname(PropertyType type)
1906 {
1907         switch (type) {
1908                 case PROP_BOOLEAN: return "BoolPropertyRNA";
1909                 case PROP_INT: return "IntPropertyRNA";
1910                 case PROP_FLOAT: return "FloatPropertyRNA";
1911                 case PROP_STRING: return "StringPropertyRNA";
1912                 case PROP_ENUM: return "EnumPropertyRNA";
1913                 case PROP_POINTER: return "PointerPropertyRNA";
1914                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1915                 default: return "UnknownPropertyRNA";
1916         }
1917 }
1918
1919 static const char *rna_property_subtypename(PropertySubType type)
1920 {
1921         switch (type) {
1922                 case PROP_NONE: return "PROP_NONE";
1923                 case PROP_FILEPATH: return "PROP_FILEPATH";
1924                 case PROP_FILENAME: return "PROP_FILENAME";
1925                 case PROP_DIRPATH: return "PROP_DIRPATH";
1926                 case PROP_BYTESTRING: return "PROP_BYTESTRING";
1927                 case PROP_TRANSLATE: return "PROP_TRANSLATE";
1928                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1929                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1930                 case PROP_FACTOR: return "PROP_FACTOR";
1931                 case PROP_ANGLE: return "PROP_ANGLE";
1932                 case PROP_TIME: return "PROP_TIME";
1933                 case PROP_DISTANCE: return "PROP_DISTANCE";
1934                 case PROP_COLOR: return "PROP_COLOR";
1935                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1936                 case PROP_DIRECTION: return "PROP_DIRECTION";
1937                 case PROP_MATRIX: return "PROP_MATRIX";
1938                 case PROP_EULER: return "PROP_EULER";
1939                 case PROP_QUATERNION: return "PROP_QUATERNION";
1940                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1941                 case PROP_VELOCITY: return "PROP_VELOCITY";
1942                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1943                 case PROP_XYZ: return "PROP_XYZ";
1944                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1945                 case PROP_COORDS: return "PROP_COORDS";
1946                 case PROP_LAYER: return "PROP_LAYER";
1947                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1948                 default: {
1949                         /* in case we don't have a type preset that includes the subtype */
1950                         if (RNA_SUBTYPE_UNIT(type)) {
1951                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1952                         }
1953                         else {
1954                                 return "PROP_SUBTYPE_UNKNOWN";
1955                         }
1956                 }
1957         }
1958 }
1959
1960 static const char *rna_property_subtype_unit(PropertySubType type)
1961 {
1962         switch (RNA_SUBTYPE_UNIT(type)) {
1963                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1964                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1965                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1966                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1967                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1968                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1969                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1970                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1971                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1972                 default:                                        return "PROP_UNIT_UNKNOWN";
1973         }
1974 }
1975
1976 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1977 {
1978         StructRNA *srna;
1979
1980         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1981                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1982         fprintf(f, "\n");
1983 }
1984
1985 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1986 {
1987         StructRNA *srna;
1988
1989         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1990
1991         srna = brna->structs.first;
1992         if (srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1993         else fprintf(f, "{NULL, ");
1994
1995         srna = brna->structs.last;
1996         if (srna) fprintf(f, "&RNA_%s}", srna->identifier);
1997         else fprintf(f, "NULL}");
1998
1999         fprintf(f, "};\n\n");
2000 }
2001
2002 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2003 {
2004         PropertyRNA *prop;
2005         StructRNA *base;
2006
2007         base = srna->base;
2008         while (base) {
2009                 fprintf(f, "\n");
2010                 for (prop = base->cont.properties.first; prop; prop = prop->next)
2011                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type),
2012                                 base->identifier, prop->identifier);
2013                 base = base->base;
2014         }
2015
2016         if (srna->cont.properties.first)
2017                 fprintf(f, "\n");
2018
2019         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2020                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type),
2021                         srna->identifier, prop->identifier);
2022         fprintf(f, "\n");
2023 }
2024
2025 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
2026 {
2027         PropertyRNA *parm;
2028
2029         for (parm = func->cont.properties.first; parm; parm = parm->next)
2030                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier,
2031                         func->identifier, parm->identifier);
2032
2033         if (func->cont.properties.first)
2034                 fprintf(f, "\n");
2035 }
2036
2037 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2038 {
2039         FunctionRNA *func;
2040         StructRNA *base;
2041
2042         base = srna->base;
2043         while (base) {
2044                 for (func = base->functions.first; func; func = func->cont.next) {
2045                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
2046                         rna_generate_parameter_prototypes(brna, base, func, f);
2047                 }
2048
2049                 if (base->functions.first)
2050                         fprintf(f, "\n");
2051
2052                 base = base->base;
2053         }
2054
2055         for (func = srna->functions.first; func; func = func->cont.next) {
2056                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
2057                 rna_generate_parameter_prototypes(brna, srna, func, f);
2058         }
2059
2060         if (srna->functions.first)
2061                 fprintf(f, "\n");
2062 }
2063
2064 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
2065 {
2066         FunctionRNA *func;
2067         PropertyDefRNA *dparm;
2068         StructDefRNA *dsrna;
2069         PropertyType type;
2070         int flag, pout, cptr, first;
2071         const char *ptrstr;
2072
2073         dsrna = rna_find_struct_def(srna);
2074         func = dfunc->func;
2075
2076         /* return type */
2077         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2078                 if (dparm->prop == func->c_ret) {
2079                         if (dparm->prop->arraydimension)
2080                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
2081                         else if (dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
2082                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2083                         else
2084                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2085
2086                         break;
2087                 }
2088         }
2089
2090         /* void if nothing to return */
2091         if (!dparm)
2092                 fprintf(f, "void ");
2093
2094         /* function name */
2095         fprintf(f, "%s(", dfunc->call);
2096
2097         first = 1;
2098
2099         /* self, context and reports parameters */
2100         if (func->flag & FUNC_USE_SELF_ID) {
2101                 fprintf(f, "struct ID *_selfid");
2102                 first = 0;
2103         }
2104         
2105         if ((func->flag & FUNC_NO_SELF) == 0) {
2106                 if (!first) fprintf(f, ", ");
2107                 if (dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
2108                 else fprintf(f, "struct %s *_self", srna->identifier);
2109                 first = 0;
2110         }
2111
2112         if (func->flag & FUNC_USE_MAIN) {
2113                 if (!first) fprintf(f, ", ");
2114                 first = 0;
2115                 fprintf(f, "Main *bmain");
2116         }
2117
2118         if (func->flag & FUNC_USE_CONTEXT) {
2119                 if (!first) fprintf(f, ", ");
2120                 first = 0;
2121                 fprintf(f, "bContext *C");
2122         }
2123
2124         if (func->flag & FUNC_USE_REPORTS) {
2125                 if (!first) fprintf(f, ", ");
2126                 first = 0;
2127                 fprintf(f, "ReportList *reports");
2128         }
2129
2130         /* defined parameters */
2131         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2132                 type = dparm->prop->type;
2133                 flag = dparm->prop->flag;
2134                 pout = (flag & PROP_OUTPUT);
2135                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2136
2137                 if (dparm->prop == func->c_ret)
2138                         continue;
2139
2140                 if (cptr || (flag & PROP_DYNAMIC))
2141                         ptrstr = pout ? "**" : "*";
2142                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
2143                         ptrstr = "*";
2144                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
2145                         ptrstr = "";
2146                 else
2147                         ptrstr = pout ? "*" : "";
2148
2149                 if (!first) fprintf(f, ", ");
2150                 first = 0;
2151
2152                 if (flag & PROP_DYNAMIC)
2153                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
2154
2155                 if (!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
2156                         fprintf(f, "%s%s %s[%u]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2157                                 dparm->prop->identifier, dparm->prop->totarraylength);
2158                 else
2159                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2160                                 ptrstr, dparm->prop->identifier);
2161
2162         }
2163
2164         fprintf(f, ");\n");
2165 }
2166
2167 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2168 {
2169         FunctionRNA *func;
2170         FunctionDefRNA *dfunc;
2171         int first = 1;
2172
2173         for (func = srna->functions.first; func; func = func->cont.next) {
2174                 dfunc = rna_find_function_def(func);
2175
2176                 if (dfunc->call) {
2177                         if (first) {
2178                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
2179                                 first = 0;
2180                         }
2181
2182                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
2183                 }
2184         }
2185
2186         fprintf(f, "\n");
2187 }
2188
2189 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop)
2190 {
2191         char *strnest = "", *errnest = "";
2192         int len, freenest = 0;
2193         
2194         if (nest != NULL) {
2195                 len = strlen(nest);
2196
2197                 strnest = MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
2198                 errnest = MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
2199
2200                 strcpy(strnest, "_"); strcat(strnest, nest);
2201                 strcpy(errnest, "."); strcat(errnest, nest);
2202
2203                 freenest = 1;
2204         }
2205
2206         switch (prop->type) {
2207                         case PROP_ENUM: {
2208                                 EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
2209                                 int i, defaultfound = 0, totflag = 0;
2210
2211                                 if (eprop->item) {
2212                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier,
2213                                                 strnest, prop->identifier, eprop->totitem+1);
2214
2215                                         for (i = 0; i<eprop->totitem; i++) {
2216                                                 fprintf(f, "{%d, ", eprop->item[i].value);
2217                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
2218                                                 fprintf(f, "%d, ", eprop->item[i].icon);
2219                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
2220                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
2221
2222                                                 if (eprop->item[i].identifier[0]) {
2223                                                         if (prop->flag & PROP_ENUM_FLAG) {
2224                                                                 totflag |= eprop->item[i].value;
2225                                                         }
2226                                                         else {
2227                                                                 if (eprop->defaultvalue == eprop->item[i].value) {
2228                                                                         defaultfound = 1;
2229                                                                 }
2230                                                         }
2231                                                 }
2232                                         }
2233
2234                                         fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
2235
2236                                         if (prop->flag & PROP_ENUM_FLAG) {
2237                                                 if (eprop->defaultvalue & ~totflag) {
2238                                                         fprintf(stderr, "%s: %s%s.%s, enum default includes unused bits (%d).\n",
2239                                                                 __func__, srna->identifier, errnest, prop->identifier,
2240                                                                 eprop->defaultvalue & ~totflag);
2241                                                         DefRNA.error = 1;
2242                                                 }
2243                                         }
2244                                         else {
2245                                                 if (!defaultfound) {
2246                                                         fprintf(stderr, "%s: %s%s.%s, enum default is not in items.\n",
2247                                                                 __func__, srna->identifier, errnest, prop->identifier);
2248                                                         DefRNA.error = 1;
2249                                                 }
2250                                         }
2251                                 }
2252                                 else {
2253                                         fprintf(stderr, "%s: %s%s.%s, enum must have items defined.\n",
2254                                                 __func__, srna->identifier, errnest, prop->identifier);
2255                                         DefRNA.error = 1;
2256                                 }
2257                                 break;
2258                         }
2259                         case PROP_BOOLEAN: {
2260                                 BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
2261                                 unsigned int i;
2262
2263                                 if (prop->arraydimension && prop->totarraylength) {
2264                                         fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2265                                                 prop->identifier, prop->totarraylength);
2266
2267                                         for (i = 0; i<prop->totarraylength; i++) {
2268                                                 if (bprop->defaultarray)
2269                                                         fprintf(f, "%d", bprop->defaultarray[i]);
2270                                                 else
2271                                                         fprintf(f, "%d", bprop->defaultvalue);
2272                                                 if (i != prop->totarraylength-1)
2273                                                         fprintf(f, ",\n\t");
2274                                         }
2275
2276                                         fprintf(f, "\n};\n\n");
2277                                 }
2278                                 break;
2279                         }
2280                         case PROP_INT: {
2281                                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
2282                                 unsigned int i;
2283
2284                                 if (prop->arraydimension && prop->totarraylength) {
2285                                         fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2286                                                 prop->identifier, prop->totarraylength);
2287
2288                                         for (i = 0; i<prop->totarraylength; i++) {
2289                                                 if (iprop->defaultarray)
2290                                                         fprintf(f, "%d", iprop->defaultarray[i]);
2291                                                 else
2292                                                         fprintf(f, "%d", iprop->defaultvalue);
2293                                                 if (i != prop->totarraylength-1)
2294                                                         fprintf(f, ",\n\t");
2295                                         }
2296
2297                                         fprintf(f, "\n};\n\n");
2298                                 }
2299                                 break;
2300                         }
2301                         case PROP_FLOAT: {
2302                                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
2303                                 unsigned int i;
2304
2305                                 if (prop->arraydimension && prop->totarraylength) {
2306                                         fprintf(f, "static float rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2307                                                 prop->identifier, prop->totarraylength);
2308
2309                                         for (i = 0; i<prop->totarraylength; i++) {
2310                                                 if (fprop->defaultarray)
2311                                                         rna_float_print(f, fprop->defaultarray[i]);
2312                                                 else
2313                                                         rna_float_print(f, fprop->defaultvalue);
2314                                                 if (i != prop->totarraylength-1)
2315                                                         fprintf(f, ",\n\t");
2316                                         }
2317
2318                                         fprintf(f, "\n};\n\n");
2319                                 }
2320                                 break;
2321                         }
2322                         default:
2323                                 break;
2324         }
2325
2326         fprintf(f, "%s%s rna_%s%s_%s = {\n",
2327                 (prop->flag & PROP_EXPORT) ? "": "",
2328                 rna_property_structname(prop->type),
2329                 srna->identifier, strnest, prop->identifier);
2330
2331         if (prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2332         else fprintf(f, "\t{NULL, ");
2333         if (prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2334         else fprintf(f, "NULL,\n");
2335         fprintf(f, "\t%d, ", prop->magic);
2336         rna_print_c_string(f, prop->identifier);
2337         fprintf(f, ", %d, ", prop->flag);
2338         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2339         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2340         fprintf(f, "%d,\n", prop->icon);
2341         rna_print_c_string(f, prop->translation_context); fprintf(f, ",\n\t");
2342         fprintf(f, "\t%s, %s|%s, %s, %u, {%u, %u, %u}, %u,\n",
2343                 RNA_property_typename(prop->type),
2344                 rna_property_subtypename(prop->subtype),
2345                 rna_property_subtype_unit(prop->subtype),
2346                 rna_function_string(prop->getlength),
2347                 prop->arraydimension,
2348                 prop->arraylength[0],
2349                 prop->arraylength[1],
2350                 prop->arraylength[2],
2351                 prop->totarraylength);
2352         fprintf(f, "\t%s%s, %d, %s, %s,\n",
2353                 (prop->flag & PROP_CONTEXT_UPDATE) ? "(UpdateFunc)": "",
2354                 rna_function_string(prop->update),
2355                 prop->noteflag,
2356                 rna_function_string(prop->editable),
2357                 rna_function_string(prop->itemeditable));
2358
2359         if (prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2360         else fprintf(f, "\t0, -1");
2361
2362         /* our own type - collections/arrays only */
2363         if (prop->srna) fprintf(f, ", &RNA_%s", (const char*)prop->srna);
2364         else fprintf(f, ", NULL");
2365
2366         fprintf(f, "},\n");
2367
2368         switch (prop->type) {
2369                         case PROP_BOOLEAN: {
2370                                 BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
2371                                 fprintf(f, "\t%s, %s, %s, %s, %d, ",
2372                                         rna_function_string(bprop->get),
2373                                         rna_function_string(bprop->set),
2374                                         rna_function_string(bprop->getarray),
2375                                         rna_function_string(bprop->setarray),
2376                                         bprop->defaultvalue);
2377                                 if (prop->arraydimension && prop->totarraylength)
2378                                         fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2379                                 else fprintf(f, "NULL\n");
2380                                 break;
2381                         }
2382                         case PROP_INT: {
2383                                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
2384                                 fprintf(f, "\t%s, %s, %s, %s, %s,\n\t",
2385                                         rna_function_string(iprop->get),
2386                                         rna_function_string(iprop->set),
2387                                         rna_function_string(iprop->getarray),
2388                                         rna_function_string(iprop->setarray),
2389                                         rna_function_string(iprop->range));
2390                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2391                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2392                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2393                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2394                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
2395                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
2396                                 if (prop->arraydimension && prop->totarraylength)
2397                                         fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2398                                 else fprintf(f, "NULL\n");
2399                                 break;
2400                         }
2401                         case PROP_FLOAT: {
2402                                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
2403                                 fprintf(f, "\t%s, %s, %s, %s, %s, ",
2404                                         rna_function_string(fprop->get),
2405                                         rna_function_string(fprop->set),
2406                                         rna_function_string(fprop->getarray),
2407                                         rna_function_string(fprop->setarray),
2408                                         rna_function_string(fprop->range));
2409                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2410                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2411                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2412                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2413                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
2414                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2415                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2416                                 if (prop->arraydimension && prop->totarraylength)
2417                                         fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2418                                 else fprintf(f, "NULL\n");
2419                                 break;
2420                         }
2421                         case PROP_STRING: {
2422                                 StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
2423                                 fprintf(f, "\t%s, %s, %s, %d, ",
2424                                         rna_function_string(sprop->get),
2425                                         rna_function_string(sprop->length),
2426                                         rna_function_string(sprop->set),
2427                                         sprop->maxlength);
2428                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2429                                 break;
2430                         }
2431                         case PROP_ENUM: {
2432                                 EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
2433                                 fprintf(f, "\t%s, %s, %s, NULL, ",
2434                                         rna_function_string(eprop->get),
2435                                         rna_function_string(eprop->set),
2436                                         rna_function_string(eprop->itemf));
2437                                 if (eprop->item)
2438                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2439                                 else
2440                                         fprintf(f, "NULL, ");
2441                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2442                                 break;
2443                         }
2444                         case PROP_POINTER: {
2445                                 PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
2446                                 fprintf(f, "\t%s, %s, %s, %s,", rna_function_string(pprop->get),
2447                                         rna_function_string(pprop->set),
2448                                         rna_function_string(pprop->typef),
2449                                         rna_function_string(pprop->poll));
2450                                 if (pprop->type) fprintf(f, "&RNA_%s\n", (const char*)pprop->type);
2451                                 else fprintf(f, "NULL\n");
2452                                 break;
2453                         }
2454                         case PROP_COLLECTION: {
2455                                 CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
2456                                 fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, ",
2457                                         rna_function_string(cprop->begin),
2458                                         rna_function_string(cprop->next),
2459                                         rna_function_string(cprop->end),
2460                                         rna_function_string(cprop->get),
2461                                         rna_function_string(cprop->length),
2462                                         rna_function_string(cprop->lookupint),
2463                                         rna_function_string(cprop->lookupstring),
2464                                         rna_function_string(cprop->assignint));
2465                                 if (cprop->item_type) fprintf(f, "&RNA_%s\n", (const char*)cprop->item_type);
2466                                 else fprintf(f, "NULL\n");
2467                                 break;
2468                         }
2469         }
2470
2471         fprintf(f, "};\n\n");
2472
2473         if (freenest) {
2474                 MEM_freeN(strnest);
2475                 MEM_freeN(errnest);
2476         }
2477 }
2478
2479 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
2480 {
2481         FunctionRNA *func;
2482         FunctionDefRNA *dfunc;
2483         PropertyRNA *prop, *parm;
2484         StructRNA *base;
2485
2486         fprintf(f, "/* %s */\n", srna->name);
2487
2488         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2489                 rna_generate_property(f, srna, NULL, prop);
2490
2491         for (func = srna->functions.first; func; func = func->cont.next) {
2492                 for (parm = func->cont.properties.first; parm; parm = parm->next)
2493                         rna_generate_property(f, srna, func->identifier, parm);
2494
2495                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2496
2497                 if (func->cont.next)
2498                         fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier,
2499                                 ((FunctionRNA*)func->cont.next)->identifier);
2500                 else
2501                         fprintf(f, "\t{NULL, ");
2502                 if (func->cont.prev)
2503                         fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier,
2504                                 ((FunctionRNA*)func->cont.prev)->identifier);
2505                 else
2506                         fprintf(f, "NULL,\n");
2507
2508                 fprintf(f, "\tNULL,\n");
2509
2510                 parm = func->cont.properties.first;
2511                 if (parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2512                 else fprintf(f, "\t{NULL, ");
2513
2514                 parm = func->cont.properties.last;
2515                 if (parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2516                 else fprintf(f, "NULL}},\n");
2517
2518                 fprintf(f, "\t");
2519                 rna_print_c_string(f, func->identifier);
2520                 fprintf(f, ", %d, ", func->flag);
2521                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2522
2523                 dfunc = rna_find_function_def(func);
2524                 if (dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2525                 else fprintf(f, "\tNULL,\n");
2526
2527                 if (func->c_ret)
2528                         fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2529                 else
2530                         fprintf(f, "\tNULL\n");
2531
2532                 fprintf(f, "};\n");
2533                 fprintf(f, "\n");
2534         }
2535
2536         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2537
2538         if (srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
2539         else fprintf(f, "\t{NULL, ");
2540         if (srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
2541         else fprintf(f, "NULL,\n");
2542
2543         fprintf(f, "\tNULL,\n");
2544
2545         prop = srna->cont.properties.first;
2546         if (prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2547         else fprintf(f, "\t{NULL, ");
2548
2549         prop = srna->cont.properties.last;
2550         if (prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2551         else fprintf(f, "NULL}},\n");
2552         fprintf(f, "\t");
2553         rna_print_c_string(f, srna->identifier);
2554         fprintf(f, "\t, NULL,NULL\n"); /* PyType - Cant initialize here */
2555         fprintf(f, ", %d, ", srna->flag);
2556         rna_print_c_string(f, srna->name);
2557         fprintf(f, ", ");
2558         rna_print_c_string(f, srna->description);
2559         fprintf(f, ", ");
2560         rna_print_c_string(f, srna->translation_context);
2561         fprintf(f, ",\n\t%d,\n", srna->icon);
2562
2563         prop = srna->nameproperty;
2564         if (prop) {
2565                 base = srna;
2566                 while (base->base && base->base->nameproperty == prop)
2567                         base = base->base;
2568
2569                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2570         }
2571         else fprintf(f, "\tNULL, ");
2572
2573         prop = srna->iteratorproperty;
2574         base = srna;
2575         while (base->base && base->base->iteratorproperty == prop)
2576                 base = base->base;
2577         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2578
2579         if (srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2580         else fprintf(f, "\tNULL,\n");
2581
2582         if (srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2583         else fprintf(f, "\tNULL,\n");
2584
2585         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2586         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2587         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2588         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2589         fprintf(f, "\t%s,\n", rna_function_string(srna->instance));
2590         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2591
2592         if (srna->reg && !srna->refine) {
2593                 fprintf(stderr, "%s: %s has a register function, must also have refine function.\n",
2594                         __func__, srna->identifier);
2595                 DefRNA.error = 1;
2596         }
2597
2598         func = srna->functions.first;
2599         if (func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2600         else fprintf(f, "\t{NULL, ");
2601
2602         func = srna->functions.last;
2603         if (func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2604         else fprintf(f, "NULL}\n");
2605
2606         fprintf(f, "};\n");
2607
2608         fprintf(f, "\n");
2609 }
2610
2611 typedef struct RNAProcessItem {
2612         const char *filename;
2613         const char *api_filename;
2614         void (*define)(BlenderRNA *brna);
2615 } RNAProcessItem;
2616
2617 static RNAProcessItem PROCESS_ITEMS[] = {
2618         {"rna_rna.c", NULL, RNA_def_rna},
2619         {"rna_ID.c", NULL, RNA_def_ID},
2620         {"rna_texture.c", "rna_texture_api.c", RNA_def_texture},
2621         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2622         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2623         {"rna_animviz.c", NULL, RNA_def_animviz},
2624         {"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
2625         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2626         {"rna_boid.c", NULL, RNA_def_boid},
2627         {"rna_brush.c", NULL, RNA_def_brush},
2628         {"rna_camera.c", "rna_camera_api.c", RNA_def_camera},
2629         {"rna_cloth.c", NULL, RNA_def_cloth},
2630         {"rna_color.c", NULL, RNA_def_color},
2631         {"rna_constraint.c", NULL, RNA_def_constraint},
2632         {"rna_context.c", NULL, RNA_def_context},
2633         {"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
2634         {"rna_curve.c", NULL, RNA_def_curve},
2635         {"rna_dynamicpaint.c", NULL, RNA_def_dynamic_paint},
2636         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2637         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2638         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2639         {"rna_group.c", NULL, RNA_def_group},
2640         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2641         {"rna_key.c", NULL, RNA_def_key},
2642         {"rna_lamp.c", NULL, RNA_def_lamp},
2643         {"rna_lattice.c", NULL, RNA_def_lattice},
2644         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2645         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2646         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2647         {"rna_meta.c", NULL, RNA_def_meta},
2648         {"rna_modifier.c", NULL, RNA_def_modifier},
2649         {"rna_nla.c", NULL, RNA_def_nla},
2650         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2651         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2652         {"rna_object_force.c", NULL, RNA_def_object_force},
2653         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2654         {"rna_particle.c", NULL, RNA_def_particle},
2655         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2656         {"rna_property.c", NULL, RNA_def_gameproperty},
2657         {"rna_render.c", NULL, RNA_def_render},
2658         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2659         {"rna_screen.c", NULL, RNA_def_screen},
2660         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2661         {"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
2662         {"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
2663         {"rna_smoke.c", NULL, RNA_def_smoke},
2664         {"rna_space.c", NULL, RNA_def_space},
2665         {"rna_speaker.c", NULL, RNA_def_speaker},
2666         {"rna_test.c", NULL, RNA_def_test},
2667         {"rna_text.c", "rna_text_api.c", RNA_def_text},
2668         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2669         {"rna_sound.c", NULL, RNA_def_sound},
2670         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2671         {"rna_userdef.c", NULL, RNA_def_userdef},
2672         {"rna_vfont.c", NULL, RNA_def_vfont},
2673         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2674         {"rna_world.c", NULL, RNA_def_world},
2675         {"rna_movieclip.c", NULL, RNA_def_movieclip},
2676         {"rna_tracking.c", NULL, RNA_def_tracking},
2677         {NULL, NULL}};
2678
2679 static void rna_generate(BlenderRNA *brna, FILE *f, const char *filename, const char *api_filename)
2680 {
2681         StructDefRNA *ds;
2682         PropertyDefRNA *dp;
2683         FunctionDefRNA *dfunc;
2684         
2685         fprintf(f,
2686                 "\n"
2687                 "/* Automatically generated struct definitions for the Data API.\n"
2688                 " * Do not edit manually, changes will be overwritten.           */\n\n"
2689                 "#define RNA_RUNTIME\n\n");
2690
2691         fprintf(f, "#include <float.h>\n");
2692         fprintf(f, "#include <stdio.h>\n");
2693         fprintf(f, "#include <limits.h>\n");
2694         fprintf(f, "#include <string.h>\n\n");
2695         fprintf(f, "#include <stddef.h>\n\n");
2696
2697         fprintf(f, "#include \"DNA_ID.h\"\n");
2698         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2699
2700         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2701         fprintf(f, "#include \"BLI_utildefines.h\"\n\n");
2702
2703         fprintf(f, "#include \"BKE_context.h\"\n");
2704         fprintf(f, "#include \"BKE_library.h\"\n");
2705         fprintf(f, "#include \"BKE_main.h\"\n");
2706         fprintf(f, "#include \"BKE_report.h\"\n");
2707
2708         fprintf(f, "#include \"RNA_define.h\"\n");
2709         fprintf(f, "#include \"RNA_types.h\"\n");
2710         fprintf(f, "#include \"rna_internal.h\"\n\n");
2711
2712         rna_generate_prototypes(brna, f);
2713
2714         fprintf(f, "#include \"%s\"\n", filename);
2715         if (api_filename)
2716                 fprintf(f, "#include \"%s\"\n", api_filename);
2717         fprintf(f, "\n");
2718
2719         fprintf(f, "/* Autogenerated Functions */\n\n");
2720
2721         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2722                 if (!filename || ds->filename == filename) {
2723                         rna_generate_property_prototypes(brna, ds->srna, f);
2724                         rna_generate_function_prototypes(brna, ds->srna, f);
2725                 }
2726         }
2727
2728         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2729                 if (!filename || ds->filename == filename)
2730                         for (dp = ds->cont.properties.first; dp; dp = dp->next)
2731                                 rna_def_property_funcs(f, ds->srna, dp);
2732
2733         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2734                 if (!filename || ds->filename == filename) {
2735                         for (dfunc = ds->functions.first; dfunc; dfunc = dfunc->cont.next)
2736                                 rna_def_function_funcs(f, ds, dfunc);
2737
2738                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2739                 }
2740         }
2741
2742         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2743                 if (!filename || ds->filename == filename)
2744                         rna_generate_struct(brna, ds->srna, f);
2745
2746         if (strcmp(filename, "rna_ID.c") == 0) {
2747                 /* this is ugly, but we cannot have c files compiled for both
2748                  * makesrna and blender with some build systems at the moment */
2749                 fprintf(f, "#include \"rna_define.c\"\n\n");
2750
2751                 rna_generate_blender(brna, f);
2752         }
2753 }
2754
2755 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2756 {
2757         StructDefRNA *ds;
2758         PropertyDefRNA *dp;
2759         StructRNA *srna;
2760
2761         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2762         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2763
2764         fprintf(f,
2765                 "/* Automatically generated function declarations for the Data API.\n"
2766                 " * Do not edit manually, changes will be overwritten.              */\n\n");
2767
2768         fprintf(f, "#include \"RNA_types.h\"\n\n");
2769
2770         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2771
2772         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2773         fprintf(f, "    { \\\n");
2774         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2775         fprintf(f, "            for (property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; "
2776                    "property##_next(&rna_macro_iter)) { \\\n");
2777         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2778
2779         fprintf(f, "#define FOREACH_END(property) \\\n");
2780         fprintf(f, "            } \\\n");
2781         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2782         fprintf(f, "    }\n\n");
2783
2784         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2785                 srna = ds->srna;
2786
2787                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2788
2789                 while (srna) {
2790                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2791                         srna = srna->base;
2792                 }
2793                 fprintf(f, "\n");
2794
2795                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2796                         rna_def_property_funcs_header(f, ds->srna, dp);
2797         }
2798
2799         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2800
2801         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2802 }
2803
2804 static const char *cpp_classes = ""
2805 "\n"
2806 "#include <string>\n"
2807 "#include <string.h> /* for memcpy */\n"
2808 "\n"
2809 "namespace BL {\n"
2810 "\n"
2811 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2812 "       inline bool sname::identifier(void) { return sname##_##identifier##_get(&ptr)? true: false; }\n"
2813 "\n"
2814 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2815 "       inline Array<int,size> sname::identifier(void) \\\n"
2816 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2817 "\n"
2818 "#define INT_PROPERTY(sname, identifier) \\\n"
2819 "       inline int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2820 "\n"
2821 "#define INT_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 FLOAT_PROPERTY(sname, identifier) \\\n"
2826 "       inline float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2827 "\n"
2828 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2829 "       inline Array<float,size> sname::identifier(void) \\\n"
2830 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2831 "\n"
2832 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2833 "       inline sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2834 "\n"
2835 "#define STRING_PROPERTY(sname, identifier) \\\n"
2836 "       inline std::string sname::identifier(void) { \\\n"
2837 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2838 "               std::string str; str.resize(len); \\\n"
2839 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2840 "\n"
2841 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2842 "       inline type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"