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