2.5: File Selector: display operator properties in the side region,
[blender-staging.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_idproperty_verify_valid(PropertyRNA *prop, IDProperty *idprop)
213 {
214         /* this verifies if the idproperty actually matches the property
215          * description and otherwise removes it. this is to ensure that
216          * rna property access is type safe, e.g. if you defined the rna
217          * to have a certain array length you can count on that staying so */
218         
219         switch(idprop->type) {
220                 case IDP_IDPARRAY:
221                         if(prop->type != PROP_COLLECTION)
222                                 return 0;
223                         break;
224                 case IDP_ARRAY:
225                         if(prop->arraylength != idprop->len)
226                                 return 0;
227
228                         if(idprop->subtype == IDP_FLOAT && prop->type != PROP_FLOAT)
229                                 return 0;
230                         if(idprop->subtype == IDP_INT && !ELEM3(prop->type, PROP_BOOLEAN, PROP_INT, PROP_ENUM))
231                                 return 0;
232
233                         break;
234                 case IDP_INT:
235                         if(!ELEM3(prop->type, PROP_BOOLEAN, PROP_INT, PROP_ENUM))
236                                 return 0;
237                         break;
238                 case IDP_FLOAT:
239                 case IDP_DOUBLE:
240                         if(prop->type != PROP_FLOAT)
241                                 return 0;
242                         break;
243                 case IDP_STRING:
244                         if(prop->type != PROP_STRING)
245                                 return 0;
246                         break;
247                 case IDP_GROUP:
248                         if(prop->type != PROP_POINTER)
249                                 return 0;
250                         break;
251                 default:
252                         return 0;
253         }
254
255         return 1;
256 }
257
258 static PropertyRNA *typemap[IDP_NUMTYPES] =
259         {(PropertyRNA*)&rna_IDProperty_string,
260          (PropertyRNA*)&rna_IDProperty_int,
261          (PropertyRNA*)&rna_IDProperty_float,
262          NULL, NULL, NULL,
263          (PropertyRNA*)&rna_IDProperty_group, NULL,
264          (PropertyRNA*)&rna_IDProperty_double};
265
266 static PropertyRNA *arraytypemap[IDP_NUMTYPES] =
267         {NULL, (PropertyRNA*)&rna_IDProperty_int_array,
268          (PropertyRNA*)&rna_IDProperty_float_array,
269          NULL, NULL, NULL,
270          (PropertyRNA*)&rna_IDProperty_collection, NULL,
271          (PropertyRNA*)&rna_IDProperty_double_array};
272
273 IDProperty *rna_idproperty_check(PropertyRNA **prop, PointerRNA *ptr)
274 {
275         /* This is quite a hack, but avoids some complexity in the API. we
276          * pass IDProperty structs as PropertyRNA pointers to the outside.
277          * We store some bytes in PropertyRNA structs that allows us to
278          * distinguish it from IDProperty structs. If it is an ID property,
279          * we look up an IDP PropertyRNA based on the type, and set the data
280          * pointer to the IDProperty. */
281
282         if((*prop)->magic == RNA_MAGIC) {
283                 if((*prop)->flag & PROP_IDPROPERTY) {
284                         IDProperty *idprop= rna_idproperty_find(ptr, (*prop)->identifier);
285
286                         if(idprop && !rna_idproperty_verify_valid(*prop, idprop)) {
287                                 IDProperty *group= RNA_struct_idproperties(ptr, 0);
288
289                                 IDP_RemFromGroup(group, idprop);
290                                 IDP_FreeProperty(idprop);
291                                 MEM_freeN(idprop);
292                                 return NULL;
293                         }
294
295                         return idprop;
296                 }
297                 else
298                         return NULL;
299         }
300
301         {
302                 IDProperty *idprop= (IDProperty*)(*prop);
303
304                 if(idprop->type == IDP_ARRAY)
305                         *prop= arraytypemap[(int)(idprop->subtype)];
306                 else 
307                         *prop= typemap[(int)(idprop->type)];
308
309                 return idprop;
310         }
311 }
312
313 PropertyRNA *rna_ensure_property(PropertyRNA *prop)
314 {
315         /* the quick version if we don't need the idproperty */
316
317         if(prop->magic == RNA_MAGIC)
318                 return prop;
319
320         {
321                 IDProperty *idprop= (IDProperty*)prop;
322
323                 if(idprop->type == IDP_ARRAY)
324                         return arraytypemap[(int)(idprop->subtype)];
325                 else 
326                         return typemap[(int)(idprop->type)];
327         }
328 }
329
330 const char *rna_ensure_property_identifier(PropertyRNA *prop)
331 {
332         if(prop->magic == RNA_MAGIC)
333                 return prop->identifier;
334         else
335                 return ((IDProperty*)prop)->name;
336 }
337
338 const char *rna_ensure_property_name(PropertyRNA *prop)
339 {
340         if(prop->magic == RNA_MAGIC)
341                 return prop->name;
342         else
343                 return ((IDProperty*)prop)->name;
344 }
345
346 int rna_ensure_property_array_length(PropertyRNA *prop)
347 {
348         if(prop->magic == RNA_MAGIC)
349                 return prop->arraylength;
350         else {
351                 IDProperty *idprop= (IDProperty*)prop;
352
353                 if(idprop->type == IDP_ARRAY)
354                         return idprop->len;
355                 else
356                         return 0;
357         }
358 }
359
360 /* Structs */
361
362 const char *RNA_struct_identifier(StructRNA *type)
363 {
364         return type->identifier;
365 }
366
367 const char *RNA_struct_ui_name(StructRNA *type)
368 {
369         return type->name;
370 }
371
372 int RNA_struct_ui_icon(StructRNA *type)
373 {
374         if(type)
375                 return type->icon;
376         else
377                 return ICON_DOT;
378 }
379
380 const char *RNA_struct_ui_description(StructRNA *type)
381 {
382         return type->description;
383 }
384
385 PropertyRNA *RNA_struct_name_property(StructRNA *type)
386 {
387         return type->nameproperty;
388 }
389
390 PropertyRNA *RNA_struct_iterator_property(StructRNA *type)
391 {
392         return type->iteratorproperty;
393 }
394
395 int RNA_struct_is_ID(StructRNA *type)
396 {
397         return (type->flag & STRUCT_ID) != 0;
398 }
399
400 int RNA_struct_is_a(StructRNA *type, StructRNA *srna)
401 {
402         StructRNA *base;
403
404         if(!type)
405                 return 0;
406
407         /* ptr->type is always maximally refined */
408         for(base=type; base; base=base->base)
409                 if(base == srna)
410                         return 1;
411         
412         return 0;
413 }
414
415 PropertyRNA *RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
416 {
417         PropertyRNA *iterprop= RNA_struct_iterator_property(ptr->type);
418         PointerRNA propptr;
419
420         if(RNA_property_collection_lookup_string(ptr, iterprop, identifier, &propptr))
421                 return propptr.data;
422         
423         return NULL;
424 }
425
426 /* Find the property which uses the given nested struct */
427 PropertyRNA *RNA_struct_find_nested(PointerRNA *ptr, StructRNA *srna)
428 {
429         PropertyRNA *prop= NULL;
430
431         RNA_STRUCT_BEGIN(ptr, iprop) {
432                 /* This assumes that there can only be one user of this nested struct */
433                 if (RNA_property_pointer_type(ptr, iprop) == srna) {
434                         prop= iprop;
435                         break;
436                 }
437         }
438         RNA_PROP_END;
439
440         return prop;
441 }
442
443 const struct ListBase *RNA_struct_defined_properties(StructRNA *srna)
444 {
445         return &srna->cont.properties;
446 }
447
448 FunctionRNA *RNA_struct_find_function(PointerRNA *ptr, const char *identifier)
449 {
450         PointerRNA tptr;
451         PropertyRNA *iterprop;
452         FunctionRNA *func;
453
454         RNA_pointer_create(NULL, &RNA_Struct, ptr->type, &tptr);
455         iterprop= RNA_struct_find_property(&tptr, "functions");
456
457         func= NULL;
458
459         RNA_PROP_BEGIN(&tptr, funcptr, iterprop) {
460                 if(strcmp(identifier, RNA_function_identifier(funcptr.data)) == 0) {
461                         func= funcptr.data;
462                         break;
463                 }
464         }
465         RNA_PROP_END;
466
467         return func;
468 }
469
470 const struct ListBase *RNA_struct_defined_functions(StructRNA *srna)
471 {
472         return &srna->functions;
473 }
474
475 StructRegisterFunc RNA_struct_register(StructRNA *type)
476 {
477         return type->reg;
478 }
479
480 StructUnregisterFunc RNA_struct_unregister(StructRNA *type)
481 {
482         do {
483                 if(type->unreg)
484                         return type->unreg;
485         } while((type=type->base));
486
487         return NULL;
488 }
489
490 void *RNA_struct_py_type_get(StructRNA *srna)
491 {
492         return srna->py_type;
493 }
494
495 void RNA_struct_py_type_set(StructRNA *srna, void *py_type)
496 {
497         srna->py_type= py_type;
498 }
499
500 void *RNA_struct_blender_type_get(StructRNA *srna)
501 {
502         return srna->blender_type;
503 }
504
505 void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
506 {
507         srna->blender_type= blender_type;
508 }
509
510 char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen)
511 {
512         PropertyRNA *nameprop;
513
514         if(ptr->data && (nameprop = RNA_struct_name_property(ptr->type)))
515                 return RNA_property_string_get_alloc(ptr, nameprop, fixedbuf, fixedlen);
516
517         return NULL;
518 }
519
520 /* Property Information */
521
522 const char *RNA_property_identifier(PropertyRNA *prop)
523 {
524         return rna_ensure_property_identifier(prop);
525 }
526
527 PropertyType RNA_property_type(PropertyRNA *prop)
528 {
529         return rna_ensure_property(prop)->type;
530 }
531
532 PropertySubType RNA_property_subtype(PropertyRNA *prop)
533 {
534         return rna_ensure_property(prop)->subtype;
535 }
536
537 int RNA_property_flag(PropertyRNA *prop)
538 {
539         return rna_ensure_property(prop)->flag;
540 }
541
542 int RNA_property_array_length(PropertyRNA *prop)
543 {
544         return rna_ensure_property_array_length(prop);
545 }
546
547 void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, int *hardmax)
548 {
549         IntPropertyRNA *iprop= (IntPropertyRNA*)rna_ensure_property(prop);
550
551         if(iprop->range) {
552                 iprop->range(ptr, hardmin, hardmax);
553         }
554         else {
555                 *hardmin= iprop->hardmin;
556                 *hardmax= iprop->hardmax;
557         }
558 }
559
560 void RNA_property_int_ui_range(PointerRNA *ptr, PropertyRNA *prop, int *softmin, int *softmax, int *step)
561 {
562         IntPropertyRNA *iprop= (IntPropertyRNA*)rna_ensure_property(prop);
563         int hardmin, hardmax;
564         
565         if(iprop->range) {
566                 iprop->range(ptr, &hardmin, &hardmax);
567                 *softmin= MAX2(iprop->softmin, hardmin);
568                 *softmax= MIN2(iprop->softmax, hardmax);
569         }
570         else {
571                 *softmin= iprop->softmin;
572                 *softmax= iprop->softmax;
573         }
574
575         *step= iprop->step;
576 }
577
578 void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin, float *hardmax)
579 {
580         FloatPropertyRNA *fprop= (FloatPropertyRNA*)rna_ensure_property(prop);
581
582         if(fprop->range) {
583                 fprop->range(ptr, hardmin, hardmax);
584         }
585         else {
586                 *hardmin= fprop->hardmin;
587                 *hardmax= fprop->hardmax;
588         }
589 }
590
591 void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
592 {
593         FloatPropertyRNA *fprop= (FloatPropertyRNA*)rna_ensure_property(prop);
594         float hardmin, hardmax;
595
596         if(fprop->range) {
597                 fprop->range(ptr, &hardmin, &hardmax);
598                 *softmin= MAX2(fprop->softmin, hardmin);
599                 *softmax= MIN2(fprop->softmax, hardmax);
600         }
601         else {
602                 *softmin= fprop->softmin;
603                 *softmax= fprop->softmax;
604         }
605
606         *step= fprop->step;
607         *precision= (float)fprop->precision;
608 }
609
610 int RNA_property_string_maxlength(PropertyRNA *prop)
611 {
612         StringPropertyRNA *sprop= (StringPropertyRNA*)rna_ensure_property(prop);
613         return sprop->maxlength;
614 }
615
616 StructRNA *RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
617 {
618         prop= rna_ensure_property(prop);
619
620         if(prop->type == PROP_POINTER) {
621                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
622
623                 if(pprop->typef)
624                         return pprop->typef(ptr);
625                 else if(pprop->type)
626                         return pprop->type;
627         }
628         else if(prop->type == PROP_COLLECTION) {
629                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
630
631                 if(cprop->type)
632                         return cprop->type;
633         }
634
635         return &RNA_UnknownType;
636 }
637
638 void RNA_property_enum_items(PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **item, int *totitem)
639 {
640         EnumPropertyRNA *eprop= (EnumPropertyRNA*)rna_ensure_property(prop);
641         int tot;
642
643         if(eprop->itemf) {
644                 *item= eprop->itemf(ptr);
645                 if(totitem) {
646                         for(tot=0; (*item)[tot].identifier; tot++);
647                         *totitem= tot;
648                 }
649         }
650         else {
651                 *item= eprop->item;
652                 if(totitem)
653                         *totitem= eprop->totitem;
654         }
655 }
656
657 int RNA_property_enum_value(PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value)
658 {       
659         const EnumPropertyItem *item;
660         
661         RNA_property_enum_items(ptr, prop, &item, NULL);
662         
663         for(; item->identifier; item++) {
664                 if(strcmp(item->identifier, identifier)==0) {
665                         *value = item->value;
666                         return 1;
667                 }
668         }
669
670         return 0;
671 }
672
673 int RNA_enum_identifier(const EnumPropertyItem *item, const int value, const char **identifier)
674 {
675         for (; item->identifier; item++) {
676                 if(item->value==value) {
677                         *identifier = item->identifier;
678                         return 1;
679                 }
680         }
681         return 0;
682 }
683
684 int RNA_enum_name(const EnumPropertyItem *item, const int value, const char **name)
685 {
686         for (; item->identifier; item++) {
687                 if(item->value==value) {
688                         *name = item->name;
689                         return 1;
690                 }
691         }
692         return 0;
693 }
694
695 int RNA_property_enum_identifier(PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier)
696 {       
697         const EnumPropertyItem *item= NULL;
698         
699         RNA_property_enum_items(ptr, prop, &item, NULL);
700         return RNA_enum_identifier(item, value, identifier);
701 }
702
703 const char *RNA_property_ui_name(PropertyRNA *prop)
704 {
705         return rna_ensure_property_name(prop);
706 }
707
708 const char *RNA_property_ui_description(PropertyRNA *prop)
709 {
710         return rna_ensure_property(prop)->description;
711 }
712
713 int RNA_property_ui_icon(PropertyRNA *prop)
714 {
715         return rna_ensure_property(prop)->icon;
716 }
717
718 int RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop)
719 {
720         ID *id;
721         int flag;
722
723         prop= rna_ensure_property(prop);
724
725         if(prop->editable)
726                 flag= prop->editable(ptr);
727         else
728                 flag= prop->flag;
729         
730         id= ptr->id.data;
731
732         return (flag & PROP_EDITABLE) && (!id || !id->lib);
733 }
734
735 int RNA_property_animateable(PointerRNA *ptr, PropertyRNA *prop)
736 {
737         int flag;
738
739         prop= rna_ensure_property(prop);
740
741         if(!(prop->flag & PROP_ANIMATEABLE))
742                 return 0;
743
744         if(prop->editable)
745                 flag= prop->editable(ptr);
746         else
747                 flag= prop->flag;
748
749         return (flag & PROP_EDITABLE);
750 }
751
752 int RNA_property_animated(PointerRNA *ptr, PropertyRNA *prop)
753 {
754         /* would need to ask animation system */
755
756         return 0;
757 }
758
759 void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
760 {
761         prop= rna_ensure_property(prop);
762
763         if(prop->update)
764                 prop->update(C, ptr);
765         if(prop->noteflag)
766                 WM_event_add_notifier(C, prop->noteflag, ptr->id.data);
767 }
768
769 /* Property Data */
770
771 int RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
772 {
773         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
774         IDProperty *idprop;
775
776         if((idprop=rna_idproperty_check(&prop, ptr)))
777                 return IDP_Int(idprop);
778         else if(bprop->get)
779                 return bprop->get(ptr);
780         else
781                 return bprop->defaultvalue;
782 }
783
784 void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, int value)
785 {
786         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
787         IDProperty *idprop;
788
789         if((idprop=rna_idproperty_check(&prop, ptr)))
790                 IDP_Int(idprop)= value;
791         else if(bprop->set)
792                 bprop->set(ptr, value);
793         else if(prop->flag & PROP_EDITABLE) {
794                 IDPropertyTemplate val = {0};
795                 IDProperty *group;
796
797                 val.i= value;
798
799                 group= RNA_struct_idproperties(ptr, 1);
800                 if(group)
801                         IDP_AddToGroup(group, IDP_New(IDP_INT, val, (char*)prop->identifier));
802         }
803 }
804
805 void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
806 {
807         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
808         IDProperty *idprop;
809
810         if((idprop=rna_idproperty_check(&prop, ptr))) {
811                 if(prop->arraylength == 0)
812                         values[0]= RNA_property_boolean_get(ptr, prop);
813                 else
814                         memcpy(values, IDP_Array(idprop), sizeof(int)*idprop->len);
815         }
816         else if(prop->arraylength == 0)
817                 values[0]= RNA_property_boolean_get(ptr, prop);
818         else if(bprop->getarray)
819                 bprop->getarray(ptr, values);
820         else if(bprop->defaultarray)
821                 memcpy(values, bprop->defaultarray, sizeof(int)*prop->arraylength);
822         else
823                 memset(values, 0, sizeof(int)*prop->arraylength);
824 }
825
826 int RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
827 {
828         int tmp[RNA_MAX_ARRAY];
829
830         RNA_property_boolean_get_array(ptr, prop, tmp);
831         return tmp[index];
832 }
833
834 void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
835 {
836         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
837         IDProperty *idprop;
838
839         if((idprop=rna_idproperty_check(&prop, ptr))) {
840                 if(prop->arraylength == 0)
841                         IDP_Int(idprop)= values[0];
842                 else
843                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
844         }
845         else if(prop->arraylength == 0)
846                 RNA_property_boolean_set(ptr, prop, values[0]);
847         else if(bprop->setarray)
848                 bprop->setarray(ptr, values);
849         else if(prop->flag & PROP_EDITABLE) {
850                 IDPropertyTemplate val = {0};
851                 IDProperty *group;
852
853                 val.array.len= prop->arraylength;
854                 val.array.type= IDP_INT;
855
856                 group= RNA_struct_idproperties(ptr, 1);
857                 if(group) {
858                         idprop= IDP_New(IDP_ARRAY, val, (char*)prop->identifier);
859                         IDP_AddToGroup(group, idprop);
860                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
861                 }
862         }
863 }
864
865 void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
866 {
867         int tmp[RNA_MAX_ARRAY];
868
869         RNA_property_boolean_get_array(ptr, prop, tmp);
870         tmp[index]= value;
871         RNA_property_boolean_set_array(ptr, prop, tmp);
872 }
873
874 int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
875 {
876         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
877         IDProperty *idprop;
878
879         if((idprop=rna_idproperty_check(&prop, ptr)))
880                 return IDP_Int(idprop);
881         else if(iprop->get)
882                 return iprop->get(ptr);
883         else
884                 return iprop->defaultvalue;
885 }
886
887 void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
888 {
889         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
890         IDProperty *idprop;
891
892         if((idprop=rna_idproperty_check(&prop, ptr)))
893                 IDP_Int(idprop)= value;
894         else if(iprop->set)
895                 iprop->set(ptr, value);
896         else if(prop->flag & PROP_EDITABLE) {
897                 IDPropertyTemplate val = {0};
898                 IDProperty *group;
899
900                 val.i= value;
901
902                 group= RNA_struct_idproperties(ptr, 1);
903                 if(group)
904                         IDP_AddToGroup(group, IDP_New(IDP_INT, val, (char*)prop->identifier));
905         }
906 }
907
908 void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
909 {
910         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
911         IDProperty *idprop;
912
913         if((idprop=rna_idproperty_check(&prop, ptr))) {
914                 if(prop->arraylength == 0)
915                         values[0]= RNA_property_int_get(ptr, prop);
916                 else
917                         memcpy(values, IDP_Array(idprop), sizeof(int)*idprop->len);
918         }
919         else if(prop->arraylength == 0)
920                 values[0]= RNA_property_int_get(ptr, prop);
921         else if(iprop->getarray)
922                 iprop->getarray(ptr, values);
923         else if(iprop->defaultarray)
924                 memcpy(values, iprop->defaultarray, sizeof(int)*prop->arraylength);
925         else
926                 memset(values, 0, sizeof(int)*prop->arraylength);
927 }
928
929 int RNA_property_int_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
930 {
931         int tmp[RNA_MAX_ARRAY];
932
933         RNA_property_int_get_array(ptr, prop, tmp);
934         return tmp[index];
935 }
936
937 void RNA_property_int_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
938 {
939         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
940         IDProperty *idprop;
941
942         if((idprop=rna_idproperty_check(&prop, ptr))) {
943                 if(prop->arraylength == 0)
944                         IDP_Int(idprop)= values[0];
945                 else
946                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);\
947         }
948         else if(prop->arraylength == 0)
949                 RNA_property_int_set(ptr, prop, values[0]);
950         else if(iprop->setarray)
951                 iprop->setarray(ptr, values);
952         else if(prop->flag & PROP_EDITABLE) {
953                 IDPropertyTemplate val = {0};
954                 IDProperty *group;
955
956                 val.array.len= prop->arraylength;
957                 val.array.type= IDP_INT;
958
959                 group= RNA_struct_idproperties(ptr, 1);
960                 if(group) {
961                         idprop= IDP_New(IDP_ARRAY, val, (char*)prop->identifier);
962                         IDP_AddToGroup(group, idprop);
963                         memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
964                 }
965         }
966 }
967
968 void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
969 {
970         int tmp[RNA_MAX_ARRAY];
971
972         RNA_property_int_get_array(ptr, prop, tmp);
973         tmp[index]= value;
974         RNA_property_int_set_array(ptr, prop, tmp);
975 }
976
977 float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
978 {
979         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
980         IDProperty *idprop;
981
982         if((idprop=rna_idproperty_check(&prop, ptr))) {
983                 if(idprop->type == IDP_FLOAT)
984                         return IDP_Float(idprop);
985                 else
986                         return (float)IDP_Double(idprop);
987         }
988         else if(fprop->get)
989                 return fprop->get(ptr);
990         else
991                 return fprop->defaultvalue;
992 }
993
994 void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
995 {
996         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
997         IDProperty *idprop;
998
999         if((idprop=rna_idproperty_check(&prop, ptr))) {
1000                 if(idprop->type == IDP_FLOAT)
1001                         IDP_Float(idprop)= value;
1002                 else
1003                         IDP_Double(idprop)= value;
1004         }
1005         else if(fprop->set) {
1006                 fprop->set(ptr, value);
1007         }
1008         else if(prop->flag & PROP_EDITABLE) {
1009                 IDPropertyTemplate val = {0};
1010                 IDProperty *group;
1011
1012                 val.f= value;
1013
1014                 group= RNA_struct_idproperties(ptr, 1);
1015                 if(group)
1016                         IDP_AddToGroup(group, IDP_New(IDP_FLOAT, val, (char*)prop->identifier));
1017         }
1018 }
1019
1020 void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
1021 {
1022         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1023         IDProperty *idprop;
1024         int i;
1025
1026         if((idprop=rna_idproperty_check(&prop, ptr))) {
1027                 if(prop->arraylength == 0)
1028                         values[0]= RNA_property_float_get(ptr, prop);
1029                 else if(idprop->subtype == IDP_FLOAT) {
1030                         memcpy(values, IDP_Array(idprop), sizeof(float)*idprop->len);
1031                 }
1032                 else {
1033                         for(i=0; i<idprop->len; i++)
1034                                 values[i]=  (float)(((double*)IDP_Array(idprop))[i]);
1035                 }
1036         }
1037         else if(prop->arraylength == 0)
1038                 values[0]= RNA_property_float_get(ptr, prop);
1039         else if(fprop->getarray)
1040                 fprop->getarray(ptr, values);
1041         else if(fprop->defaultarray)
1042                 memcpy(values, fprop->defaultarray, sizeof(float)*prop->arraylength);
1043         else
1044                 memset(values, 0, sizeof(float)*prop->arraylength);
1045 }
1046
1047 float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
1048 {
1049         float tmp[RNA_MAX_ARRAY];
1050
1051         RNA_property_float_get_array(ptr, prop, tmp);
1052         return tmp[index];
1053 }
1054
1055 void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
1056 {
1057         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1058         IDProperty *idprop;
1059         int i;
1060
1061         if((idprop=rna_idproperty_check(&prop, ptr))) {
1062                 if(prop->arraylength == 0)
1063                         IDP_Double(idprop)= values[0];
1064                 else if(idprop->subtype == IDP_FLOAT) {
1065                         memcpy(IDP_Array(idprop), values, sizeof(float)*idprop->len);
1066                 }
1067                 else {
1068                         for(i=0; i<idprop->len; i++)
1069                                 ((double*)IDP_Array(idprop))[i]= values[i];
1070                 }
1071         }
1072         else if(prop->arraylength == 0)
1073                 RNA_property_float_set(ptr, prop, values[0]);
1074         else if(fprop->setarray) {
1075                 fprop->setarray(ptr, values);
1076         }
1077         else if(prop->flag & PROP_EDITABLE) {
1078                 IDPropertyTemplate val = {0};
1079                 IDProperty *group;
1080
1081                 val.array.len= prop->arraylength;
1082                 val.array.type= IDP_FLOAT;
1083
1084                 group= RNA_struct_idproperties(ptr, 1);
1085                 if(group) {
1086                         idprop= IDP_New(IDP_ARRAY, val, (char*)prop->identifier);
1087                         IDP_AddToGroup(group, idprop);
1088                         memcpy(IDP_Array(idprop), values, sizeof(float)*idprop->len);
1089                 }
1090         }
1091 }
1092
1093 void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
1094 {
1095         float tmp[RNA_MAX_ARRAY];
1096
1097         RNA_property_float_get_array(ptr, prop, tmp);
1098         tmp[index]= value;
1099         RNA_property_float_set_array(ptr, prop, tmp);
1100 }
1101
1102 void RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop, char *value)
1103 {
1104         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1105         IDProperty *idprop;
1106
1107         if((idprop=rna_idproperty_check(&prop, ptr)))
1108                 strcpy(value, IDP_String(idprop));
1109         else if(sprop->get)
1110                 sprop->get(ptr, value);
1111         else
1112                 strcpy(value, sprop->defaultvalue);
1113 }
1114
1115 char *RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen)
1116 {
1117         char *buf;
1118         int length;
1119
1120         length= RNA_property_string_length(ptr, prop);
1121
1122         if(length+1 < fixedlen)
1123                 buf= fixedbuf;
1124         else
1125                 buf= MEM_callocN(sizeof(char)*(length+1), "RNA_string_get_alloc");
1126
1127         RNA_property_string_get(ptr, prop, buf);
1128
1129         return buf;
1130 }
1131
1132 int RNA_property_string_length(PointerRNA *ptr, PropertyRNA *prop)
1133 {
1134         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1135         IDProperty *idprop;
1136
1137         if((idprop=rna_idproperty_check(&prop, ptr)))
1138                 return strlen(IDP_String(idprop));
1139         else if(sprop->length)
1140                 return sprop->length(ptr);
1141         else
1142                 return strlen(sprop->defaultvalue);
1143 }
1144
1145 void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
1146 {
1147         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1148         IDProperty *idprop;
1149
1150         if((idprop=rna_idproperty_check(&prop, ptr)))
1151                 IDP_AssignString(idprop, (char*)value);
1152         else if(sprop->set)
1153                 sprop->set(ptr, value);
1154         else if(prop->flag & PROP_EDITABLE) {
1155                 IDPropertyTemplate val = {0};
1156                 IDProperty *group;
1157
1158                 val.str= (char*)value;
1159
1160                 group= RNA_struct_idproperties(ptr, 1);
1161                 if(group)
1162                         IDP_AddToGroup(group, IDP_New(IDP_STRING, val, (char*)prop->identifier));
1163         }
1164 }
1165
1166 int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
1167 {
1168         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1169         IDProperty *idprop;
1170
1171         if((idprop=rna_idproperty_check(&prop, ptr)))
1172                 return IDP_Int(idprop);
1173         else if(eprop->get)
1174                 return eprop->get(ptr);
1175         else
1176                 return eprop->defaultvalue;
1177 }
1178
1179
1180 void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
1181 {
1182         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1183         IDProperty *idprop;
1184
1185         if((idprop=rna_idproperty_check(&prop, ptr)))
1186                 IDP_Int(idprop)= value;
1187         else if(eprop->set) {
1188                 eprop->set(ptr, value);
1189         }
1190         else if(prop->flag & PROP_EDITABLE) {
1191                 IDPropertyTemplate val = {0};
1192                 IDProperty *group;
1193
1194                 val.i= value;
1195
1196                 group= RNA_struct_idproperties(ptr, 1);
1197                 if(group)
1198                         IDP_AddToGroup(group, IDP_New(IDP_INT, val, (char*)prop->identifier));
1199         }
1200 }
1201
1202 PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
1203 {
1204         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1205         IDProperty *idprop;
1206
1207         if((idprop=rna_idproperty_check(&prop, ptr))) {
1208                 pprop= (PointerPropertyRNA*)prop;
1209
1210                 /* for groups, data is idprop itself */
1211                 return rna_pointer_inherit_refine(ptr, pprop->type, idprop);
1212         }
1213         else if(pprop->get) {
1214                 return pprop->get(ptr);
1215         }
1216         else {
1217                 PointerRNA result;
1218                 memset(&result, 0, sizeof(result));
1219                 return result;
1220         }
1221 }
1222
1223 void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value)
1224 {
1225         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1226
1227         if(pprop->set)
1228                 pprop->set(ptr, ptr_value);
1229 }
1230
1231 void RNA_property_pointer_add(PointerRNA *ptr, PropertyRNA *prop)
1232 {
1233         IDProperty *idprop;
1234
1235         if((idprop=rna_idproperty_check(&prop, ptr))) {
1236                 /* already exists */
1237         }
1238         else if(prop->flag & PROP_IDPROPERTY) {
1239                 IDPropertyTemplate val = {0};
1240                 IDProperty *group;
1241
1242                 val.i= 0;
1243
1244                 group= RNA_struct_idproperties(ptr, 1);
1245                 if(group)
1246                         IDP_AddToGroup(group, IDP_New(IDP_GROUP, val, (char*)prop->identifier));
1247         }
1248         else
1249                 printf("RNA_property_pointer_add %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);
1250 }
1251
1252 void RNA_property_pointer_remove(PointerRNA *ptr, PropertyRNA *prop)
1253 {
1254         IDProperty *idprop, *group;
1255
1256         if((idprop=rna_idproperty_check(&prop, ptr))) {
1257                 group= RNA_struct_idproperties(ptr, 0);
1258                 
1259                 if(group) {
1260                         IDP_RemFromGroup(group, idprop);
1261                         IDP_FreeProperty(idprop);
1262                         MEM_freeN(idprop);
1263                 }
1264         }
1265         else
1266                 printf("RNA_property_pointer_remove %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);
1267 }
1268
1269 static void rna_property_collection_get_idp(CollectionPropertyIterator *iter)
1270 {
1271         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)iter->prop;
1272
1273         iter->ptr.data= rna_iterator_array_get(iter);
1274         iter->ptr.type= cprop->type;
1275         rna_pointer_inherit_id(cprop->type, &iter->parent, &iter->ptr);
1276 }
1277
1278 void RNA_property_collection_begin(PointerRNA *ptr, PropertyRNA *prop, CollectionPropertyIterator *iter)
1279 {
1280         IDProperty *idprop;
1281
1282         memset(iter, 0, sizeof(*iter));
1283
1284         if((idprop=rna_idproperty_check(&prop, ptr)) || (prop->flag & PROP_IDPROPERTY)) {
1285                 iter->parent= *ptr;
1286                 iter->prop= prop;
1287
1288                 if(idprop)
1289                         rna_iterator_array_begin(iter, IDP_IDPArray(idprop), sizeof(IDProperty), idprop->len, NULL);
1290                 else
1291                         rna_iterator_array_begin(iter, NULL, sizeof(IDProperty), 0, NULL);
1292
1293                 if(iter->valid)
1294                         rna_property_collection_get_idp(iter);
1295
1296                 iter->idprop= 1;
1297         }
1298         else {
1299                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1300                 cprop->begin(iter, ptr);
1301         }
1302 }
1303
1304 void RNA_property_collection_next(CollectionPropertyIterator *iter)
1305 {
1306         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)iter->prop;
1307
1308         if(iter->idprop) {
1309                 rna_iterator_array_next(iter);
1310
1311                 if(iter->valid)
1312                         rna_property_collection_get_idp(iter);
1313         }
1314         else
1315                 cprop->next(iter);
1316 }
1317
1318 void RNA_property_collection_end(CollectionPropertyIterator *iter)
1319 {
1320         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)iter->prop;
1321
1322         if(iter->idprop)
1323                 rna_iterator_array_end(iter);
1324         else
1325                 cprop->end(iter);
1326 }
1327
1328 int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
1329 {
1330         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1331         IDProperty *idprop;
1332
1333         if((idprop=rna_idproperty_check(&prop, ptr))) {
1334                 return idprop->len;
1335         }
1336         else if(cprop->length) {
1337                 return cprop->length(ptr);
1338         }
1339         else {
1340                 CollectionPropertyIterator iter;
1341                 int length= 0;
1342
1343                 RNA_property_collection_begin(ptr, prop, &iter);
1344                 for(; iter.valid; RNA_property_collection_next(&iter))
1345                         length++;
1346                 RNA_property_collection_end(&iter);
1347
1348                 return length;
1349         }
1350 }
1351
1352 void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
1353 {
1354         IDProperty *idprop;
1355         //CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1356
1357         if((idprop=rna_idproperty_check(&prop, ptr))) {
1358                 IDPropertyTemplate val = {0};
1359                 IDProperty *item;
1360
1361                 item= IDP_New(IDP_GROUP, val, "");
1362                 IDP_AppendArray(idprop, item);
1363                 IDP_FreeProperty(item);
1364                 MEM_freeN(item);
1365         }
1366         else if(prop->flag & PROP_IDPROPERTY) {
1367                 IDProperty *group, *item;
1368                 IDPropertyTemplate val = {0};
1369
1370                 group= RNA_struct_idproperties(ptr, 1);
1371                 if(group) {
1372                         idprop= IDP_NewIDPArray(prop->identifier);
1373                         IDP_AddToGroup(group, idprop);
1374
1375                         item= IDP_New(IDP_GROUP, val, "");
1376                         IDP_AppendArray(idprop, item);
1377                         IDP_FreeProperty(item);
1378                         MEM_freeN(item);
1379                 }
1380         }
1381 #if 0
1382         else if(cprop->add){
1383                 if(!(cprop->add->flag & FUNC_USE_CONTEXT)) { /* XXX check for this somewhere else */
1384                         ParameterList *params= RNA_parameter_list_create(ptr, cprop->add);
1385                         RNA_function_call(NULL, NULL, ptr, cprop->add, params);
1386                         RNA_parameter_list_free(params);
1387                 }
1388         }
1389 #endif
1390         else
1391                 printf("RNA_property_collection_add %s.%s: not implemented for this property.\n", ptr->type->identifier, prop->identifier);
1392
1393         if(r_ptr) {
1394                 if(idprop) {
1395                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1396
1397                         r_ptr->data= IDP_GetIndexArray(idprop, idprop->len-1);
1398                         r_ptr->type= cprop->type;
1399                         rna_pointer_inherit_id(NULL, ptr, r_ptr);
1400                 }
1401                 else
1402                         memset(r_ptr, 0, sizeof(*r_ptr));
1403         }
1404 }
1405
1406 void RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key)
1407 {
1408         IDProperty *idprop;
1409         //CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1410
1411         if((idprop=rna_idproperty_check(&prop, ptr))) {
1412                 IDProperty tmp, *array;
1413                 int len;
1414
1415                 len= idprop->len;
1416                 array= IDP_IDPArray(idprop);
1417
1418                 if(key >= 0 && key < len) {
1419                         if(key+1 < len) {
1420                                 /* move element to be removed to the back */
1421                                 memcpy(&tmp, &array[key], sizeof(IDProperty));
1422                                 memmove(array+key, array+key+1, sizeof(IDProperty)*(len-key+1));
1423                                 memcpy(&array[len-1], &tmp, sizeof(IDProperty));
1424                         }
1425
1426                         IDP_ResizeIDPArray(idprop, len-1);
1427                 }
1428         }
1429         else if(prop->flag & PROP_IDPROPERTY);
1430 #if 0
1431         else if(cprop->remove){
1432                 if(!(cprop->remove->flag & FUNC_USE_CONTEXT)) { /* XXX check for this somewhere else */
1433                         ParameterList *params= RNA_parameter_list_create(ptr, cprop->remove);
1434                         RNA_function_call(NULL, NULL, ptr, cprop->remove, params);
1435                         RNA_parameter_list_free(params);
1436                 }
1437         }
1438 #endif
1439         else
1440                 printf("RNA_property_collection_remove %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);
1441 }
1442
1443 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
1444 {
1445         IDProperty *idprop;
1446
1447         if((idprop=rna_idproperty_check(&prop, ptr)))
1448                 IDP_ResizeIDPArray(idprop, 0);
1449 }
1450
1451 int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
1452 {
1453         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1454
1455         if(cprop->lookupint) {
1456                 /* we have a callback defined, use it */
1457                 *r_ptr= cprop->lookupint(ptr, key);
1458                 return (r_ptr->data != NULL);
1459         }
1460         else {
1461                 /* no callback defined, just iterate and find the nth item */
1462                 CollectionPropertyIterator iter;
1463                 int i;
1464
1465                 RNA_property_collection_begin(ptr, prop, &iter);
1466                 for(i=0; iter.valid; RNA_property_collection_next(&iter), i++) {
1467                         if(i == key) {
1468                                 *r_ptr= iter.ptr;
1469                                 break;
1470                         }
1471                 }
1472                 RNA_property_collection_end(&iter);
1473
1474                 if(!iter.valid)
1475                         memset(r_ptr, 0, sizeof(*r_ptr));
1476
1477                 return iter.valid;
1478         }
1479 }
1480
1481 int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr)
1482 {
1483         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1484
1485         if(cprop->lookupstring) {
1486                 /* we have a callback defined, use it */
1487                 *r_ptr= cprop->lookupstring(ptr, key);
1488                 return (r_ptr->data != NULL);
1489         }
1490         else {
1491                 /* no callback defined, compare with name properties if they exist */
1492                 CollectionPropertyIterator iter;
1493                 PropertyRNA *nameprop;
1494                 char name[256], *nameptr;
1495                 int found= 0;
1496
1497                 RNA_property_collection_begin(ptr, prop, &iter);
1498                 for(; iter.valid; RNA_property_collection_next(&iter)) {
1499                         if(iter.ptr.data && iter.ptr.type->nameproperty) {
1500                                 nameprop= iter.ptr.type->nameproperty;
1501
1502                                 nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name));
1503
1504                                 if(strcmp(nameptr, key) == 0) {
1505                                         *r_ptr= iter.ptr;
1506                                         found= 1;
1507                                 }
1508
1509                                 if((char *)&name != nameptr)
1510                                         MEM_freeN(nameptr);
1511
1512                                 if(found)
1513                                         break;
1514                         }
1515                 }
1516                 RNA_property_collection_end(&iter);
1517
1518                 if(!iter.valid)
1519                         memset(r_ptr, 0, sizeof(*r_ptr));
1520
1521                 return iter.valid;
1522         }
1523 }
1524
1525 /* Standard iterator functions */
1526
1527 void rna_iterator_listbase_begin(CollectionPropertyIterator *iter, ListBase *lb, IteratorSkipFunc skip)
1528 {
1529         ListBaseIterator *internal;
1530
1531         internal= MEM_callocN(sizeof(ListBaseIterator), "ListBaseIterator");
1532         internal->link= (lb)? lb->first: NULL;
1533         internal->skip= skip;
1534
1535         iter->internal= internal;
1536         iter->valid= (internal->link != NULL);
1537
1538         if(skip && iter->valid && skip(iter, internal->link))
1539                 rna_iterator_listbase_next(iter);
1540 }
1541
1542 void rna_iterator_listbase_next(CollectionPropertyIterator *iter)
1543 {
1544         ListBaseIterator *internal= iter->internal;
1545
1546         if(internal->skip) {
1547                 do {
1548                         internal->link= internal->link->next;
1549                         iter->valid= (internal->link != NULL);
1550                 } while(iter->valid && internal->skip(iter, internal->link));
1551         }
1552         else {
1553                 internal->link= internal->link->next;
1554                 iter->valid= (internal->link != NULL);
1555         }
1556 }
1557
1558 void *rna_iterator_listbase_get(CollectionPropertyIterator *iter)
1559 {
1560         ListBaseIterator *internal= iter->internal;
1561
1562         return internal->link;
1563 }
1564
1565 void rna_iterator_listbase_end(CollectionPropertyIterator *iter)
1566 {
1567         MEM_freeN(iter->internal);
1568         iter->internal= NULL;
1569 }
1570
1571 void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length, IteratorSkipFunc skip)
1572 {
1573         ArrayIterator *internal;
1574
1575         if(ptr == NULL)
1576                 length= 0;
1577
1578         internal= MEM_callocN(sizeof(ArrayIterator), "ArrayIterator");
1579         internal->ptr= ptr;
1580         internal->endptr= ((char*)ptr)+length*itemsize;
1581         internal->itemsize= itemsize;
1582         internal->skip= skip;
1583
1584         iter->internal= internal;
1585         iter->valid= (internal->ptr != internal->endptr);
1586
1587         if(skip && iter->valid && skip(iter, internal->ptr))
1588                 rna_iterator_array_next(iter);
1589 }
1590
1591 void rna_iterator_array_next(CollectionPropertyIterator *iter)
1592 {
1593         ArrayIterator *internal= iter->internal;
1594
1595         if(internal->skip) {
1596                 do {
1597                         internal->ptr += internal->itemsize;
1598                         iter->valid= (internal->ptr != internal->endptr);
1599                 } while(iter->valid && internal->skip(iter, internal->ptr));
1600         }
1601         else {
1602                 internal->ptr += internal->itemsize;
1603                 iter->valid= (internal->ptr != internal->endptr);
1604         }
1605 }
1606
1607 void *rna_iterator_array_get(CollectionPropertyIterator *iter)
1608 {
1609         ArrayIterator *internal= iter->internal;
1610
1611         return internal->ptr;
1612 }
1613
1614 void *rna_iterator_array_dereference_get(CollectionPropertyIterator *iter)
1615 {
1616         ArrayIterator *internal= iter->internal;
1617
1618         /* for ** arrays */
1619         return *(void**)(internal->ptr);
1620 }
1621
1622 void rna_iterator_array_end(CollectionPropertyIterator *iter)
1623 {
1624         MEM_freeN(iter->internal);
1625         iter->internal= NULL;
1626 }
1627
1628 /* RNA Path - Experiment */
1629
1630 static char *rna_path_token(const char **path, char *fixedbuf, int fixedlen, int bracket)
1631 {
1632         const char *p;
1633         char *buf;
1634         int i, j, len, escape;
1635
1636         len= 0;
1637
1638         if(bracket) {
1639                 /* get data between [], check escaping ] with \] */
1640                 if(**path == '[') (*path)++;
1641                 else return NULL;
1642
1643                 p= *path;
1644
1645                 escape= 0;
1646                 while(*p && (*p != ']' || escape)) {
1647                         escape= (*p == '\\');
1648                         len++;
1649                         p++;
1650                 }
1651
1652                 if(*p != ']') return NULL;
1653         }
1654         else {
1655                 /* get data until . or [ */
1656                 p= *path;
1657
1658                 while(*p && *p != '.' && *p != '[') {
1659                         len++;
1660                         p++;
1661                 }
1662         }
1663         
1664         /* empty, return */
1665         if(len == 0)
1666                 return NULL;
1667         
1668         /* try to use fixed buffer if possible */
1669         if(len+1 < fixedlen)
1670                 buf= fixedbuf;
1671         else
1672                 buf= MEM_callocN(sizeof(char)*(len+1), "rna_path_token");
1673
1674         /* copy string, taking into account escaped ] */
1675         if(bracket) {
1676                 for(p=*path, i=0, j=0; i<len; i++, p++) {
1677                         if(*p == '\\' && *(p+1) == ']');
1678                         else buf[j++]= *p;
1679                 }
1680
1681                 buf[j]= 0;
1682         }
1683         else {
1684                 memcpy(buf, *path, sizeof(char)*len);
1685                 buf[len]= '\0';
1686         }
1687
1688         /* set path to start of next token */
1689         if(*p == ']') p++;
1690         if(*p == '.') p++;
1691         *path= p;
1692
1693         return buf;
1694 }
1695
1696 int RNA_path_resolve(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
1697 {
1698         PropertyRNA *prop;
1699         PointerRNA curptr, nextptr;
1700         char fixedbuf[256], *token;
1701         int len, intkey;
1702
1703         prop= NULL;
1704         curptr= *ptr;
1705
1706         while(*path) {
1707                 /* look up property name in current struct */
1708                 token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 0);
1709
1710                 if(!token)
1711                         return 0;
1712
1713                 prop= RNA_struct_find_property(&curptr, token);
1714
1715                 if(token != fixedbuf)
1716                         MEM_freeN(token);
1717
1718                 if(!prop)
1719                         return 0;
1720
1721                 /* now look up the value of this property if it is a pointer or
1722                  * collection, otherwise return the property rna so that the
1723                  * caller can read the value of the property itself */
1724                 if(RNA_property_type(prop) == PROP_POINTER) {
1725                         nextptr= RNA_property_pointer_get(&curptr, prop);
1726
1727                         if(nextptr.data)
1728                                 curptr= nextptr;
1729                         else
1730                                 return 0;
1731                 }
1732                 else if(RNA_property_type(prop) == PROP_COLLECTION && *path) {
1733                         /* resolve the lookup with [] brackets */
1734                         token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1);
1735
1736                         if(!token)
1737                                 return 0;
1738
1739                         len= strlen(token);
1740                         
1741                         /* check for "" to see if it is a string */
1742                         if(len >= 2 && token[0] == '"' && token[len-1] == '"') {
1743                                 /* strip away "" */
1744                                 token[len-1]= 0;
1745                                 RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr);
1746                         }
1747                         else {
1748                                 /* otherwise do int lookup */
1749                                 intkey= atoi(token);
1750                                 RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr);
1751                         }
1752
1753                         if(token != fixedbuf)
1754                                 MEM_freeN(token);
1755
1756                         if(nextptr.data)
1757                                 curptr= nextptr;
1758                         else
1759                                 return 0;
1760                 }
1761         }
1762
1763         *r_ptr= curptr;
1764         *r_prop= prop;
1765
1766         return 1;
1767 }
1768
1769 char *RNA_path_append(const char *path, PointerRNA *ptr, PropertyRNA *prop, int intkey, const char *strkey)
1770 {
1771         DynStr *dynstr;
1772         const char *s;
1773         char appendstr[128], *result;
1774         
1775         dynstr= BLI_dynstr_new();
1776
1777         /* add .identifier */
1778         if(path) {
1779                 BLI_dynstr_append(dynstr, (char*)path);
1780                 if(*path)
1781                         BLI_dynstr_append(dynstr, ".");
1782         }
1783
1784         BLI_dynstr_append(dynstr, (char*)RNA_property_identifier(prop));
1785
1786         if(RNA_property_type(prop) == PROP_COLLECTION) {
1787                 /* add ["strkey"] or [intkey] */
1788                 BLI_dynstr_append(dynstr, "[");
1789
1790                 if(strkey) {
1791                         BLI_dynstr_append(dynstr, "\"");
1792                         for(s=strkey; *s; s++) {
1793                                 if(*s == '[') {
1794                                         appendstr[0]= '\\';
1795                                         appendstr[1]= *s;
1796                                         appendstr[2]= 0;
1797                                 }
1798                                 else {
1799                                         appendstr[0]= *s;
1800                                         appendstr[1]= 0;
1801                                 }
1802                                 BLI_dynstr_append(dynstr, appendstr);
1803                         }
1804                         BLI_dynstr_append(dynstr, "\"");
1805                 }
1806                 else {
1807                         sprintf(appendstr, "%d", intkey);
1808                         BLI_dynstr_append(dynstr, appendstr);
1809                 }
1810
1811                 BLI_dynstr_append(dynstr, "]");
1812         }
1813
1814         result= BLI_dynstr_get_cstring(dynstr);
1815         BLI_dynstr_free(dynstr);
1816
1817         return result;
1818 }
1819
1820 char *RNA_path_back(const char *path)
1821 {
1822         char fixedbuf[256];
1823         const char *previous, *current;
1824         char *result, *token;
1825         int i;
1826
1827         if(!path)
1828                 return NULL;
1829
1830         previous= NULL;
1831         current= path;
1832
1833         /* parse token by token until the end, then we back up to the previous
1834          * position and strip of the next token to get the path one step back */
1835         while(*current) {
1836                 token= rna_path_token(&current, fixedbuf, sizeof(fixedbuf), 0);
1837
1838                 if(!token)
1839                         return NULL;
1840                 if(token != fixedbuf)
1841                         MEM_freeN(token);
1842
1843                 /* in case of collection we also need to strip off [] */
1844                 token= rna_path_token(&current, fixedbuf, sizeof(fixedbuf), 1);
1845                 if(token && token != fixedbuf)
1846                         MEM_freeN(token);
1847                 
1848                 if(!*current)
1849                         break;
1850
1851                 previous= current;
1852         }
1853
1854         if(!previous)
1855                 return NULL;
1856
1857         /* copy and strip off last token */
1858         i= previous - path;
1859         result= BLI_strdup(path);
1860
1861         if(i > 0 && result[i-1] == '.') i--;
1862         result[i]= 0;
1863
1864         return result;
1865 }
1866
1867 char *RNA_path_from_ID_to_property(PointerRNA *ptr, PropertyRNA *prop)
1868 {
1869         char *ptrpath=NULL, *path;
1870         const char *propname;
1871
1872         if(!ptr->id.data || !ptr->data || !prop)
1873                 return NULL;
1874         
1875         if(!RNA_struct_is_ID(ptr->type)) {
1876                 if(ptr->type->path) {
1877                         /* if type has a path to some ID, use it */
1878                         ptrpath= ptr->type->path(ptr);
1879                 }
1880                 else if(ptr->type->nested) {
1881                         PointerRNA parentptr;
1882                         PropertyRNA *userprop;
1883                         
1884                         /* find the property in the struct we're nested in that references this struct, and 
1885                          * use its identifier as the first part of the path used...
1886                          */
1887                         RNA_pointer_create(ptr->id.data, ptr->type->nested, ptr->data, &parentptr);
1888                         userprop= RNA_struct_find_nested(&parentptr, ptr->type); 
1889                         
1890                         if(userprop)
1891                                 ptrpath= BLI_strdup(RNA_property_identifier(userprop));
1892                         else
1893                                 return NULL; // can't do anything about this case yet...
1894                 }
1895                 else
1896                         return NULL;
1897         }
1898
1899         propname= RNA_property_identifier(prop);
1900
1901         if(ptrpath) {
1902                 path= BLI_sprintfN("%s.%s", ptrpath, propname);
1903                 MEM_freeN(ptrpath);
1904         }
1905         else
1906                 path= BLI_strdup(propname);
1907         
1908         return path;
1909 }
1910
1911 /* Quick name based property access */
1912
1913 int RNA_boolean_get(PointerRNA *ptr, const char *name)
1914 {
1915         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1916
1917         if(prop) {
1918                 return RNA_property_boolean_get(ptr, prop);
1919         }
1920         else {
1921                 printf("RNA_boolean_get: %s.%s not found.\n", ptr->type->identifier, name);
1922                 return 0;
1923         }
1924 }
1925
1926 void RNA_boolean_set(PointerRNA *ptr, const char *name, int value)
1927 {
1928         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1929
1930         if(prop)
1931                 RNA_property_boolean_set(ptr, prop, value);
1932         else
1933                 printf("RNA_boolean_set: %s.%s not found.\n", ptr->type->identifier, name);
1934 }
1935
1936 void RNA_boolean_get_array(PointerRNA *ptr, const char *name, int *values)
1937 {
1938         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1939
1940         if(prop)
1941                 RNA_property_boolean_get_array(ptr, prop, values);
1942         else
1943                 printf("RNA_boolean_get_array: %s.%s not found.\n", ptr->type->identifier, name);
1944 }
1945
1946 void RNA_boolean_set_array(PointerRNA *ptr, const char *name, const int *values)
1947 {
1948         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1949
1950         if(prop)
1951                 RNA_property_boolean_set_array(ptr, prop, values);
1952         else
1953                 printf("RNA_boolean_set_array: %s.%s not found.\n", ptr->type->identifier, name);
1954 }
1955
1956 int RNA_int_get(PointerRNA *ptr, const char *name)
1957 {
1958         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1959
1960         if(prop) {
1961                 return RNA_property_int_get(ptr, prop);
1962         }
1963         else {
1964                 printf("RNA_int_get: %s.%s not found.\n", ptr->type->identifier, name);
1965                 return 0;
1966         }
1967 }
1968
1969 void RNA_int_set(PointerRNA *ptr, const char *name, int value)
1970 {
1971         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1972
1973         if(prop)
1974                 RNA_property_int_set(ptr, prop, value);
1975         else
1976                 printf("RNA_int_set: %s.%s not found.\n", ptr->type->identifier, name);
1977 }
1978
1979 void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
1980 {
1981         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1982
1983         if(prop)
1984                 RNA_property_int_get_array(ptr, prop, values);
1985         else
1986                 printf("RNA_int_get_array: %s.%s not found.\n", ptr->type->identifier, name);
1987 }
1988
1989 void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
1990 {
1991         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
1992
1993         if(prop)
1994                 RNA_property_int_set_array(ptr, prop, values);
1995         else
1996                 printf("RNA_int_set_array: %s.%s not found.\n", ptr->type->identifier, name);
1997 }
1998
1999 float RNA_float_get(PointerRNA *ptr, const char *name)
2000 {
2001         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2002
2003         if(prop) {
2004                 return RNA_property_float_get(ptr, prop);
2005         }
2006         else {
2007                 printf("RNA_float_get: %s.%s not found.\n", ptr->type->identifier, name);
2008                 return 0;
2009         }
2010 }
2011
2012 void RNA_float_set(PointerRNA *ptr, const char *name, float value)
2013 {
2014         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2015
2016         if(prop)
2017                 RNA_property_float_set(ptr, prop, value);
2018         else
2019                 printf("RNA_float_set: %s.%s not found.\n", ptr->type->identifier, name);
2020 }
2021
2022 void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
2023 {
2024         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2025
2026         if(prop)
2027                 RNA_property_float_get_array(ptr, prop, values);
2028         else
2029                 printf("RNA_float_get_array: %s.%s not found.\n", ptr->type->identifier, name);
2030 }
2031
2032 void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
2033 {
2034         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2035
2036         if(prop)
2037                 RNA_property_float_set_array(ptr, prop, values);
2038         else
2039                 printf("RNA_float_set_array: %s.%s not found.\n", ptr->type->identifier, name);
2040 }
2041
2042 int RNA_enum_get(PointerRNA *ptr, const char *name)
2043 {
2044         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2045
2046         if(prop) {
2047                 return RNA_property_enum_get(ptr, prop);
2048         }
2049         else {
2050                 printf("RNA_enum_get: %s.%s not found.\n", ptr->type->identifier, name);
2051                 return 0;
2052         }
2053 }
2054
2055 void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
2056 {
2057         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2058
2059         if(prop)
2060                 RNA_property_enum_set(ptr, prop, value);
2061         else
2062                 printf("RNA_enum_set: %s.%s not found.\n", ptr->type->identifier, name);
2063 }
2064
2065 int RNA_enum_is_equal(PointerRNA *ptr, const char *name, const char *enumname)
2066 {
2067         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2068         const EnumPropertyItem *item;
2069
2070         if(prop) {
2071                 RNA_property_enum_items(ptr, prop, &item, NULL);
2072
2073                 for(; item->identifier; item++)
2074                         if(strcmp(item->identifier, enumname) == 0)
2075                                 return (item->value == RNA_property_enum_get(ptr, prop));
2076
2077                 printf("RNA_enum_is_equal: %s.%s item %s not found.\n", ptr->type->identifier, name, enumname);
2078                 return 0;
2079         }
2080         else {
2081                 printf("RNA_enum_is_equal: %s.%s not found.\n", ptr->type->identifier, name);
2082                 return 0;
2083         }
2084 }
2085
2086 int RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *value)
2087 {
2088         for( ; item->identifier; item++) {
2089                 if(strcmp(item->identifier, identifier)==0) {
2090                         *value= item->value;
2091                         return 1;
2092                 }
2093         }
2094         
2095         return 0;
2096 }
2097
2098 int     RNA_enum_id_from_value(const EnumPropertyItem *item, int value, const char **identifier)
2099 {
2100         for( ; item->identifier; item++) {
2101                 if(item->value==value) {
2102                         *identifier= item->identifier;
2103                         return 1;
2104                 }
2105         }
2106
2107         return 0;
2108 }
2109
2110
2111
2112 void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
2113 {
2114         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2115
2116         if(prop)
2117                 RNA_property_string_get(ptr, prop, value);
2118         else
2119                 printf("RNA_string_get: %s.%s not found.\n", ptr->type->identifier, name);
2120 }
2121
2122 char *RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen)
2123 {
2124         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2125
2126         if(prop) {
2127                 return RNA_property_string_get_alloc(ptr, prop, fixedbuf, fixedlen);
2128         }
2129         else {
2130                 printf("RNA_string_get_alloc: %s.%s not found.\n", ptr->type->identifier, name);
2131                 return 0;
2132         }
2133 }
2134
2135 int RNA_string_length(PointerRNA *ptr, const char *name)
2136 {
2137         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2138
2139         if(prop) {
2140                 return RNA_property_string_length(ptr, prop);
2141         }
2142         else {
2143                 printf("RNA_string_length: %s.%s not found.\n", ptr->type->identifier, name);
2144                 return 0;
2145         }
2146 }
2147
2148 void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
2149 {
2150         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2151
2152         if(prop)
2153                 RNA_property_string_set(ptr, prop, value);
2154         else
2155                 printf("RNA_string_set: %s.%s not found.\n", ptr->type->identifier, name);
2156 }
2157
2158 PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
2159 {
2160         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2161
2162         if(prop) {
2163                 return RNA_property_pointer_get(ptr, prop);
2164         }
2165         else {
2166                 PointerRNA result;
2167
2168                 printf("RNA_pointer_get: %s.%s not found.\n", ptr->type->identifier, name);
2169
2170                 memset(&result, 0, sizeof(result));
2171                 return result;
2172         }
2173 }
2174
2175 void RNA_pointer_add(PointerRNA *ptr, const char *name)
2176 {
2177         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2178
2179         if(prop)
2180                 RNA_property_pointer_add(ptr, prop);
2181         else
2182                 printf("RNA_pointer_set: %s.%s not found.\n", ptr->type->identifier, name);
2183 }
2184
2185 void RNA_collection_begin(PointerRNA *ptr, const char *name, CollectionPropertyIterator *iter)
2186 {
2187         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2188
2189         if(prop)
2190                 RNA_property_collection_begin(ptr, prop, iter);
2191         else
2192                 printf("RNA_collection_begin: %s.%s not found.\n", ptr->type->identifier, name);
2193 }
2194
2195 void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
2196 {
2197         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2198
2199         if(prop)
2200                 RNA_property_collection_add(ptr, prop, r_value);
2201         else
2202                 printf("RNA_collection_add: %s.%s not found.\n", ptr->type->identifier, name);
2203 }
2204
2205 void RNA_collection_clear(PointerRNA *ptr, const char *name)
2206 {
2207         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2208
2209         if(prop)
2210                 RNA_property_collection_clear(ptr, prop);
2211         else
2212                 printf("RNA_collection_clear: %s.%s not found.\n", ptr->type->identifier, name);
2213 }
2214
2215 int RNA_collection_length(PointerRNA *ptr, const char *name)
2216 {
2217         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2218
2219         if(prop) {
2220                 return RNA_property_collection_length(ptr, prop);
2221         }
2222         else {
2223                 printf("RNA_collection_length: %s.%s not found.\n", ptr->type->identifier, name);
2224                 return 0;
2225         }
2226 }
2227
2228 int RNA_property_is_set(PointerRNA *ptr, const char *name)
2229 {
2230         PropertyRNA *prop= RNA_struct_find_property(ptr, name);
2231
2232         if(prop) {
2233                 return (rna_idproperty_find(ptr, name) != NULL);
2234         }
2235         else {
2236                 printf("RNA_property_is_set: %s.%s not found.\n", ptr->type->identifier, name);
2237                 return 0;
2238         }
2239 }
2240
2241 /* string representation of a property, python
2242  * compatible but can be used for display too*/
2243 char *RNA_pointer_as_string(PointerRNA *ptr)
2244 {
2245         DynStr *dynstr= BLI_dynstr_new();
2246         char *cstring;
2247         
2248         const char *propname;
2249         int first_time = 1;
2250         
2251         BLI_dynstr_append(dynstr, "{");
2252         
2253         RNA_STRUCT_BEGIN(ptr, prop) {
2254                 propname = RNA_property_identifier(prop);
2255                 
2256                 if(strcmp(propname, "rna_type")==0)
2257                         continue;
2258                 
2259                 if(first_time==0)
2260                         BLI_dynstr_append(dynstr, ", ");
2261                 first_time= 0;
2262                 
2263                 cstring = RNA_property_as_string(ptr, prop);
2264                 BLI_dynstr_appendf(dynstr, "\"%s\":%s", propname, cstring);
2265                 MEM_freeN(cstring);
2266         }
2267         RNA_STRUCT_END;
2268
2269         BLI_dynstr_append(dynstr, "}"); 
2270         
2271         
2272         cstring = BLI_dynstr_get_cstring(dynstr);
2273         BLI_dynstr_free(dynstr);
2274         return cstring;
2275 }
2276
2277 char *RNA_property_as_string(PointerRNA *ptr, PropertyRNA *prop)
2278 {
2279         int type = RNA_property_type(prop);
2280         int len = RNA_property_array_length(prop);
2281         int i;
2282
2283         DynStr *dynstr= BLI_dynstr_new();
2284         char *cstring;
2285         
2286
2287         /* see if we can coorce into a python type - PropertyType */
2288         switch (type) {
2289         case PROP_BOOLEAN:
2290                 if(len==0) {
2291                         BLI_dynstr_append(dynstr, RNA_property_boolean_get(ptr, prop) ? "True" : "False");
2292                 }
2293                 else {
2294                         BLI_dynstr_append(dynstr, "(");
2295                         for(i=0; i<len; i++) {
2296                                 BLI_dynstr_appendf(dynstr, i?", %s":"%s", RNA_property_boolean_get_index(ptr, prop, i) ? "True" : "False");
2297                         }
2298                         BLI_dynstr_append(dynstr, ")");
2299                 }
2300                 break;
2301         case PROP_INT:
2302                 if(len==0) {
2303                         BLI_dynstr_appendf(dynstr, "%d", RNA_property_int_get(ptr, prop));
2304                 }
2305                 else {
2306                         BLI_dynstr_append(dynstr, "(");
2307                         for(i=0; i<len; i++) {
2308                                 BLI_dynstr_appendf(dynstr, i?", %d":"%d", RNA_property_int_get_index(ptr, prop, i));
2309                         }
2310                         BLI_dynstr_append(dynstr, ")");
2311                 }
2312                 break;
2313         case PROP_FLOAT:
2314                 if(len==0) {
2315                         BLI_dynstr_appendf(dynstr, "%g", RNA_property_float_get(ptr, prop));
2316                 }
2317                 else {
2318                         BLI_dynstr_append(dynstr, "(");
2319                         for(i=0; i<len; i++) {
2320                                 BLI_dynstr_appendf(dynstr, i?", %g":"%g", RNA_property_float_get_index(ptr, prop, i));
2321                         }
2322                         BLI_dynstr_append(dynstr, ")");
2323                 }
2324                 break;
2325         case PROP_STRING:
2326         {
2327                 /* string arrays dont exist */
2328                 char *buf;
2329                 buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1);
2330                 BLI_dynstr_appendf(dynstr, "\"%s\"", buf);
2331                 MEM_freeN(buf);
2332                 break;
2333         }
2334         case PROP_ENUM:
2335         {
2336                 /* string arrays dont exist */
2337                 const char *identifier;
2338                 int val = RNA_property_enum_get(ptr, prop);
2339
2340                 if(RNA_property_enum_identifier(ptr, prop, val, &identifier)) {
2341                         BLI_dynstr_appendf(dynstr, "'%s'", identifier);
2342                 }
2343                 else {
2344                         BLI_dynstr_appendf(dynstr, "'<UNKNOWN ENUM>'", identifier);
2345                 }
2346                 break;
2347         }
2348         case PROP_POINTER:
2349         {
2350                 BLI_dynstr_append(dynstr, "'<POINTER>'"); /* TODO */
2351                 break;
2352         }
2353         case PROP_COLLECTION:
2354         {
2355                 int first_time = 1;
2356                 CollectionPropertyIterator collect_iter;
2357                 BLI_dynstr_append(dynstr, "[");
2358                 
2359                 for(RNA_property_collection_begin(ptr, prop, &collect_iter); collect_iter.valid; RNA_property_collection_next(&collect_iter)) {
2360                         PointerRNA itemptr= collect_iter.ptr;
2361                         
2362                         if(first_time==0)
2363                                 BLI_dynstr_append(dynstr, ", ");
2364                         first_time= 0;
2365                         
2366                         /* now get every prop of the collection */
2367                         cstring= RNA_pointer_as_string(&itemptr);
2368                         BLI_dynstr_append(dynstr, cstring);
2369                         MEM_freeN(cstring);
2370                 }
2371                 
2372                 RNA_property_collection_end(&collect_iter);
2373                 BLI_dynstr_append(dynstr, "]");
2374                 break;
2375         }
2376         default:
2377                 BLI_dynstr_append(dynstr, "'<UNKNOWN TYPE>'"); /* TODO */
2378                 break;
2379         }
2380
2381         cstring = BLI_dynstr_get_cstring(dynstr);
2382         BLI_dynstr_free(dynstr);
2383         return cstring;
2384 }
2385
2386 /* Function */
2387
2388 const char *RNA_function_identifier(FunctionRNA *func)
2389 {
2390         return func->identifier;
2391 }
2392
2393 PropertyRNA *RNA_function_return(FunctionRNA *func)
2394 {
2395         return func->ret;
2396 }
2397
2398 const char *RNA_function_ui_description(FunctionRNA *func)
2399 {
2400         return func->description;
2401 }
2402
2403 int RNA_function_flag(FunctionRNA *func)
2404 {
2405         return func->flag;
2406 }
2407
2408 PropertyRNA *RNA_function_get_parameter(PointerRNA *ptr, FunctionRNA *func, int index)
2409 {
2410         PropertyRNA *parm;
2411         int i;
2412
2413         parm= func->cont.properties.first;
2414         for(i= 0; parm; parm= parm->next, i++)
2415                 if(i==index)
2416                         return parm;
2417
2418         return NULL;
2419 }
2420
2421 PropertyRNA *RNA_function_find_parameter(PointerRNA *ptr, FunctionRNA *func, const char *identifier)
2422 {
2423         PropertyRNA *parm;
2424
2425         parm= func->cont.properties.first;
2426         for(; parm; parm= parm->next)
2427                 if(strcmp(parm->identifier, identifier)==0)
2428                         return parm;
2429
2430         return NULL;
2431 }
2432
2433 const struct ListBase *RNA_function_defined_parameters(FunctionRNA *func)
2434 {
2435         return &func->cont.properties;
2436 }
2437
2438 /* Utility */
2439
2440 ParameterList *RNA_parameter_list_create(PointerRNA *ptr, FunctionRNA *func)
2441 {
2442         ParameterList *parms;
2443         PropertyRNA *parm;
2444         int tot;
2445
2446         parms= MEM_callocN(sizeof(ParameterList), "ParameterList");
2447
2448         parm= func->cont.properties.first;
2449         for(tot= 0; parm; parm= parm->next)
2450                 tot+= rna_parameter_size(parm);
2451
2452         parms->data= MEM_callocN(tot, "RNA_parameter_list_create");
2453         parms->func= func;
2454
2455         return parms;
2456 }
2457
2458 void RNA_parameter_list_free(ParameterList *parms)
2459 {
2460         MEM_freeN(parms->data);
2461         parms->data= NULL;
2462
2463         parms->func= NULL;
2464
2465         MEM_freeN(parms);
2466 }
2467
2468 void RNA_parameter_list_begin(ParameterList *parms, ParameterIterator *iter)
2469 {
2470         PropertyType ptype;
2471
2472         RNA_pointer_create(NULL, &RNA_Function, parms->func, &iter->funcptr);
2473
2474         iter->parms= parms;
2475         iter->parm= parms->func->cont.properties.first;
2476         iter->valid= iter->parm != NULL;
2477         iter->offset= 0;
2478
2479         if(iter->valid) {
2480                 iter->size= rna_parameter_size(iter->parm);
2481                 iter->data= (((char*)iter->parms->data)+iter->offset);
2482                 ptype= RNA_property_type(iter->parm);
2483         }
2484 }
2485
2486 void RNA_parameter_list_next(ParameterIterator *iter)
2487 {
2488         PropertyType ptype;
2489
2490         iter->offset+= iter->size;
2491         iter->parm= iter->parm->next;
2492         iter->valid= iter->parm != NULL;
2493
2494         if(iter->valid) {
2495                 iter->size= rna_parameter_size(iter->parm);
2496                 iter->data= (((char*)iter->parms->data)+iter->offset);
2497                 ptype= RNA_property_type(iter->parm);
2498         }
2499 }
2500
2501 void RNA_parameter_list_end(ParameterIterator *iter)
2502 {
2503         /* nothing to do */
2504 }
2505
2506 void RNA_parameter_get(ParameterList *parms, PropertyRNA *parm, void **value)
2507 {
2508         ParameterIterator iter;
2509
2510         RNA_parameter_list_begin(parms, &iter);
2511
2512         for(; iter.valid; RNA_parameter_list_next(&iter))
2513                 if(iter.parm==parm) 
2514                         break;
2515
2516         if(iter.valid)
2517                 *value= iter.data;
2518         else
2519                 *value= NULL;
2520
2521         RNA_parameter_list_end(&iter);
2522 }
2523
2524 void RNA_parameter_get_lookup(ParameterList *parms, const char *identifier, void **value)
2525 {
2526         PropertyRNA *parm;
2527
2528         parm= parms->func->cont.properties.first;
2529         for(; parm; parm= parm->next)
2530                 if(strcmp(RNA_property_identifier(parm), identifier)==0)
2531                         break;
2532
2533         if(parm)
2534                 RNA_parameter_get(parms, parm, value);
2535 }
2536
2537 void RNA_parameter_set(ParameterList *parms, PropertyRNA *parm, void *value)
2538 {
2539         ParameterIterator iter;
2540
2541         RNA_parameter_list_begin(parms, &iter);
2542
2543         for(; iter.valid; RNA_parameter_list_next(&iter))
2544                 if(iter.parm==parm) 
2545                         break;
2546
2547         if(iter.valid)
2548                 memcpy(iter.data, value, iter.size);
2549
2550         RNA_parameter_list_end(&iter);
2551 }
2552
2553 void RNA_parameter_set_lookup(ParameterList *parms, const char *identifier, void *value)
2554 {
2555         PropertyRNA *parm;
2556
2557         parm= parms->func->cont.properties.first;
2558         for(; parm; parm= parm->next)
2559                 if(strcmp(RNA_property_identifier(parm), identifier)==0)
2560                         break;
2561
2562         if(parm)
2563                 RNA_parameter_set(parms, parm, value);
2564 }
2565
2566 int RNA_function_call(bContext *C, ReportList *reports, PointerRNA *ptr, FunctionRNA *func, ParameterList *parms)
2567 {
2568         if(func->call) {
2569                 func->call(C, reports, ptr, parms);
2570
2571                 return 0;
2572         }
2573
2574         return -1;
2575 }
2576
2577 int RNA_function_call_lookup(bContext *C, ReportList *reports, PointerRNA *ptr, const char *identifier, ParameterList *parms)
2578 {
2579         FunctionRNA *func;
2580
2581         func= RNA_struct_find_function(ptr, identifier);
2582
2583         if(func)
2584                 return RNA_function_call(C, reports, ptr, func, parms);
2585
2586         return -1;
2587 }
2588
2589 int RNA_function_call_direct(bContext *C, ReportList *reports, PointerRNA *ptr, FunctionRNA *func, const char *format, ...)
2590 {
2591         va_list args;
2592         int ret;
2593
2594         va_start(args, format);
2595
2596         ret= RNA_function_call_direct_va(C, reports, ptr, func, format, args);
2597
2598         va_end(args);
2599
2600         return ret;
2601 }
2602
2603 int RNA_function_call_direct_lookup(bContext *C, ReportList *reports, PointerRNA *ptr, const char *identifier, const char *format, ...)
2604 {
2605         FunctionRNA *func;
2606
2607         func= RNA_struct_find_function(ptr, identifier);
2608
2609         if(func) {
2610                 va_list args;
2611                 int ret;
2612
2613                 va_start(args, format);
2614
2615                 ret= RNA_function_call_direct_va(C, reports, ptr, func, format, args);
2616
2617                 va_end(args);
2618
2619                 return ret;
2620         }
2621
2622         return -1;
2623 }
2624
2625 static int rna_function_format_array_length(const char *format, int ofs, int flen)
2626 {
2627         char lenbuf[16];
2628         int idx= 0;
2629
2630         if (format[ofs++]=='[')
2631                 for (; ofs<flen && format[ofs]!=']' && idx<sizeof(*lenbuf)-1; idx++, ofs++)
2632                         lenbuf[idx]= format[ofs];
2633
2634         if (ofs<flen && format[ofs++]==']') {
2635                 /* XXX put better error reporting for ofs>=flen or idx over lenbuf capacity */
2636                 lenbuf[idx]= '\0';
2637                 return atoi(lenbuf);
2638         }
2639
2640         return 0;
2641 }
2642
2643 static int rna_function_parameter_parse(PointerRNA *ptr, PropertyRNA *prop, PropertyType type, char ftype, int len, void *dest, void *src, StructRNA *srna, const char *tid, const char *fid, const char *pid)
2644 {
2645         /* ptr is always a function pointer, prop always a parameter */
2646
2647         switch (type) {
2648         case PROP_BOOLEAN:
2649                 {
2650                         if (ftype!='b') {
2651                                 fprintf(stderr, "%s.%s: wrong type for parameter %s, a boolean was expected\n", tid, fid, pid);
2652                                 return -1;
2653                         }
2654
2655                         if (len==0)
2656                                 *((int*)dest)= *((int*)src);
2657                         else
2658                                 memcpy(dest, src, len*sizeof(int));
2659
2660                         break;
2661                 }
2662         case PROP_INT:
2663                 {
2664                         if (ftype!='i') {
2665                                 fprintf(stderr, "%s.%s: wrong type for parameter %s, an integer was expected\n", tid, fid, pid);
2666                                 return -1;
2667                         }
2668
2669                         if (len==0)
2670                                 *((int*)dest)= *((int*)src);
2671                         else
2672                                 memcpy(dest, src, len*sizeof(int));
2673
2674                         break;
2675                 }
2676         case PROP_FLOAT:
2677                 {
2678                         if (ftype!='f') {
2679                                 fprintf(stderr, "%s.%s: wrong type for parameter %s, a float was expected\n", tid, fid, pid);
2680                                 return -1;
2681                         }
2682
2683                         if (len==0)
2684                                 *((float*)dest)= *((float*)src);
2685                         else
2686                                 memcpy(dest, src, len*sizeof(float));
2687
2688                         break;
2689                 }
2690         case PROP_STRING:
2691                 {
2692                         if (ftype!='s') {
2693                                 fprintf(stderr, "%s.%s: wrong type for parameter %s, a string was expected\n", tid, fid, pid);
2694                                 return -1;
2695                         }
2696
2697                         *((char**)dest)= *((char**)src);
2698
2699                         break;
2700                 }
2701         case PROP_ENUM:
2702                 {
2703                         if (ftype!='e') {
2704                                 fprintf(stderr, "%s.%s: wrong type for parameter %s, an enum was expected\n", tid, fid, pid);
2705                                 return -1;
2706                         }
2707
2708                         *((int*)dest)= *((int*)src);
2709
2710                         break;
2711                 }
2712         case PROP_POINTER:
2713                 {
2714                         StructRNA *ptype;
2715
2716                         if (ftype!='O') {
2717                                 fprintf(stderr, "%s.%s: wrong type for parameter %s, an object was expected\n", tid, fid, pid);
2718                                 return -1;
2719                         }
2720
2721                         ptype= RNA_property_pointer_type(ptr, prop);
2722
2723                         if(prop->flag & PROP_RNAPTR) {
2724                                 *((PointerRNA*)dest)= *((PointerRNA*)src);
2725                         }
2726                         else if (ptype!=srna) {
2727                                 if (!RNA_struct_is_a(srna, ptype)) {
2728                                         fprintf(stderr, "%s.%s: wrong type for parameter %s, an object of type %s was expected, passed an object of type %s\n", tid, fid, pid, RNA_struct_identifier(ptype), RNA_struct_identifier(ptype));
2729                                         return -1;
2730                                 }
2731
2732                                 *((void**)dest)= *((void**)src);
2733                         }
2734
2735                         break;
2736                 }
2737         case PROP_COLLECTION:
2738                 {
2739                         /* XXX collections are not supported yet */
2740                         fprintf(stderr, "%s.%s: for parameter %s, collections are not supported yet\n", tid, fid, pid);
2741                         return -1;
2742                 }
2743         default: 
2744                 {
2745                         if (len==0)
2746                                 fprintf(stderr, "%s.%s: unknown type for parameter %s\n", tid, fid, pid);
2747                         else
2748                                 fprintf(stderr, "%s.%s: unknown array type for parameter %s\n", tid, fid, pid);
2749
2750                         return -1;
2751                 }
2752         }
2753
2754         return 0;
2755 }
2756
2757 int RNA_function_call_direct_va(bContext *C, ReportList *reports, PointerRNA *ptr, FunctionRNA *func, const char *format, va_list args)
2758 {
2759         PointerRNA funcptr;
2760         ParameterList *parms;
2761         ParameterIterator iter;
2762         PropertyRNA *pret, *parm;
2763         PropertyType type;
2764         int i, ofs, flen, flag, len, alen, err= 0;
2765         const char *tid, *fid, *pid=NULL;
2766         char ftype;
2767         void **retdata=NULL;
2768
2769         RNA_pointer_create(NULL, &RNA_Function, func, &funcptr);
2770
2771         tid= RNA_struct_identifier(ptr->type);
2772         fid= RNA_function_identifier(func);
2773         pret= RNA_function_return(func);
2774         flen= strlen(format);
2775
2776         parms= RNA_parameter_list_create(ptr, func);
2777         RNA_parameter_list_begin(parms, &iter);
2778
2779         for(i= 0, ofs= 0; iter.valid; RNA_parameter_list_next(&iter), i++) {
2780                 parm= iter.parm;
2781
2782                 if(parm==pret) {
2783                         retdata= iter.data;
2784                         continue;
2785                 }
2786
2787                 pid= RNA_property_identifier(parm);
2788                 flag= RNA_property_flag(parm);
2789
2790                 if (ofs>=flen || format[ofs]=='N') {
2791                         if (flag & PROP_REQUIRED) {
2792                                 err= -1;
2793                                 fprintf(stderr, "%s.%s: missing required parameter %s\n", tid, fid, pid);
2794                                 break;
2795                         }
2796                         ofs++;
2797                         continue;
2798                 }
2799
2800                 type= RNA_property_type(parm);
2801                 ftype= format[ofs++];
2802                 len= RNA_property_array_length(parm);
2803                 alen= rna_function_format_array_length(format, ofs, flen);
2804
2805                 if (len!=alen) {
2806                         err= -1;
2807                         fprintf(stderr, "%s.%s: for parameter %s, was expecting an array of %i elements, passed %i elements instead\n", tid, fid, pid, len, alen);
2808                         break;
2809                 }
2810
2811                 switch (type) {
2812                 case PROP_BOOLEAN:
2813                 case PROP_INT:
2814                 case PROP_ENUM:
2815                         {
2816                                 int arg= va_arg(args, int);
2817                                 err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, iter.data, &arg, NULL, tid, fid, pid);
2818                                 break;
2819                         }
2820                 case PROP_FLOAT:
2821                         {
2822                                 double arg= va_arg(args, double);
2823                                 err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, iter.data, &arg, NULL, tid, fid, pid);
2824                                 break;
2825                         }
2826                 case PROP_STRING:
2827                         {
2828                                 char *arg= va_arg(args, char*);
2829                                 err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, iter.data, &arg, NULL, tid, fid, pid);
2830                                 break;
2831                         }
2832                 case PROP_POINTER:
2833                         {
2834                                 StructRNA *srna= va_arg(args, StructRNA*);
2835                                 void *arg= va_arg(args, void*);
2836                                 err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, iter.data, &arg, srna, tid, fid, pid);
2837                                 break;
2838                         }
2839                 default:
2840                         {
2841                                 /* handle errors */
2842                                 err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, iter.data, NULL, NULL, tid, fid, pid);
2843                                 break;
2844                         }
2845                 }
2846
2847                 if (err!=0)
2848                         break;
2849         }
2850
2851         if (err==0)
2852                 err= RNA_function_call(C, reports, ptr, func, parms);
2853
2854         /* XXX throw error when more parameters than those needed are passed or leave silent? */
2855         if (err==0 && pret && ofs<flen && format[ofs++]=='R') {
2856                 parm= pret;
2857
2858                 type= RNA_property_type(parm);
2859                 ftype= format[ofs++];
2860                 len= RNA_property_array_length(parm);
2861                 alen= rna_function_format_array_length(format, ofs, flen);
2862
2863                 if (len!=alen) {
2864                         err= -1;
2865                         fprintf(stderr, "%s.%s: for return parameter %s, was expecting an array of %i elements, passed %i elements instead\n", tid, fid, pid, len, alen);
2866                 }
2867                 else {
2868                         switch (type) {
2869                         case PROP_BOOLEAN:
2870                         case PROP_INT:
2871                         case PROP_ENUM:
2872                                 {
2873                                         int *arg= va_arg(args, int*);
2874                                         err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, arg, retdata, NULL, tid, fid, pid);
2875                                         break;
2876                                 }
2877                         case PROP_FLOAT:
2878                                 {
2879                                         float *arg= va_arg(args, float*);
2880                                         err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, arg, retdata, NULL, tid, fid, pid);
2881                                         break;
2882                                 }
2883                         case PROP_STRING:
2884                                 {
2885                                         char **arg= va_arg(args, char**);
2886                                         err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, arg, retdata, NULL, tid, fid, pid);
2887                                         break;
2888                                 }
2889                         case PROP_POINTER:
2890                                 {
2891                                         StructRNA *srna= va_arg(args, StructRNA*);
2892                                         void **arg= va_arg(args, void**);
2893                                         err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, arg, retdata, srna, tid, fid, pid);
2894                                         break;
2895                                 }
2896                         default:
2897                                 {
2898                                         /* handle errors */
2899                                         err= rna_function_parameter_parse(&funcptr, parm, type, ftype, len, NULL, NULL, NULL, tid, fid, pid);
2900                                         break;
2901                                 }
2902                         }
2903                 }
2904         }
2905
2906         RNA_parameter_list_end(&iter);
2907         RNA_parameter_list_free(parms);
2908
2909         return err;
2910 }
2911
2912 int RNA_function_call_direct_va_lookup(bContext *C, ReportList *reports, PointerRNA *ptr, const char *identifier, const char *format, va_list args)
2913 {
2914         FunctionRNA *func;
2915
2916         func= RNA_struct_find_function(ptr, identifier);
2917
2918         if(func)
2919                 return RNA_function_call_direct_va(C, reports, ptr, func, format, args);
2920
2921         return 0;
2922 }
2923