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