forked from bartvdbraak/blender
minor changes to Martni's commit 30961
- removed the immediate option from C/api and now store in python only, when python loads modules it sets it to False. - unloading a module would clear the entire TypeMap for all modules, only remove the module types that is being unloaded. - added some checks for bad class registering, report errors rather then crashing.
This commit is contained in:
parent
3d81ee3e4a
commit
55e64f0ba4
@ -102,6 +102,9 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
|
|||||||
import traceback
|
import traceback
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
# must be set back to True on exits
|
||||||
|
_bpy_types._register_immediate = False
|
||||||
|
|
||||||
t_main = time.time()
|
t_main = time.time()
|
||||||
|
|
||||||
loaded_modules = set()
|
loaded_modules = set()
|
||||||
@ -218,6 +221,8 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
|
|||||||
if _bpy.app.debug:
|
if _bpy.app.debug:
|
||||||
print("Python Script Load Time %.4f" % (time.time() - t_main))
|
print("Python Script Load Time %.4f" % (time.time() - t_main))
|
||||||
|
|
||||||
|
_bpy_types._register_immediate = True
|
||||||
|
|
||||||
|
|
||||||
def expandpath(path):
|
def expandpath(path):
|
||||||
"""
|
"""
|
||||||
|
@ -546,20 +546,24 @@ TypeMap = {}
|
|||||||
# and unregistered on unload
|
# and unregistered on unload
|
||||||
PropertiesMap = {}
|
PropertiesMap = {}
|
||||||
|
|
||||||
|
# Using our own loading function we set this to false
|
||||||
|
# so when running a script directly in the text editor
|
||||||
|
# registers moduals instantly.
|
||||||
|
_register_immediate = True
|
||||||
|
|
||||||
def UnloadModule(module):
|
def UnloadModule(module):
|
||||||
global TypeMap, PropertiesMap
|
for t in TypeMap.setdefault(module, ()):
|
||||||
for t in TypeMap.get(module, []):
|
|
||||||
bpy_types.unregister(t)
|
bpy_types.unregister(t)
|
||||||
|
|
||||||
TypeMap = {}
|
del TypeMap[module]
|
||||||
|
|
||||||
for t in PropertiesMap.get(module, []):
|
for t in PropertiesMap.setdefault(module, ()):
|
||||||
bpy_types.unregister(t)
|
bpy_types.unregister(t)
|
||||||
|
|
||||||
PropertiesMap = {}
|
del PropertiesMap[module]
|
||||||
|
|
||||||
def LoadModule(module, force=False):
|
def LoadModule(module, force=False):
|
||||||
for t in TypeMap.get(module, []):
|
for t in TypeMap.get(module, ()):
|
||||||
bpy_types.register(t)
|
bpy_types.register(t)
|
||||||
|
|
||||||
_bpy.LoadModule = LoadModule
|
_bpy.LoadModule = LoadModule
|
||||||
@ -567,8 +571,8 @@ _bpy.UnloadModule = UnloadModule
|
|||||||
|
|
||||||
class RNAMeta(type):
|
class RNAMeta(type):
|
||||||
@classmethod
|
@classmethod
|
||||||
def _immediate(cls):
|
def _register_immediate(cls):
|
||||||
return bpy_types.immediate();
|
return _register_immediate
|
||||||
|
|
||||||
def __new__(cls, name, bases, classdict, **args):
|
def __new__(cls, name, bases, classdict, **args):
|
||||||
result = type.__new__(cls, name, bases, classdict)
|
result = type.__new__(cls, name, bases, classdict)
|
||||||
@ -578,7 +582,7 @@ class RNAMeta(type):
|
|||||||
ClassMap = TypeMap
|
ClassMap = TypeMap
|
||||||
|
|
||||||
# Register right away if needed
|
# Register right away if needed
|
||||||
if cls._immediate():
|
if cls._register_immediate():
|
||||||
bpy_types.register(result)
|
bpy_types.register(result)
|
||||||
ClassMap = PropertiesMap
|
ClassMap = PropertiesMap
|
||||||
|
|
||||||
@ -586,16 +590,14 @@ class RNAMeta(type):
|
|||||||
if "." in module:
|
if "." in module:
|
||||||
module = module[:module.index(".")]
|
module = module[:module.index(".")]
|
||||||
|
|
||||||
if not module in ClassMap:
|
ClassMap.setdefault(module, []).append(result)
|
||||||
ClassMap[module] = []
|
|
||||||
|
|
||||||
ClassMap[module].append(result)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
class RNAMetaRegister(RNAMeta):
|
class RNAMetaRegister(RNAMeta):
|
||||||
@classmethod
|
@classmethod
|
||||||
def _immediate(cls):
|
def _register_immediate(cls):
|
||||||
return True;
|
return True
|
||||||
|
|
||||||
class OrderedMeta(RNAMeta):
|
class OrderedMeta(RNAMeta):
|
||||||
|
|
||||||
|
@ -330,8 +330,6 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bpy_set_immediate_register(1);
|
|
||||||
|
|
||||||
bpy_context_set(C, &gilstate);
|
bpy_context_set(C, &gilstate);
|
||||||
|
|
||||||
if (text) {
|
if (text) {
|
||||||
@ -397,8 +395,6 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
|
|||||||
|
|
||||||
bpy_context_clear(C, &gilstate);
|
bpy_context_clear(C, &gilstate);
|
||||||
|
|
||||||
bpy_set_immediate_register(0);
|
|
||||||
|
|
||||||
return py_result ? 1:0;
|
return py_result ? 1:0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4175,11 +4175,9 @@ static PyObject *pyrna_basetype_getattro( BPy_BaseTypeRNA *self, PyObject *pynam
|
|||||||
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self);
|
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self);
|
||||||
static PyObject *pyrna_basetype_register(PyObject *self, PyObject *py_class);
|
static PyObject *pyrna_basetype_register(PyObject *self, PyObject *py_class);
|
||||||
static PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *py_class);
|
static PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *py_class);
|
||||||
static PyObject *pyrna_register_immediate(PyObject *self);
|
|
||||||
|
|
||||||
static struct PyMethodDef pyrna_basetype_methods[] = {
|
static struct PyMethodDef pyrna_basetype_methods[] = {
|
||||||
{"__dir__", (PyCFunction)pyrna_basetype_dir, METH_NOARGS, ""},
|
{"__dir__", (PyCFunction)pyrna_basetype_dir, METH_NOARGS, ""},
|
||||||
{"immediate", (PyCFunction)pyrna_register_immediate, METH_NOARGS, ""},
|
|
||||||
{"register", (PyCFunction)pyrna_basetype_register, METH_O, ""},
|
{"register", (PyCFunction)pyrna_basetype_register, METH_O, ""},
|
||||||
{"unregister", (PyCFunction)pyrna_basetype_unregister, METH_O, ""},
|
{"unregister", (PyCFunction)pyrna_basetype_unregister, METH_O, ""},
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
@ -4546,10 +4544,17 @@ static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *par
|
|||||||
PyGILState_STATE gilstate;
|
PyGILState_STATE gilstate;
|
||||||
|
|
||||||
bContext *C= BPy_GetContext(); // XXX - NEEDS FIXING, QUITE BAD.
|
bContext *C= BPy_GetContext(); // XXX - NEEDS FIXING, QUITE BAD.
|
||||||
bpy_context_set(C, &gilstate);
|
|
||||||
|
|
||||||
py_class= RNA_struct_py_type_get(ptr->type);
|
py_class= RNA_struct_py_type_get(ptr->type);
|
||||||
|
|
||||||
|
/* rare case. can happen when registering subclasses */
|
||||||
|
if(py_class==NULL) {
|
||||||
|
fprintf(stderr, "bpy_class_call(): unable to get python class for rna struct '%.200s'\n", RNA_struct_identifier(ptr->type));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bpy_context_set(C, &gilstate);
|
||||||
|
|
||||||
/* exception, operators store their PyObjects for re-use */
|
/* exception, operators store their PyObjects for re-use */
|
||||||
if(ptr->data) {
|
if(ptr->data) {
|
||||||
if(RNA_struct_is_a(ptr->type, &RNA_Operator)) {
|
if(RNA_struct_is_a(ptr->type, &RNA_Operator)) {
|
||||||
@ -4738,7 +4743,16 @@ void pyrna_alloc_types(void)
|
|||||||
prop = RNA_struct_find_property(&ptr, "structs");
|
prop = RNA_struct_find_property(&ptr, "structs");
|
||||||
|
|
||||||
RNA_PROP_BEGIN(&ptr, itemptr, prop) {
|
RNA_PROP_BEGIN(&ptr, itemptr, prop) {
|
||||||
Py_DECREF(pyrna_struct_Subtype(&itemptr));
|
PyObject *item= pyrna_struct_Subtype(&itemptr);
|
||||||
|
if(item == NULL) {
|
||||||
|
if(PyErr_Occurred()) {
|
||||||
|
PyErr_Print();
|
||||||
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Py_DECREF(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RNA_PROP_END;
|
RNA_PROP_END;
|
||||||
|
|
||||||
@ -4771,22 +4785,6 @@ void pyrna_free_types(void)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int IMMEDIATE = 0;
|
|
||||||
|
|
||||||
void bpy_set_immediate_register(int value)
|
|
||||||
{
|
|
||||||
IMMEDIATE = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject *pyrna_register_immediate(PyObject *self)
|
|
||||||
{
|
|
||||||
if (IMMEDIATE) {
|
|
||||||
Py_RETURN_TRUE;
|
|
||||||
} else {
|
|
||||||
Py_RETURN_FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Note! MemLeak XXX
|
/* Note! MemLeak XXX
|
||||||
*
|
*
|
||||||
* There is currently a bug where moving registering a python class does
|
* There is currently a bug where moving registering a python class does
|
||||||
@ -4817,6 +4815,14 @@ static PyObject *pyrna_basetype_register(PyObject *self, PyObject *py_class)
|
|||||||
if(srna==NULL)
|
if(srna==NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
/* fails in cases, cant use this check but would like to :| */
|
||||||
|
/*
|
||||||
|
if(RNA_struct_py_type_get(srna)) {
|
||||||
|
PyErr_Format(PyExc_ValueError, "bpy.types.register(...): %.200s's parent class %.200s is alredy registered, this is not allowed.", ((PyTypeObject*)py_class)->tp_name, RNA_struct_identifier(srna));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/* check that we have a register callback for this type */
|
/* check that we have a register callback for this type */
|
||||||
reg= RNA_struct_register(srna);
|
reg= RNA_struct_register(srna);
|
||||||
|
|
||||||
|
@ -86,8 +86,6 @@ int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_
|
|||||||
|
|
||||||
int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix);
|
int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix);
|
||||||
|
|
||||||
void bpy_set_immediate_register(int value);
|
|
||||||
|
|
||||||
int pyrna_deferred_register_props(struct StructRNA *srna, PyObject *class_dict);
|
int pyrna_deferred_register_props(struct StructRNA *srna, PyObject *class_dict);
|
||||||
|
|
||||||
/* called before stopping python */
|
/* called before stopping python */
|
||||||
|
Loading…
Reference in New Issue
Block a user