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