93e83492efad0c6d484358f7afb669e25bebbabb
[blender.git] / source / blender / makesrna / intern / rna_access.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Blender Foundation (2008).
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_ID.h"
31 #include "DNA_windowmanager_types.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_dynstr.h"
35 #include "BLI_ghash.h"
36
37 #include "BKE_context.h"
38 #include "BKE_idprop.h"
39 #include "BKE_report.h"
40 #include "BKE_utildefines.h"
41
42 #include "WM_api.h"
43
44 #include "RNA_access.h"
45 #include "RNA_define.h"
46 #include "RNA_types.h"
47
48 #include "rna_internal.h"
49
50 /* Init/Exit */
51
52 void RNA_init()
53 {
54         StructRNA *srna;
55         PropertyRNA *prop;
56
57         for(srna=BLENDER_RNA.structs.first; srna; srna=srna->cont.next) {
58                 if(!srna->cont.prophash) {
59                         srna->cont.prophash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
60
61                         for(prop=srna->cont.properties.first; prop; prop=prop->next)
62                                 if(!(prop->flag & PROP_BUILTIN))
63                                         BLI_ghash_insert(srna->cont.prophash, (void*)prop->identifier, prop);
64                 }
65         }
66 }
67
68 void RNA_exit()
69 {
70         StructRNA *srna;
71
72         for(srna=BLENDER_RNA.structs.first; srna; srna=srna->cont.next) {
73                 if(srna->cont.prophash) {
74                         BLI_ghash_free(srna->cont.prophash, NULL, NULL);
75                         srna->cont.prophash= NULL;
76                 }
77         }
78
79         RNA_free(&BLENDER_RNA);
80 }
81
82 /* Pointer */
83
84 PointerRNA PointerRNA_NULL = {{0}, 0, 0};
85
86 void RNA_main_pointer_create(struct Main *main, PointerRNA *r_ptr)
87 {
88         r_ptr->id.data= NULL;
89         r_ptr->type= &RNA_Main;
90         r_ptr->data= main;
91 }
92
93 void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
94 {
95         PointerRNA tmp;
96         StructRNA *type, *idtype= NULL;
97
98         if(id) {
99                 memset(&tmp, 0, sizeof(tmp));
100                 tmp.data= id;
101                 idtype= rna_ID_refine(&tmp);
102                 
103                 while(idtype->refine) {
104                         type= idtype->refine(&tmp);
105
106                         if(type == idtype)
107                                 break;
108                         else
109                                 idtype= type;
110                 }
111         }
112         
113         r_ptr->id.data= id;
114         r_ptr->type= idtype;
115         r_ptr->data= id;
116 }
117
118 void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
119 {
120         PointerRNA tmp;
121         StructRNA *idtype= NULL;
122
123         if(id) {
124                 memset(&tmp, 0, sizeof(tmp));
125                 tmp.data= id;
126                 idtype= rna_ID_refine(&tmp);
127         }
128
129         r_ptr->id.data= id;
130         r_ptr->type= type;
131         r_ptr->data= data;
132
133         if(data) {
134                 while(r_ptr->type && r_ptr->type->refine) {
135                         StructRNA *rtype= r_ptr->type->refine(r_ptr);
136
137                         if(rtype == r_ptr->type)
138                                 break;
139                         else
140                                 r_ptr->type= rtype;
141                 }
142         }
143 }
144
145 static void rna_pointer_inherit_id(StructRNA *type, PointerRNA *parent, PointerRNA *ptr)
146 {
147         if(type && type->flag & STRUCT_ID) {
148                 ptr->id.data= ptr->data;
149         }
150         else {
151                 ptr->id.data= parent->id.data;
152         }
153 }
154
155 void RNA_blender_rna_pointer_create(PointerRNA *r_ptr)
156 {
157         r_ptr->id.data= NULL;
158         r_ptr->type= &RNA_BlenderRNA;
159         r_ptr->data= &BLENDER_RNA;
160 }
161
162 PointerRNA rna_pointer_inherit_refine(PointerRNA *ptr, StructRNA *type, void *data)
163 {
164         PointerRNA result;
165
166         if(data) {
167                 result.data= data;
168                 result.type= type;
169                 rna_pointer_inherit_id(type, ptr, &result);
170
171                 while(result.type->refine) {
172                         type= result.type->refine(&result);
173
174                         if(type == result.type)
175                                 break;
176                         else
177                                 result.type= type;
178                 }
179         }
180         else
181                 memset(&result, 0, sizeof(result));
182         
183         return result;
184 }
185
186 /* ID Properties */
187
188 IDProperty *RNA_struct_idproperties(PointerRNA *ptr, int create)
189 {
190         StructRNA *type= ptr->type;
191
192         if(type && type->idproperties)
193                 return type->idproperties(ptr, create);
194         
195         return NULL;
196 }
197
198 static IDProperty *rna_idproperty_find(PointerRNA *ptr, const char *name)
199 {
200         IDProperty *group= RNA_struct_idproperties(ptr, 0);
201         IDProperty *idprop;
202
203         if(group) {
204                 for(idprop=group->data.group.first; idprop; idprop=idprop->next)
205                         if(strcmp(idprop->name, name) == 0)
206                                 return idprop;
207         }
208         
209         return NULL;
210 }
211
212 static int rna_ensure_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
213 {
214         if(prop->magic == RNA_MAGIC) {
215                 int arraylen[RNA_MAX_ARRAY_DIMENSION];
216                 return (prop->getlength)? prop->getlength(ptr, arraylen): prop->totarraylength;
217         }
218         else {
219                 IDProperty *idprop= (IDProperty*)prop;
220
221                 if(idprop->type == IDP_ARRAY)
222                         return idprop->len;
223                 else
224                         return 0;
225         }
226 }
227
228 static int rna_ensure_property_array_check(PointerRNA *ptr, PropertyRNA *prop)
229 {
230         if(prop->magic == RNA_MAGIC) {
231                 return (prop->getlength || prop->totarraylength) ? 1:0;
232         }
233         else {
234                 IDProperty *idprop= (IDProperty*)prop;
235
236                 return idprop->type == IDP_ARRAY ? 1:0;
237         }
238 }
239
240 static void rna_ensure_property_multi_array_length(PointerRNA *ptr, PropertyRNA *prop, int length[])
241 {
242         if(prop->magic == RNA_MAGIC) {
243                 if(prop->getlength)
244                         prop->getlength(ptr, length);
245                 else
246                         memcpy(length, prop->arraylength, prop->arraydimension*sizeof(int));
247         }
248         else {
249                 IDProperty *idprop= (IDProperty*)prop;
250
251                 if(idprop->type == IDP_ARRAY)
252                         length[0]= idprop->len;
253                 else
254                         length[0]= 0;
255         }
256 }
257
258 static int rna_idproperty_verify_valid(PointerRNA *ptr, PropertyRNA *prop, IDProperty *idprop)
259 {
260         /* this verifies if the idproperty actually matches the property
261          * description and otherwise removes it. this is to ensure that
262          * rna property access is type safe, e.g. if you defined the rna
263          * to have a certain array length you can count on that staying so */
264         
265         switch(idprop->type) {
266                 case IDP_IDPARRAY:
267                         if(prop->type != PROP_COLLECTION)
268                                 return 0;
269                         break;
270                 case IDP_ARRAY:
271                         if(rna_ensure_property_array_length(ptr, prop) != idprop->len)
272                                 return 0;
273
274                         if(idprop->subtype == IDP_FLOAT && prop->type != PROP_FLOAT)
275                                 return 0;
276                         if(idprop->subtype == IDP_INT && !ELEM3(prop->type, PROP_BOOLEAN, PROP_INT, PROP_ENUM))
277                                 return 0;
278
279                         break;
280                 case IDP_INT:
281                         if(!ELEM3(prop->type, PROP_BOOLEAN, PROP_INT, PROP_ENUM))
282                                 return 0;
283                         break;
284                 case IDP_FLOAT:
285                 case IDP_DOUBLE:
286                         if(prop->type != PROP_FLOAT)
287                                 return 0;
288                         break;
289                 case IDP_STRING:
290                         if(prop->type != PROP_STRING)
291                                 return 0;
292                         break;
293                 case IDP_GROUP:
294                         if(prop->type != PROP_POINTER)
295                                 return 0;
296                         break;
297                 default:
298                         return 0;
299         }
300
301         return 1;
302 }
303
304 static PropertyRNA *typemap[IDP_NUMTYPES] =
305         {(PropertyRNA*)&rna_IDProperty_string,
306          (PropertyRNA*)&rna_IDProperty_int,
307          (PropertyRNA*)&rna_IDProperty_float,
308          NULL, NULL, NULL,
309          (PropertyRNA*)&rna_IDProperty_group, NULL,
310          (PropertyRNA*)&rna_IDProperty_double};
311
312 static PropertyRNA *arraytypemap[IDP_NUMTYPES] =
313         {NULL, (PropertyRNA*)&rna_IDProperty_int_array,
314          (PropertyRNA*)&rna_IDProperty_float_array,
315          NULL, NULL, NULL,
316          (PropertyRNA*)&rna_IDProperty_collection, NULL,
317          (PropertyRNA*)&rna_IDProperty_double_array};
318
319 IDProperty *rna_idproperty_check(PropertyRNA **prop, PointerRNA *ptr)
320 {
321         /* This is quite a hack, but avoids some complexity in the API. we
322          * pass IDProperty structs as PropertyRNA pointers to the outside.
323          * We store some bytes in PropertyRNA structs that allows us to
324          * distinguish it from IDProperty structs. If it is an ID property,
325          * we look up an IDP PropertyRNA based on the type, and set the data
326          * pointer to the IDProperty. */
327
328         if((*prop)->magic == RNA_MAGIC) {
329                 if((*prop)->flag & PROP_IDPROPERTY) {
330                         IDProperty *idprop= rna_idproperty_find(ptr, (*prop)->identifier);
331
332                         if(idprop && !rna_idproperty_verify_valid(ptr, *prop, idprop)) {
333                                 IDProperty *group= RNA_struct_idproperties(ptr, 0);
334
335                                 IDP_RemFromGroup(group, idprop);
336                                 IDP_FreeProperty(idprop);
337                                 MEM_freeN(idprop);
338                                 return NULL;
339                         }
340
341                         return idprop;
342                 }
343                 else
344                         return NULL;
345         }
346
347         {
348                 IDProperty *idprop= (IDProperty*)(*prop);
349
350                 if(idprop->type == IDP_ARRAY)
351                         *prop= arraytypemap[(int)(idprop->subtype)];
352                 else 
353                         *prop= typemap[(int)(idprop->type)];
354
355                 return idprop;
356         }
357 }
358
359 static PropertyRNA *rna_ensure_property(PropertyRNA *prop)
360 {
361         /* the quick version if we don't need the idproperty */
362
363         if(prop->magic == RNA_MAGIC)
364                 return prop;
365
366         {
367                 IDProperty *idprop= (IDProperty*)prop;
368
369                 if(idprop->type == IDP_ARRAY)
370                         return arraytypemap[(int)(idprop->subtype)];
371                 else 
372                         return typemap[(int)(idprop->type)];
373         }
374 }
375
376 static const char *rna_ensure_property_identifier(PropertyRNA *prop)
377 {
378         if(prop->magic == RNA_MAGIC)
379                 return prop->identifier;
380         else
381                 return ((IDProperty*)prop)->name;
382 }
383
384 static const char *rna_ensure_property_description(PropertyRNA *prop)
385 {
386         if(prop->magic == RNA_MAGIC)
387                 return prop->description;
388         else
389                 return ((IDProperty*)prop)->name; /* XXX - not correct */
390 }
391
392 static const char *rna_ensure_property_name(PropertyRNA *prop)
393 {
394         if(prop->magic == RNA_MAGIC)
395                 return prop->name;
396         else
397                 return ((IDProperty*)prop)->name;
398 }
399
400 /* Structs */
401
402 const char *RNA_struct_identifier(StructRNA *type)
403 {
404         return type->identifier;
405 }
406
407 const char *RNA_struct_ui_name(StructRNA *type)
408 {
409         return type->name;
410 }
411
412 int RNA_struct_ui_icon(StructRNA *type)
413 {
414         if(type)
415                 return type->icon;
416         else
417                 return ICON_DOT;
418 }
419
420 const char *RNA_struct_ui_description(StructRNA *type)
421 {
422         return type->description;
423 }
424
425 PropertyRNA *RNA_struct_name_property(StructRNA *type)
426 {
427         return type->nameproperty;
428 }
429
430 PropertyRNA *RNA_struct_iterator_property(StructRNA *type)
431 {
432         return type->iteratorproperty;
433 }
434
435 StructRNA *RNA_struct_base(StructRNA *type)
436 {
437         return type->base;
438 }
439
440 int RNA_struct_is_ID(StructRNA *type)
441 {
442         return (type->flag & STRUCT_ID) != 0;
443 }
444
445 int RNA_struct_is_a(StructRNA *type, StructRNA *srna)
446 {
447         StructRNA *base;
448
449         if(!type)
450                 return 0;
451
452         /* ptr->type is always maximally refined */
453         for(base=type; base; base=base->base)
454                 if(base == srna)
455                         return 1;
456         
457         return 0;
458 }
459
460 PropertyRNA *RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
461 {
462         PropertyRNA *iterprop= RNA_struct_iterator_property(ptr->type);
463         PointerRNA propptr;
464
465         if(RNA_property_collection_lookup_string(ptr, iterprop, identifier, &propptr))
466                 return propptr.data;
467         
468         return NULL;
469 }
470
471 /* Find the property which uses the given nested struct */
472 PropertyRNA *RNA_struct_find_nested(PointerRNA *ptr, StructRNA *srna)
473 {
474         PropertyRNA *prop= NULL;
475
476         RNA_STRUCT_BEGIN(ptr, iprop) {
477                 /* This assumes that there can only be one user of this nested struct */
478                 if (RNA_property_pointer_type(ptr, iprop) == srna) {
479                         prop= iprop;
480                         break;
481                 }
482         }
483         RNA_PROP_END;
484
485         return prop;
486 }
487
488 const struct ListBase *RNA_struct_defined_properties(StructRNA *srna)
489 {
490         return &srna->cont.properties;
491 }
492
493 FunctionRNA *RNA_struct_find_function(PointerRNA *ptr, const char *identifier)
494 {
495         PointerRNA tptr;
496         PropertyRNA *iterprop;
497         FunctionRNA *func;
498
499         RNA_pointer_create(NULL, &RNA_Struct, ptr->type, &tptr);
500         iterprop= RNA_struct_find_property(&tptr, "functions");
501
502         func= NULL;
503
504         RNA_PROP_BEGIN(&tptr, funcptr, iterprop) {
505                 if(strcmp(identifier, RNA_function_identifier(funcptr.data)) == 0) {
506                         func= funcptr.data;
507                         break;
508                 }
509         }
510         RNA_PROP_END;
511
512         return func;
513 }
514
515 const struct ListBase *RNA_struct_defined_functions(StructRNA *srna)
516 {
517         return &srna->functions;
518 }
519
520 StructRegisterFunc RNA_struct_register(StructRNA *type)
521 {
522         return type->reg;
523 }
524
525 StructUnregisterFunc RNA_struct_unregister(StructRNA *type)
526 {
527         do {
528                 if(type->unreg)
529                         return type->unreg;
530         } while((type=type->base));
531
532         return NULL;
533 }
534
535 void *RNA_struct_py_type_get(StructRNA *srna)
536 {
537         return srna->py_type;
538 }
539
540 void RNA_struct_py_type_set(StructRNA *srna, void *py_type)
541 {
542         srna->py_type= py_type;
543 }
544
545 void *RNA_struct_blender_type_get(StructRNA *srna)
546 {
547         return srna->blender_type;
548 }
549
550 void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
551 {
552         srna->blender_type= blender_type;
553 }
554
555 char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen)
556 {
557         PropertyRNA *nameprop;
558
559         if(ptr->data && (nameprop = RNA_struct_name_property(ptr->type)))
560                 return RNA_property_string_get_alloc(ptr, nameprop, fixedbuf, fixedlen);
561
562         return NULL;
563 }
564
565 /* Property Information */
566
567 const char *RNA_property_identifier(PropertyRNA *prop)
568 {
569         return rna_ensure_property_identifier(prop);
570 }
571
572 const char *RNA_property_description(PropertyRNA *prop)
573 {
574         return rna_ensure_property_description(prop);
575 }
576
577 PropertyType RNA_property_type(PropertyRNA *prop)
578 {
579         return rna_ensure_property(prop)->type;
580 }
581
582 PropertySubType RNA_property_subtype(PropertyRNA *prop)
583 {
584         return rna_ensure_property(prop)->subtype;
585 }
586
587 PropertyUnit RNA_property_unit(PropertyRNA *prop)
588 {
589         return RNA_SUBTYPE_UNIT(rna_ensure_property(prop)->subtype);
590 }
591
592 int RNA_property_flag(PropertyRNA *prop)
593 {
594         return rna_ensure_property(prop)->flag;
595 }
596
597 int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
598 {
599         return rna_ensure_property_array_length(ptr, prop);
600 }
601
602 int RNA_property_array_check(PointerRNA *ptr, PropertyRNA *prop)
603 {
604         return rna_ensure_property_array_check(ptr, prop);
605 }
606
607 /* used by BPY to make an array from the python object */
608 int RNA_property_array_dimension(PointerRNA *ptr, PropertyRNA *prop, int length[])
609 {
610         PropertyRNA *rprop= rna_ensure_property(prop);
611
612         if(length && rprop->arraydimension > 1)
613                 rna_ensure_property_multi_array_length(ptr, prop, length);
614
615         return rprop->arraydimension;
616 }
617
618 /* Return the size of Nth dimension. */
619 int RNA_property_multi_array_length(PointerRNA *ptr, PropertyRNA *prop, int dim)
620 {
621         int len[RNA_MAX_ARRAY_DIMENSION];
622
623         rna_ensure_property_multi_array_length(ptr, prop, len);
624
625         return len[dim];
626 }
627
628 char RNA_property_array_item_char(PropertyRNA *prop, int index)
629 {
630         const char *vectoritem= "XYZW";
631         const char *quatitem= "WXYZ";
632         const char *coloritem= "RGBA";
633         PropertySubType subtype= rna_ensure_property(prop)->subtype;
634
635         /* get string to use for array index */
636         if ((index < 4) && ELEM(subtype, PROP_QUATERNION, PROP_AXISANGLE))
637                 return quatitem[index];
638         else if((index < 4) && ELEM6(subtype, PROP_TRANSLATION, PROP_DIRECTION, PROP_XYZ, PROP_EULER, PROP_VELOCITY, PROP_ACCELERATION))
639                 return vectoritem[index];
640         else if ((index < 4) && ELEM(subtype, PROP_COLOR, PROP_RGB))
641                 return coloritem[index];
642         else
643                 return '\0';
644 }
645
646 void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, int *hardmax)
647 {
648         IntPropertyRNA *iprop= (IntPropertyRNA*)rna_ensure_property(prop);
649
650         if(iprop->range) {
651                 iprop->range(ptr, hardmin, hardmax);
652         }
653         else {
654                 *hardmin= iprop->hardmin;
655                 *hardmax= iprop->hardmax;
656         }
657 }
658
659 void RNA_property_int_ui_range(PointerRNA *ptr, PropertyRNA *prop, int *softmin, int *softmax, int *step)
660 {
661         IntPropertyRNA *iprop= (IntPropertyRNA*)rna_ensure_property(prop);
662         int hardmin, hardmax;
663         
664         if(iprop->range) {
665                 iprop->range(ptr, &hardmin, &hardmax);
666                 *softmin= MAX2(iprop->softmin, hardmin);
667                 *softmax= MIN2(iprop->softmax, hardmax);
668         }
669         else {
670                 *softmin= iprop->softmin;
671                 *softmax= iprop->softmax;
672         }
673
674         *step= iprop->step;
675 }
676
677 void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin, float *hardmax)
678 {
679         FloatPropertyRNA *fprop= (FloatPropertyRNA*)rna_ensure_property(prop);
680
681         if(fprop->range) {
682                 fprop->range(ptr, hardmin, hardmax);
683         }
684         else {
685                 *hardmin= fprop->hardmin;
686                 *hardmax= fprop->hardmax;
687         }
688 }
689
690 void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
691 {
692         FloatPropertyRNA *fprop= (FloatPropertyRNA*)rna_ensure_property(prop);
693         float hardmin, hardmax;
694
695         if(fprop->range) {
696                 fprop->range(ptr, &hardmin, &hardmax);
697                 *softmin= MAX2(fprop->softmin, hardmin);
698                 *softmax= MIN2(fprop->softmax, hardmax);
699         }
700         else {
701                 *softmin= fprop->softmin;
702                 *softmax= fprop->softmax;
703         }
704
705         *step= fprop->step;
706         *precision= (float)fprop->precision;
707 }
708
709 int RNA_property_string_maxlength(PropertyRNA *prop)
710 {
711         StringPropertyRNA *sprop= (StringPropertyRNA*)rna_ensure_property(prop);
712         return sprop->maxlength;
713 }
714
715 StructRNA *RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
716 {
717         prop= rna_ensure_property(prop);
718
719         if(prop->type == PROP_POINTER) {
720                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
721
722                 if(pprop->typef)
723                         return pprop->typef(ptr);
724                 else if(pprop->type)
725                         return pprop->type;
726         }
727         else if(prop->type == PROP_COLLECTION) {
728                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
729
730                 if(cprop->type)
731                         return cprop->type;
732         }
733
734         return &RNA_UnknownType;
735 }
736
737 /* Reuse for dynamic types  */
738 EnumPropertyItem DummyRNA_NULL_items[] = {
739         {0, NULL, 0, NULL, NULL}
740 };
741
742 void RNA_property_enum_items(bContext *C, PointerRNA *ptr, PropertyRNA *prop, EnumPropertyItem **item, int *totitem, int *free)
743 {
744         EnumPropertyRNA *eprop= (EnumPropertyRNA*)rna_ensure_property(prop);
745
746         *free= 0;
747
748         if(eprop->itemf && C) {
749                 int tot= 0;
750                 *item= eprop->itemf(C, ptr, free);
751
752                 if(totitem) {
753                         if(*item) {
754                                 for( ; (*item)[tot].identifier; tot++);
755                         }
756
757                         *totitem= tot;
758                 }
759         }
760         else {
761                 *item= eprop->item;
762                 if(totitem)
763                         *totitem= eprop->totitem;
764         }
765 }
766
767 int RNA_property_enum_value(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value)
768 {       
769         EnumPropertyItem *item;
770         int free;
771         
772         RNA_property_enum_items(C, ptr, prop, &item, NULL, &free);
773         
774         for(; item->identifier; item++) {
775                 if(item->identifier[0] && strcmp(item->identifier, identifier)==0) {
776                         *value = item->value;
777                         return 1;
778                 }
779         }
780
781         if(free)
782                 MEM_freeN(item);
783
784         return 0;
785 }
786
787 int RNA_enum_identifier(EnumPropertyItem *item, const int value, const char **identifier)
788 {
789         for (; item->identifier; item++) {
790                 if(item->identifier[0] && item->value==value) {
791                         *identifier = item->identifier;
792                         return 1;
793                 }
794         }
795         return 0;
796 }
797
798 int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name)
799 {
800         for (; item->identifier; item++) {
801                 if(item->identifier[0] && item->value==value) {
802                         *name = item->name;
803                         return 1;
804                 }
805         }
806         return 0;
807 }
808
809 int RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier)
810 {       
811         EnumPropertyItem *item= NULL;
812         int result, free;
813         
814         RNA_property_enum_items(C, ptr, prop, &item, NULL, &free);
815         if(item) {
816                 result= RNA_enum_identifier(item, value, identifier);
817                 if(free)
818                         MEM_freeN(item);
819
820                 return result;
821         }
822         return 0;
823 }
824
825 const char *RNA_property_ui_name(PropertyRNA *prop)
826 {
827         return rna_ensure_property_name(prop);
828 }
829
830 const char *RNA_property_ui_description(PropertyRNA *prop)
831 {
832         return rna_ensure_property(prop)->description;
833 }
834
835 int RNA_property_ui_icon(PropertyRNA *prop)
836 {
837         return rna_ensure_property(prop)->icon;
838 }
839
840 int RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop)
841 {
842         ID *id;
843         int flag;
844
845         prop= rna_ensure_property(prop);
846
847         if(prop->editable)
848                 flag= prop->editable(ptr);
849         else
850                 flag= prop->flag;
851         
852         id= ptr->id.data;
853
854         return (flag & PROP_EDITABLE) && (!id || !id->lib || (flag & PROP_LIB_EXCEPTION));
855 }
856
857 int RNA_property_animateable(PointerRNA *ptr, PropertyRNA *prop)
858 {
859         int flag;
860
861         prop= rna_ensure_property(prop);
862
863         if(!(prop->flag & PROP_ANIMATEABLE))
864                 return 0;
865
866         if(prop->editable)
867                 flag= prop->editable(ptr);
868         else
869                 flag= prop->flag;
870
871         return (flag & PROP_EDITABLE);
872 }
873
874 int RNA_property_animated(PointerRNA *ptr, PropertyRNA *prop)
875 {
876         /* would need to ask animation system */
877
878         return 0;
879 }
880
881 void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
882 {
883         prop= rna_ensure_property(prop);
884
885         if(prop->update)
886                 prop->update(C, ptr);
887         if(prop->noteflag)
888                 WM_event_add_notifier(C, prop->noteflag, ptr->id.data);
889 }
890
891 /* Property Data */
892
893 int RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
894 {
895         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
896         IDProperty *idprop;
897
898         if((idprop=rna_idproperty_check(&prop, ptr)))
899                 return IDP_Int(idprop);
900         else if(bprop->get)
901                 return bprop->get(ptr);
902         else
903                 return bprop->defaultvalue;
904 }
905
906 void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, int value)
907 {
908         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
909         IDProperty *idprop;
910
911         /* just incase other values are passed */
912         if(value) value= 1;
913
914         if((idprop=rna_idproperty_check(&prop, ptr)))
915                 IDP_Int(idprop)= value;
916         else if(bprop->set)
917                 bprop->set(ptr, value);
918         else if(prop->flag & PROP_EDITABLE) {
919                 IDPropertyTemplate val = {0};
920                 IDProperty *group;
921
922                 val.i= value;
923
924                 group= RNA_struct_idproperties(ptr, 1);
925                 if(group)
926                         IDP_AddToGroup(group, IDP_New(IDP_INT, val, (char*)prop->identifier));
927         }
928 }
929
930 void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
931 {
932         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
933         IDProperty *idprop;
934
935         if((idprop=rna_idproperty_check(&prop, ptr))) {
936                 if(prop->arraydimension == 0)
937                         values[0]= RNA_property_boolean_get(ptr, prop);
938                 else
939                         memcpy(values, IDP_Array(idprop), sizeof(int)*idprop->len);
940         }
941         else if(prop->arraydimension == 0)
942                 values[0]= RNA_property_boolean_get(ptr, prop);
943         else if(bprop->getarray)
944                 bprop->getarray(ptr, values);
945         else if(bprop->defaultarray)
946                 memcpy(values, bprop->defaultarray, sizeof(int)*prop->totarraylength);
947         else
948                 memset(values, 0, sizeof(int)*prop->totarraylength);
949 }
950
951 int RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
952 {
953         int tmp[RNA_MAX_ARRAY_LENGTH];
954         int len= rna_ensure_property_array_length(ptr, prop);
955
956         if(len <= RNA_MAX_ARRAY_LENGTH) {
957                 RNA_property_boolean_get_array(ptr, prop, tmp);
958                 return tmp[index];
959         }
960         else {
961                 int *tmparray, value;
962
963                 tmparray= MEM_callocN(sizeof(int)*len, "RNA_property_boolean_get_index");
964                 RNA_property_boolean_get_array(ptr, prop, tmparray);
965                 value= tmparray[index];
966                 MEM_freeN(tmparray);
967
968                 return value;
969         }
970 }
971
972 void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
973 {
974         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
975         IDProperty *idprop;
976
977         if((idprop=rna_idproperty_check(&prop, ptr))) {
978                 if(prop->arraydimension == 0)
979                         IDP_Int(idprop)= values[0];
980                 else
981                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
982         }
983         else if(prop->arraydimension == 0)
984                 RNA_property_boolean_set(ptr, prop, values[0]);
985         else if(bprop->setarray)
986                 bprop->setarray(ptr, values);
987         else if(prop->flag & PROP_EDITABLE) {
988                 IDPropertyTemplate val = {0};
989                 IDProperty *group;
990
991                 val.array.len= prop->totarraylength;
992                 val.array.type= IDP_INT;
993
994                 group= RNA_struct_idproperties(ptr, 1);
995                 if(group) {
996                         idprop= IDP_New(IDP_ARRAY, val, (char*)prop->identifier);
997                         IDP_AddToGroup(group, idprop);
998                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
999                 }
1000         }
1001 }
1002
1003 void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
1004 {
1005         int tmp[RNA_MAX_ARRAY_LENGTH];
1006         int len= rna_ensure_property_array_length(ptr, prop);
1007
1008         if(len <= RNA_MAX_ARRAY_LENGTH) {
1009                 RNA_property_boolean_get_array(ptr, prop, tmp);
1010                 tmp[index]= value;
1011                 RNA_property_boolean_set_array(ptr, prop, tmp);
1012         }
1013         else {
1014                 int *tmparray;
1015
1016                 tmparray= MEM_callocN(sizeof(int)*len, "RNA_property_boolean_get_index");
1017                 RNA_property_boolean_get_array(ptr, prop, tmparray);
1018                 tmparray[index]= value;
1019                 RNA_property_boolean_set_array(ptr, prop, tmparray);
1020                 MEM_freeN(tmparray);
1021         }
1022 }
1023
1024 int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
1025 {
1026         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1027         IDProperty *idprop;
1028
1029         if((idprop=rna_idproperty_check(&prop, ptr)))
1030                 return IDP_Int(idprop);
1031         else if(iprop->get)
1032                 return iprop->get(ptr);
1033         else
1034                 return iprop->defaultvalue;
1035 }
1036
1037 void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
1038 {
1039         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1040         IDProperty *idprop;
1041
1042         if((idprop=rna_idproperty_check(&prop, ptr)))
1043                 IDP_Int(idprop)= value;
1044         else if(iprop->set)
1045                 iprop->set(ptr, value);
1046         else if(prop->flag & PROP_EDITABLE) {
1047                 IDPropertyTemplate val = {0};
1048                 IDProperty *group;
1049
1050                 val.i= value;
1051
1052                 group= RNA_struct_idproperties(ptr, 1);
1053                 if(group)
1054                         IDP_AddToGroup(group, IDP_New(IDP_INT, val, (char*)prop->identifier));
1055         }
1056 }
1057
1058 void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
1059 {
1060         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1061         IDProperty *idprop;
1062
1063         if((idprop=rna_idproperty_check(&prop, ptr))) {
1064                 if(prop->arraydimension == 0)
1065                         values[0]= RNA_property_int_get(ptr, prop);
1066                 else
1067                         memcpy(values, IDP_Array(idprop), sizeof(int)*idprop->len);
1068         }
1069         else if(prop->arraydimension == 0)
1070                 values[0]= RNA_property_int_get(ptr, prop);
1071         else if(iprop->getarray)
1072                 iprop->getarray(ptr, values);
1073         else if(iprop->defaultarray)
1074                 memcpy(values, iprop->defaultarray, sizeof(int)*prop->totarraylength);
1075         else
1076                 memset(values, 0, sizeof(int)*prop->totarraylength);
1077 }
1078
1079 int RNA_property_int_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
1080 {
1081         int tmp[RNA_MAX_ARRAY_LENGTH];
1082         int len= rna_ensure_property_array_length(ptr, prop);
1083
1084         if(len <= RNA_MAX_ARRAY_LENGTH) {
1085                 RNA_property_int_get_array(ptr, prop, tmp);
1086                 return tmp[index];
1087         }
1088         else {
1089                 int *tmparray, value;
1090
1091                 tmparray= MEM_callocN(sizeof(int)*len, "RNA_property_int_get_index");
1092                 RNA_property_int_get_array(ptr, prop, tmparray);
1093                 value= tmparray[index];
1094                 MEM_freeN(tmparray);
1095
1096                 return value;
1097         }
1098 }
1099
1100 void RNA_property_int_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
1101 {
1102         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1103         IDProperty *idprop;
1104
1105         if((idprop=rna_idproperty_check(&prop, ptr))) {
1106                 if(prop->arraydimension == 0)
1107                         IDP_Int(idprop)= values[0];
1108                 else
1109                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);\
1110         }
1111         else if(prop->arraydimension == 0)
1112                 RNA_property_int_set(ptr, prop, values[0]);
1113         else if(iprop->setarray)
1114                 iprop->setarray(ptr, values);
1115         else if(prop->flag & PROP_EDITABLE) {
1116                 IDPropertyTemplate val = {0};
1117                 IDProperty *group;
1118
1119                 val.array.len= prop->totarraylength;
1120                 val.array.type= IDP_INT;
1121
1122                 group= RNA_struct_idproperties(ptr, 1);
1123                 if(group) {
1124                         idprop= IDP_New(IDP_ARRAY, val, (char*)prop->identifier);
1125                         IDP_AddToGroup(group, idprop);
1126                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
1127                 }
1128         }
1129 }
1130
1131 void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
1132 {
1133         int tmp[RNA_MAX_ARRAY_LENGTH];
1134         int len= rna_ensure_property_array_length(ptr, prop);
1135
1136         if(len <= RNA_MAX_ARRAY_LENGTH) {
1137                 RNA_property_int_get_array(ptr, prop, tmp);
1138                 tmp[index]= value;
1139                 RNA_property_int_set_array(ptr, prop, tmp);
1140         }
1141         else {
1142                 int *tmparray;
1143
1144                 tmparray= MEM_callocN(sizeof(int)*len, "RNA_property_int_get_index");
1145                 RNA_property_int_get_array(ptr, prop, tmparray);
1146                 tmparray[index]= value;
1147                 RNA_property_int_set_array(ptr, prop, tmparray);
1148                 MEM_freeN(tmparray);
1149         }
1150 }
1151
1152 float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
1153 {
1154         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1155         IDProperty *idprop;
1156
1157         if((idprop=rna_idproperty_check(&prop, ptr))) {
1158                 if(idprop->type == IDP_FLOAT)
1159                         return IDP_Float(idprop);
1160                 else
1161                         return (float)IDP_Double(idprop);
1162         }
1163         else if(fprop->get)
1164                 return fprop->get(ptr);
1165         else
1166                 return fprop->defaultvalue;
1167 }
1168
1169 void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
1170 {
1171         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1172         IDProperty *idprop;
1173
1174         if((idprop=rna_idproperty_check(&prop, ptr))) {
1175                 if(idprop->type == IDP_FLOAT)
1176                         IDP_Float(idprop)= value;
1177                 else
1178                         IDP_Double(idprop)= value;
1179         }
1180         else if(fprop->set) {
1181                 fprop->set(ptr, value);
1182         }
1183         else if(prop->flag & PROP_EDITABLE) {
1184                 IDPropertyTemplate val = {0};
1185                 IDProperty *group;
1186
1187                 val.f= value;
1188
1189                 group= RNA_struct_idproperties(ptr, 1);
1190                 if(group)
1191                         IDP_AddToGroup(group, IDP_New(IDP_FLOAT, val, (char*)prop->identifier));
1192         }
1193 }
1194
1195 void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
1196 {
1197         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1198         IDProperty *idprop;
1199         int i;
1200
1201         if((idprop=rna_idproperty_check(&prop, ptr))) {
1202                 if(prop->arraydimension == 0)
1203                         values[0]= RNA_property_float_get(ptr, prop);
1204                 else if(idprop->subtype == IDP_FLOAT) {
1205                         memcpy(values, IDP_Array(idprop), sizeof(float)*idprop->len);
1206                 }
1207                 else {
1208                         for(i=0; i<idprop->len; i++)
1209                                 values[i]=  (float)(((double*)IDP_Array(idprop))[i]);
1210                 }
1211         }
1212         else if(prop->arraydimension == 0)
1213                 values[0]= RNA_property_float_get(ptr, prop);
1214         else if(fprop->getarray)
1215                 fprop->getarray(ptr, values);
1216         else if(fprop->defaultarray)
1217                 memcpy(values, fprop->defaultarray, sizeof(float)*prop->totarraylength);
1218         else
1219                 memset(values, 0, sizeof(float)*prop->totarraylength);
1220 }
1221
1222 float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
1223 {
1224         float tmp[RNA_MAX_ARRAY_LENGTH];
1225         int len= rna_ensure_property_array_length(ptr, prop);
1226
1227         if(len <= RNA_MAX_ARRAY_LENGTH) {
1228                 RNA_property_float_get_array(ptr, prop, tmp);
1229                 return tmp[index];
1230         }
1231         else {
1232                 float *tmparray, value;
1233
1234                 tmparray= MEM_callocN(sizeof(float)*len, "RNA_property_float_get_index");
1235                 RNA_property_float_get_array(ptr, prop, tmparray);
1236                 value= tmparray[index];
1237                 MEM_freeN(tmparray);
1238
1239                 return value;
1240         }
1241
1242 }
1243
1244 void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
1245 {
1246         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1247         IDProperty *idprop;
1248         int i;
1249
1250         if((idprop=rna_idproperty_check(&prop, ptr))) {
1251                 if(prop->arraydimension == 0)
1252                         IDP_Double(idprop)= values[0];
1253                 else if(idprop->subtype == IDP_FLOAT) {
1254                         memcpy(IDP_Array(idprop), values, sizeof(float)*idprop->len);
1255                 }
1256                 else {
1257                         for(i=0; i<idprop->len; i++)
1258                                 ((double*)IDP_Array(idprop))[i]= values[i];
1259                 }
1260         }
1261         else if(prop->arraydimension == 0)
1262                 RNA_property_float_set(ptr, prop, values[0]);
1263         else if(fprop->setarray) {
1264                 fprop->setarray(ptr, values);
1265         }
1266         else if(prop->flag & PROP_EDITABLE) {
1267                 IDPropertyTemplate val = {0};
1268                 IDProperty *group;
1269
1270                 val.array.len= prop->totarraylength;
1271                 val.array.type= IDP_FLOAT;
1272
1273                 group= RNA_struct_idproperties(ptr, 1);
1274                 if(group) {
1275                         idprop= IDP_New(IDP_ARRAY, val, (char*)prop->identifier);
1276                         IDP_AddToGroup(group, idprop);
1277                         memcpy(IDP_Array(idprop), values, sizeof(float)*idprop->len);
1278                 }
1279         }
1280 }
1281
1282 void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
1283 {
1284         float tmp[RNA_MAX_ARRAY_LENGTH];
1285         int len= rna_ensure_property_array_length(ptr, prop);
1286
1287         if(len <= RNA_MAX_ARRAY_LENGTH) {
1288                 RNA_property_float_get_array(ptr, prop, tmp);
1289                 tmp[index]= value;
1290                 RNA_property_float_set_array(ptr, prop, tmp);
1291         }
1292         else {
1293                 float *tmparray;
1294
1295                 tmparray= MEM_callocN(sizeof(float)*len, "RNA_property_float_get_index");
1296                 RNA_property_float_get_array(ptr, prop, tmparray);
1297                 tmparray[index]= value;
1298                 RNA_property_float_set_array(ptr, prop, tmparray);
1299                 MEM_freeN(tmparray);
1300         }
1301 }
1302
1303 void RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop, char *value)
1304 {
1305         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1306         IDProperty *idprop;
1307
1308         if((idprop=rna_idproperty_check(&prop, ptr)))
1309                 strcpy(value, IDP_String(idprop));
1310         else if(sprop->get)
1311                 sprop->get(ptr, value);
1312         else
1313                 strcpy(value, sprop->defaultvalue);
1314 }
1315
1316 char *RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen)
1317 {
1318         char *buf;
1319         int length;
1320
1321         length= RNA_property_string_length(ptr, prop);
1322
1323         if(length+1 < fixedlen)
1324                 buf= fixedbuf;
1325         else
1326                 buf= MEM_callocN(sizeof(char)*(length+1), "RNA_string_get_alloc");
1327
1328         RNA_property_string_get(ptr, prop, buf);
1329
1330         return buf;
1331 }
1332
1333 int RNA_property_string_length(PointerRNA *ptr, PropertyRNA *prop)
1334 {
1335         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1336         IDProperty *idprop;
1337
1338         if((idprop=rna_idproperty_check(&prop, ptr)))
1339                 return strlen(IDP_String(idprop));
1340         else if(sprop->length)
1341                 return sprop->length(ptr);
1342         else
1343                 return strlen(sprop->defaultvalue);
1344 }
1345
1346 void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
1347 {
1348         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1349         IDProperty *idprop;
1350
1351         if((idprop=rna_idproperty_check(&prop, ptr)))
1352                 IDP_AssignString(idprop, (char*)value);
1353         else if(sprop->set)
1354                 sprop->set(ptr, value);
1355         else if(prop->flag & PROP_EDITABLE) {
1356                 IDPropertyTemplate val = {0};
1357                 IDProperty *group;
1358
1359                 val.str= (char*)value;
1360
1361                 group= RNA_struct_idproperties(ptr, 1);
1362                 if(group)
1363                         IDP_AddToGroup(group, IDP_New(IDP_STRING, val, (char*)prop->identifier));
1364         }
1365 }
1366
1367 int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
1368 {
1369         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1370         IDProperty *idprop;
1371
1372         if((idprop=rna_idproperty_check(&prop, ptr)))
1373                 return IDP_Int(idprop);
1374         else if(eprop->get)
1375                 return eprop->get(ptr);
1376         else
1377                 return eprop->defaultvalue;
1378 }
1379
1380
1381 void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
1382 {
1383         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1384         IDProperty *idprop;
1385
1386         if((idprop=rna_idproperty_check(&prop, ptr)))
1387                 IDP_Int(idprop)= value;
1388         else if(eprop->set) {
1389                 eprop->set(ptr, value);
1390         }
1391         else if(prop->flag & PROP_EDITABLE) {
1392                 IDPropertyTemplate val = {0};
1393                 IDProperty *group;
1394
1395                 val.i= value;
1396
1397                 group= RNA_struct_idproperties(ptr, 1);
1398                 if(group)
1399                         IDP_AddToGroup(group, IDP_New(IDP_INT, val, (char*)prop->identifier));
1400         }
1401 }
1402
1403 PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
1404 {
1405         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1406         IDProperty *idprop;
1407
1408         if((idprop=rna_idproperty_check(&prop, ptr))) {
1409                 pprop= (PointerPropertyRNA*)prop;
1410
1411                 /* for groups, data is idprop itself */
1412                 return rna_pointer_inherit_refine(ptr, pprop->type, idprop);
1413         }
1414         else if(pprop->get) {
1415                 return pprop->get(ptr);
1416         }
1417         else if(prop->flag & PROP_IDPROPERTY) {
1418                 /* XXX temporary hack to add it automatically, reading should
1419                    never do any write ops, to ensure thread safety etc .. */
1420                 RNA_property_pointer_add(ptr, prop);
1421                 return RNA_property_pointer_get(ptr, prop);
1422         }
1423         else {
1424                 PointerRNA result;
1425
1426                 memset(&result, 0, sizeof(result));
1427                 return result;
1428         }
1429 }
1430
1431 void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value)
1432 {
1433         IDProperty *idprop;
1434
1435         if((idprop=rna_idproperty_check(&prop, ptr))) {
1436                 /* not supported */
1437         }
1438         else {
1439                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1440
1441                 if(pprop->set && !((prop->flag & PROP_NEVER_NULL) && ptr_value.data == NULL))
1442                         pprop->set(ptr, ptr_value);
1443         }
1444 }
1445
1446 void RNA_property_pointer_add(PointerRNA *ptr, PropertyRNA *prop)
1447 {
1448         IDProperty *idprop;
1449
1450         if((idprop=rna_idproperty_check(&prop, ptr))) {
1451                 /* already exists */
1452         }
1453         else if(prop->flag & PROP_IDPROPERTY) {
1454                 IDPropertyTemplate val = {0};
1455                 IDProperty *group;
1456
1457                 val.i= 0;
1458
1459                 group= RNA_struct_idproperties(ptr, 1);
1460                 if(group)
1461                         IDP_AddToGroup(group, IDP_New(IDP_GROUP, val, (char*)prop->identifier));
1462         }
1463         else
1464                 printf("RNA_property_pointer_add %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);
1465 }
1466
1467 void RNA_property_pointer_remove(PointerRNA *ptr, PropertyRNA *prop)
1468 {
1469         IDProperty *idprop, *group;
1470
1471         if((idprop=rna_idproperty_check(&prop, ptr))) {
1472                 group= RNA_struct_idproperties(ptr, 0);
1473                 
1474                 if(group) {
1475                         IDP_RemFromGroup(group, idprop);
1476                         IDP_FreeProperty(idprop);
1477                         MEM_freeN(idprop);
1478                 }
1479         }
1480         else
1481                 printf("RNA_property_pointer_remove %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);
1482 }
1483
1484 static void rna_property_collection_get_idp(CollectionPropertyIterator *iter)
1485 {
1486         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)iter->prop;
1487
1488         iter->ptr.data= rna_iterator_array_get(iter);
1489         iter->ptr.type= cprop->type;
1490         rna_pointer_inherit_id(cprop->type, &iter->parent, &iter->ptr);
1491 }
1492
1493 void RNA_property_collection_begin(PointerRNA *ptr, PropertyRNA *prop, CollectionPropertyIterator *iter)
1494 {
1495         IDProperty *idprop;
1496
1497         memset(iter, 0, sizeof(*iter));
1498
1499         if((idprop=rna_idproperty_check(&prop, ptr)) || (prop->flag & PROP_IDPROPERTY)) {
1500                 iter->parent= *ptr;
1501                 iter->prop= prop;
1502
1503                 if(idprop)
1504                         rna_iterator_array_begin(iter, IDP_IDPArray(idprop), sizeof(IDProperty), idprop->len, 0, NULL);
1505                 else
1506                         rna_iterator_array_begin(iter, NULL, sizeof(IDProperty), 0, 0, NULL);
1507
1508                 if(iter->valid)
1509                         rna_property_collection_get_idp(iter);
1510
1511                 iter->idprop= 1;
1512         }
1513         else {
1514                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1515                 cprop->begin(iter, ptr);
1516         }
1517 }
1518
1519 void RNA_property_collection_next(CollectionPropertyIterator *iter)
1520 {
1521         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)iter->prop;
1522
1523         if(iter->idprop) {
1524                 rna_iterator_array_next(iter);
1525
1526                 if(iter->valid)
1527                         rna_property_collection_get_idp(iter);
1528         }
1529         else
1530                 cprop->next(iter);
1531 }
1532
1533 void RNA_property_collection_end(CollectionPropertyIterator *iter)
1534 {
1535         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)iter->prop;
1536
1537         if(iter->idprop)
1538                 rna_iterator_array_end(iter);
1539         else
1540                 cprop->end(iter);
1541 }
1542
1543 int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
1544 {
1545         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1546         IDProperty *idprop;
1547
1548         if((idprop=rna_idproperty_check(&prop, ptr))) {
1549                 return idprop->len;
1550         }
1551         else if(cprop->length) {
1552                 return cprop->length(ptr);
1553         }
1554         else {
1555                 CollectionPropertyIterator iter;
1556                 int length= 0;
1557
1558                 RNA_property_collection_begin(ptr, prop, &iter);
1559                 for(; iter.valid; RNA_property_collection_next(&iter))
1560                         length++;
1561                 RNA_property_collection_end(&iter);
1562
1563                 return length;
1564         }
1565 }
1566
1567 void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
1568 {
1569         IDProperty *idprop;
1570         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1571
1572         if((idprop=rna_idproperty_check(&prop, ptr))) {
1573                 IDPropertyTemplate val = {0};
1574                 IDProperty *item;
1575
1576                 item= IDP_New(IDP_GROUP, val, "");
1577                 IDP_AppendArray(idprop, item);
1578                 IDP_FreeProperty(item);
1579                 MEM_freeN(item);
1580         }
1581         else if(prop->flag & PROP_IDPROPERTY) {
1582                 IDProperty *group, *item;
1583                 IDPropertyTemplate val = {0};
1584
1585                 group= RNA_struct_idproperties(ptr, 1);
1586                 if(group) {
1587                         idprop= IDP_NewIDPArray(prop->identifier);
1588                         IDP_AddToGroup(group, idprop);
1589
1590                         item= IDP_New(IDP_GROUP, val, "");
1591                         IDP_AppendArray(idprop, item);
1592                         IDP_FreeProperty(item);
1593                         MEM_freeN(item);
1594                 }
1595         }
1596         else if(cprop->add){
1597                 if(!(cprop->add->flag & FUNC_USE_CONTEXT)) { /* XXX check for this somewhere else */
1598                         ParameterList params;
1599                         RNA_parameter_list_create(&params, ptr, cprop->add);
1600                         RNA_function_call(NULL, NULL, ptr, cprop->add, &params);
1601                         RNA_parameter_list_free(&params);
1602                 }
1603         }
1604         /*else
1605                 printf("RNA_property_collection_add %s.%s: not implemented for this property.\n", ptr->type->identifier, prop->identifier);*/
1606
1607         if(r_ptr) {
1608                 if(idprop) {
1609                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1610
1611                         r_ptr->data= IDP_GetIndexArray(idprop, idprop->len-1);
1612                         r_ptr->type= cprop->type;
1613                         rna_pointer_inherit_id(NULL, ptr, r_ptr);
1614                 }
1615                 else
1616                         memset(r_ptr, 0, sizeof(*r_ptr));
1617         }
1618 }
1619
1620 int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key)
1621 {
1622         IDProperty *idprop;
1623         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1624
1625         if((idprop=rna_idproperty_check(&prop, ptr))) {
1626                 IDProperty tmp, *array;
1627                 int len;
1628
1629                 len= idprop->len;
1630                 array= IDP_IDPArray(idprop);
1631
1632                 if(key >= 0 && key < len) {
1633                         if(key+1 < len) {
1634                                 /* move element to be removed to the back */
1635                                 memcpy(&tmp, &array[key], sizeof(IDProperty));
1636                                 memmove(array+key, array+key+1, sizeof(IDProperty)*(len-key+1));
1637                                 memcpy(&array[len-1], &tmp, sizeof(IDProperty));
1638                         }
1639
1640                         IDP_ResizeIDPArray(idprop, len-1);
1641                 }
1642
1643                 return 1;
1644         }
1645         else if(prop->flag & PROP_IDPROPERTY)
1646                 return 1;
1647         else if(cprop->remove){
1648                 if(!(cprop->remove->flag & FUNC_USE_CONTEXT)) { /* XXX check for this somewhere else */
1649                         ParameterList params;
1650                         RNA_parameter_list_create(&params, ptr, cprop->remove);
1651                         RNA_function_call(NULL, NULL, ptr, cprop->remove, &params);
1652                         RNA_parameter_list_free(&params);
1653                 }
1654
1655                 return 0;
1656         }
1657         /*else
1658                 printf("RNA_property_collection_remove %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);*/
1659         
1660         return 0;
1661 }
1662
1663 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
1664 {
1665         IDProperty *idprop;
1666
1667         if((idprop=rna_idproperty_check(&prop, ptr)))
1668                 IDP_ResizeIDPArray(idprop, 0);
1669 }
1670
1671 int RNA_property_collection_lookup_index(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *t_ptr)
1672 {
1673         CollectionPropertyIterator iter;
1674         int index= 0;
1675         
1676         RNA_property_collection_begin(ptr, prop, &iter);
1677         for(index=0; iter.valid; RNA_property_collection_next(&iter), index++) {
1678                 if (iter.ptr.data == t_ptr->data)
1679                         break;
1680         }
1681         RNA_property_collection_end(&iter);
1682         
1683         /* did we find it? */
1684         if (iter.valid)
1685                 return index;
1686         else
1687                 return -1;
1688 }
1689
1690 int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
1691 {
1692         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1693
1694         if(cprop->lookupint) {
1695                 /* we have a callback defined, use it */
1696                 *r_ptr= cprop->lookupint(ptr, key);
1697                 return (r_ptr->data != NULL);
1698         }
1699         else {
1700                 /* no callback defined, just iterate and find the nth item */
1701                 CollectionPropertyIterator iter;
1702                 int i;
1703
1704                 RNA_property_collection_begin(ptr, prop, &iter);
1705                 for(i=0; iter.valid; RNA_property_collection_next(&iter), i++) {
1706                         if(i == key) {
1707                                 *r_ptr= iter.ptr;
1708                                 break;
1709                         }
1710                 }
1711                 RNA_property_collection_end(&iter);
1712
1713                 if(!iter.valid)
1714                         memset(r_ptr, 0, sizeof(*r_ptr));
1715
1716                 return iter.valid;
1717         }
1718 }
1719
1720 int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr)
1721 {
1722         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1723
1724         if(cprop->lookupstring) {
1725                 /* we have a callback defined, use it */
1726                 *r_ptr= cprop->lookupstring(ptr, key);
1727                 return (r_ptr->data != NULL);
1728         }
1729         else {
1730                 /* no callback defined, compare with name properties if they exist */
1731                 CollectionPropertyIterator iter;
1732                 PropertyRNA *nameprop;
1733                 char name[256], *nameptr;
1734                 int found= 0;
1735
1736                 RNA_property_collection_begin(ptr, prop, &iter);
1737                 for(; iter.valid; RNA_property_collection_next(&iter)) {
1738                         if(iter.ptr.data && iter.ptr.type->nameproperty) {
1739                                 nameprop= iter.ptr.type->nameproperty;
1740
1741                                 nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name));
1742
1743                                 if(strcmp(nameptr, key) == 0) {
1744                                         *r_ptr= iter.ptr;
1745                                         found= 1;
1746                                 }
1747
1748                                 if((char *)&name != nameptr)
1749                                         MEM_freeN(nameptr);
1750
1751                                 if(found)
1752                                         break;
1753                         }
1754                 }
1755                 RNA_property_collection_end(&iter);
1756
1757                 if(!iter.valid)
1758                         memset(r_ptr, 0, sizeof(*r_ptr));
1759
1760                 return iter.valid;
1761         }
1762 }
1763
1764 PropertyRNA *RNA_property_collection_active(PropertyRNA *prop)
1765 {
1766         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1767         return cprop->active;
1768 }
1769
1770 int RNA_property_collection_raw_array(PointerRNA *ptr, PropertyRNA *prop, PropertyRNA *itemprop, RawArray *array)
1771 {
1772         CollectionPropertyIterator iter;
1773         ArrayIterator *internal;
1774         char *arrayp;
1775
1776         if(!(prop->flag & PROP_RAW_ARRAY) || !(itemprop->flag & PROP_RAW_ACCESS))
1777                 return 0;
1778
1779         RNA_property_collection_begin(ptr, prop, &iter);
1780
1781         if(iter.valid) {
1782                 /* get data from array iterator and item property */
1783                 internal= iter.internal;
1784                 arrayp= (iter.valid)? iter.ptr.data: NULL;
1785
1786                 if(internal->skip || !RNA_property_editable(&iter.ptr, itemprop)) {
1787                         /* we might skip some items, so it's not a proper array */
1788                         RNA_property_collection_end(&iter);
1789                         return 0;
1790                 }
1791
1792                 array->array= arrayp + itemprop->rawoffset;
1793                 array->stride= internal->itemsize;
1794                 array->len= ((char*)internal->endptr - arrayp)/internal->itemsize;
1795                 array->type= itemprop->rawtype;
1796         }
1797         else
1798                 memset(array, 0, sizeof(RawArray));
1799
1800         RNA_property_collection_end(&iter);
1801
1802         return 1;
1803 }
1804
1805 #define RAW_GET(dtype, var, raw, a) \
1806 { \
1807         switch(raw.type) { \
1808                 case PROP_RAW_CHAR: var = (dtype)((char*)raw.array)[a]; break; \
1809                 case PROP_RAW_SHORT: var = (dtype)((short*)raw.array)[a]; break; \
1810                 case PROP_RAW_INT: var = (dtype)((int*)raw.array)[a]; break; \
1811                 case PROP_RAW_FLOAT: var = (dtype)((float*)raw.array)[a]; break; \
1812                 case PROP_RAW_DOUBLE: var = (dtype)((double*)raw.array)[a]; break; \
1813                 default: var = (dtype)0; \
1814         } \
1815 }
1816
1817 #define RAW_SET(dtype, raw, a, var) \
1818 { \
1819         switch(raw.type) { \
1820                 case PROP_RAW_CHAR: ((char*)raw.array)[a] = (char)var; break; \
1821                 case PROP_RAW_SHORT: ((short*)raw.array)[a] = (short)var; break; \
1822                 case PROP_RAW_INT: ((int*)raw.array)[a] = (int)var; break; \
1823                 case PROP_RAW_FLOAT: ((float*)raw.array)[a] = (float)var; break; \
1824                 case PROP_RAW_DOUBLE: ((double*)raw.array)[a] = (double)var; break; \
1825         } \
1826 }
1827
1828 int RNA_raw_type_sizeof(RawPropertyType type)
1829 {
1830         switch(type) {
1831                 case PROP_RAW_CHAR: return sizeof(char);
1832                 case PROP_RAW_SHORT: return sizeof(short);
1833                 case PROP_RAW_INT: return sizeof(int);
1834                 case PROP_RAW_FLOAT: return sizeof(float);
1835                 case PROP_RAW_DOUBLE: return sizeof(double);
1836                 default: return 0;
1837         }
1838 }
1839
1840 static int rna_raw_access(ReportList *reports, PointerRNA *ptr, PropertyRNA *prop, char *propname, void *inarray, RawPropertyType intype, int inlen, int set)
1841 {
1842         StructRNA *ptype;
1843         PointerRNA itemptr;
1844         PropertyRNA *itemprop, *iprop;
1845         PropertyType itemtype=0;
1846         RawArray in;
1847         int itemlen= 0;
1848
1849         /* initialize in array, stride assumed 0 in following code */
1850         in.array= inarray;
1851         in.type= intype;
1852         in.len= inlen;
1853         in.stride= 0;
1854
1855         ptype= RNA_property_pointer_type(ptr, prop);
1856
1857         /* try to get item property pointer */
1858         RNA_pointer_create(NULL, ptype, NULL, &itemptr);
1859         itemprop= RNA_struct_find_property(&itemptr, propname);
1860
1861         if(itemprop) {
1862                 /* we have item property pointer */
1863                 RawArray out;
1864
1865                 /* check type */
1866                 itemtype= RNA_property_type(itemprop);
1867
1868                 if(!ELEM3(itemtype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1869                         BKE_report(reports, RPT_ERROR, "Only boolean, int and float properties supported.");
1870                         return 0;
1871                 }
1872
1873                 /* check item array */
1874                 itemlen= RNA_property_array_length(&itemptr, itemprop);
1875
1876                 /* try to access as raw array */
1877                 if(RNA_property_collection_raw_array(ptr, prop, itemprop, &out)) {
1878                         if(in.len != itemlen*out.len) {
1879                                 BKE_reportf(reports, RPT_ERROR, "Array length mismatch (expected %d, got %d).", out.len*itemlen, in.len);
1880                                 return 0;
1881                         }
1882                         
1883                         /* matching raw types */
1884                         if(out.type == in.type) {
1885                                 void *inp= in.array;
1886                                 void *outp= out.array;
1887                                 int a, size;
1888
1889                                 itemlen= (itemlen == 0)? 1: itemlen;
1890                                 size= RNA_raw_type_sizeof(out.type) * itemlen;
1891
1892                                 for(a=0; a<out.len; a++) {
1893                                         if(set) memcpy(outp, inp, size);
1894                                         else memcpy(inp, outp, size);
1895
1896                                         inp= (char*)inp + size;
1897                                         outp= (char*)outp + out.stride;
1898                                 }
1899
1900                                 return 1;
1901                         }
1902
1903                         /* could also be faster with non-matching types,
1904                          * for now we just do slower loop .. */
1905                 }
1906         }
1907
1908         {
1909                 void *tmparray= NULL;
1910                 int tmplen= 0;
1911                 int err= 0, j, a= 0;
1912
1913                 /* no item property pointer, can still be id property, or
1914                  * property of a type derived from the collection pointer type */
1915                 RNA_PROP_BEGIN(ptr, itemptr, prop) {
1916                         if(itemptr.data) {
1917                                 if(itemprop) {
1918                                         /* we got the property already */
1919                                         iprop= itemprop;
1920                                 }
1921                                 else {
1922                                         /* not yet, look it up and verify if it is valid */
1923                                         iprop= RNA_struct_find_property(&itemptr, propname);
1924
1925                                         if(iprop) {
1926                                                 itemlen= RNA_property_array_length(&itemptr, iprop);
1927                                                 itemtype= RNA_property_type(iprop);
1928                                         }
1929                                         else {
1930                                                 BKE_reportf(reports, RPT_ERROR, "Property named %s not found.", propname);
1931                                                 err= 1;
1932                                                 break;
1933                                         }
1934
1935                                         if(!ELEM3(itemtype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1936                                                 BKE_report(reports, RPT_ERROR, "Only boolean, int and float properties supported.");
1937                                                 err= 1;
1938                                                 break;
1939                                         }
1940                                 }
1941
1942                                 /* editable check */
1943                                 if(RNA_property_editable(&itemptr, iprop)) {
1944                                         if(a+itemlen > in.len) {
1945                                                 BKE_reportf(reports, RPT_ERROR, "Array length mismatch (got %d, expected more).", in.len);
1946                                                 err= 1;
1947                                                 break;
1948                                         }
1949
1950                                         if(itemlen == 0) {
1951                                                 /* handle conversions */
1952                                                 if(set) {
1953                                                         switch(itemtype) {
1954                                                                 case PROP_BOOLEAN: {
1955                                                                         int b;
1956                                                                         RAW_GET(int, b, in, a);
1957                                                                         RNA_property_boolean_set(&itemptr, iprop, b);
1958                                                                         break;
1959                                                                 }
1960                                                                 case PROP_INT: {
1961                                                                         int i;
1962                                                                         RAW_GET(int, i, in, a);
1963                                                                         RNA_property_int_set(&itemptr, iprop, i);
1964                                                                         break;
1965                                                                 }
1966                                                                 case PROP_FLOAT: {
1967                                                                         float f;
1968                                                                         RAW_GET(float, f, in, a);
1969                                                                         RNA_property_float_set(&itemptr, iprop, f);
1970                                                                         break;
1971                                                                 }
1972                                                                 default:
1973                                                                         break;
1974                                                         }
1975                                                 }
1976                                                 else {
1977                                                         switch(itemtype) {
1978                                                                 case PROP_BOOLEAN: {
1979                                                                         int b= RNA_property_boolean_get(&itemptr, iprop);
1980                                                                         RAW_SET(int, in, a, b);
1981                                                                         break;
1982                                                                 }
1983                                                                 case PROP_INT: {
1984                                                                         int i= RNA_property_int_get(&itemptr, iprop);
1985                                                                         RAW_SET(int, in, a, i);
1986                                                                         break;
1987                                                                 }
1988                                                                 case PROP_FLOAT: {
1989                                                                         float f= RNA_property_float_get(&itemptr, iprop);
1990                                                                         RAW_SET(float, in, a, f);
1991                                                                         break;
1992                                                                 }
1993                                                                 default:
1994                                                                         break;
1995                                                         }
1996                                                 }
1997                                                 a++;
1998                                         }
1999                                         else {
2000                                                 /* allocate temporary array if needed */
2001                                                 if(tmparray && tmplen != itemlen) {
2002                                                         MEM_freeN(tmparray);
2003                                                         tmparray= NULL;
2004                                                 }
2005                                                 if(!tmparray) {
2006                                                         tmparray= MEM_callocN(sizeof(float)*itemlen, "RNA tmparray\n");
2007                                                         tmplen= itemlen;
2008                                                 }
2009
2010                                                 /* handle conversions */
2011                                                 if(set) {
2012                                                         switch(itemtype) {
2013                                                                 case PROP_BOOLEAN: {
2014                                                                         for(j=0; j<itemlen; j++, a++)
2015                                                                                 RAW_GET(int, ((int*)tmparray)[j], in, a);
2016                                                                         RNA_property_boolean_set_array(&itemptr, iprop, tmparray);
2017                                                                         break;
2018                                                                 }
2019                                                                 case PROP_INT: {
2020                                                                         for(j=0; j<itemlen; j++, a++)
2021                                                                                 RAW_GET(int, ((int*)tmparray)[j], in, a);
2022                                                                         RNA_property_int_set_array(&itemptr, iprop, tmparray);
2023                                                                         break;
2024                                                                 }
2025                                                                 case PROP_FLOAT: {
2026                                                                         for(j=0; j<itemlen; j++, a++)
2027                                                                                 RAW_GET(float, ((float*)tmparray)[j], in, a);
2028                                                                         RNA_property_float_set_array(&itemptr, iprop, tmparray);
2029                                                                         break;
2030                                                                 }
2031                                                                 default:
2032                                                                         break;
2033                                                         }
2034                                                 }
2035                                                 else {
2036                                                         switch(itemtype) {
2037                                                                 case PROP_BOOLEAN: {
2038                                                                         RNA_property_boolean_get_array(&itemptr, iprop, tmparray);
2039                                                                         for(j=0; j<itemlen; j++, a++)
2040                                                                                 RAW_SET(int, in, a, ((int*)tmparray)[j]);
2041                                                                         break;
2042                                                                 }
2043                                                                 case PROP_INT: {
2044                                                                         RNA_property_int_get_array(&itemptr, iprop, tmparray);
2045                                                                         for(j=0; j<itemlen; j++, a++)
2046                                                                                 RAW_SET(int, in, a, ((int*)tmparray)[j]);
2047                                                                         break;
2048                                                                 }
2049                                                                 case PROP_FLOAT: {
2050                                                                         RNA_property_float_get_array(&itemptr, iprop, tmparray);
2051                                                                         for(j=0; j<itemlen; j++, a++)
2052                                                                                 RAW_SET(float, in, a, ((float*)tmparray)[j]);
2053                                                                         break;
2054                                                                 }
2055                                                                 default:
2056                                                                         break;
2057                                                         }
2058                                                 }
2059                                         }
2060                                 }
2061                         }
2062                 }
2063                 RNA_PROP_END;
2064
2065                 if(tmparray)
2066                         MEM_freeN(tmparray);
2067
2068                 return !err;
2069         }
2070 }
2071
2072 RawPropertyType RNA_property_raw_type(PropertyRNA *prop)
2073 {
2074         return prop->rawtype;
2075 }
2076
2077 int RNA_property_collection_raw_get(ReportList *reports, PointerRNA *ptr, PropertyRNA *prop, char *propname, void *array, RawPropertyType type, int len)
2078 {
2079         return rna_raw_access(reports, ptr, prop, propname, array, type, len, 0);
2080 }
2081
2082 int RNA_property_collection_raw_set(ReportList *reports, PointerRNA *ptr, PropertyRNA *prop, char *propname, void *array, RawPropertyType type, int len)
2083 {
2084         return rna_raw_access(reports, ptr, prop, propname, array, type, len, 1);
2085 }
2086
2087 /* Standard iterator functions */
2088
2089 void rna_iterator_listbase_begin(CollectionPropertyIterator *iter, ListBase *lb, IteratorSkipFunc skip)
2090 {
2091         ListBaseIterator *internal;
2092
2093         internal= MEM_callocN(sizeof(ListBaseIterator), "ListBaseIterator");
2094         internal->link= (lb)? lb->first: NULL;
2095         internal->skip= skip;
2096
2097         iter->internal= internal;
2098         iter->valid= (internal->link != NULL);
2099
2100         if(skip && iter->valid && skip(iter, internal->link))
2101                 rna_iterator_listbase_next(iter);
2102 }
2103
2104 void rna_iterator_listbase_next(CollectionPropertyIterator *iter)
2105 {
2106         ListBaseIterator *internal= iter->internal;
2107
2108         if(internal->skip) {
2109                 do {
2110                         internal->link= internal->link->next;
2111                         iter->valid= (internal->link != NULL);
2112                 } while(iter->valid && internal->skip(iter, internal->link));
2113         }
2114         else {
2115                 internal->link= internal->link->next;
2116                 iter->valid= (internal->link != NULL);
2117         }
2118 }
2119
2120 void *rna_iterator_listbase_get(CollectionPropertyIterator *iter)
2121 {
2122         ListBaseIterator *internal= iter->internal;
2123
2124         return internal->link;
2125 }
2126
2127 void rna_iterator_listbase_end(CollectionPropertyIterator *iter)
2128 {
2129         MEM_freeN(iter->internal);
2130         iter->internal= NULL;
2131 }
2132
2133 void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length, int free_ptr, IteratorSkipFunc skip)
2134 {
2135         ArrayIterator *internal;
2136
2137         if(ptr == NULL)
2138                 length= 0;
2139
2140         internal= MEM_callocN(sizeof(ArrayIterator), "ArrayIterator");
2141         internal->ptr= ptr;
2142         internal->free_ptr= free_ptr ? ptr:NULL;
2143         internal->endptr= ((char*)ptr)+length*itemsize;
2144         internal->itemsize= itemsize;
2145         internal->skip= skip;
2146
2147         iter->internal= internal;
2148         iter->valid= (internal->ptr != internal->endptr);
2149
2150         if(skip && iter->valid && skip(iter, internal->ptr))
2151                 rna_iterator_array_next(iter);
2152 }
2153
2154 void rna_iterator_array_next(CollectionPropertyIterator *iter)
2155 {
2156         ArrayIterator *internal= iter->internal;
2157
2158         if(internal->skip) {
2159                 do {
2160                         internal->ptr += internal->itemsize;
2161                         iter->valid= (internal->ptr != internal->endptr);
2162                 } while(iter->valid && internal->skip(iter, internal->ptr));
2163         }
2164         else {
2165                 internal->ptr += internal->itemsize;
2166                 iter->valid= (internal->ptr != internal->endptr);
2167         }
2168 }
2169
2170 void *rna_iterator_array_get(CollectionPropertyIterator *iter)
2171 {
2172         ArrayIterator *internal= iter->internal;
2173
2174         return internal->ptr;
2175 }
2176
2177 void *rna_iterator_array_dereference_get(CollectionPropertyIterator *iter)
2178 {
2179         ArrayIterator *internal= iter->internal;
2180
2181         /* for ** arrays */
2182         return *(void**)(internal->ptr);
2183 }
2184
2185 void rna_iterator_array_end(CollectionPropertyIterator *iter)
2186 {
2187         ArrayIterator *internal= iter->internal;
2188         
2189         if(internal->free_ptr) {
2190                 MEM_freeN(internal->free_ptr);
2191                 internal->free_ptr= NULL;
2192         }
2193         MEM_freeN(iter->internal);
2194         iter->internal= NULL;
2195 }
2196
2197 /* RNA Path - Experiment */
2198
2199 static char *rna_path_token(const char **path, char *fixedbuf, int fixedlen, int bracket)
2200 {
2201         const char *p;
2202         char *buf;
2203         int i, j, len, escape;
2204
2205         len= 0;
2206
2207         if(bracket) {
2208                 /* get data between [], check escaping ] with \] */
2209                 if(**path == '[') (*path)++;
2210                 else return NULL;
2211
2212                 p= *path;
2213
2214                 escape= 0;
2215                 while(*p && (*p != ']' || escape)) {
2216                         escape= (*p == '\\');
2217                         len++;
2218                         p++;
2219                 }
2220
2221                 if(*p != ']') return NULL;
2222         }
2223         else {
2224                 /* get data until . or [ */
2225                 p= *path;
2226
2227                 while(*p && *p != '.' && *p != '[') {
2228                         len++;
2229                         p++;
2230                 }
2231         }
2232         
2233         /* empty, return */
2234         if(len == 0)
2235                 return NULL;
2236         
2237         /* try to use fixed buffer if possible */
2238         if(len+1 < fixedlen)
2239                 buf= fixedbuf;
2240         else
2241                 buf= MEM_callocN(sizeof(char)*(len+1), "rna_path_token");
2242
2243         /* copy string, taking into account escaped ] */
2244         if(bracket) {
2245                 for(p=*path, i=0, j=0; i<len; i++, p++) {
2246                         if(*p == '\\' && *(p+1) == ']');
2247                         else buf[j++]= *p;
2248                 }
2249
2250                 buf[j]= 0;
2251         }
2252         else {
2253                 memcpy(buf, *path, sizeof(char)*len);
2254                 buf[len]= '\0';
2255         }
2256
2257         /* set path to start of next token */
2258         if(*p == ']') p++;
2259         if(*p == '.') p++;
2260         *path= p;
2261
2262         return buf;
2263 }
2264
2265 /* Resolve the given RNA path to find the pointer+property indicated at the end of the path */
2266 int RNA_path_resolve(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
2267 {
2268         PropertyRNA *prop;
2269         PointerRNA curptr, nextptr;
2270         char fixedbuf[256], *token;
2271         int len, intkey;
2272
2273         prop= NULL;
2274         curptr= *ptr;
2275
2276         if(path==NULL)
2277                 return 0;
2278
2279         while(*path) {
2280                 /* look up property name in current struct */
2281                 token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 0);
2282
2283                 if(!token)
2284                         return 0;
2285
2286                 prop= RNA_struct_find_property(&curptr, token);
2287
2288                 if(token != fixedbuf)
2289                         MEM_freeN(token);
2290
2291                 if(!prop)
2292                         return 0;
2293
2294                 /* now look up the value of this property if it is a pointer or
2295                  * collection, otherwise return the property rna so that the
2296                  * caller can read the value of the property itself */
2297                 if(RNA_property_type(prop) == PROP_POINTER) {
2298                         nextptr= RNA_property_pointer_get(&curptr, prop);
2299
2300                         if(nextptr.data)
2301                                 curptr= nextptr;
2302                         else
2303                                 return 0;
2304                 }
2305                 else if(RNA_property_type(prop) == PROP_COLLECTION && *path) {
2306                         /* resolve the lookup with [] brackets */
2307                         token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1);
2308
2309                         if(!token)
2310                                 return 0;
2311
2312                         len= strlen(token);
2313                         
2314                         /* check for "" to see if it is a string */
2315                         if(len >= 2 && token[0] == '"' && token[len-1] == '"') {
2316                                 /* strip away "" */
2317                                 token[len-1]= 0;
2318                                 RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr);
2319                         }
2320                         else {
2321                                 /* otherwise do int lookup */
2322                                 intkey= atoi(token);
2323                                 RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr);
2324                         }
2325
2326                         if(token != fixedbuf)
2327                                 MEM_freeN(token);
2328
2329                         if(nextptr.data)
2330                                 curptr= nextptr;
2331                         else
2332                                 return 0;
2333                 }
2334         }
2335
2336         *r_ptr= curptr;
2337         *r_prop= prop;
2338
2339         return 1;
2340 }
2341
2342 char *RNA_path_append(const char *path, PointerRNA *ptr, PropertyRNA *prop, int intkey, const char *strkey)
2343 {
2344         DynStr *dynstr;
2345         const char *s;
2346         char appendstr[128], *result;
2347         
2348         dynstr= BLI_dynstr_new();
2349
2350         /* add .identifier */
2351         if(path) {
2352                 BLI_dynstr_append(dynstr, (char*)path);
2353                 if(*path)
2354                         BLI_dynstr_append(dynstr, ".");
2355         }
2356
2357         BLI_dynstr_append(dynstr, (char*)RNA_property_identifier(prop));
2358
2359         if(RNA_property_type(prop) == PROP_COLLECTION) {
2360                 /* add ["strkey"] or [intkey] */
2361                 BLI_dynstr_append(dynstr, "[");
2362
2363                 if(strkey) {
2364                         BLI_dynstr_append(dynstr, "\"");
2365                         for(s=strkey; *s; s++) {
2366                                 if(*s == '[') {
2367                                         appendstr[0]= '\\';
2368                                         appendstr[1]= *s;
2369                                         appendstr[2]= 0;
2370                                 }
2371                                 else {
2372                                         appendstr[0]= *s;
2373                                         appendstr[1]= 0;
2374                                 }
2375                                 BLI_dynstr_append(dynstr, appendstr);
2376                         }
2377                         BLI_dynstr_append(dynstr, "\"");
2378                 }
2379                 else {
2380                         sprintf(appendstr, "%d", intkey);
2381                         BLI_dynstr_append(dynstr, appendstr);
2382                 }
2383
2384                 BLI_dynstr_append(dynstr, "]");
2385         }
2386
2387         result= BLI_dynstr_get_cstring(dynstr);
2388         BLI_dynstr_free(dynstr);
2389
2390         return result;
2391 }
2392
2393 char *RNA_path_back(const char *path)
2394 {
2395         char fixedbuf[256];
2396         const char *previous, *current;
2397         char *result, *token;
2398         int i;
2399
2400         if(!path)
2401                 return NULL;
2402
2403         previous= NULL;
2404         current= path;
2405
2406         /* parse token by token until the end, then we back up to the previous
2407          * position and strip of the next token to get the path one step back */
2408         while(*current) {
2409                 token= rna_path_token(&current, fixedbuf, sizeof(fixedbuf), 0);
2410
2411                 if(!token)
2412                         return NULL;
2413                 if(token != fixedbuf)
2414                         MEM_freeN(token);
2415
2416                 /* in case of collection we also need to strip off [] */
2417                 token= rna_path_token(&current, fixedbuf, sizeof(fixedbuf), 1);
2418                 if(token && token != fixedbuf)
2419                         MEM_freeN(token);
2420                 
2421                 if(!*current)
2422                         break;
2423
2424                 previous= current;
2425         }
2426
2427         if(!previous)
2428                 return NULL;
2429
2430         /* copy and strip off last token */
2431         i= previous - path;
2432         result= BLI_strdup(path);
2433
2434         if(i > 0 && result[i-1] == '.') i--;
2435         result[i]= 0;
2436
2437         return result;
2438 }
2439
2440 char *RNA_path_from_ID_to_struct(PointerRNA *ptr)
2441 {
2442         char *ptrpath=NULL;
2443
2444         if(!ptr->id.data || !ptr->data)
2445                 return NULL;
2446         
2447         if(!RNA_struct_is_ID(ptr->type)) {
2448                 if(ptr->type->path) {
2449                         /* if type has a path to some ID, use it */
2450                         ptrpath= ptr->type->path(ptr);
2451                 }
2452                 else if(ptr->type->nested && RNA_struct_is_ID(ptr->type->nested)) {
2453                         PointerRNA parentptr;
2454                         PropertyRNA *userprop;
2455                         
2456                         /* find the property in the struct we're nested in that references this struct, and 
2457                          * use its identifier as the first part of the path used...
2458                          */
2459                         RNA_id_pointer_create(ptr->id.data, &parentptr);
2460                         userprop= RNA_struct_find_nested(&parentptr, ptr->type); 
2461                         
2462                         if(userprop)
2463                                 ptrpath= BLI_strdup(RNA_property_identifier(userprop));
2464                         else
2465                                 return NULL; // can't do anything about this case yet...
2466                 }
2467                 else
2468                         return NULL;
2469         }
2470         
2471         return ptrpath;
2472 }
2473
2474 char *RNA_path_from_ID_to_property(PointerRNA *ptr, PropertyRNA *prop)
2475 {
2476         const char *propname;
2477         char *ptrpath, *path;
2478
2479         if(!ptr->id.data || !ptr->data || !prop)
2480                 return NULL;
2481         
2482         /* path from ID to the struct holding this property */
2483         ptrpath= RNA_path_from_ID_to_struct(ptr);
2484
2485         propname= RNA_property_identifier(prop);
2486
2487         if(ptrpath) {
2488                 path= BLI_sprintfN("%s.%s", ptrpath, propname);
2489                 MEM_freeN(ptrpath);
2490         }
2491         else
2492                 path= BLI_strdup(propname);
2493         
2494         return path;
2495 }
2496
2497 /* Quick name based property access */
2498
2499 int RNA_boolean_get(PointerRNA *ptr, const char *name)
2500 {
2501         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2502
2503         if(prop) {
2504                 return RNA_property_boolean_get(ptr, prop);
2505         }
2506         else {
2507                 printf("RNA_boolean_get: %s.%s not found.\n", ptr->type->identifier, name);
2508                 return 0;
2509         }
2510 }
2511
2512 void RNA_boolean_set(PointerRNA *ptr, const char *name, int value)
2513 {
2514         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2515
2516         if(prop)
2517                 RNA_property_boolean_set(ptr, prop, value);
2518         else
2519                 printf("RNA_boolean_set: %s.%s not found.\n", ptr->type->identifier, name);
2520 }
2521
2522 void RNA_boolean_get_array(PointerRNA *ptr, const char *name, int *values)
2523 {
2524         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2525
2526         if(prop)
2527                 RNA_property_boolean_get_array(ptr, prop, values);
2528         else
2529                 printf("RNA_boolean_get_array: %s.%s not found.\n", ptr->type->identifier, name);
2530 }
2531
2532 void RNA_boolean_set_array(PointerRNA *ptr, const char *name, const int *values)
2533 {
2534         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2535
2536         if(prop)
2537                 RNA_property_boolean_set_array(ptr, prop, values);
2538         else
2539                 printf("RNA_boolean_set_array: %s.%s not found.\n", ptr->type->identifier, name);
2540 }
2541
2542 int RNA_int_get(PointerRNA *ptr, const char *name)
2543 {
2544         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2545
2546         if(prop) {
2547                 return RNA_property_int_get(ptr, prop);
2548         }
2549         else {
2550                 printf("RNA_int_get: %s.%s not found.\n", ptr->type->identifier, name);
2551                 return 0;
2552         }
2553 }
2554
2555 void RNA_int_set(PointerRNA *ptr, const char *name, int value)
2556 {
2557         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2558
2559         if(prop)
2560                 RNA_property_int_set(ptr, prop, value);
2561         else
2562                 printf("RNA_int_set: %s.%s not found.\n", ptr->type->identifier, name);
2563 }
2564
2565 void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
2566 {
2567         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2568
2569         if(prop)
2570                 RNA_property_int_get_array(ptr, prop, values);
2571         else
2572                 printf("RNA_int_get_array: %s.%s not found.\n", ptr->type->identifier, name);
2573 }
2574
2575 void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
2576 {
2577         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2578
2579         if(prop)
2580                 RNA_property_int_set_array(ptr, prop, values);
2581         else
2582                 printf("RNA_int_set_array: %s.%s not found.\n", ptr->type->identifier, name);
2583 }
2584
2585 float RNA_float_get(PointerRNA *ptr, const char *name)
2586 {
2587         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2588
2589         if(prop) {
2590                 return RNA_property_float_get(ptr, prop);
2591         }
2592         else {
2593                 printf("RNA_float_get: %s.%s not found.\n", ptr->type->identifier, name);
2594                 return 0;
2595         }
2596 }
2597
2598 void RNA_float_set(PointerRNA *ptr, const char *name, float value)
2599 {
2600         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2601
2602         if(prop)
2603                 RNA_property_float_set(ptr, prop, value);
2604         else
2605                 printf("RNA_float_set: %s.%s not found.\n", ptr->type->identifier, name);
2606 }
2607
2608 void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
2609 {
2610         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2611
2612         if(prop)
2613                 RNA_property_float_get_array(ptr, prop, values);
2614         else
2615                 printf("RNA_float_get_array: %s.%s not found.\n", ptr->type->identifier, name);
2616 }
2617
2618 void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
2619 {
2620         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2621
2622         if(prop)
2623                 RNA_property_float_set_array(ptr, prop, values);
2624         else
2625                 printf("RNA_float_set_array: %s.%s not found.\n", ptr->type->identifier, name);
2626 }
2627
2628 int RNA_enum_get(PointerRNA *ptr, const char *name)
2629 {
2630         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2631
2632         if(prop) {
2633                 return RNA_property_enum_get(ptr, prop);
2634         }
2635         else {
2636                 printf("RNA_enum_get: %s.%s not found.\n", ptr->type->identifier, name);
2637                 return 0;
2638         }
2639 }
2640
2641 void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
2642 {
2643         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2644
2645         if(prop)
2646                 RNA_property_enum_set(ptr, prop, value);
2647         else
2648                 printf("RNA_enum_set: %s.%s not found.\n", ptr->type->identifier, name);
2649 }
2650
2651 int RNA_enum_is_equal(bContext *C, PointerRNA *ptr, const char *name, const char *enumname)
2652 {
2653         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2654         EnumPropertyItem *item;
2655         int free;
2656
2657         if(prop) {
2658                 RNA_property_enum_items(C, ptr, prop, &item, NULL, &free);
2659
2660                 for(; item->identifier; item++)
2661                         if(strcmp(item->identifier, enumname) == 0)
2662                                 return (item->value == RNA_property_enum_get(ptr, prop));
2663
2664                 if(free)
2665                         MEM_freeN(item);
2666
2667                 printf("RNA_enum_is_equal: %s.%s item %s not found.\n", ptr->type->identifier, name, enumname);
2668                 return 0;
2669         }
2670         else {
2671                 printf("RNA_enum_is_equal: %s.%s not found.\n", ptr->type->identifier, name);
2672                 return 0;
2673         }
2674 }
2675
2676 int RNA_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value)
2677 {
2678         for( ; item->identifier; item++) {
2679                 if(strcmp(item->identifier, identifier)==0) {
2680                         *value= item->value;
2681                         return 1;
2682                 }
2683         }
2684         
2685         return 0;
2686 }
2687
2688 int     RNA_enum_id_from_value(EnumPropertyItem *item, int value, const char **identifier)
2689 {
2690         for( ; item->identifier; item++) {
2691                 if(item->value==value) {
2692                         *identifier= item->identifier;
2693                         return 1;
2694                 }
2695         }
2696
2697         return 0;
2698 }
2699
2700
2701
2702 void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
2703 {
2704         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2705
2706         if(prop)
2707                 RNA_property_string_get(ptr, prop, value);
2708         else
2709                 printf("RNA_string_get: %s.%s not found.\n", ptr->type->identifier, name);
2710 }
2711
2712 char *RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen)
2713 {
2714         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2715
2716         if(prop) {
2717                 return RNA_property_string_get_alloc(ptr, prop, fixedbuf, fixedlen);
2718         }
2719         else {
2720                 printf("RNA_string_get_alloc: %s.%s not found.\n", ptr->type->identifier, name);
2721                 return 0;
2722         }
2723 }
2724
2725 int RNA_string_length(PointerRNA *ptr, const char *name)
2726 {
2727         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2728
2729         if(prop) {
2730                 return RNA_property_string_length(ptr, prop);
2731         }
2732         else {
2733                 printf("RNA_string_length: %s.%s not found.\n", ptr->type->identifier, name);
2734                 return 0;
2735         }
2736 }
2737
2738 void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
2739 {
2740         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2741
2742         if(prop)
2743                 RNA_property_string_set(ptr, prop, value);
2744         else
2745                 printf("RNA_string_set: %s.%s not found.\n", ptr->type->identifier, name);
2746 }
2747
2748 PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
2749 {
2750         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2751
2752         if(prop) {
2753                 return RNA_property_pointer_get(ptr, prop);
2754         }
2755         else {
2756                 PointerRNA result;
2757
2758                 printf("RNA_pointer_get: %s.%s not found.\n", ptr->type->identifier, name);
2759
2760                 memset(&result, 0, sizeof(result));
2761                 return result;
2762         }
2763 }
2764
2765 void RNA_pointer_set(PointerRNA *ptr, const char *name, PointerRNA ptr_value)
2766 {
2767         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2768
2769         if(prop) {
2770                 RNA_property_pointer_set(ptr, prop, ptr_value);
2771         }
2772         else {
2773                 printf("RNA_pointer_set: %s.%s not found.\n", ptr->type->identifier, name);
2774         }
2775 }
2776
2777 void RNA_pointer_add(PointerRNA *ptr, const char *name)
2778 {
2779         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2780
2781         if(prop)
2782                 RNA_property_pointer_add(ptr, prop);
2783         else
2784                 printf("RNA_pointer_set: %s.%s not found.\n", ptr->type->identifier, name);
2785 }
2786
2787 void RNA_collection_begin(PointerRNA *ptr, const char *name, CollectionPropertyIterator *iter)
2788 {
2789         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2790
2791         if(prop)
2792                 RNA_property_collection_begin(ptr, prop, iter);
2793         else
2794                 printf("RNA_collection_begin: %s.%s not found.\n", ptr->type->identifier, name);
2795 }
2796
2797 void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
2798 {
2799         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2800
2801         if(prop)
2802                 RNA_property_collection_add(ptr, prop, r_value);
2803         else
2804                 printf("RNA_collection_add: %s.%s not found.\n", ptr->type->identifier, name);
2805 }
2806
2807 void RNA_collection_clear(PointerRNA *ptr, const char *name)
2808 {
2809         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2810
2811         if(prop)
2812                 RNA_property_collection_clear(ptr, prop);
2813         else
2814                 printf("RNA_collection_clear: %s.%s not found.\n", ptr->type->identifier, name);
2815 }
2816
2817 int RNA_collection_length(PointerRNA *ptr, const char *name)
2818 {
2819         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2820
2821         if(prop) {
2822                 return RNA_property_collection_length(ptr, prop);
2823         }
2824         else {
2825                 printf("RNA_collection_length: %s.%s not found.\n", ptr->type->identifier, name);
2826                 return 0;
2827         }
2828 }
2829
2830 int RNA_property_is_set(PointerRNA *ptr, const char *name)
2831 {
2832         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2833
2834         if(prop) {
2835                 if(prop->flag & PROP_IDPROPERTY)
2836                         return (rna_idproperty_find(ptr, name) != NULL);
2837                 else
2838                         return 1;
2839         }
2840         else {
2841                 // printf("RNA_property_is_set: %s.%s not found.\n", ptr->type->identifier, name);
2842                 return 0;
2843         }
2844 }
2845
2846 /* string representation of a property, python
2847  * compatible but can be used for display too*/
2848 char *RNA_pointer_as_string(PointerRNA *ptr)
2849 {
2850         DynStr *dynstr= BLI_dynstr_new();
2851         char *cstring;
2852         
2853         const char *propname;
2854         int first_time = 1;
2855         
2856         BLI_dynstr_append(dynstr, "{");
2857         
2858         RNA_STRUCT_BEGIN(ptr, prop) {
2859                 propname = RNA_property_identifier(prop);
2860                 
2861                 if(strcmp(propname, "rna_type")==0)
2862                         continue;
2863                 
2864                 if(first_time==0)
2865                         BLI_dynstr_append(dynstr, ", ");
2866                 first_time= 0;
2867                 
2868                 cstring = RNA_property_as_string(NULL, ptr, prop);
2869                 BLI_dynstr_appendf(dynstr, "\"%s\":%s", propname, cstring);
2870                 MEM_freeN(cstring);
2871         }
2872         RNA_STRUCT_END;
2873
2874         BLI_dynstr_append(dynstr, "}"); 
2875         
2876         
2877         cstring = BLI_dynstr_get_cstring(dynstr);
2878         BLI_dynstr_free(dynstr);
2879         return cstring;
2880 }
2881
2882 char *RNA_property_as_string(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
2883 {
2884         int type = RNA_property_type(prop);
2885         int len = RNA_property_array_length(ptr, prop);
2886         int i;
2887
2888         DynStr *dynstr= BLI_dynstr_new();
2889         char *cstring;
2890         
2891
2892         /* see if we can coorce into a python type - PropertyType */
2893         switch (type) {
2894         case PROP_BOOLEAN:
2895                 if(len==0) {
2896                         BLI_dynstr_append(dynstr, RNA_property_boolean_get(ptr, prop) ? "True" : "False");
2897                 }
2898                 else {
2899                         BLI_dynstr_append(dynstr, "(");
2900                         for(i=0; i<len; i++) {
2901                                 BLI_dynstr_appendf(dynstr, i?", %s":"%s", RNA_property_boolean_get_index(ptr, prop, i) ? "True" : "False");
2902                         }
2903                         if(len==1)
2904                                 BLI_dynstr_append(dynstr, ","); /* otherwise python wont see it as a tuple */
2905                         BLI_dynstr_append(dynstr, ")");
2906                 }
2907                 break;
2908         case PROP_INT:
2909                 if(len==0) {
2910                         BLI_dynstr_appendf(dynstr, "%d", RNA_property_int_get(ptr, prop));
2911                 }
2912                 else {
2913                         BLI_dynstr_append(dynstr, "(");
2914                         for(i=0; i<len; i++) {
2915                                 BLI_dynstr_appendf(dynstr, i?", %d":"%d", RNA_property_int_get_index(ptr, prop, i));
2916                         }
2917                         if(len==1)
2918                                 BLI_dynstr_append(dynstr, ","); /* otherwise python wont see it as a tuple */
2919                         BLI_dynstr_append(dynstr, ")");
2920                 }
2921                 break;
2922         case PROP_FLOAT:
2923                 if(len==0) {
2924                         BLI_dynstr_appendf(dynstr, "%g", RNA_property_float_get(ptr, prop));
2925                 }
2926                 else {
2927                         BLI_dynstr_append(dynstr, "(");
2928                         for(i=0; i<len; i++) {
2929                                 BLI_dynstr_appendf(dynstr, i?", %g":"%g", RNA_property_float_get_index(ptr, prop, i));
2930                         }
2931                         if(len==1)
2932                                 BLI_dynstr_append(dynstr, ","); /* otherwise python wont see it as a tuple */
2933                         BLI_dynstr_append(dynstr, ")");
2934                 }
2935                 break;
2936         case PROP_STRING:
2937         {
2938                 /* string arrays dont exist */
2939                 char *buf;
2940                 buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1);
2941                 BLI_dynstr_appendf(dynstr, "\"%s\"", buf);
2942                 MEM_freeN(buf);
2943                 break;
2944         }
2945         case PROP_ENUM:
2946         {
2947                 /* string arrays dont exist */
2948                 const char *identifier;
2949                 int val = RNA_property_enum_get(ptr, prop);
2950
2951                 if(RNA_property_enum_identifier(C, ptr, prop, val, &identifier)) {
2952                         BLI_dynstr_appendf(dynstr, "'%s'", identifier);
2953                 }
2954                 else {
2955                         BLI_dynstr_appendf(dynstr, "'<UNKNOWN ENUM>'", identifier);
2956                 }
2957                 break;
2958         }
2959         case PROP_POINTER:
2960         {
2961                 BLI_dynstr_append(dynstr, "'<POINTER>'"); /* TODO */
2962                 break;
2963         }
2964         case PROP_COLLECTION:
2965         {
2966                 int first_time = 1;
2967                 CollectionPropertyIterator collect_iter;
2968                 BLI_dynstr_append(dynstr, "[");
2969                 
2970                 for(RNA_property_collection_begin(ptr, prop, &collect_iter); collect_iter.valid; RNA_property_collection_next(&collect_iter)) {
2971                         PointerRNA itemptr= collect_iter.ptr;
2972                         
2973                         if(first_time==0)
2974                                 BLI_dynstr_append(dynstr, ", ");
2975                         first_time= 0;
2976                         
2977                         /* now get every prop of the collection */
2978                         cstring= RNA_pointer_as_string(&itemptr);
2979                         BLI_dynstr_append(dynstr, cstring);
2980                         MEM_freeN(cstring);
2981                 }
2982                 
2983                 RNA_property_collection_end(&collect_iter);
2984                 BLI_dynstr_append(dynstr, "]");
2985                 break;
2986         }
2987         default:
2988                 BLI_dynstr_append(dynstr, "'<UNKNOWN TYPE>'"); /* TODO */
2989                 break;
2990         }
2991
2992         cstring = BLI_dynstr_get_cstring(dynstr);
2993         BLI_dynstr_free(dynstr);
2994         return cstring;
2995 }
2996
2997 /* Function */
2998
2999 const char *RNA_function_identifier(FunctionRNA *func)
3000 {
3001         return func->identifier;
3002 }
3003
3004 PropertyRNA *RNA_function_return(FunctionRNA *func)
3005 {
3006         return func->ret;
3007 }
3008
3009 const char *RNA_function_ui_description(FunctionRNA *func)
3010 {
3011         return func->description;
3012 }
3013
3014 int RNA_function_flag(FunctionRNA *func)
3015 {
3016         return func->flag;
3017 }
3018
3019 PropertyRNA *RNA_function_get_parameter(PointerRNA *ptr, FunctionRNA *func, int index)
3020 {
3021         PropertyRNA *parm;
3022         int i;
3023
3024         parm= func->cont.properties.first;
3025         for(i= 0; parm; parm= parm->next, i++)
3026                 if(i==index)
3027                         return parm;
3028
3029         return NULL;
3030 }
3031
3032 PropertyRNA *RNA_function_find_parameter(PointerRNA *ptr, FunctionRNA *func, const char *identifier)
3033 {
3034         PropertyRNA *parm;
3035
3036         parm= func->cont.properties.first;
3037         for(; parm; parm= parm->next)
3038                 if(strcmp(parm->identifier, identifier)==0)
3039                         return parm;
3040
3041         return NULL;
3042 }
3043
3044 const struct ListBase *RNA_function_defined_parameters(FunctionRNA *func)
3045 {
3046         return &func->cont.properties;
3047 }
3048
3049 /* Utility */
3050
3051 ParameterList *RNA_parameter_list_create(ParameterList *parms, PointerRNA *ptr, FunctionRNA *func)
3052 {
3053         PropertyRNA *parm;
3054         void *data;
3055         int tot= 0, size;
3056
3057         /* allocate data */
3058         for(parm= func->cont.properties.first; parm; parm= parm->next)
3059                 tot+= rna_parameter_size(parm);
3060
3061         parms->data= MEM_callocN(tot, "RNA_parameter_list_create");
3062         parms->func= func;
3063         parms->tot= tot;
3064
3065         /* set default values */
3066         data= parms->data;
3067
3068         for(parm= func->cont.properties.first; parm; parm= parm->next) {
3069                 size= rna_parameter_size(parm);
3070
3071                 if(!(parm->flag & PROP_REQUIRED)) {
3072                         switch(parm->type) {
3073                                 case PROP_BOOLEAN:
3074                                         if(parm->arraydimension) memcpy(data, &((BooleanPropertyRNA*)parm)->defaultarray, size);
3075                                         else memcpy(data, &((BooleanPropertyRNA*)parm)->defaultvalue, size);
3076                                         break;
3077                                 case PROP_INT:
3078                                         if(parm->arraydimension) memcpy(data, &((IntPropertyRNA*)parm)->defaultarray, size);
3079                                         else memcpy(data, &((IntPropertyRNA*)parm)->defaultvalue, size);
3080                                         break;
3081                                 case PROP_FLOAT:
3082                                         if(parm->arraydimension) memcpy(data, &((FloatPropertyRNA*)parm)->defaultarray, size);
3083                                         else memcpy(data, &((FloatPropertyRNA*)parm)->defaultvalue, size);
3084                                         break;
3085                                 case PROP_ENUM:
3086                                         memcpy(data, &((EnumPropertyRNA*)parm)->defaultvalue, size);
3087                                         break;
3088                                 case PROP_STRING: {
3089                                         const char *defvalue= ((StringPropertyRNA*)parm)->defaultvalue;
3090                                         if(defvalue && defvalue[0])
3091                                                 memcpy(data, &defvalue, size);
3092                                         break;
3093                                 }
3094                                 case PROP_POINTER:
3095                                 case PROP_COLLECTION:
3096                                         break;
3097                         }
3098                 }
3099
3100                 data= ((char*)data) + size;
3101         }
3102
3103         return parms;
3104 }
3105
3106 void RNA_parameter_list_free(ParameterList *parms)
3107 {
3108         PropertyRNA *parm;
3109         int tot;
3110
3111         parm= parms->func->cont.properties.first;
3112         for(tot= 0; parm; parm= parm->next) {
3113                 if(parm->type == PROP_COLLECTION)
3114                         BLI_freelistN((ListBase*)((char*)parms->data+tot));
3115                 else if (parm->flag & PROP_DYNAMIC) {
3116                         /* for dynamic arrays and strings, data is a pointer to an array */
3117                         char *array= *(char**)((char*)parms->data+tot);
3118                         if(array)
3119                                 MEM_freeN(array);
3120                 }
3121
3122                 tot+= rna_parameter_size(parm);
3123         }
3124
3125         MEM_freeN(parms->data);
3126         parms->data= NULL;
3127
3128         parms->func= NULL;
3129 }
3130
3131 int  RNA_parameter_list_size(ParameterList *parms)
3132 {
3133         return parms->tot;
3134 }
3135
3136 void RNA_parameter_list_begin(ParameterList *parms, ParameterIterator *iter)
3137 {
3138         PropertyType ptype;
3139
3140         RNA_pointer_create(NULL, &RNA_Function, parms->func, &iter->funcptr);
3141
3142         iter->parms= parms;
3143         iter->parm= parms->func->cont.properties.first;
3144         iter->valid= iter->parm != NULL;
3145         iter->offset= 0;
3146
3147         if(iter->valid) {
3148                 iter->size= rna_parameter_size(iter->parm);
3149                 iter->data= (((char*)iter->parms->data)+iter->offset);
3150                 ptype= RNA_property_type(iter->parm);
3151         }
3152 }
3153
3154 void RNA_parameter_list_next(ParameterIterator *iter)
3155 {
3156         PropertyType ptype;
3157
3158         iter->offset+= iter->size;
3159         iter->parm= iter->parm->next;
3160         iter->valid= iter->parm != NULL;
3161
3162         if(iter->valid) {
3163                 iter->size= rna_parameter_size(iter->parm);
3164                 iter->data= (((char*)iter->parms->data)+iter->offset);
3165                 ptype= RNA_property_type(iter->parm);
3166         }
3167 }
3168
3169 void RNA_parameter_list_end(ParameterIterator *iter)
3170 {
3171         /* nothing to do */
3172 }
3173
3174 void RNA_parameter_get(ParameterList *parms, PropertyRNA *parm, void **value)
3175 {
3176         ParameterIterator iter;
3177
3178         RNA_parameter_list_begin(parms, &iter);
3179
3180         for(; iter.valid; RNA_parameter_list_next(&iter))
3181                 if(iter.parm==parm) 
3182                         break;
3183
3184         if(iter.valid)
3185                 *value= iter.data;
3186         else
3187                 *value= NULL;
3188
3189         RNA_parameter_list_end(&iter);
3190 }
3191
3192 void RNA_parameter_get_lookup(ParameterList *parms, const char *identifier, void **value)
3193 {
3194         PropertyRNA *parm;
3195
3196         parm= parms->func->cont.properties.first;
3197         for(; parm; parm= parm->next)
3198                 if(strcmp(RNA_property_identifier(parm), identifier)==0)
3199                         break;
3200
3201         if(parm)
3202                 RNA_parameter_get(parms, parm, value);
3203 }
3204
3205 void RNA_parameter_set(ParameterList *parms, PropertyRNA *parm, void *value)
3206 {
3207         ParameterIterator iter;
3208
3209         RNA_parameter_list_begin(parms, &iter);
3210
3211         for(; iter.valid; RNA_parameter_list_next(&iter))
3212                 if(iter.parm==parm) 
3213                         break;
3214
3215         if(iter.valid)
3216                 memcpy(iter.data, value, iter.size);
3217
3218         RNA_parameter_list_end(&iter);
3219 }
3220
3221 void RNA_parameter_set_lookup(ParameterList *parms, const char *identifier, void *value)
3222 {
3223         PropertyRNA *parm;
3224
3225         parm= parms->func->cont.properties.first;
3226         for(; parm; parm= parm->next)
3227                 if(strcmp(RNA_property_identifier(parm), identifier)==0)
3228                         break;
3229
3230         if(parm)
3231                 RNA_parameter_set(parms, parm, value);
3232 }
3233
3234 int RNA_function_call(bContext *C, ReportList *reports, PointerRNA *ptr, FunctionRNA *func, ParameterList *parms)
3235 {
3236         if(func->call) {
3237                 func->call(C, reports, ptr, parms);
3238
3239                 return 0;
3240         }
3241
3242         return -1;
3243 }
3244
3245 int RNA_function_call_lookup(bContext *C, ReportList *reports, PointerRNA *ptr, const char *identifier, ParameterList *parms)
3246 {
3247         FunctionRNA *func;
3248
3249         func= RNA_struct_find_function(ptr, identifier);
3250
3251         if(func)
3252                 return RNA_function_call(C, reports, ptr, func, parms);
3253
3254         return -1;
3255 }
3256
3257 int RNA_function_call_direct(bContext *C, ReportList *reports, PointerRNA *ptr, FunctionRNA *func, const char *format, ...)
3258 {
3259         va_list args;
3260         int ret;
3261
3262         va_start(args, format);
3263
3264         ret= RNA_function_call_direct_va(C, reports, ptr, func, format, args);
3265
3266         va_end(args);
3267
3268         return ret;
3269 }
3270
3271 int RNA_function_call_direct_lookup(bContext *C, ReportList *reports, PointerRNA *ptr, const char *identifier, const char *format, ...)
3272 {
3273         FunctionRNA *func;
3274
3275         func= RNA_struct_find_function(ptr, identifier);
3276
3277         if(func) {
3278                 va_list args;
3279                 int ret;
3280
3281                 va_start(args, format);
3282
3283                 ret= RNA_function_call_direct_va(C, reports, ptr, func, format, args);
3284
3285                 va_end(args);
3286
3287                 return ret;
3288         }
3289
3290         return -1;
3291 }
3292
3293 static int rna_function_format_array_length(const char *format, int ofs, int flen)
3294 {
3295         char lenbuf[16];