forked from bartvdbraak/blender
function to remove property
eg: bpy.types.Scene.IntProperty(attr="myprop") # adds bpy.types.Scene.RemoveProperty(attr="myprop") # removes
This commit is contained in:
parent
94cd746566
commit
5548e86795
@ -193,6 +193,7 @@ void RNA_def_func_duplicate_pointers(FunctionRNA *func);
|
||||
void RNA_def_func_free_pointers(FunctionRNA *func);
|
||||
void RNA_def_property_duplicate_pointers(PropertyRNA *prop);
|
||||
void RNA_def_property_free_pointers(PropertyRNA *prop);
|
||||
int RNA_def_property_free_identifier(StructOrFunctionRNA *cont_, const char *identifier);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -2722,5 +2722,39 @@ void RNA_def_property_free_pointers(PropertyRNA *prop)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RNA_def_property_free(StructOrFunctionRNA *cont_, PropertyRNA *prop)
|
||||
{
|
||||
ContainerRNA *cont= cont_;
|
||||
|
||||
RNA_def_property_free_pointers(prop);
|
||||
|
||||
if(prop->flag & PROP_RUNTIME) {
|
||||
if(cont->prophash)
|
||||
BLI_ghash_remove(cont->prophash, (void*)prop->identifier, NULL, NULL);
|
||||
|
||||
rna_freelinkN(&cont->properties, prop);
|
||||
}
|
||||
}
|
||||
|
||||
/* note: only intended for removing dynamic props */
|
||||
int RNA_def_property_free_identifier(StructOrFunctionRNA *cont_, const char *identifier)
|
||||
{
|
||||
ContainerRNA *cont= cont_;
|
||||
PropertyRNA *prop;
|
||||
|
||||
for(prop= cont->properties.first; prop; prop= prop->next) {
|
||||
if(strcmp(prop->identifier, identifier)==0) {
|
||||
if(prop->flag & PROP_RUNTIME) {
|
||||
RNA_def_property_free(cont_, prop);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -860,6 +860,42 @@ PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char BPy_RemoveProperty_doc[] =
|
||||
".. function:: RemoveProperty(attr)\n"
|
||||
"\n"
|
||||
" Removes a dynamically defined property.\n"
|
||||
"\n"
|
||||
" :arg attr: Property name.\n"
|
||||
" :type attr: string";
|
||||
PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
|
||||
{
|
||||
StructRNA *srna;
|
||||
|
||||
srna= srna_from_self(self, "RemoveProperty(...):");
|
||||
if(srna==NULL && PyErr_Occurred()) {
|
||||
return NULL; /* self's type was compatible but error getting the srna */
|
||||
}
|
||||
else if(srna==NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type.");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
static const char *kwlist[] = {"attr", NULL};
|
||||
|
||||
char *id=NULL;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "s:RemoveProperty", (char **)kwlist, &id))
|
||||
return NULL;
|
||||
|
||||
if(RNA_def_property_free_identifier(srna, id) == 0) {
|
||||
PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property.", id);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
static struct PyMethodDef props_methods[] = {
|
||||
{"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
|
||||
{"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
|
||||
@ -871,6 +907,9 @@ static struct PyMethodDef props_methods[] = {
|
||||
{"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
|
||||
{"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
|
||||
{"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
|
||||
|
||||
/* only useful as a bpy_struct method */
|
||||
/* {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc}, */
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
@ -41,6 +41,8 @@ PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw);
|
||||
PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw);
|
||||
PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw);
|
||||
|
||||
PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw);
|
||||
|
||||
#define PYRNA_STACK_ARRAY 32
|
||||
|
||||
#endif
|
||||
|
@ -3795,6 +3795,8 @@ static struct PyMethodDef pyrna_struct_subtype_methods[] = {
|
||||
{"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, ""},
|
||||
{"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, ""},
|
||||
|
||||
{"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, ""},
|
||||
|
||||
// {"__get_rna", (PyCFunction)BPy_GetStructRNA, METH_NOARGS, ""},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user