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