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