active property for collections for things like scene.objects.active
authorCampbell Barton <ideasman42@gmail.com>
Tue, 3 Nov 2009 16:07:29 +0000 (16:07 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 3 Nov 2009 16:07:29 +0000 (16:07 +0000)
will add more properties later

release/scripts/io/add_mesh_torus.py
source/blender/makesrna/RNA_access.h
source/blender/makesrna/RNA_define.h
source/blender/makesrna/intern/makesrna.c
source/blender/makesrna/intern/rna_access.c
source/blender/makesrna/intern/rna_define.c
source/blender/makesrna/intern/rna_internal_types.h
source/blender/makesrna/intern/rna_scene.c
source/blender/python/intern/bpy_rna.c

index 8a6a3a58ed60ca86f9b1209e96c45b3857786681..27ef587ce5aa4da2ae5d227f447df2607de26219 100644 (file)
@@ -117,7 +117,7 @@ class AddTorusPrimitive(bpy.types.Operator):
         ob_new = bpy.data.add_object('MESH', "Torus")
         ob_new.data = mesh
         scene.add_object(ob_new)
-        scene.active_object = ob_new
+        scene.objects.active = ob_new
         ob_new.selected = True
 
         ob_new.location = tuple(context.scene.cursor_location)
index 44b36e65aa38b47af2f7d75fe6f4b016eea40934..dc730ee46f9b9c55bf902c544dcd4d125ed9d2c3 100644 (file)
@@ -678,6 +678,7 @@ int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop);
 int RNA_property_collection_lookup_index(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *t_ptr);
 int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr);
 int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr);
+PropertyRNA *RNA_property_collection_active(PropertyRNA *prop);
 
 /* efficient functions to set properties for arrays */
 int RNA_property_collection_raw_array(PointerRNA *ptr, PropertyRNA *prop, PropertyRNA *itemprop, RawArray *array);
index 37b175fbf12679002784fcf42d04865c1906bfb2..ed4a371b9c385b817ee67c5f24a57eae3feb22dd 100644 (file)
@@ -164,6 +164,7 @@ void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char
 void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set);
 void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *typef);
 void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *length, const char *lookupint, const char *lookupstring, const char *add, const char *remove);
+void RNA_def_property_collection_active(PropertyRNA *prop, PropertyRNA *prop_act);
 
 /* Function */
 
index b16a0f00fd27b18dd643163bb1153dff10b23cd4..6689cda3f2b954d7b19b4dcaf16a6ddccaef1f3c 100644 (file)
@@ -1834,6 +1834,9 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr
                                else fprintf(f, "NULL, ");
                                if(cprop->remove) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->remove);
                                else fprintf(f, "NULL, ");
+
+                               if(cprop->active) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, cprop->active->identifier);
+
                                if(cprop->type) fprintf(f, "&RNA_%s\n", (char*)cprop->type);
                                else fprintf(f, "NULL\n");
                                break;
index fac18ba7942692ea258dc9f464e0cf964c9a109c..f0aaceec4aa28882dd0a1af25fd45bd9a8a29452 100644 (file)
@@ -1756,6 +1756,12 @@ int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, co
        }
 }
 
+PropertyRNA *RNA_property_collection_active(PropertyRNA *prop)
+{
+       CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
+       return cprop->active;
+}
+
 int RNA_property_collection_raw_array(PointerRNA *ptr, PropertyRNA *prop, PropertyRNA *itemprop, RawArray *array)
 {
        CollectionPropertyIterator iter;
index 48de7ace222095affa417035bf3129fde684cfac..46c4a8af73a00ec799a338d9b6afe7681f4e0466 100644 (file)
@@ -1957,6 +1957,26 @@ void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, con
        }
 }
 
+void RNA_def_property_collection_active(PropertyRNA *prop, PropertyRNA *prop_act)
+{
+       if(!DefRNA.preprocess) {
+               fprintf(stderr, "RNA_def_property_collection_active: only during preprocessing.\n");
+               return;
+       }
+
+       switch(prop->type) {
+               case PROP_COLLECTION: {
+                       CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
+                       cprop->active= prop_act;
+                       break;
+               }
+               default:
+                       fprintf(stderr, "RNA_def_property_collection_active: %s.%s, type is not collection.\n", prop->identifier, prop_act->identifier);
+                       DefRNA.error= 1;
+                       break;
+       }
+}
+
 /* Compact definitions */
 
 PropertyRNA *RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, int default_value, const char *ui_name, const char *ui_description)
index 645bc50ed897c51fa4411db8f92df406eac1ad6c..434e4ee6fe9c76a65e4b63eafb44ebec48ba7122 100644 (file)
@@ -257,6 +257,8 @@ typedef struct CollectionPropertyRNA {
        PropCollectionLookupStringFunc lookupstring;    /* optional */
        FunctionRNA *add, *remove;
 
+       PropertyRNA *active;
+
        struct StructRNA *type;
 } CollectionPropertyRNA;
 
index e7aeac96aef6d75eb7113ed98e846c48cb3ff5f3..667f88a3a337a3dd5f157738f0e63027c877e350 100644 (file)
@@ -2175,6 +2175,7 @@ void RNA_def_scene(BlenderRNA *brna)
 {
        StructRNA *srna;
        PropertyRNA *prop;
+       PropertyRNA *prop_act;
        FunctionRNA *func;
        
        static EnumPropertyItem audio_distance_model_items[] = {
@@ -2206,15 +2207,6 @@ void RNA_def_scene(BlenderRNA *brna)
        RNA_def_property_pointer_funcs(prop, NULL, "rna_Scene_set_set", NULL);
        RNA_def_property_ui_text(prop, "Set Scene", "Background set scene.");
 
-       prop= RNA_def_property(srna, "active_object", PROP_POINTER, PROP_NONE);
-       RNA_def_property_struct_type(prop, "Object");
-       RNA_def_property_pointer_funcs(prop, "rna_Scene_active_object_get", "rna_Scene_active_object_set", NULL);
-       RNA_def_property_flag(prop, PROP_EDITABLE);
-       RNA_def_property_ui_text(prop, "Object", "Object to use as projector transform.");
-       /* Could call: ED_base_object_activate(C, scene->basact);
-        * but would be a bad level call and it seems the notifier is enough */
-       RNA_def_property_update(prop, NC_SCENE|ND_OB_ACTIVE, NULL);
-
        prop= RNA_def_property(srna, "world", PROP_POINTER, PROP_NONE);
        RNA_def_property_flag(prop, PROP_EDITABLE);
        RNA_def_property_ui_text(prop, "World", "World used for rendering the scene.");
@@ -2232,12 +2224,35 @@ void RNA_def_scene(BlenderRNA *brna)
        RNA_def_property_struct_type(prop, "Base");
        RNA_def_property_ui_text(prop, "Bases", "");
 
+       { /* Collection active property */
+               prop_act= RNA_def_property(srna, "base_active", PROP_POINTER, PROP_NONE);
+               RNA_def_property_struct_type(prop_act, "Base");
+               RNA_def_property_pointer_sdna(prop_act, NULL, "basact");
+               RNA_def_property_flag(prop_act, PROP_EDITABLE);
+               RNA_def_property_ui_text(prop_act, "Active Base", "Active object in the scene.");
+               RNA_def_property_update(prop_act, NC_SCENE|ND_OB_ACTIVE, NULL);
+               RNA_def_property_collection_active(prop, prop_act);
+       }
+
        prop= RNA_def_property(srna, "objects", PROP_COLLECTION, PROP_NONE);
        RNA_def_property_collection_sdna(prop, NULL, "base", NULL);
        RNA_def_property_struct_type(prop, "Object");
        RNA_def_property_ui_text(prop, "Objects", "");
        RNA_def_property_collection_funcs(prop, 0, 0, 0, "rna_Scene_objects_get", 0, 0, 0, 0, 0);
 
+       { /* Collection active property */
+               prop_act= RNA_def_property(srna, "objects_active", PROP_POINTER, PROP_NONE);
+               RNA_def_property_struct_type(prop_act, "Object");
+               RNA_def_property_pointer_funcs(prop_act, "rna_Scene_active_object_get", "rna_Scene_active_object_set", NULL);
+               RNA_def_property_flag(prop_act, PROP_EDITABLE);
+               RNA_def_property_ui_text(prop_act, "Object", "Object to use as projector transform.");
+               /* Could call: ED_base_object_activate(C, scene->basact);
+                * but would be a bad level call and it seems the notifier is enough */
+               RNA_def_property_update(prop_act, NC_SCENE|ND_OB_ACTIVE, NULL);
+
+               RNA_def_property_collection_active(prop, prop_act);
+       }
+
        /* Layers */
        prop= RNA_def_property(srna, "visible_layers", PROP_BOOLEAN, PROP_LAYER_MEMBER);
        RNA_def_property_boolean_sdna(prop, NULL, "lay", 1);
index cd1f7dbc102992031248d9c9b690ebad4b1d5c00..eee863e5d1b73f013ba63baf9605c81b20cc4042 100644 (file)
@@ -1412,6 +1412,54 @@ static int pyrna_struct_setattro( BPy_StructRNA * self, PyObject *pyname, PyObje
        return pyrna_py_to_prop(&self->ptr, prop, NULL, value, "StructRNA - Attribute (setattr):");
 }
 
+static PyObject *pyrna_prop_getattro( BPy_PropertyRNA * self, PyObject *pyname )
+{
+       char *name = _PyUnicode_AsString(pyname);
+
+       if(strcmp(name, "active")==0) {
+               PropertyRNA *prop_act;
+
+               if (RNA_property_type(self->prop) != PROP_COLLECTION) {
+                       PyErr_SetString( PyExc_TypeError, "this BPy_PropertyRNA object is not a collection");
+                       return NULL;
+               }
+
+               prop_act= RNA_property_collection_active(self->prop);
+               if (prop_act==NULL) {
+                       PyErr_SetString( PyExc_TypeError, "collection has no active");
+                       return NULL;
+               }
+
+               return pyrna_prop_to_py(&self->ptr, prop_act);
+       }
+
+       return PyObject_GenericGetAttr((PyObject *)self, pyname);
+}
+
+//--------------- setattr-------------------------------------------
+static int pyrna_prop_setattro( BPy_PropertyRNA * self, PyObject *pyname, PyObject * value )
+{
+       char *name = _PyUnicode_AsString(pyname);
+       if(strcmp(name, "active")==0) {
+               PropertyRNA *prop_act;
+
+               if (RNA_property_type(self->prop) != PROP_COLLECTION) {
+                       PyErr_SetString( PyExc_TypeError, "this BPy_PropertyRNA object is not a collection");
+                       return -1;
+               }
+
+               prop_act= RNA_property_collection_active(self->prop);
+               if (prop_act==NULL) {
+                       PyErr_SetString( PyExc_TypeError, "collection has no active");
+                       return -1;
+               }
+
+               return pyrna_py_to_prop(&self->ptr, prop_act, NULL, value, "StructRNA - Attribute (setattr):");
+       }
+
+       return PyObject_GenericSetAttr((PyObject *)self, pyname, value);
+}
+
 static PyObject *pyrna_prop_keys(BPy_PropertyRNA *self)
 {
        PyObject *ret;
@@ -1850,7 +1898,6 @@ static struct PyMethodDef pyrna_prop_methods[] = {
        /* array accessor function */
        {"foreach_get", (PyCFunction)pyrna_prop_foreach_get, METH_VARARGS, NULL},
        {"foreach_set", (PyCFunction)pyrna_prop_foreach_set, METH_VARARGS, NULL},
-
        {NULL, NULL, 0, NULL}
 };
 
@@ -2321,8 +2368,10 @@ PyTypeObject pyrna_prop_Type = {
        NULL,                                           /* hashfunc tp_hash; */
        NULL,                       /* ternaryfunc tp_call; */
        NULL,                       /* reprfunc tp_str; */
-       NULL, /*PyObject_GenericGetAttr - MINGW Complains, assign later */      /* getattrofunc tp_getattro; */ /* will only use these if this is a subtype of a py class */
-       NULL, /*PyObject_GenericSetAttr - MINGW Complains, assign later */      /* setattrofunc tp_setattro; */
+
+       /* will only use these if this is a subtype of a py class */
+       ( getattrofunc ) pyrna_prop_getattro,   /* getattrofunc tp_getattro; */
+       ( setattrofunc ) pyrna_prop_setattro,   /* setattrofunc tp_setattro; */
 
        /* Functions to access object as input/output buffer */
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
@@ -2589,10 +2638,6 @@ PyObject *BPY_rna_module( void )
        mathutils_rna_matrix_cb_index= Mathutils_RegisterCallback(&mathutils_rna_matrix_cb);
 #endif
        
-       /* This can't be set in the pytype struct because some compilers complain */
-       pyrna_prop_Type.tp_getattro = PyObject_GenericGetAttr; 
-       pyrna_prop_Type.tp_setattro = PyObject_GenericSetAttr; 
-       
        if( PyType_Ready( &pyrna_struct_Type ) < 0 )
                return NULL;