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