23 #include "../generic/py_capi_utils.h"
24 #include "../generic/python_utildefines.h"
28 #include "RNA_prototypes.h"
67 if (!PyUnicode_Check(o)) {
68 PyErr_Format(PyExc_TypeError,
"expected a string (got %.200s)", Py_TYPE(o)->tp_name);
71 const char *gz_prop_id = PyUnicode_AsUTF8(o);
74 PyErr_Format(PyExc_ValueError,
75 "Gizmo target property '%s.%s' not found!",
93 const char *gz_prop_id = PyUnicode_AsUTF8(o);
94 PyErr_Format(PyExc_ValueError,
95 "Gizmo target property '%s.%s' has not been initialized, "
96 "Call \"target_set_prop\" first!",
111 if (!PyUnicode_Check(o)) {
112 PyErr_Format(PyExc_TypeError,
"expected a string (got %.200s)", Py_TYPE(o)->tp_name);
115 const char *gz_prop_id = PyUnicode_AsUTF8(o);
119 PyErr_Format(PyExc_ValueError,
120 "Gizmo target property '%s.%s' not found!",
140 #define BPY_GIZMO_FN_SLOT_LEN (BPY_GIZMO_FN_SLOT_RANGE_GET + 1)
151 const PyGILState_STATE gilstate = PyGILState_Ensure();
160 float *value = value_p;
162 if ((*value = PyFloat_AsDouble(
ret)) == -1.0f && PyErr_Occurred()) {
172 "Gizmo get callback: ") == -1) {
178 PyErr_SetString(PyExc_AttributeError,
"internal error, unsupported type");
184 PyGILState_Release(gilstate);
193 PyGILState_Release(gilstate);
200 const PyGILState_STATE gilstate = PyGILState_Ensure();
204 PyObject *args = PyTuple_New(1);
207 const float *value = value_p;
210 py_value = PyFloat_FromDouble(*value);
215 if (py_value ==
NULL) {
218 PyTuple_SET_ITEM(args, 0, py_value);
221 PyErr_SetString(PyExc_AttributeError,
"internal error, unsupported type");
232 PyGILState_Release(gilstate);
241 PyGILState_Release(gilstate);
250 const PyGILState_STATE gilstate = PyGILState_Ensure();
257 if (!PyTuple_Check(
ret)) {
258 PyErr_Format(PyExc_TypeError,
"Expected a tuple, not %.200s", Py_TYPE(
ret)->tp_name);
262 if (PyTuple_GET_SIZE(
ret) != 2) {
263 PyErr_Format(PyExc_TypeError,
"Expected a tuple of size 2, not %d", PyTuple_GET_SIZE(
ret));
269 for (
int i = 0; i < 2; i++) {
270 if (((range[i] = PyFloat_AsDouble(PyTuple_GET_ITEM(
ret, i))) == -1.0f && PyErr_Occurred()) ==
278 memcpy(value_p, range,
sizeof(range));
281 PyErr_SetString(PyExc_AttributeError,
"internal error, unsupported type");
286 PyGILState_Release(gilstate);
295 PyGILState_Release(gilstate);
302 const PyGILState_STATE gilstate = PyGILState_Ensure();
304 Py_XDECREF(
data->fn_slots[i]);
306 PyGILState_Release(gilstate);
312 bpy_gizmo_target_set_handler_doc,
313 ".. method:: target_set_handler(target, get, set, range=None):\n"
315 " Assigns callbacks to a gizmos property.\n"
317 " :arg target: Target property name.\n"
318 " :type target: string\n"
319 " :arg get: Function that returns the value for this property (single value or sequence).\n"
320 " :type get: callable\n"
321 " :arg set: Function that takes a single value argument and applies it.\n"
322 " :type set: callable\n"
323 " :arg range: Function that returns a (min, max) tuple for gizmos that use a range.\n"
324 " :type range: callable\n");
327 const PyGILState_STATE gilstate = PyGILState_Ensure();
333 .gz_with_target_type = {
NULL,
NULL},
334 .py_fn_slots = {
NULL},
340 static const char *
const _keywords[] = {
"self",
"target",
"get",
"set",
"range",
NULL};
341 static _PyArg_Parser _parser = {
348 ":target_set_handler",
352 if (!_PyArg_ParseTupleAndKeywordsFast(args,
357 &
params.gz_with_target_type.gz,
360 &
params.gz_with_target_type,
372 const int slots_required = 2;
373 const int slots_start = 2;
376 if (i < slots_required) {
377 PyErr_Format(PyExc_ValueError,
"Argument '%s' not given", _keywords[slots_start + i]);
381 else if (!PyCallable_Check(
params.py_fn_slots[i])) {
382 PyErr_Format(PyExc_ValueError,
"Argument '%s' not callable", _keywords[slots_start + i]);
392 Py_XINCREF(
params.py_fn_slots[i]);
405 PyGILState_Release(gilstate);
410 PyGILState_Release(gilstate);
421 ".. method:: target_get_value(target):\n"
423 " Get the value of this target property.\n"
425 " :arg target: Target property name.\n"
426 " :type target: string\n"
427 " :return: The value of the target property.\n"
428 " :rtype: Single value or array based on the target type\n");
437 static const char *
const _keywords[] = {
"self",
"target",
NULL};
438 static _PyArg_Parser _parser = {
445 if (!_PyArg_ParseTupleAndKeywordsFast(args,
450 &
params.gz_with_target.gz,
453 &
params.gz_with_target)) {
463 if (array_len != 0) {
470 return PyFloat_FromDouble(value);
475 PyErr_SetString(PyExc_RuntimeError,
"Not yet supported type");
485 ".. method:: target_set_value(target):\n"
487 " Set the value of this target property.\n"
489 " :arg target: Target property name.\n"
490 " :type target: string\n");
501 static const char *
const _keywords[] = {
"self",
"target",
"value",
NULL};
502 static _PyArg_Parser _parser = {
510 if (!_PyArg_ParseTupleAndKeywordsFast(args,
515 &
params.gz_with_target.gz,
530 if (array_len != 0) {
537 "Gizmo target property array: ") == -1) {
544 if ((value = PyFloat_AsDouble(
params.value)) == -1.0f && PyErr_Occurred()) {
552 PyErr_SetString(PyExc_RuntimeError,
"Not yet supported type");
562 ".. method:: target_get_range(target):\n"
564 " Get the range for this target property.\n"
566 " :arg target: Target property name.\n"
567 " :return: The range of this property (min, max).\n"
568 " :rtype: tuple pair.\n");
577 static const char *
const _keywords[] = {
"self",
"target",
NULL};
578 static _PyArg_Parser _parser = {
585 if (!_PyArg_ParseTupleAndKeywordsFast(args,
590 &
params.gz_with_target.gz,
593 &
params.gz_with_target)) {
607 PyErr_SetString(PyExc_RuntimeError,
"Not yet supported type");
624 static PyMethodDef method_def_array[] = {
626 {
"target_set_handler",
628 METH_VARARGS | METH_KEYWORDS,
629 bpy_gizmo_target_set_handler_doc},
633 METH_VARARGS | METH_KEYWORDS,
634 bpy_gizmo_target_get_value_doc},
637 METH_VARARGS | METH_KEYWORDS,
638 bpy_gizmo_target_set_value_doc},
641 METH_VARARGS | METH_KEYWORDS,
642 bpy_gizmo_target_get_range_doc},
646 for (
int i = 0; i <
ARRAY_SIZE(method_def_array); i++) {
647 PyMethodDef *m = &method_def_array[i];
648 PyObject *func = PyCFunction_New(m,
NULL);
649 PyObject *func_inst = PyInstanceMethod_New(func);
650 char name_prefix[128];
651 PyOS_snprintf(name_prefix,
sizeof(name_prefix),
"_rna_gizmo_%s", m->ml_name);
653 PyModule_AddObject(mod_par, name_prefix, func_inst);
#define BLI_array_alloca(arr, realsize)
Read Guarded memory(de)allocation.
struct bContext * BPY_context_get(void)
#define BPy_StructRNA_Check(v)
static PyObject * bpy_gizmo_target_get_value(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
static void py_rna_gizmo_handler_range_get_cb(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, void *value_p)
bool BPY_rna_gizmo_module(PyObject *mod_par)
static int py_rna_gizmo_target_id_parse(PyObject *o, void *p)
static void py_rna_gizmo_handler_set_cb(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, const void *value_p)
static int py_rna_gizmo_target_id_parse_and_ensure_is_valid(PyObject *o, void *p)
static PyObject * bpy_gizmo_target_get_range(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
static PyObject * bpy_gizmo_target_set_value(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
#define BPY_GIZMO_FN_SLOT_LEN
static PyObject * bpy_gizmo_target_set_handler(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
static void py_rna_gizmo_handler_get_cb(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, void *value_p)
static int py_rna_gizmo_target_type_id_parse(PyObject *o, void *p)
static void py_rna_gizmo_handler_free_cb(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop)
PyDoc_STRVAR(bpy_gizmo_target_set_handler_doc, ".. method:: target_set_handler(target, get, set, range=None):\n" "\n" " Assigns callbacks to a gizmos property.\n" "\n" " :arg target: Target property name.\n" " :type target: string\n" " :arg get: Function that returns the value for this property (single value or sequence).\n" " :type get: callable\n" " :arg set: Function that takes a single value argument and applies it.\n" " :type set: callable\n" " :arg range: Function that returns a (min, max) tuple for gizmos that use a range.\n" " :type range: callable\n")
@ BPY_GIZMO_FN_SLOT_RANGE_GET
static int py_rna_gizmo_parse(PyObject *o, void *p)
void(* MEM_freeN)(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
int PyC_AsArray(void *array, const size_t array_item_size, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const char *error_prefix)
PyObject * PyC_Tuple_PackArray_F32(const float *array, uint len)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
PyObject * fn_slots[BPY_GIZMO_FN_SLOT_LEN]
const wmGizmoPropertyType * gz_prop_type
wmGizmoProperty * gz_prop
const struct wmGizmoPropertyType * type
struct wmGizmoProperty::@1185 custom_func
const struct wmGizmoType * type
void WM_gizmo_target_property_float_set(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float value)
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
void WM_gizmo_target_property_float_get_array(const wmGizmo *gz, wmGizmoProperty *gz_prop, float *value)
void WM_gizmo_target_property_def_func_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, const wmGizmoPropertyFnParams *params)
float WM_gizmo_target_property_float_get(const wmGizmo *gz, wmGizmoProperty *gz_prop)
void WM_gizmo_target_property_float_set_array(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float *value)
const wmGizmoPropertyType * WM_gizmotype_target_property_find(const wmGizmoType *gzt, const char *idname)
int WM_gizmo_target_property_array_length(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop)
bool WM_gizmo_target_property_float_range_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, float range[2])