python support for defining region drawing callbacks, while not directly related to operators, this means python can now make operators that draw in the 3D viewport interactively.
nicer then 2.4x space handelers because you can register draw handelers to draw pre/post 3d space or in pixel space.
This commit is contained in:
		@@ -26,6 +26,7 @@
 | 
			
		||||
#include "bpy_rna.h"
 | 
			
		||||
#include "bpy_props.h"
 | 
			
		||||
#include "bpy_util.h"
 | 
			
		||||
#include "bpy_rna_callback.h"
 | 
			
		||||
//#include "blendef.h"
 | 
			
		||||
#include "BLI_dynstr.h"
 | 
			
		||||
#include "BLI_listbase.h"
 | 
			
		||||
@@ -2662,6 +2663,11 @@ static struct PyMethodDef pyrna_struct_methods[] = {
 | 
			
		||||
	{"path_to_id", (PyCFunction)pyrna_struct_path_to_id, METH_VARARGS, NULL},
 | 
			
		||||
	{"recast_type", (PyCFunction)pyrna_struct_recast_type, METH_NOARGS, NULL},
 | 
			
		||||
	{"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
 | 
			
		||||
 | 
			
		||||
	/* experemental */
 | 
			
		||||
	{"callback_add", (PyCFunction)pyrna_callback_add, METH_VARARGS, NULL},
 | 
			
		||||
	{"callback_remove", (PyCFunction)pyrna_callback_remove, METH_VARARGS, NULL},
 | 
			
		||||
 | 
			
		||||
	{NULL, NULL, 0, NULL}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										112
									
								
								source/blender/python/intern/bpy_rna_callback.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								source/blender/python/intern/bpy_rna_callback.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
/**
 | 
			
		||||
 * $Id:
 | 
			
		||||
 *
 | 
			
		||||
 * ***** 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 *****
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "Python.h"
 | 
			
		||||
 | 
			
		||||
#include "bpy_rna.h"
 | 
			
		||||
#include "bpy_util.h"
 | 
			
		||||
 | 
			
		||||
#include "BLI_path_util.h"
 | 
			
		||||
#include "DNA_screen_types.h"
 | 
			
		||||
#include "BKE_context.h"
 | 
			
		||||
#include "ED_space_api.h"
 | 
			
		||||
 | 
			
		||||
EnumPropertyItem region_draw_mode_items[] = {
 | 
			
		||||
	{REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Pose View", ""},
 | 
			
		||||
	{REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
 | 
			
		||||
	{REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
 | 
			
		||||
	{0, NULL, 0, NULL, NULL}};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void cb_region_draw(const bContext *C, ARegion *ar, void *customdata)
 | 
			
		||||
{
 | 
			
		||||
	PyObject *cb_func, *cb_args, *result;
 | 
			
		||||
	PyGILState_STATE gilstate;
 | 
			
		||||
 | 
			
		||||
	bpy_context_set((bContext *)C, &gilstate);
 | 
			
		||||
 | 
			
		||||
	cb_func= PyTuple_GET_ITEM((PyObject *)customdata, 0);
 | 
			
		||||
	cb_args= PyTuple_GET_ITEM((PyObject *)customdata, 1);
 | 
			
		||||
	result = PyObject_CallObject(cb_func, cb_args);
 | 
			
		||||
 | 
			
		||||
	if(result) {
 | 
			
		||||
		Py_DECREF(result);
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		PyErr_Print();
 | 
			
		||||
		PyErr_Clear();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bpy_context_clear((bContext *)C, &gilstate);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
 | 
			
		||||
{
 | 
			
		||||
	void *handle;
 | 
			
		||||
 | 
			
		||||
	PyObject *cb_func, *cb_args;
 | 
			
		||||
	char *cb_type= NULL;
 | 
			
		||||
	int cb_type_enum;
 | 
			
		||||
 | 
			
		||||
	if (!PyArg_ParseTuple(args, "OO|s:callback_add", &cb_func, &cb_args, &cb_type))
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
 | 
			
		||||
		if(RNA_enum_value_from_id(region_draw_mode_items, cb_type, &cb_type_enum)==0) {
 | 
			
		||||
			PyErr_SetString(PyExc_ValueError, "callbcak_add(): enum invalid type");
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		handle= ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_type_enum);
 | 
			
		||||
		Py_INCREF(args);
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		PyErr_SetString(PyExc_TypeError, "callbcak_add(): type does not suppport cllbacks");
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return PyCapsule_New((void *)handle, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
 | 
			
		||||
{
 | 
			
		||||
	PyObject *py_handle;
 | 
			
		||||
	PyObject *py_args;
 | 
			
		||||
	void *handle;
 | 
			
		||||
	void *customdata;
 | 
			
		||||
 | 
			
		||||
	if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle))
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	handle= PyCapsule_GetPointer(py_handle, NULL);
 | 
			
		||||
 | 
			
		||||
	if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
 | 
			
		||||
		customdata= ED_region_draw_cb_customdata(handle);
 | 
			
		||||
		Py_DECREF((PyObject *)customdata);
 | 
			
		||||
 | 
			
		||||
		ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Py_RETURN_NONE;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								source/blender/python/intern/bpy_rna_callback.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								source/blender/python/intern/bpy_rna_callback.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
/**
 | 
			
		||||
 * $Id:
 | 
			
		||||
 *
 | 
			
		||||
 * ***** 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 *****
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
struct BPy_StructRNA;
 | 
			
		||||
struct PyObject;
 | 
			
		||||
 | 
			
		||||
struct PyObject *pyrna_callback_add(struct BPy_StructRNA *self, struct PyObject *args);
 | 
			
		||||
struct PyObject *pyrna_callback_remove(struct BPy_StructRNA *self, struct PyObject *args);
 | 
			
		||||
		Reference in New Issue
	
	Block a user