| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file blender/python/intern/bpy_app_handlers.c
 | 
					
						
							|  |  |  |  *  \ingroup pythonintern | 
					
						
							| 
									
										
										
										
											2011-11-05 08:21:12 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file defines a 'PyStructSequence' accessed via 'bpy.app.handlers', | 
					
						
							|  |  |  |  * which exposes various lists that the script author can add callback | 
					
						
							|  |  |  |  * functions into (called via blenders generic BLI_cb api) | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | #include "BLI_callbacks.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "RNA_types.h"
 | 
					
						
							|  |  |  | #include "RNA_access.h"
 | 
					
						
							|  |  |  | #include "bpy_rna.h"
 | 
					
						
							|  |  |  | #include "bpy_app_handlers.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:22 +11:00
										 |  |  | #include "../generic/python_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-15 01:52:28 +00:00
										 |  |  | #include "BPY_extern.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | void bpy_app_generic_callback(struct Main *main, struct ID *id, void *arg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyTypeObject BlenderAppCbType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyStructSequence_Field app_cb_info_fields[] = { | 
					
						
							| 
									
										
										
										
											2014-12-01 18:27:45 +01:00
										 |  |  | 	{(char *)"frame_change_pre",  (char *)"on frame change for playback and rendering (before)"}, | 
					
						
							|  |  |  | 	{(char *)"frame_change_post", (char *)"on frame change for playback and rendering (after)"}, | 
					
						
							|  |  |  | 	{(char *)"render_pre",        (char *)"on render (before)"}, | 
					
						
							|  |  |  | 	{(char *)"render_post",       (char *)"on render (after)"}, | 
					
						
							|  |  |  | 	{(char *)"render_write",      (char *)"on writing a render frame (directly after the frame is written)"}, | 
					
						
							|  |  |  | 	{(char *)"render_stats",      (char *)"on printing render statistics"}, | 
					
						
							|  |  |  | 	{(char *)"render_init",       (char *)"on initialization of a render job"}, | 
					
						
							|  |  |  | 	{(char *)"render_complete",   (char *)"on completion of render job"}, | 
					
						
							|  |  |  | 	{(char *)"render_cancel",     (char *)"on canceling a render job"}, | 
					
						
							|  |  |  | 	{(char *)"load_pre",          (char *)"on loading a new blend file (before)"}, | 
					
						
							|  |  |  | 	{(char *)"load_post",         (char *)"on loading a new blend file (after)"}, | 
					
						
							|  |  |  | 	{(char *)"save_pre",          (char *)"on saving a blend file (before)"}, | 
					
						
							|  |  |  | 	{(char *)"save_post",         (char *)"on saving a blend file (after)"}, | 
					
						
							|  |  |  | 	{(char *)"version_update",    (char *)"on ending the versioning code"}, | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* sets the permanent tag */ | 
					
						
							|  |  |  | #   define APP_CB_OTHER_FIELDS 1
 | 
					
						
							| 
									
										
										
										
											2011-11-04 04:27:46 +00:00
										 |  |  | 	{(char *)"persistent",        (char *)"Function decorator for callback functions not to be removed when loading new files"}, | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	{NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyStructSequence_Desc app_cb_info_desc = { | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	(char *)"bpy.app.handlers",     /* name */ | 
					
						
							| 
									
										
										
										
											2014-12-01 18:27:45 +01:00
										 |  |  | 	(char *)"This module contains callback lists",  /* doc */ | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	app_cb_info_fields,    /* fields */ | 
					
						
							| 
									
										
										
										
											2014-06-17 02:47:57 +10:00
										 |  |  | 	ARRAY_SIZE(app_cb_info_fields) - 1 | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2014-06-17 02:47:57 +10:00
										 |  |  | #  if (BLI_CB_EVT_TOT != ARRAY_SIZE(app_cb_info_fields))
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  | #    error "Callbacks are out of sync"
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | /* --------------------------------------------------------------------------*/ | 
					
						
							|  |  |  | /* permanent tagging code */ | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | #define PERMINENT_CB_ID "_bpy_persistent"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *bpy_app_handlers_persistent_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *UNUSED(kwds)) | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *value; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 08:50:06 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "O:bpy.app.handlers.persistent", &value)) | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (PyFunction_Check(value)) { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		PyObject **dict_ptr = _PyObject_GetDictPtr(value); | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 		if (dict_ptr == NULL) { | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | 			                "bpy.app.handlers.persistent wasn't able to " | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 			                "get the dictionary from the function passed"); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | 			/* set id */ | 
					
						
							|  |  |  | 			if (*dict_ptr == NULL) { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 				*dict_ptr = PyDict_New(); | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			PyDict_SetItemString(*dict_ptr, PERMINENT_CB_ID, Py_None); | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Py_INCREF(value); | 
					
						
							|  |  |  | 		return value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | 		                "bpy.app.handlers.persistent expected a function"); | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | /* dummy type because decorators can't be PyCFunctions */ | 
					
						
							|  |  |  | static PyTypeObject BPyPersistent_Type = { | 
					
						
							| 
									
										
										
										
											2011-11-03 12:01:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-27 15:34:55 -04:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	PyVarObject_HEAD_INIT(NULL, 0) | 
					
						
							| 
									
										
										
										
											2011-11-03 12:01:18 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							| 
									
										
										
										
											2011-11-03 12:01:18 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	"persistent",                               /* tp_name */ | 
					
						
							|  |  |  | 	0,                                          /* tp_basicsize */ | 
					
						
							|  |  |  | 	0,                                          /* tp_itemsize */ | 
					
						
							|  |  |  | 	/* methods */ | 
					
						
							|  |  |  | 	0,                                          /* tp_dealloc */ | 
					
						
							|  |  |  | 	0,                                          /* tp_print */ | 
					
						
							|  |  |  | 	0,                                          /* tp_getattr */ | 
					
						
							|  |  |  | 	0,                                          /* tp_setattr */ | 
					
						
							|  |  |  | 	0,                                          /* tp_reserved */ | 
					
						
							|  |  |  | 	0,                                          /* tp_repr */ | 
					
						
							|  |  |  | 	0,                                          /* tp_as_number */ | 
					
						
							|  |  |  | 	0,                                          /* tp_as_sequence */ | 
					
						
							|  |  |  | 	0,                                          /* tp_as_mapping */ | 
					
						
							|  |  |  | 	0,                                          /* tp_hash */ | 
					
						
							|  |  |  | 	0,                                          /* tp_call */ | 
					
						
							|  |  |  | 	0,                                          /* tp_str */ | 
					
						
							|  |  |  | 	0,                                          /* tp_getattro */ | 
					
						
							|  |  |  | 	0,                                          /* tp_setattro */ | 
					
						
							|  |  |  | 	0,                                          /* tp_as_buffer */ | 
					
						
							|  |  |  | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | 
					
						
							|  |  |  | 	Py_TPFLAGS_BASETYPE,                        /* tp_flags */ | 
					
						
							|  |  |  | 	0,                                          /* tp_doc */ | 
					
						
							|  |  |  | 	0,                                          /* tp_traverse */ | 
					
						
							|  |  |  | 	0,                                          /* tp_clear */ | 
					
						
							|  |  |  | 	0,                                          /* tp_richcompare */ | 
					
						
							|  |  |  | 	0,                                          /* tp_weaklistoffset */ | 
					
						
							|  |  |  | 	0,                                          /* tp_iter */ | 
					
						
							|  |  |  | 	0,                                          /* tp_iternext */ | 
					
						
							|  |  |  | 	0,                                          /* tp_methods */ | 
					
						
							|  |  |  | 	0,                                          /* tp_members */ | 
					
						
							|  |  |  | 	0,                                          /* tp_getset */ | 
					
						
							|  |  |  | 	0,                                          /* tp_base */ | 
					
						
							|  |  |  | 	0,                                          /* tp_dict */ | 
					
						
							|  |  |  | 	0,                                          /* tp_descr_get */ | 
					
						
							|  |  |  | 	0,                                          /* tp_descr_set */ | 
					
						
							|  |  |  | 	0,                                          /* tp_dictoffset */ | 
					
						
							|  |  |  | 	0,                                          /* tp_init */ | 
					
						
							|  |  |  | 	0,                                          /* tp_alloc */ | 
					
						
							|  |  |  | 	bpy_app_handlers_persistent_new,            /* tp_new */ | 
					
						
							|  |  |  | 	0,                                          /* tp_free */ | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyObject *py_cb_array[BLI_CB_EVT_TOT] = {NULL}; | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject *make_app_cb_info(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *app_cb_info; | 
					
						
							| 
									
										
										
										
											2014-05-11 16:22:05 +10:00
										 |  |  | 	int pos; | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	app_cb_info = PyStructSequence_New(&BlenderAppCbType); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	if (app_cb_info == NULL) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	for (pos = 0; pos < BLI_CB_EVT_TOT; pos++) { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (app_cb_info_fields[pos].name == NULL) { | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 			Py_FatalError("invalid callback slots 1"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		PyStructSequence_SET_ITEM(app_cb_info, pos, (py_cb_array[pos] = PyList_New(0))); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 	if (app_cb_info_fields[pos + APP_CB_OTHER_FIELDS].name != NULL) { | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 		Py_FatalError("invalid callback slots 2"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 	/* custom function */ | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | 	PyStructSequence_SET_ITEM(app_cb_info, pos++, (PyObject *)&BPyPersistent_Type); | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	return app_cb_info; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *BPY_app_handlers_struct(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-27 15:34:55 -04:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	BPyPersistent_Type.ob_base.ob_base.ob_type = &PyType_Type; | 
					
						
							| 
									
										
										
										
											2011-11-03 12:01:18 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 09:13:47 +00:00
										 |  |  | 	if (PyType_Ready(&BPyPersistent_Type) < 0) { | 
					
						
							|  |  |  | 		BLI_assert(!"error initializing 'bpy.app.handlers.persistent'"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	PyStructSequence_InitType(&BlenderAppCbType, &app_cb_info_desc); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	ret = make_app_cb_info(); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* prevent user from creating new instances */ | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	BlenderAppCbType.tp_init = NULL; | 
					
						
							|  |  |  | 	BlenderAppCbType.tp_new = NULL; | 
					
						
							|  |  |  | 	BlenderAppCbType.tp_hash = (hashfunc)_Py_HashPointer; /* without this we can't do set(sys.modules) [#29635] */ | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* assign the C callbacks */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (ret) { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		static bCallbackFuncStore funcstore_array[BLI_CB_EVT_TOT] = {{NULL}}; | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 		bCallbackFuncStore *funcstore; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		int pos = 0; | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		for (pos = 0; pos < BLI_CB_EVT_TOT; pos++) { | 
					
						
							|  |  |  | 			funcstore = &funcstore_array[pos]; | 
					
						
							|  |  |  | 			funcstore->func = bpy_app_generic_callback; | 
					
						
							|  |  |  | 			funcstore->alloc = 0; | 
					
						
							|  |  |  | 			funcstore->arg = SET_INT_IN_POINTER(pos); | 
					
						
							| 
									
										
										
										
											2012-05-05 00:23:55 +00:00
										 |  |  | 			BLI_callback_add(funcstore, pos); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | void BPY_app_handlers_reset(const short do_all) | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-10-13 17:44:27 +02:00
										 |  |  | 	PyGILState_STATE gilstate; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	int pos = 0; | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-13 17:44:27 +02:00
										 |  |  | 	gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 	if (do_all) { | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 		for (pos = 0; pos < BLI_CB_EVT_TOT; pos++) { | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 			/* clear list */ | 
					
						
							|  |  |  | 			PyList_SetSlice(py_cb_array[pos], 0, PY_SSIZE_T_MAX, NULL); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* save string conversion thrashing */ | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		PyObject *perm_id_str = PyUnicode_FromString(PERMINENT_CB_ID); | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		for (pos = 0; pos < BLI_CB_EVT_TOT; pos++) { | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 			/* clear only items without PERMINENT_CB_ID */ | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 			PyObject *ls = py_cb_array[pos]; | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 			Py_ssize_t i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			PyObject *item; | 
					
						
							|  |  |  | 			PyObject **dict_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 			for (i = PyList_GET_SIZE(ls) - 1; i >= 0; i--) { | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 				if ((PyFunction_Check((item = PyList_GET_ITEM(ls, i)))) && | 
					
						
							|  |  |  | 				    (dict_ptr = _PyObject_GetDictPtr(item)) && | 
					
						
							|  |  |  | 				    (*dict_ptr) && | 
					
						
							|  |  |  | 				    (PyDict_GetItem(*dict_ptr, perm_id_str) != NULL)) | 
					
						
							| 
									
										
										
										
											2011-11-03 06:53:52 +00:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					/* keep */ | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else { | 
					
						
							|  |  |  | 					/* remove */ | 
					
						
							|  |  |  | 					/* PySequence_DelItem(ls, i); */ /* more obvious buw slower */ | 
					
						
							|  |  |  | 					PyList_SetSlice(ls, i, i + 1, NULL); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Py_DECREF(perm_id_str); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-13 17:44:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	PyGILState_Release(gilstate); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* the actual callback - not necessarily called from py */ | 
					
						
							|  |  |  | void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	PyObject *cb_list = py_cb_array[GET_INT_FROM_POINTER(arg)]; | 
					
						
							| 
									
										
										
										
											2012-03-20 20:37:40 +00:00
										 |  |  | 	if (PyList_GET_SIZE(cb_list) > 0) { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		PyGILState_STATE gilstate = PyGILState_Ensure(); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-15 09:11:17 +00:00
										 |  |  | 		PyObject *args = PyTuple_New(1);  /* save python creating each call */ | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 		PyObject *func; | 
					
						
							|  |  |  | 		PyObject *ret; | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 		Py_ssize_t pos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* setup arguments */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (id) { | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 			PointerRNA id_ptr; | 
					
						
							|  |  |  | 			RNA_id_pointer_create(id, &id_ptr); | 
					
						
							|  |  |  | 			PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:22 +11:00
										 |  |  | 			PyTuple_SET_ITEM(args, 0, Py_INCREF_RET(Py_None)); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 20:37:40 +00:00
										 |  |  | 		/* Iterate the list and run the callbacks
 | 
					
						
							|  |  |  | 		 * note: don't store the list size since the scripts may remove themselves */ | 
					
						
							|  |  |  | 		for (pos = 0; pos < PyList_GET_SIZE(cb_list); pos++) { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 			func = PyList_GET_ITEM(cb_list, pos); | 
					
						
							|  |  |  | 			ret = PyObject_Call(func, args, NULL); | 
					
						
							|  |  |  | 			if (ret == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-20 13:32:52 +02:00
										 |  |  | 				/* Don't set last system variables because they might cause some
 | 
					
						
							|  |  |  | 				 * dangling pointers to external render engines (when exception | 
					
						
							|  |  |  | 				 * happens during rendering) which will break logic of render pipeline | 
					
						
							|  |  |  | 				 * which expects to be the only user of render engine when rendering | 
					
						
							|  |  |  | 				 * is finished. | 
					
						
							|  |  |  | 				 */ | 
					
						
							|  |  |  | 				PyErr_PrintEx(0); | 
					
						
							| 
									
										
										
										
											2011-06-24 16:54:30 +00:00
										 |  |  | 				PyErr_Clear(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				Py_DECREF(ret); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Py_DECREF(args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		PyGILState_Release(gilstate); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |