提供: wiki
< Dev:2.5‎ | Source‎ | Architecture‎ | RNA
移動先: 案内検索


Example code of how to use the api in RNA_access.h to access data and get information about a struct at runtime.

#include "RNA_access.h"

void set_object_location_x(Main *main)
	PointerRNA mainptr, obptr;
	PropertyRNA *objects, *loc;

	/* main.objects["MyObject"].loc[0]= 3.1415 */

	RNA_main_pointer_create(main, &mainptr);

	objects= RNA_struct_find_property(&mainptr, "objects");
	RNA_property_collection_lookup_string(&mainptr, objects, "MyObject", &obptr);

	loc= RNA_struct_find_property(&obptr, "loc");
	RNA_property_float_set_index(&obptr, loc, 0, 3.1415f);

void set_mesh_vertex_location_z(Mesh *mesh)
	PointerRNA meshptr, vertptr;
	PropertyRNA *verts, *co;

	/* mesh.verts[7].co[2]= 2.7182 */

	RNA_id_pointer_create(&mesh->id, &meshptr);

	verts= RNA_struct_find_property(&meshptr, "verts");
	RNA_property_collection_lookup_int(&meshptr, verts, 7, &vertptr);

	co= RNA_struct_find_property(&vertptr, "co");
	RNA_property_float_set_index(&vertpr, co, 2, 2.7182f);

void set_vertex_selected(Mesh *mesh, MVert *mvert)
	PointerRNA vertptr;
	PropertyRNA *selected;

	/* vert.selected= true */

	RNA_pointer_create(&mesh->id, &RNA_MVert, mvert, &vertptr);

	selected= RNA_struct_find_property(&vertptr, "selected");
	RNA_property_boolean_set(&vertptr, selected, 1);

void print_mesh_information(Mesh *me)
	PointerRNA meshptr;
	PropertyRNA *properties, *nameprop, *prop;
	CollectionPropertyIterator iter;
	char *name;
	int numproperties;

	/* print all properties in mesh */

	RNA_id_pointer_create(&mesh->id, &meshptr);

	/* these two properties are special, one gives the name, the
	 * other gives a collection of all properties in the object */

	nameprop= RNA_struct_name_property(&meshptr);
	name= RNA_property_string_get_alloc(&meshptr, nameprop, NULL, 0);

	properties= RNA_struct_iterator_property(&meshptr);
	numproperties= RNA_property_collection_length(&meshptr, properties);

	printf("Object %s has %d properties:\n", name, numproperties);


	/* now iterate over the properties in the mesh */

	RNA_property_collection_begin(&meshptr, properties, &iter);
	for(; iter.valid; RNA_property_collection_next(&iter)) {
		prop= (PropertyRNA*);
		printf("\t%s\n", RNA_property_identifier(prop));


Example code of how to use the api in RNA_access.h for operators. First thing we have to do is define properties. The struct is already defined as wmOperatorType.srna when this function is called, we just have to define properties, for example:

void ED_TIME_OT_change_frame(wmOperatorType *ot)
    PropertyRNA *prop;

    /* identifiers */

    /* api callbacks */

    /* rna */
    prop= RNA_def_property(ot->srna, "frame", PROP_INT, PROP_NONE);
    RNA_def_property_range(prop, 0, MAXFRAME);
    RNA_def_property_ui_text(prop, "Frame", "");

Next we can use these properties when the operator runs. All properties must be defined in advance, otherwise runtime access will not work.

static int change_frame_invoke(bContext *C, wmOperator *op, wmEvent *event)
    RNA_int_set(op->ptr, "frame", frame_from_event(C, event));

static int change_frame_exec(bContext *C, wmOperator *op, wmEvent *event)
    int frame= RNA_int_get(op->ptr, "frame");