4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Contributor(s): Blender Foundation (2008).
22 * ***** END GPL LICENSE BLOCK *****
31 #include "MEM_guardedalloc.h"
33 #include "DNA_genfile.h"
34 #include "DNA_sdna_types.h"
36 #include "RNA_access.h"
37 #include "RNA_define.h"
38 #include "RNA_types.h"
40 #include "rna_internal.h"
42 /* Global used during defining */
46 /* Duplicated code since we can't link in blenkernel or blenlib */
48 #define MIN2(x,y) ((x)<(y)? (x): (y))
49 #define MAX2(x,y) ((x)>(y)? (x): (y))
51 void rna_addtail(ListBase *listbase, void *vlink)
56 link->prev = listbase->last;
58 if (listbase->last) ((Link *)listbase->last)->next = link;
59 if (listbase->first == 0) listbase->first = link;
60 listbase->last = link;
63 void rna_freelistN(ListBase *listbase)
67 for(link=listbase->first; link; link=next) {
72 listbase->first= listbase->last= NULL;
75 /* DNA utility function for looking up members */
77 typedef struct DNAStructMember {
83 static int rna_member_cmp(const char *name, const char *oname)
87 /* compare without pointer or array part */
94 if(name[a]=='[' && oname[a]==0) return 1;
96 if(name[a] != oname[a]) return 0;
99 if(name[a]==0 && oname[a] == '.') return 2;
100 if(name[a]==0 && oname[a] == '-' && oname[a+1] == '>') return 3;
102 return (name[a] == oname[a]);
105 static int rna_find_sdna_member(SDNA *sdna, const char *structname, const char *membername, DNAStructMember *smember)
109 int a, structnr, totmember, cmp;
111 structnr= DNA_struct_find_nr(sdna, structname);
115 sp= sdna->structs[structnr];
119 for(a=0; a<totmember; a++, sp+=2) {
120 dnaname= sdna->names[sp[1]];
122 cmp= rna_member_cmp(dnaname, membername);
125 smember->type= sdna->types[sp[0]];
126 smember->name= dnaname;
127 smember->arraylength= DNA_elem_array_size(smember->name, strlen(smember->name));
131 membername= strstr(membername, ".") + strlen(".");
132 return rna_find_sdna_member(sdna, sdna->types[sp[0]], membername, smember);
135 membername= strstr(membername, "->") + strlen("->");
136 return rna_find_sdna_member(sdna, sdna->types[sp[0]], membername, smember);
143 /* Blender Data Definition */
145 BlenderRNA *RNA_create()
149 brna= MEM_callocN(sizeof(BlenderRNA), "BlenderRNA");
151 DefRNA.sdna= DNA_sdna_from_data(DNAstr, DNAlen, 0);
152 DefRNA.structs.first= DefRNA.structs.last= NULL;
158 void RNA_define_free(BlenderRNA *brna)
163 for(alloc=DefRNA.allocs.first; alloc; alloc=alloc->next)
164 MEM_freeN(alloc->mem);
165 rna_freelistN(&DefRNA.allocs);
167 for(srna=DefRNA.structs.first; srna; srna=srna->next)
168 rna_freelistN(&srna->properties);
170 rna_freelistN(&DefRNA.structs);
173 DNA_sdna_free(DefRNA.sdna);
180 void RNA_free(BlenderRNA *brna)
184 RNA_define_free(brna);
186 for(srna=brna->structs.first; srna; srna=srna->next)
187 rna_freelistN(&srna->properties);
189 rna_freelistN(&brna->structs);
194 /* Struct Definition */
196 StructRNA *RNA_def_struct(BlenderRNA *brna, const char *cname, const char *name)
201 ds= MEM_callocN(sizeof(StructDefRNA), "StructDefRNA");
202 rna_addtail(&DefRNA.structs, ds);
204 srna= MEM_callocN(sizeof(StructRNA), "StructRNA");
210 rna_addtail(&brna->structs, srna);
212 RNA_def_struct_sdna(srna, srna->cname);
217 void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
219 StructDefRNA *ds= DefRNA.structs.last;
221 if(!DNA_struct_find_nr(DefRNA.sdna, structname)) {
223 fprintf(stderr, "RNA_def_struct_sdna: %s not found.\n", structname);
229 ds->dnaname= structname;
232 void RNA_def_struct_name_property(struct StructRNA *srna, struct PropertyRNA *prop)
234 if(prop->type != PROP_STRING) {
235 fprintf(stderr, "RNA_def_struct_name_property: %s.%s, must be a string property.\n", srna->cname, prop->cname);
239 srna->nameproperty= prop;
242 void RNA_def_struct_flag(StructRNA *srna, int flag)
247 /* Property Definition */
249 PropertyRNA *RNA_def_property(StructRNA *srna, const char *cname, int type, int subtype)
255 ds= DefRNA.structs.last;
256 dp= MEM_callocN(sizeof(PropertyDefRNA), "PropertyDefRNA");
257 rna_addtail(&ds->properties, dp);
261 prop= MEM_callocN(sizeof(BooleanPropertyRNA), "BooleanPropertyRNA");
264 IntPropertyRNA *iprop;
265 iprop= MEM_callocN(sizeof(IntPropertyRNA), "IntPropertyRNA");
266 prop= &iprop->property;
268 iprop->hardmin= (subtype == PROP_UNSIGNED)? 0: INT_MIN;
269 iprop->hardmax= INT_MAX;
271 iprop->softmin= (subtype == PROP_UNSIGNED)? 0: -10000; /* rather arbitrary .. */
272 iprop->softmax= 10000;
276 FloatPropertyRNA *fprop;
277 fprop= MEM_callocN(sizeof(FloatPropertyRNA), "FloatPropertyRNA");
278 prop= &fprop->property;
280 fprop->hardmin= (subtype == PROP_UNSIGNED)? 0.0f: -FLT_MAX;
281 fprop->hardmax= FLT_MAX;
283 fprop->softmin= (subtype == PROP_UNSIGNED)? 0.0f: -10000.0f; /* rather arbitrary .. */
284 fprop->softmax= 10000.0f;
288 StringPropertyRNA *sprop;
289 sprop= MEM_callocN(sizeof(StringPropertyRNA), "StringPropertyRNA");
290 prop= &sprop->property;
292 sprop->defaultvalue= "";
297 prop= MEM_callocN(sizeof(EnumPropertyRNA), "EnumPropertyRNA");
300 prop= MEM_callocN(sizeof(PointerPropertyRNA), "PointerPropertyRNA");
302 case PROP_COLLECTION:
303 prop= MEM_callocN(sizeof(CollectionPropertyRNA), "CollectionPropertyRNA");
306 fprintf(stderr, "RNA_def_property: %s.%s, invalid property type.\n", ds->srna->cname, cname);
316 prop->subtype= subtype;
318 prop->description= "";
320 if(type == PROP_COLLECTION)
322 else if(type == PROP_POINTER)
323 prop->flag= PROP_EDITABLE;
325 prop->flag= PROP_EDITABLE|PROP_DRIVEABLE;
330 RNA_def_property_boolean_sdna(prop, srna->cname, cname, 0);
335 RNA_def_property_int_sdna(prop, srna->cname, cname);
341 RNA_def_property_float_sdna(prop, srna->cname, cname);
347 RNA_def_property_string_sdna(prop, srna->cname, cname);
353 RNA_def_property_enum_sdna(prop, srna->cname, cname);
358 RNA_def_property_pointer_sdna(prop, srna->cname, cname);
361 case PROP_COLLECTION:
363 RNA_def_property_collection_sdna(prop, srna->cname, cname, NULL);
368 rna_addtail(&srna->properties, prop);
373 void RNA_def_property_flag(PropertyRNA *prop, int flag)
375 StructDefRNA *ds= DefRNA.structs.last;
379 if(prop->type != PROP_POINTER && prop->type != PROP_COLLECTION) {
380 if(flag & (PROP_EVALUATE_DEPENDENCY|PROP_INVERSE_EVALUATE_DEPENDENCY|PROP_RENDER_DEPENDENCY|PROP_INVERSE_RENDER_DEPENDENCY)) {
381 fprintf(stderr, "RNA_def_property_flag: %s.%s, only pointer and collection types can create dependencies.\n", ds->srna->cname, prop->cname);
387 void RNA_def_property_array(PropertyRNA *prop, int arraylength)
389 StructDefRNA *ds= DefRNA.structs.last;
395 prop->arraylength= arraylength;
398 fprintf(stderr, "RNA_def_property_array: %s.%s, only boolean/int/float can be array.\n", ds->srna->cname, prop->cname);
404 void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
407 prop->description= description;
410 void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, double precision)
412 StructDefRNA *ds= DefRNA.structs.last;
416 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
417 iprop->softmin= (int)min;
418 iprop->softmax= (int)max;
419 iprop->step= (int)step;
423 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
424 fprop->softmin= (float)min;
425 fprop->softmax= (float)max;
426 fprop->step= (float)step;
427 fprop->precision= (float)precision;
431 fprintf(stderr, "RNA_def_property_ui_range: %s.%s, invalid type for ui range.\n", ds->srna->cname, prop->cname);
437 void RNA_def_property_range(PropertyRNA *prop, double min, double max)
439 StructDefRNA *ds= DefRNA.structs.last;
443 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
444 iprop->hardmin= (int)min;
445 iprop->hardmax= (int)max;
446 iprop->softmin= MAX2((int)min, iprop->hardmin);
447 iprop->softmax= MIN2((int)max, iprop->hardmax);
451 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
452 fprop->hardmin= (float)min;
453 fprop->hardmax= (float)max;
454 fprop->softmin= MAX2((float)min, fprop->hardmin);
455 fprop->softmax= MIN2((float)max, fprop->hardmax);
459 fprintf(stderr, "RNA_def_property_range: %s.%s, invalid type for range.\n", ds->srna->cname, prop->cname);
465 void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
467 StructDefRNA *ds= DefRNA.structs.last;
471 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
472 pprop->structtype = (StructRNA*)type;
475 case PROP_COLLECTION: {
476 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
477 cprop->structtype = (StructRNA*)type;
481 fprintf(stderr, "RNA_def_property_struct_type: %s.%s, invalid type for struct type.\n", ds->srna->cname, prop->cname);
487 void RNA_def_property_enum_items(PropertyRNA *prop, const PropertyEnumItem *item)
489 StructDefRNA *ds= DefRNA.structs.last;
494 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
497 for(i=0; item[i].cname; i++)
503 fprintf(stderr, "RNA_def_property_struct_type: %s.%s, invalid type for struct type.\n", ds->srna->cname, prop->cname);
509 void RNA_def_property_string_maxlength(PropertyRNA *prop, int maxlength)
511 StructDefRNA *ds= DefRNA.structs.last;
515 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
516 sprop->maxlength= maxlength;
520 fprintf(stderr, "RNA_def_property_string_maxlength: %s.%s, type is not string.\n", ds->srna->cname, prop->cname);
526 void RNA_def_property_boolean_default(PropertyRNA *prop, int value)
528 StructDefRNA *ds= DefRNA.structs.last;
532 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
533 bprop->defaultvalue= value;
537 fprintf(stderr, "RNA_def_property_boolean_default: %s.%s, type is not boolean.\n", ds->srna->cname, prop->cname);
543 void RNA_def_property_boolean_array_default(PropertyRNA *prop, const int *array)
545 StructDefRNA *ds= DefRNA.structs.last;
549 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
550 bprop->defaultarray= array;
554 fprintf(stderr, "RNA_def_property_boolean_default: %s.%s, type is not boolean.\n", ds->srna->cname, prop->cname);
560 void RNA_def_property_int_default(PropertyRNA *prop, int value)
562 StructDefRNA *ds= DefRNA.structs.last;
566 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
567 iprop->defaultvalue= value;
571 fprintf(stderr, "RNA_def_property_int_default: %s.%s, type is not int.\n", ds->srna->cname, prop->cname);
577 void RNA_def_property_int_array_default(PropertyRNA *prop, const int *array)
579 StructDefRNA *ds= DefRNA.structs.last;
583 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
584 iprop->defaultarray= array;
588 fprintf(stderr, "RNA_def_property_int_default: %s.%s, type is not int.\n", ds->srna->cname, prop->cname);
594 void RNA_def_property_float_default(PropertyRNA *prop, float value)
596 StructDefRNA *ds= DefRNA.structs.last;
600 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
601 fprop->defaultvalue= value;
605 fprintf(stderr, "RNA_def_property_float_default: %s.%s, type is not float.\n", ds->srna->cname, prop->cname);
611 void RNA_def_property_float_array_default(PropertyRNA *prop, const float *array)
613 StructDefRNA *ds= DefRNA.structs.last;
617 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
618 fprop->defaultarray= array;
622 fprintf(stderr, "RNA_def_property_float_default: %s.%s, type is not float.\n", ds->srna->cname, prop->cname);
628 void RNA_def_property_string_default(PropertyRNA *prop, const char *value)
630 StructDefRNA *ds= DefRNA.structs.last;
634 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
635 sprop->defaultvalue= value;
639 fprintf(stderr, "RNA_def_property_string_default: %s.%s, type is not string.\n", ds->srna->cname, prop->cname);
645 void RNA_def_property_enum_default(PropertyRNA *prop, int value)
647 StructDefRNA *ds= DefRNA.structs.last;
651 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
652 eprop->defaultvalue= value;
656 fprintf(stderr, "RNA_def_property_enum_default: %s.%s, type is not enum.\n", ds->srna->cname, prop->cname);
664 static PropertyDefRNA *rna_def_property_sdna(PropertyRNA *prop, const char *structname, const char *propname)
666 DNAStructMember smember;
667 StructDefRNA *ds= DefRNA.structs.last;
668 PropertyDefRNA *dp= ds->properties.last;
671 structname= ds->dnaname;
673 propname= prop->cname;
675 if(!rna_find_sdna_member(DefRNA.sdna, structname, propname, &smember)) {
677 fprintf(stderr, "rna_def_property_sdna: %s.%s not found.\n", structname, propname);
683 if(smember.arraylength > 1)
684 prop->arraylength= smember.arraylength;
686 prop->arraylength= 0;
688 dp->dnastructname= structname;
689 dp->dnaname= propname;
690 dp->dnatype= smember.type;
691 dp->dnaarraylength= smember.arraylength;
696 void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int bit)
700 if((dp=rna_def_property_sdna(prop, structname, propname)))
704 void RNA_def_property_int_sdna(PropertyRNA *prop, const char *structname, const char *propname)
707 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
709 if((dp= rna_def_property_sdna(prop, structname, propname))) {
710 /* SDNA doesn't pass us unsigned unfortunately .. */
711 if(strcmp(dp->dnatype, "char") == 0) {
712 iprop->hardmin= iprop->softmin= CHAR_MIN;
713 iprop->hardmax= iprop->softmax= CHAR_MAX;
715 else if(strcmp(dp->dnatype, "short") == 0) {
716 iprop->hardmin= iprop->softmin= SHRT_MIN;
717 iprop->hardmax= iprop->softmax= SHRT_MAX;
719 else if(strcmp(dp->dnatype, "int") == 0) {
720 iprop->hardmin= INT_MIN;
721 iprop->hardmax= INT_MAX;
723 iprop->softmin= -10000; /* rather arbitrary .. */
724 iprop->softmax= 10000;
727 if(prop->subtype == PROP_UNSIGNED)
728 iprop->hardmin= iprop->softmin= 0;
732 void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
734 rna_def_property_sdna(prop, structname, propname);
737 void RNA_def_property_enum_sdna(PropertyRNA *prop, const char *structname, const char *propname)
741 if((dp=rna_def_property_sdna(prop, structname, propname))) {
742 if(prop->arraylength) {
743 prop->arraylength= 0;
745 fprintf(stderr, "RNA_def_property_enum_sdna: %s.%s, array not supported for enum type.\n", structname, propname);
752 void RNA_def_property_string_sdna(PropertyRNA *prop, const char *structname, const char *propname)
755 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
757 if((dp=rna_def_property_sdna(prop, structname, propname))) {
758 if(prop->arraylength) {
759 sprop->maxlength= prop->arraylength;
760 prop->arraylength= 0;
765 void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
769 if((dp=rna_def_property_sdna(prop, structname, propname))) {
770 if(prop->arraylength) {
771 prop->arraylength= 0;
773 fprintf(stderr, "RNA_def_property_pointer_sdna: %s.%s, array not supported for pointer type.\n", structname, propname);
780 void RNA_def_property_collection_sdna(PropertyRNA *prop, const char *structname, const char *propname, const char *lengthpropname)
783 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
785 if((dp=rna_def_property_sdna(prop, structname, propname))) {
786 if(prop->arraylength) {
787 prop->arraylength= 0;
790 fprintf(stderr, "RNA_def_property_collection_sdna: %s.%s, array not supported for collection type.\n", structname, propname);
795 if(strcmp(dp->dnatype, "ListBase") == 0) {
796 cprop->next= (PropCollectionNextFunc)"rna_iterator_listbase_next";
797 cprop->get= (PropCollectionGetFunc)"rna_iterator_listbase_get";
801 if(dp && lengthpropname) {
802 DNAStructMember smember;
803 StructDefRNA *ds= DefRNA.structs.last;
806 structname= ds->dnaname;
808 if(!rna_find_sdna_member(DefRNA.sdna, structname, lengthpropname, &smember)) {
810 fprintf(stderr, "RNA_def_property_collection_sdna: %s.%s not found.\n", structname, lengthpropname);
815 dp->dnalengthstructname= structname;
816 dp->dnalengthname= lengthpropname;
818 cprop->next= (PropCollectionNextFunc)"rna_iterator_array_next";
819 cprop->get= (PropCollectionGetFunc)"rna_iterator_array_get";
820 cprop->end= (PropCollectionEndFunc)"rna_iterator_array_end";
827 void RNA_def_property_notify_func(PropertyRNA *prop, const char *notify)
829 if(notify) prop->notify= (PropNotifyFunc)notify;
832 void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
834 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
836 if(prop->arraylength) {
837 if(get) bprop->getarray= (PropBooleanArrayGetFunc)get;
838 if(set) bprop->setarray= (PropBooleanArraySetFunc)set;
841 if(get) bprop->get= (PropBooleanGetFunc)get;
842 if(set) bprop->set= (PropBooleanSetFunc)set;
846 void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set)
848 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
850 if(prop->arraylength) {
851 if(get) iprop->getarray= (PropIntArrayGetFunc)get;
852 if(set) iprop->setarray= (PropIntArraySetFunc)set;
855 if(get) iprop->get= (PropIntGetFunc)get;
856 if(set) iprop->set= (PropIntSetFunc)set;
860 void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set)
862 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
864 if(prop->arraylength) {
865 if(get) fprop->getarray= (PropFloatArrayGetFunc)get;
866 if(set) fprop->setarray= (PropFloatArraySetFunc)set;
869 if(get) fprop->get= (PropFloatGetFunc)get;
870 if(set) fprop->set= (PropFloatSetFunc)set;
874 void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set)
876 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
878 if(get) eprop->get= (PropEnumGetFunc)get;
879 if(set) eprop->set= (PropEnumSetFunc)set;
882 void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
884 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
886 if(get) sprop->get= (PropStringGetFunc)get;
887 if(length) sprop->length= (PropStringLengthFunc)length;
888 if(set) sprop->set= (PropStringSetFunc)set;
891 void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *type, const char *set)
893 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
895 if(get) pprop->get= (PropPointerGetFunc)get;
896 if(type) pprop->type= (PropPointerTypeFunc)type;
897 if(set) pprop->set= (PropPointerSetFunc)set;
900 void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *type, const char *length, const char *lookupint, const char *lookupstring)
902 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
904 if(begin) cprop->begin= (PropCollectionBeginFunc)begin;
905 if(next) cprop->next= (PropCollectionNextFunc)next;
906 if(end) cprop->end= (PropCollectionEndFunc)end;
907 if(get) cprop->get= (PropCollectionGetFunc)get;
908 if(type) cprop->type= (PropCollectionTypeFunc)type;
909 if(length) cprop->length= (PropCollectionLengthFunc)length;
910 if(lookupint) cprop->lookupint= (PropCollectionLookupIntFunc)lookupint;
911 if(lookupstring) cprop->lookupstring= (PropCollectionLookupStringFunc)lookupstring;