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