[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22589] branches/blender2.5/blender/source /blender: 2.5: RNA, defining enums, pointers and collections properties is now

Brecht Van Lommel brecht at blender.org
Tue Aug 18 03:29:26 CEST 2009


Revision: 22589
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22589
Author:   blendix
Date:     2009-08-18 03:29:25 +0200 (Tue, 18 Aug 2009)

Log Message:
-----------
2.5: RNA, defining enums, pointers and collections properties is now
possible from python, but it's still work in progress.

Pointers and collections are restricted to types derived from
IDPropertyGroup (same as for operators), because RNA knows how to
allocate/deallocate those.

Collections have .add() and .remove(number) functions that can be
used. The remove function should be fixed to take an other argument
than a number.

With the IDPropertyGroup restriction, pointers are more like nested
structs. They don't have add(), remove() yet, not sure where to put
them. Currently the pointer / nested struct is automatically allocated
in the get() function, this needs to be fixed, rule is that RNA get()
will not change any data for thread safety.

Also, it is only possible to add properties to structs after they have
been registered, which needs to be improved as well.

Example code:
http://www.pasteall.org/7201/python

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/blenkernel/intern/idprop.c
    branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c
    branches/blender2.5/blender/source/blender/blenloader/intern/writefile.c
    branches/blender2.5/blender/source/blender/editors/screen/screen_ops.c
    branches/blender2.5/blender/source/blender/makesrna/RNA_access.h
    branches/blender2.5/blender/source/blender/makesrna/RNA_types.h
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_ID.c
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_define.c
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_internal.h
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_main.c
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_render.c
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_ui.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h

Modified: branches/blender2.5/blender/source/blender/blenkernel/intern/idprop.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenkernel/intern/idprop.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/blenkernel/intern/idprop.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -167,7 +167,7 @@
 		for (i=newlen; i<prop->len; i++) {
 			IDP_FreeProperty(GETPROP(prop, i));
 		}
-		memcpy(newarr, prop->data.pointer, newlen*prop->len*sizeof(IDProperty));
+		memcpy(newarr, prop->data.pointer, newlen*sizeof(IDProperty));
 	}
 
 	if(prop->data.pointer)

Modified: branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -1366,7 +1366,7 @@
 
 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
 {
-	IDProperty **array;
+	IDProperty *array;
 	int i;
 
 	/*since we didn't save the extra buffer, set totallen to len.*/
@@ -1374,11 +1374,10 @@
 	prop->data.pointer = newdataadr(fd, prop->data.pointer);
 
 	if (switch_endian) {
-		test_pointer_array(fd, prop->data.pointer);
-		array= (IDProperty**) prop->data.pointer;
+		array= (IDProperty*) prop->data.pointer;
 
 		for(i=0; i<prop->len; i++)
-			IDP_DirectLinkProperty(array[i], switch_endian, fd);
+			IDP_DirectLinkProperty(&array[i], switch_endian, fd);
 	}
 }
 

Modified: branches/blender2.5/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenloader/intern/writefile.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/blenloader/intern/writefile.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -407,13 +407,13 @@
 {
 	/*REMEMBER to set totalen to len in the linking code!!*/
 	if (prop->data.pointer) {
-		IDProperty **array = prop->data.pointer;
+		IDProperty *array = prop->data.pointer;
 		int a;
 
-		writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
+		writestruct(wd, DATA, "IDProperty", prop->len, array);
 
 		for(a=0; a<prop->len; a++)
-			IDP_WriteProperty(array[a], wd);
+			IDP_WriteProperty_OnlyData(&array[a], wd);
 	}
 }
 

Modified: branches/blender2.5/blender/source/blender/editors/screen/screen_ops.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/screen/screen_ops.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/editors/screen/screen_ops.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -1582,7 +1582,6 @@
 	ot->poll= ED_operator_screenactive;
 	
 	/* rna */
-	RNA_def_pointer_runtime(ot->srna, "screen", &RNA_Screen, "Screen", "");
 	RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
 }
 

Modified: branches/blender2.5/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/RNA_access.h	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/makesrna/RNA_access.h	2009-08-18 01:29:25 UTC (rev 22589)
@@ -649,7 +649,7 @@
 void RNA_property_pointer_add(PointerRNA *ptr, PropertyRNA *prop);
 void RNA_property_pointer_remove(PointerRNA *ptr, PropertyRNA *prop);
 void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr);
-void RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key);
+int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key);
 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop);
 
 /* Path

Modified: branches/blender2.5/blender/source/blender/makesrna/RNA_types.h
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/RNA_types.h	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/makesrna/RNA_types.h	2009-08-18 01:29:25 UTC (rev 22589)
@@ -263,7 +263,7 @@
 typedef int (*StructCallbackFunc)(struct PointerRNA *ptr, struct FunctionRNA *func, ParameterList *list);
 typedef void (*StructFreeFunc)(void *data);
 typedef struct StructRNA *(*StructRegisterFunc)(const struct bContext *C, struct ReportList *reports, void *data,
-	StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free);
+	const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free);
 typedef void (*StructUnregisterFunc)(const struct bContext *C, struct StructRNA *type);
 
 typedef struct StructRNA StructRNA;

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_ID.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_ID.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_ID.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -152,6 +152,30 @@
 	return ptr->data;
 }
 
+void rna_IDPropertyGroup_unregister(const bContext *C, StructRNA *type)
+{
+	RNA_struct_free(&BLENDER_RNA, type);
+}
+
+StructRNA *rna_IDPropertyGroup_register(const bContext *C, ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
+{
+	PointerRNA dummyptr;
+
+	/* create dummy pointer */
+	RNA_pointer_create(NULL, &RNA_IDPropertyGroup, NULL, &dummyptr);
+
+	/* validate the python class */
+	if(validate(&dummyptr, data, NULL) != 0)
+		return NULL;
+
+	return RNA_def_struct(&BLENDER_RNA, identifier, "IDPropertyGroup");  // XXX
+}
+
+StructRNA* rna_IDPropertyGroup_refine(PointerRNA *ptr)
+{
+	return ptr->type;
+}
+
 #else
 
 static void rna_def_ID_properties(BlenderRNA *brna)
@@ -210,6 +234,8 @@
 	srna= RNA_def_struct(brna, "IDPropertyGroup", NULL);
 	RNA_def_struct_ui_text(srna, "ID Property Group", "Group of ID properties.");
 	RNA_def_struct_idproperties_func(srna, "rna_IDPropertyGroup_idproperties");
+	RNA_def_struct_register_funcs(srna, "rna_IDPropertyGroup_register", "rna_IDPropertyGroup_unregister");
+	RNA_def_struct_refine_func(srna, "rna_IDPropertyGroup_refine");
 }
 
 static void rna_def_ID(BlenderRNA *brna)

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -1259,8 +1259,15 @@
 	else if(pprop->get) {
 		return pprop->get(ptr);
 	}
+	else if(prop->flag & PROP_IDPROPERTY) {
+		/* XXX temporary hack to add it automatically, reading should
+		   never do any write ops, to ensure thread safety etc .. */
+		RNA_property_pointer_add(ptr, prop);
+		return RNA_property_pointer_get(ptr, prop);
+	}
 	else {
 		PointerRNA result;
+
 		memset(&result, 0, sizeof(result));
 		return result;
 	}
@@ -1398,7 +1405,7 @@
 void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
 {
 	IDProperty *idprop;
-	//CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
+	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
 
 	if((idprop=rna_idproperty_check(&prop, ptr))) {
 		IDPropertyTemplate val = {0};
@@ -1424,7 +1431,6 @@
 			MEM_freeN(item);
 		}
 	}
-#if 0
 	else if(cprop->add){
 		if(!(cprop->add->flag & FUNC_USE_CONTEXT)) { /* XXX check for this somewhere else */
 			ParameterList params;
@@ -1433,9 +1439,8 @@
 			RNA_parameter_list_free(&params);
 		}
 	}
-#endif
-	else
-		printf("RNA_property_collection_add %s.%s: not implemented for this property.\n", ptr->type->identifier, prop->identifier);
+	/*else
+		printf("RNA_property_collection_add %s.%s: not implemented for this property.\n", ptr->type->identifier, prop->identifier);*/
 
 	if(r_ptr) {
 		if(idprop) {
@@ -1450,10 +1455,10 @@
 	}
 }
 
-void RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key)
+int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key)
 {
 	IDProperty *idprop;
-	//CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
+	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
 
 	if((idprop=rna_idproperty_check(&prop, ptr))) {
 		IDProperty tmp, *array;
@@ -1472,20 +1477,25 @@
 
 			IDP_ResizeIDPArray(idprop, len-1);
 		}
+
+		return 1;
 	}
-	else if(prop->flag & PROP_IDPROPERTY);
-#if 0
+	else if(prop->flag & PROP_IDPROPERTY)
+		return 1;
 	else if(cprop->remove){
 		if(!(cprop->remove->flag & FUNC_USE_CONTEXT)) { /* XXX check for this somewhere else */
 			ParameterList params;
-			RNA_parameter_list_create(&ptr, cprop->remove);
+			RNA_parameter_list_create(&params, ptr, cprop->remove);
 			RNA_function_call(NULL, NULL, ptr, cprop->remove, &params);
 			RNA_parameter_list_free(&params);
 		}
+
+		return 0;
 	}
-#endif
-	else
-		printf("RNA_property_collection_remove %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);
+	/*else
+		printf("RNA_property_collection_remove %s.%s: only supported for id properties.\n", ptr->type->identifier, prop->identifier);*/
+	
+	return 0;
 }
 
 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_define.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_define.c	2009-08-18 01:19:00 UTC (rev 22588)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_define.c	2009-08-18 01:29:25 UTC (rev 22589)
@@ -2419,6 +2419,7 @@
 	EnumPropertyItem *earray;
 	float *farray;
 	int *iarray;
+	int a;
 
 	if(prop->identifier) prop->identifier= BLI_strdup(prop->identifier);
 	if(prop->name) prop->name= BLI_strdup(prop->name);
@@ -2452,7 +2453,14 @@
 				earray= MEM_callocN(sizeof(EnumPropertyItem)*(eprop->totitem+1), "RNA_def_property_store"),
 				memcpy(earray, eprop->item, sizeof(EnumPropertyItem)*(eprop->totitem+1));
 				eprop->item= earray;
+
+				for(a=0; a<eprop->totitem; a++) {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list