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(strct=DefRNA.structs.first; strct; strct=strct->next)
168 rna_freelistN(&strct->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(strct=brna->structs.first; strct; strct=strct->next)
187 rna_freelistN(&strct->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 strct= MEM_callocN(sizeof(StructRNA), "StructRNA");
210 rna_addtail(&brna->structs, strct);
212 RNA_def_struct_sdna(strct, strct->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: must be a string property.\n");
239 srna->nameproperty= prop;
242 /* Property Definition */
244 PropertyRNA *RNA_def_property(StructRNA *strct, const char *cname, int type, int subtype)
250 ds= DefRNA.structs.last;
251 dp= MEM_callocN(sizeof(PropertyDefRNA), "PropertyDefRNA");
252 rna_addtail(&ds->properties, dp);
256 prop= MEM_callocN(sizeof(BooleanPropertyRNA), "BooleanPropertyRNA");
259 IntPropertyRNA *iprop;
260 iprop= MEM_callocN(sizeof(IntPropertyRNA), "IntPropertyRNA");
261 prop= &iprop->property;
263 iprop->hardmin= (subtype == PROP_UNSIGNED)? 0: INT_MIN;
264 iprop->hardmax= INT_MAX;
266 iprop->softmin= (subtype == PROP_UNSIGNED)? 0: -10000; /* rather arbitrary .. */
267 iprop->softmax= 10000;
271 FloatPropertyRNA *fprop;
272 fprop= MEM_callocN(sizeof(FloatPropertyRNA), "FloatPropertyRNA");
273 prop= &fprop->property;
275 fprop->hardmin= (subtype == PROP_UNSIGNED)? 0: -FLT_MAX;
276 fprop->hardmax= FLT_MAX;
278 fprop->softmin= (subtype == PROP_UNSIGNED)? 0: -10000; /* rather arbitrary .. */
279 fprop->softmax= 10000;
283 StringPropertyRNA *sprop;
284 sprop= MEM_callocN(sizeof(StringPropertyRNA), "StringPropertyRNA");
285 prop= &sprop->property;
287 sprop->defaultvalue= "";
292 prop= MEM_callocN(sizeof(EnumPropertyRNA), "EnumPropertyRNA");
295 prop= MEM_callocN(sizeof(PointerPropertyRNA), "PointerPropertyRNA");
297 case PROP_COLLECTION:
298 prop= MEM_callocN(sizeof(CollectionPropertyRNA), "CollectionPropertyRNA");
301 fprintf(stderr, "RNA_def_property: invalid property type.\n");
311 prop->subtype= subtype;
313 prop->description= "";
314 prop->flag= PROP_EDITABLE|PROP_EVALUATEABLE;
319 RNA_def_property_boolean_sdna(prop, strct->cname, cname, 0);
324 RNA_def_property_int_sdna(prop, strct->cname, cname);
330 RNA_def_property_float_sdna(prop, strct->cname, cname);
336 RNA_def_property_string_sdna(prop, strct->cname, cname);
342 RNA_def_property_enum_sdna(prop, strct->cname, cname);
347 RNA_def_property_pointer_sdna(prop, strct->cname, cname);
350 case PROP_COLLECTION:
352 RNA_def_property_collection_sdna(prop, strct->cname, cname);
357 rna_addtail(&strct->properties, prop);
362 void RNA_def_property_access(PropertyRNA *prop, int editable, int evaluatable)
364 if(editable) prop->flag |= PROP_EDITABLE;
365 else prop->flag &= ~PROP_EDITABLE;
367 if(evaluatable) prop->flag |= PROP_EVALUATEABLE;
368 else prop->flag &= ~PROP_EVALUATEABLE;
371 void RNA_def_property_array(PropertyRNA *prop, int arraylength)
377 prop->arraylength= arraylength;
380 fprintf(stderr, "RNA_def_property_array: only boolean/int/float can be array.\n");
386 void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
389 prop->description= description;
392 void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, double precision)
396 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
397 iprop->softmin= (int)min;
398 iprop->softmax= (int)max;
399 iprop->step= (int)step;
403 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
404 fprop->softmin= (float)min;
405 fprop->softmax= (float)max;
406 fprop->step= (float)step;
407 fprop->precision= (float)precision;
411 fprintf(stderr, "RNA_def_property_ui_range: invalid type for ui range.\n");
417 void RNA_def_property_range(PropertyRNA *prop, double min, double max)
421 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
422 iprop->hardmin= (int)min;
423 iprop->hardmax= (int)max;
424 iprop->softmin= MAX2(min, iprop->hardmin);
425 iprop->softmax= MIN2(max, iprop->hardmax);
429 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
430 fprop->hardmin= (float)min;
431 fprop->hardmax= (float)max;
432 fprop->softmin= MAX2(min, fprop->hardmin);
433 fprop->softmax= MIN2(max, fprop->hardmax);
437 fprintf(stderr, "RNA_def_property_range: invalid type for range.\n");
443 void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
447 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
448 pprop->structtype = (StructRNA*)type;
451 case PROP_COLLECTION: {
452 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
453 cprop->structtype = (StructRNA*)type;
457 fprintf(stderr, "RNA_def_property_struct_type: invalid type for struct type.\n");
463 void RNA_def_property_enum_items(PropertyRNA *prop, const PropertyEnumItem *item)
469 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
472 for(i=0; item[i].cname; i++)
478 fprintf(stderr, "RNA_def_property_struct_type: invalid type for struct type.\n");
484 void RNA_def_property_string_maxlength(PropertyRNA *prop, int maxlength)
488 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
489 sprop->maxlength= maxlength;
493 fprintf(stderr, "RNA_def_property_string_maxlength: type is not string.\n");
499 void RNA_def_property_boolean_default(PropertyRNA *prop, int value)
503 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
504 bprop->defaultvalue= value;
508 fprintf(stderr, "RNA_def_property_boolean_default: type is not boolean.\n");
514 void RNA_def_property_boolean_array_default(PropertyRNA *prop, const int *array)
518 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
519 bprop->defaultarray= array;
523 fprintf(stderr, "RNA_def_property_boolean_default: type is not boolean.\n");
529 void RNA_def_property_int_default(PropertyRNA *prop, int value)
533 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
534 iprop->defaultvalue= value;
538 fprintf(stderr, "RNA_def_property_int_default: type is not int.\n");
544 void RNA_def_property_int_array_default(PropertyRNA *prop, const int *array)
548 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
549 iprop->defaultarray= array;
553 fprintf(stderr, "RNA_def_property_int_default: type is not int.\n");
559 void RNA_def_property_float_default(PropertyRNA *prop, float value)
563 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
564 fprop->defaultvalue= value;
568 fprintf(stderr, "RNA_def_property_float_default: type is not float.\n");
574 void RNA_def_property_float_array_default(PropertyRNA *prop, const float *array)
578 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
579 fprop->defaultarray= array;
583 fprintf(stderr, "RNA_def_property_float_default: type is not float.\n");
589 void RNA_def_property_string_default(PropertyRNA *prop, const char *value)
593 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
594 sprop->defaultvalue= value;
598 fprintf(stderr, "RNA_def_property_string_default: type is not string.\n");
604 void RNA_def_property_enum_default(PropertyRNA *prop, int value)
608 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
609 eprop->defaultvalue= value;
613 fprintf(stderr, "RNA_def_property_enum_default: type is not enum.\n");
621 static PropertyDefRNA *rna_def_property_sdna(PropertyRNA *prop, const char *structname, const char *propname)
623 DNAStructMember smember;
624 StructDefRNA *ds= DefRNA.structs.last;
625 PropertyDefRNA *dp= ds->properties.last;
628 structname= ds->dnaname;
630 propname= prop->cname;
632 if(!rna_find_sdna_member(DefRNA.sdna, structname, propname, &smember)) {
634 fprintf(stderr, "rna_def_property_sdna: %s.%s not found.\n", structname, propname);
640 if(smember.arraylength > 1)
641 prop->arraylength= smember.arraylength;
643 prop->arraylength= 0;
645 dp->dnastructname= structname;
646 dp->dnaname= propname;
647 dp->dnatype= smember.type;
648 dp->dnaarraylength= smember.arraylength;
653 void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int bit)
657 if((dp=rna_def_property_sdna(prop, structname, propname)))
661 void RNA_def_property_int_sdna(PropertyRNA *prop, const char *structname, const char *propname)
664 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
666 if((dp= rna_def_property_sdna(prop, structname, propname))) {
667 /* SDNA doesn't pass us unsigned unfortunately .. */
668 if(strcmp(dp->dnatype, "char") == 0) {
669 iprop->hardmin= iprop->softmin= CHAR_MIN;
670 iprop->hardmax= iprop->softmax= CHAR_MAX;
672 else if(strcmp(dp->dnatype, "short") == 0) {
673 iprop->hardmin= iprop->softmin= SHRT_MIN;
674 iprop->hardmax= iprop->softmax= SHRT_MAX;
676 else if(strcmp(dp->dnatype, "int") == 0) {
677 iprop->hardmin= INT_MIN;
678 iprop->hardmax= INT_MAX;
680 iprop->softmin= -10000; /* rather arbitrary .. */
681 iprop->softmax= 10000;
684 if(prop->subtype == PROP_UNSIGNED)
685 iprop->hardmin= iprop->softmin= 0;
689 void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
691 rna_def_property_sdna(prop, structname, propname);
694 void RNA_def_property_enum_sdna(PropertyRNA *prop, const char *structname, const char *propname)
698 if((dp=rna_def_property_sdna(prop, structname, propname))) {
699 if(prop->arraylength) {
700 prop->arraylength= 0;
702 fprintf(stderr, "RNA_def_property_enum_sdna: %s.%s, array not supported for enum type.\n", structname, propname);
709 void RNA_def_property_string_sdna(PropertyRNA *prop, const char *structname, const char *propname)
712 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
714 if((dp=rna_def_property_sdna(prop, structname, propname))) {
715 if(prop->arraylength) {
716 sprop->maxlength= prop->arraylength;
717 prop->arraylength= 0;
722 void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
726 if((dp=rna_def_property_sdna(prop, structname, propname))) {
727 if(prop->arraylength) {
728 prop->arraylength= 0;
730 fprintf(stderr, "RNA_def_property_pointer_sdna: %s.%s, array not supported for pointer type.\n", structname, propname);
737 void RNA_def_property_collection_sdna(PropertyRNA *prop, const char *structname, const char *propname)
740 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
742 if((dp=rna_def_property_sdna(prop, structname, propname))) {
743 if(prop->arraylength) {
744 prop->arraylength= 0;
747 fprintf(stderr, "RNA_def_property_collection_sdna: %s.%s, array not supported for collection type.\n", structname, propname);
752 if(strcmp(dp->dnatype, "ListBase") == 0) {
753 cprop->next= (PropCollectionNextFunc)"rna_iterator_listbase_next";
754 cprop->get= (PropCollectionGetFunc)"rna_iterator_listbase_get";
761 void RNA_def_property_notify_func(PropertyRNA *prop, char *notify)
763 if(notify) prop->notify= (PropNotifyFunc)notify;
766 void RNA_def_property_boolean_funcs(PropertyRNA *prop, char *get, char *set)
768 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
770 if(prop->arraylength) {
771 if(get) bprop->getarray= (PropBooleanArrayGetFunc)get;
772 if(set) bprop->setarray= (PropBooleanArraySetFunc)set;
775 if(get) bprop->get= (PropBooleanGetFunc)get;
776 if(set) bprop->set= (PropBooleanSetFunc)set;
780 void RNA_def_property_int_funcs(PropertyRNA *prop, char *get, char *set)
782 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
784 if(prop->arraylength) {
785 if(get) iprop->getarray= (PropIntArrayGetFunc)get;
786 if(set) iprop->setarray= (PropIntArraySetFunc)set;
789 if(get) iprop->get= (PropIntGetFunc)get;
790 if(set) iprop->set= (PropIntSetFunc)set;
794 void RNA_def_property_float_funcs(PropertyRNA *prop, char *get, char *set)
796 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
798 if(prop->arraylength) {
799 if(get) fprop->getarray= (PropFloatArrayGetFunc)get;
800 if(set) fprop->setarray= (PropFloatArraySetFunc)set;
803 if(get) fprop->get= (PropFloatGetFunc)get;
804 if(set) fprop->set= (PropFloatSetFunc)set;
808 void RNA_def_property_enum_funcs(PropertyRNA *prop, char *get, char *set)
810 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
812 if(get) eprop->get= (PropEnumGetFunc)get;
813 if(set) eprop->set= (PropEnumSetFunc)set;
816 void RNA_def_property_string_funcs(PropertyRNA *prop, char *get, char *length, char *set)
818 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
820 if(get) sprop->get= (PropStringGetFunc)get;
821 if(length) sprop->length= (PropStringLengthFunc)length;
822 if(set) sprop->set= (PropStringSetFunc)set;
825 void RNA_def_property_pointer_funcs(PropertyRNA *prop, char *get, char *type, char *set)
827 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
829 if(get) pprop->get= (PropPointerGetFunc)get;
830 if(type) pprop->type= (PropPointerTypeFunc)type;
831 if(set) pprop->set= (PropPointerSetFunc)set;
834 void RNA_def_property_collection_funcs(PropertyRNA *prop, char *begin, char *next, char *end, char *get, char *type, char *length, char *lookupint, char *lookupstring)
836 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
838 if(begin) cprop->begin= (PropCollectionBeginFunc)begin;
839 if(next) cprop->next= (PropCollectionNextFunc)next;
840 if(end) cprop->end= (PropCollectionEndFunc)end;
841 if(get) cprop->get= (PropCollectionGetFunc)get;
842 if(type) cprop->type= (PropCollectionTypeFunc)type;
843 if(length) cprop->length= (PropCollectionLengthFunc)length;
844 if(lookupint) cprop->lookupint= (PropCollectionLookupIntFunc)lookupint;
845 if(lookupstring) cprop->lookupstring= (PropCollectionLookupStringFunc)lookupstring;