22
20
* ***** END GPL LICENSE BLOCK *****
23
/** \file blender/python/intern/bpy_rna.c
24
* \ingroup pythonintern
26
* This file is the main interface between python and blenders data api (RNA),
27
* exposing RNA to python so blender data can be accessed in a python like way.
29
* The two main types are 'BPy_StructRNA' and 'BPy_PropertyRNA' - the base
30
* classes for most of the data python accesses in blender.
36
#include <float.h> /* FLT_MIN/MAX */
38
#include "RNA_types.h"
25
40
#include "bpy_rna.h"
41
#include "bpy_rna_anim.h"
26
42
#include "bpy_props.h"
27
43
#include "bpy_util.h"
28
44
#include "bpy_rna_callback.h"
29
//#include "blendef.h"
45
#include "bpy_intern_string.h"
47
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
48
#include "MEM_guardedalloc.h"
30
51
#include "BLI_dynstr.h"
52
#include "BLI_string.h"
31
53
#include "BLI_listbase.h"
32
#include "float.h" /* FLT_MIN/MAX */
54
#include "BLI_math_rotation.h"
55
#include "BLI_utildefines.h"
57
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
58
#include "BLI_ghash.h"
34
61
#include "RNA_enum_types.h"
62
#include "RNA_define.h" /* RNA_def_property_free_identifier */
63
#include "RNA_access.h"
36
65
#include "MEM_guardedalloc.h"
37
#include "BKE_utildefines.h"
68
#include "BKE_idcode.h"
38
69
#include "BKE_context.h"
39
70
#include "BKE_global.h" /* evil G.* */
40
71
#include "BKE_report.h"
72
#include "BKE_idprop.h"
42
74
#include "BKE_animsys.h"
43
75
#include "BKE_fcurve.h"
45
/* only for keyframing */
46
#include "DNA_scene_types.h"
47
#include "DNA_anim_types.h"
48
#include "ED_keyframing.h"
77
#include "../generic/idprop_py_api.h" /* for IDprop lookups */
78
#include "../generic/py_capi_utils.h"
80
#ifdef WITH_INTERNATIONAL
81
#include "BLF_translation.h"
84
#define USE_PEDANTIC_WRITE
50
85
#define USE_MATHUTILS
53
#include "../generic/mathutils.h" /* so we can have mathutils callbacks */
54
#include "../generic/IDProp.h" /* for IDprop lookups */
57
static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
58
static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self);
86
#define USE_STRING_COERCE
88
static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);
89
static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self);
91
#define BPY_DOC_ID_PROP_TYPE_NOTE \
94
" Only :class:`bpy.types.ID`, :class:`bpy.types.Bone` and \n" \
95
" :class:`bpy.types.PoseBone` classes support custom properties.\n"
98
int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
100
if (pysrna->ptr.type) {
103
PyErr_Format(PyExc_ReferenceError,
104
"StructRNA of type %.200s has been removed",
105
Py_TYPE(pysrna)->tp_name);
109
int pyrna_prop_validity_check(BPy_PropertyRNA *self)
111
if (self->ptr.type) {
114
PyErr_Format(PyExc_ReferenceError,
115
"PropertyRNA of type %.200s.%.200s has been removed",
116
Py_TYPE(self)->tp_name, RNA_property_identifier(self->prop));
120
#if defined(USE_PYRNA_INVALIDATE_GC) || defined(USE_PYRNA_INVALIDATE_WEAKREF)
121
static void pyrna_invalidate(BPy_DummyPointerRNA *self)
123
self->ptr.type = NULL; /* this is checked for validity */
124
self->ptr.id.data = NULL; /* should not be needed but prevent bad pointer access, just in case */
128
#ifdef USE_PYRNA_INVALIDATE_GC
129
#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g) + 1))
131
/* only for sizeof() */
132
struct gc_generation {
138
static void id_release_gc(struct ID *id)
141
// unsigned int i = 0;
142
for (j = 0; j < 3; j++) {
143
/* hack below to get the 2 other lists from _PyGC_generation0 that are normally not exposed */
144
PyGC_Head *gen = (PyGC_Head *)(((char *)_PyGC_generation0) + (sizeof(gc_generation) * j));
145
PyGC_Head *g = gen->gc.gc_next;
146
while ((g = g->gc.gc_next) != gen) {
147
PyObject *ob = FROM_GC(g);
148
if (PyType_IsSubtype(Py_TYPE(ob), &pyrna_struct_Type) || PyType_IsSubtype(Py_TYPE(ob), &pyrna_prop_Type)) {
149
BPy_DummyPointerRNA *ob_ptr = (BPy_DummyPointerRNA *)ob;
150
if (ob_ptr->ptr.id.data == id) {
151
pyrna_invalidate(ob_ptr);
152
// printf("freeing: %p %s, %.200s\n", (void *)ob, id->name, Py_TYPE(ob)->tp_name);
158
// printf("id_release_gc freed '%s': %d\n", id->name, i);
162
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
163
//#define DEBUG_RNA_WEAKREF
165
struct GHash *id_weakref_pool = NULL;
166
static PyObject *id_free_weakref_cb(PyObject *weakinfo_pair, PyObject *weakref);
167
static PyMethodDef id_free_weakref_cb_def = {"id_free_weakref_cb", (PyCFunction)id_free_weakref_cb, METH_O, NULL};
169
/* adds a reference to the list, remember to decref */
170
static GHash *id_weakref_pool_get(ID *id)
172
GHash *weakinfo_hash = NULL;
174
if (id_weakref_pool) {
175
weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
178
/* first time, allocate pool */
179
id_weakref_pool = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "rna_global_pool");
180
weakinfo_hash = NULL;
183
if (weakinfo_hash == NULL) {
184
/* we're using a ghash as a set, could use libHX's HXMAP_SINGULAR but would be an extra dep. */
185
weakinfo_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "rna_id");
186
BLI_ghash_insert(id_weakref_pool, (void *)id, weakinfo_hash);
189
return weakinfo_hash;
192
/* called from pyrna_struct_CreatePyObject() and pyrna_prop_CreatePyObject() */
193
void id_weakref_pool_add(ID *id, BPy_DummyPointerRNA *pyrna)
196
PyObject *weakref_capsule;
197
PyObject *weakref_cb_py;
199
/* create a new function instance and insert the list as 'self' so we can remove ourself from it */
200
GHash *weakinfo_hash = id_weakref_pool_get(id); /* new or existing */
202
weakref_capsule = PyCapsule_New(weakinfo_hash, NULL, NULL);
203
weakref_cb_py = PyCFunction_New(&id_free_weakref_cb_def, weakref_capsule);
204
Py_DECREF(weakref_capsule);
206
/* add weakref to weakinfo_hash list */
207
weakref = PyWeakref_NewRef((PyObject *)pyrna, weakref_cb_py);
209
Py_DECREF(weakref_cb_py); /* function owned by the weakref now */
211
/* important to add at the end, since first removal looks at the end */
212
BLI_ghash_insert(weakinfo_hash, (void *)weakref, id); /* using a hash table as a set, all 'id's are the same */
213
/* weakinfo_hash owns the weakref */
217
/* workaround to get the last id without a lookup */
218
static ID *_id_tmp_ptr;
219
static void value_id_set(void *id)
221
_id_tmp_ptr = (ID *)id;
224
static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash);
225
static PyObject *id_free_weakref_cb(PyObject *weakinfo_capsule, PyObject *weakref)
227
/* important to search backwards */
228
GHash *weakinfo_hash = PyCapsule_GetPointer(weakinfo_capsule, NULL);
231
if (BLI_ghash_size(weakinfo_hash) > 1) {
232
BLI_ghash_remove(weakinfo_hash, weakref, NULL, NULL);
234
else { /* get the last id and free it */
235
BLI_ghash_remove(weakinfo_hash, weakref, NULL, value_id_set);
236
id_release_weakref_list(_id_tmp_ptr, weakinfo_hash);
244
static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash)
246
GHashIterator weakinfo_hash_iter;
248
BLI_ghashIterator_init(&weakinfo_hash_iter, weakinfo_hash);
250
#ifdef DEBUG_RNA_WEAKREF
251
fprintf(stdout, "id_release_weakref: '%s', %d items\n", id->name, BLI_ghash_size(weakinfo_hash));
254
while (!BLI_ghashIterator_isDone(&weakinfo_hash_iter)) {
255
PyObject *weakref = (PyObject *)BLI_ghashIterator_getKey(&weakinfo_hash_iter);
256
PyObject *item = PyWeakref_GET_OBJECT(weakref);
257
if (item != Py_None) {
259
#ifdef DEBUG_RNA_WEAKREF
260
PyC_ObSpit("id_release_weakref item ", item);
263
pyrna_invalidate((BPy_DummyPointerRNA *)item);
268
BLI_ghashIterator_step(&weakinfo_hash_iter);
271
BLI_ghash_remove(id_weakref_pool, (void *)id, NULL, NULL);
272
BLI_ghash_free(weakinfo_hash, NULL, NULL);
274
if (BLI_ghash_size(id_weakref_pool) == 0) {
275
BLI_ghash_free(id_weakref_pool, NULL, NULL);
276
id_weakref_pool = NULL;
277
#ifdef DEBUG_RNA_WEAKREF
278
printf("id_release_weakref freeing pool\n");
283
static void id_release_weakref(struct ID *id)
285
GHash *weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
287
id_release_weakref_list(id, weakinfo_hash);
291
#endif /* USE_PYRNA_INVALIDATE_WEAKREF */
293
void BPY_id_release(struct ID *id)
295
#ifdef USE_PYRNA_INVALIDATE_GC
299
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
300
if (id_weakref_pool) {
301
PyGILState_STATE gilstate = PyGILState_Ensure();
303
id_release_weakref(id);
305
PyGILState_Release(gilstate);
307
#endif /* USE_PYRNA_INVALIDATE_WEAKREF */
312
#ifdef USE_PEDANTIC_WRITE
313
static short rna_disallow_writes = FALSE;
315
static int rna_id_write_error(PointerRNA *ptr, PyObject *key)
317
ID *id = ptr->id.data;
319
const short idcode = GS(id->name);
320
if (!ELEM(idcode, ID_WM, ID_SCR)) { /* may need more added here */
321
const char *idtype = BKE_idcode_to_name(idcode);
323
if (key && PyUnicode_Check(key)) pyname = _PyUnicode_AsString(key);
324
else pyname = "<UNKNOWN>";
326
/* make a nice string error */
327
BLI_assert(idtype != NULL);
328
PyErr_Format(PyExc_AttributeError,
329
"Writing to ID classes in this context is not allowed: "
330
"%.200s, %.200s datablock, error setting %.200s.%.200s",
331
id->name + 2, idtype, RNA_struct_identifier(ptr->type), pyname);
338
#endif // USE_PEDANTIC_WRITE
341
#ifdef USE_PEDANTIC_WRITE
342
int pyrna_write_check(void)
344
return !rna_disallow_writes;
347
void pyrna_write_set(int val)
349
rna_disallow_writes = !val;
351
#else // USE_PEDANTIC_WRITE
352
int pyrna_write_check(void)
356
void pyrna_write_set(int UNUSED(val))
360
#endif // USE_PEDANTIC_WRITE
59
362
static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self);
363
static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self);
364
static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix);
365
static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item);
368
#include "../mathutils/mathutils.h" /* so we can have mathutils callbacks */
370
static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop,
371
Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length);
60
372
static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, short order_fallback);
62
374
/* bpyrna vector/euler/quat callbacks */
63
static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */
375
static unsigned char mathutils_rna_array_cb_index = -1; /* index for our callbacks */
65
/* not used yet but may want to use the subtype below */
377
/* subtype not used much yet */
66
378
#define MATHUTILS_CB_SUBTYPE_EUL 0
67
379
#define MATHUTILS_CB_SUBTYPE_VEC 1
68
380
#define MATHUTILS_CB_SUBTYPE_QUAT 2
69
#define MATHUTILS_CB_SUBTYPE_COLOR 0
381
#define MATHUTILS_CB_SUBTYPE_COLOR 3
71
383
static int mathutils_rna_generic_check(BaseMathObject *bmo)
73
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
74
return self->prop ? 1:0;
385
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
387
PYRNA_PROP_CHECK_INT(self);
389
return self->prop ? 0 : -1;
77
392
static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
79
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
394
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
396
PYRNA_PROP_CHECK_INT(self);
398
if (self->prop == NULL)
83
401
RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
85
403
/* Euler order exception */
86
if(subtype==MATHUTILS_CB_SUBTYPE_EUL) {
87
EulerObject *eul= (EulerObject *)bmo;
88
PropertyRNA *prop_eul_order= NULL;
89
eul->order= pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
404
if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
405
EulerObject *eul = (EulerObject *)bmo;
406
PropertyRNA *prop_eul_order = NULL;
407
eul->order = pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
95
413
static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
97
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
415
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
418
PYRNA_PROP_CHECK_INT(self);
420
if (self->prop == NULL)
423
#ifdef USE_PEDANTIC_WRITE
424
if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
427
#endif // USE_PEDANTIC_WRITE
429
if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
430
PyErr_Format(PyExc_AttributeError,
431
"bpy_prop \"%.200s.%.200s\" is read-only",
432
RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
102
436
RNA_property_float_range(&self->ptr, self->prop, &min, &max);
104
if(min != FLT_MIN || max != FLT_MAX) {
105
int i, len= RNA_property_array_length(&self->ptr, self->prop);
106
for(i=0; i<len; i++) {
438
if (min != FLT_MIN || max != FLT_MAX) {
439
int i, len = RNA_property_array_length(&self->ptr, self->prop);
440
for (i = 0; i < len; i++) {
107
441
CLAMP(bmo->data[i], min, max);
111
445
RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
112
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
446
if (RNA_property_update_check(self->prop)) {
447
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
114
450
/* Euler order exception */
115
if(subtype==MATHUTILS_CB_SUBTYPE_EUL) {
116
EulerObject *eul= (EulerObject *)bmo;
117
PropertyRNA *prop_eul_order= NULL;
118
short order= pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
119
if(order != eul->order) {
451
if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
452
EulerObject *eul = (EulerObject *)bmo;
453
PropertyRNA *prop_eul_order = NULL;
454
short order = pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
455
if (order != eul->order) {
120
456
RNA_property_enum_set(&self->ptr, prop_eul_order, eul->order);
121
RNA_property_update(BPy_GetContext(), &self->ptr, prop_eul_order);
457
if (RNA_property_update_check(prop_eul_order)) {
458
RNA_property_update(BPy_GetContext(), &self->ptr, prop_eul_order);
127
static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int subtype, int index)
129
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
134
bmo->data[index]= RNA_property_float_get_index(&self->ptr, self->prop, index);
138
static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int subtype, int index)
140
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
465
static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
467
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
469
PYRNA_PROP_CHECK_INT(self);
471
if (self->prop == NULL)
474
bmo->data[index] = RNA_property_float_get_index(&self->ptr, self->prop, index);
478
static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
480
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
482
PYRNA_PROP_CHECK_INT(self);
484
if (self->prop == NULL)
487
#ifdef USE_PEDANTIC_WRITE
488
if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
491
#endif // USE_PEDANTIC_WRITE
493
if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
494
PyErr_Format(PyExc_AttributeError,
495
"bpy_prop \"%.200s.%.200s\" is read-only",
496
RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
145
500
RNA_property_float_clamp(&self->ptr, self->prop, &bmo->data[index]);
146
501
RNA_property_float_set_index(&self->ptr, self->prop, index, bmo->data[index]);
147
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
503
if (RNA_property_update_check(self->prop)) {
504
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
151
Mathutils_Callback mathutils_rna_array_cb = {
152
(BaseMathCheckFunc) mathutils_rna_generic_check,
153
(BaseMathGetFunc) mathutils_rna_vector_get,
154
(BaseMathSetFunc) mathutils_rna_vector_set,
155
(BaseMathGetIndexFunc) mathutils_rna_vector_get_index,
156
(BaseMathSetIndexFunc) mathutils_rna_vector_set_index
510
static Mathutils_Callback mathutils_rna_array_cb = {
511
(BaseMathCheckFunc) mathutils_rna_generic_check,
512
(BaseMathGetFunc) mathutils_rna_vector_get,
513
(BaseMathSetFunc) mathutils_rna_vector_set,
514
(BaseMathGetIndexFunc) mathutils_rna_vector_get_index,
515
(BaseMathSetIndexFunc) mathutils_rna_vector_set_index
160
519
/* bpyrna matrix callbacks */
161
static int mathutils_rna_matrix_cb_index= -1; /* index for our callbacks */
520
static unsigned char mathutils_rna_matrix_cb_index = -1; /* index for our callbacks */
163
static int mathutils_rna_matrix_get(BaseMathObject *bmo, int subtype)
522
static int mathutils_rna_matrix_get(BaseMathObject *bmo, int UNUSED(subtype))
165
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
524
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
526
PYRNA_PROP_CHECK_INT(self);
528
if (self->prop == NULL)
170
531
RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
174
static int mathutils_rna_matrix_set(BaseMathObject *bmo, int subtype)
535
static int mathutils_rna_matrix_set(BaseMathObject *bmo, int UNUSED(subtype))
176
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
537
BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
539
PYRNA_PROP_CHECK_INT(self);
541
if (self->prop == NULL)
544
#ifdef USE_PEDANTIC_WRITE
545
if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
548
#endif // USE_PEDANTIC_WRITE
550
if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
551
PyErr_Format(PyExc_AttributeError,
552
"bpy_prop \"%.200s.%.200s\" is read-only",
553
RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
180
557
/* can ignore clamping here */
181
558
RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
182
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
560
if (RNA_property_update_check(self->prop)) {
561
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
186
Mathutils_Callback mathutils_rna_matrix_cb = {
566
static Mathutils_Callback mathutils_rna_matrix_cb = {
187
567
mathutils_rna_generic_check,
188
568
mathutils_rna_matrix_get,
189
569
mathutils_rna_matrix_set,
194
/* same as RNA_enum_value_from_id but raises an exception */
195
int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix)
574
static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, short order_fallback)
197
if(RNA_enum_value_from_id(item, identifier, value) == 0) {
198
char *enum_str= BPy_enum_as_string(item);
199
PyErr_Format(PyExc_TypeError, "%s: '%.200s' not found in (%s)", error_prefix, identifier, enum_str);
576
/* attempt to get order */
577
if (*prop_eul_order == NULL)
578
*prop_eul_order = RNA_struct_find_property(ptr, "rotation_mode");
580
if (*prop_eul_order) {
581
short order = RNA_property_enum_get(ptr, *prop_eul_order);
582
if (order >= EULER_ORDER_XYZ && order <= EULER_ORDER_ZYX) /* could be quat or axisangle */
586
return order_fallback;
207
#define PROP_ALL_VECTOR_SUBTYPES PROP_TRANSLATION: case PROP_DIRECTION: case PROP_VELOCITY: case PROP_ACCELERATION: case PROP_XYZ: case PROP_XYZ_LENGTH
589
#endif // USE_MATHUTILS
591
/* note that PROP_NONE is included as a vector subtype. this is because its handy to
592
* have x/y access to fcurve keyframes and other fixed size float arrays of length 2-4. */
593
#define PROP_ALL_VECTOR_SUBTYPES \
595
case PROP_TRANSLATION: \
596
case PROP_DIRECTION: \
597
case PROP_VELOCITY: \
598
case PROP_ACCELERATION: \
600
case PROP_XYZ_LENGTH \
209
603
PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
605
PyObject *ret = NULL;
213
607
#ifdef USE_MATHUTILS
214
608
int subtype, totdim;
217
int flag= RNA_property_flag(prop);
611
const int flag = RNA_property_flag(prop);
219
613
/* disallow dynamic sized arrays to be wrapped since the size could change
220
614
* to a size mathutils does not support */
221
615
if ((RNA_property_type(prop) != PROP_FLOAT) || (flag & PROP_DYNAMIC))
224
len= RNA_property_array_length(ptr, prop);
225
subtype= RNA_property_subtype(prop);
226
totdim= RNA_property_array_dimension(ptr, prop, NULL);
618
len = RNA_property_array_length(ptr, prop);
619
subtype = RNA_property_subtype(prop);
620
totdim = RNA_property_array_dimension(ptr, prop, NULL);
227
621
is_thick = (flag & PROP_THICK_WRAP);
229
623
if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) {
231
ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
233
switch(RNA_property_subtype(prop)) {
234
case PROP_ALL_VECTOR_SUBTYPES:
235
if(len>=2 && len <= 4) {
237
ret= newVectorObject(NULL, len, Py_NEW, NULL);
238
RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
241
PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_VEC);
242
Py_DECREF(ret); /* the vector owns now */
243
ret= vec_cb; /* return the vector instead */
250
ret= newMatrixObject(NULL, 4, 4, Py_NEW, NULL);
251
RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->contigPtr);
254
PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_matrix_cb_index, FALSE);
255
Py_DECREF(ret); /* the matrix owns now */
256
ret= mat_cb; /* return the matrix instead */
261
ret= newMatrixObject(NULL, 3, 3, Py_NEW, NULL);
262
RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->contigPtr);
265
PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_matrix_cb_index, FALSE);
266
Py_DECREF(ret); /* the matrix owns now */
267
ret= mat_cb; /* return the matrix instead */
272
case PROP_QUATERNION:
273
if(len==3) { /* euler */
275
/* attempt to get order, only needed for thixk types since wrapped with update via callbacks */
276
PropertyRNA *prop_eul_order= NULL;
277
short order= pyrna_rotation_euler_order_get(ptr, &prop_eul_order, ROT_MODE_XYZ);
279
ret= newEulerObject(NULL, order, Py_NEW, NULL); // TODO, get order from RNA
280
RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
283
/* order will be updated from callback on use */
284
PyObject *eul_cb= newEulerObject_cb(ret, ROT_MODE_XYZ, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
285
Py_DECREF(ret); /* the euler owns now */
286
ret= eul_cb; /* return the euler instead */
291
ret= newQuaternionObject(NULL, Py_NEW, NULL);
292
RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
295
PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_QUAT);
296
Py_DECREF(ret); /* the quat owns now */
297
ret= quat_cb; /* return the quat instead */
302
if(len==3) { /* color */
304
ret= newColorObject(NULL, Py_NEW, NULL); // TODO, get order from RNA
305
RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col);
308
PyObject *col_cb= newColorObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_COLOR);
309
Py_DECREF(ret); /* the color owns now */
310
ret= col_cb; /* return the color instead */
625
ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the mathutils PyObject */
628
case PROP_ALL_VECTOR_SUBTYPES:
629
if (len >= 2 && len <= 4) {
631
ret = Vector_CreatePyObject(NULL, len, Py_NEW, NULL);
632
RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
635
PyObject *vec_cb = Vector_CreatePyObject_cb(ret, len, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_VEC);
636
Py_DECREF(ret); /* the vector owns now */
637
ret = vec_cb; /* return the vector instead */
644
ret = Matrix_CreatePyObject(NULL, 4, 4, Py_NEW, NULL);
645
RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
648
PyObject *mat_cb = Matrix_CreatePyObject_cb(ret, 4, 4, mathutils_rna_matrix_cb_index, FALSE);
649
Py_DECREF(ret); /* the matrix owns now */
650
ret = mat_cb; /* return the matrix instead */
655
ret = Matrix_CreatePyObject(NULL, 3, 3, Py_NEW, NULL);
656
RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
659
PyObject *mat_cb = Matrix_CreatePyObject_cb(ret, 3, 3, mathutils_rna_matrix_cb_index, FALSE);
660
Py_DECREF(ret); /* the matrix owns now */
661
ret = mat_cb; /* return the matrix instead */
666
case PROP_QUATERNION:
667
if (len == 3) { /* euler */
669
/* attempt to get order, only needed for thick types since wrapped with update via callbacks */
670
PropertyRNA *prop_eul_order = NULL;
671
short order = pyrna_rotation_euler_order_get(ptr, &prop_eul_order, EULER_ORDER_XYZ);
673
ret = Euler_CreatePyObject(NULL, order, Py_NEW, NULL); // TODO, get order from RNA
674
RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
677
/* order will be updated from callback on use */
678
PyObject *eul_cb = Euler_CreatePyObject_cb(ret, EULER_ORDER_XYZ, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
679
Py_DECREF(ret); /* the euler owns now */
680
ret = eul_cb; /* return the euler instead */
685
ret = Quaternion_CreatePyObject(NULL, Py_NEW, NULL);
686
RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
689
PyObject *quat_cb = Quaternion_CreatePyObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_QUAT);
690
Py_DECREF(ret); /* the quat owns now */
691
ret = quat_cb; /* return the quat instead */
696
case PROP_COLOR_GAMMA:
697
if (len == 3) { /* color */
699
ret = Color_CreatePyObject(NULL, Py_NEW, NULL); // TODO, get order from RNA
700
RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col);
703
PyObject *col_cb = Color_CreatePyObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_COLOR);
704
Py_DECREF(ret); /* the color owns now */
705
ret = col_cb; /* return the color instead */
320
715
/* this is an array we cant reference (since its not thin wrappable)
321
716
* and cannot be coerced into a mathutils type, so return as a list */
322
717
ret = pyrna_prop_array_subscript_slice(NULL, ptr, prop, 0, len, len);
324
ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
720
ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the mathutils PyObject */
723
#else // USE_MATHUTILS
726
#endif // USE_MATHUTILS
335
static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, short order_fallback)
731
/* same as RNA_enum_value_from_id but raises an exception */
732
int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix)
337
/* attempt to get order */
338
if(*prop_eul_order==NULL)
339
*prop_eul_order= RNA_struct_find_property(ptr, "rotation_mode");
341
if(*prop_eul_order) {
342
short order= RNA_property_enum_get(ptr, *prop_eul_order);
343
if (order >= ROT_MODE_XYZ && order <= ROT_MODE_ZYX) /* could be quat or axisangle */
734
if (RNA_enum_value_from_id(item, identifier, value) == 0) {
735
const char *enum_str = BPy_enum_as_string(item);
736
PyErr_Format(PyExc_ValueError,
737
"%s: '%.200s' not found in (%s)",
738
error_prefix, identifier, enum_str);
739
MEM_freeN((void *)enum_str);
347
return order_fallback;
350
static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
352
return (a->ptr.data==b->ptr.data) ? 0 : -1;
355
static int pyrna_prop_compare( BPy_PropertyRNA * a, BPy_PropertyRNA * b )
357
return (a->prop==b->prop && a->ptr.data==b->ptr.data ) ? 0 : -1;
747
* checking the 'ptr->data' matches works in almost all cases,
748
* however there are a few RNA properties that are fake sub-structs and
749
* share the pointer with the parent, in those cases this happens 'a.b == a'
750
* see: r43352 for example.
752
* So compare the 'ptr->type' as well to avoid this problem.
753
* It's highly unlikely this would happen that 'ptr->data' and 'ptr->prop' would match,
754
* but _not_ 'ptr->type' but include this check for completeness.
757
static int pyrna_struct_compare(BPy_StructRNA *a, BPy_StructRNA *b)
759
return (((a->ptr.data == b->ptr.data) &&
760
(a->ptr.type == b->ptr.type)) ? 0 : -1);
763
static int pyrna_prop_compare(BPy_PropertyRNA *a, BPy_PropertyRNA *b)
765
return (((a->prop == b->prop) &&
766
(a->ptr.data == b->ptr.data) &&
767
(a->ptr.type == b->ptr.type)) ? 0 : -1);
360
770
static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
363
int ok= -1; /* zero is true */
773
int ok = -1; /* zero is true */
365
775
if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b))
366
ok= pyrna_struct_compare((BPy_StructRNA *)a, (BPy_StructRNA *)b);
776
ok = pyrna_struct_compare((BPy_StructRNA *)a, (BPy_StructRNA *)b);
370
ok = !ok; /* pass through */
372
res = ok ? Py_False : Py_True;
780
ok = !ok; /* pass through */
782
res = ok ? Py_False : Py_True;
379
res = Py_NotImplemented;
789
res = Py_NotImplemented;
796
return Py_INCREF(res), res;
390
799
static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
393
int ok= -1; /* zero is true */
802
int ok = -1; /* zero is true */
395
804
if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b))
396
ok= pyrna_prop_compare((BPy_PropertyRNA *)a, (BPy_PropertyRNA *)b);
805
ok = pyrna_prop_compare((BPy_PropertyRNA *)a, (BPy_PropertyRNA *)b);
400
ok = !ok; /* pass through */
402
res = ok ? Py_False : Py_True;
809
ok = !ok; /* pass through */
811
res = ok ? Py_False : Py_True;
409
res = Py_NotImplemented;
818
res = Py_NotImplemented;
825
return Py_INCREF(res), res;
420
828
/*----------------------repr--------------------------------------------*/
421
static PyObject *pyrna_struct_repr( BPy_StructRNA *self )
829
static PyObject *pyrna_struct_str(BPy_StructRNA *self)
834
if (!PYRNA_STRUCT_IS_VALID(self)) {
835
return PyUnicode_FromFormat("<bpy_struct, %.200s dead>",
836
Py_TYPE(self)->tp_name);
426
839
/* print name if available */
427
name= RNA_struct_name_get_alloc(&self->ptr, NULL, FALSE);
429
pyob= PyUnicode_FromFormat( "<bpy_struct, %.200s(\"%.200s\")>", RNA_struct_identifier(self->ptr.type), name);
434
return PyUnicode_FromFormat( "<bpy_struct, %.200s at %p>", RNA_struct_identifier(self->ptr.type), self->ptr.data);
437
static PyObject *pyrna_prop_repr( BPy_PropertyRNA *self )
840
name = RNA_struct_name_get_alloc(&self->ptr, NULL, 0, NULL);
842
ret = PyUnicode_FromFormat("<bpy_struct, %.200s(\"%.200s\")>",
843
RNA_struct_identifier(self->ptr.type),
845
MEM_freeN((void *)name);
849
return PyUnicode_FromFormat("<bpy_struct, %.200s at %p>",
850
RNA_struct_identifier(self->ptr.type),
854
static PyObject *pyrna_struct_repr(BPy_StructRNA *self)
856
ID *id = self->ptr.id.data;
860
if (id == NULL || !PYRNA_STRUCT_IS_VALID(self))
861
return pyrna_struct_str(self); /* fallback */
863
tmp_str = PyUnicode_FromString(id->name + 2);
865
if (RNA_struct_is_ID(self->ptr.type)) {
866
ret = PyUnicode_FromFormat("bpy.data.%s[%R]",
867
BKE_idcode_to_name_plural(GS(id->name)),
872
path = RNA_path_from_ID_to_struct(&self->ptr);
874
if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
875
ret = PyUnicode_FromFormat("bpy.data...%s",
879
ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
880
BKE_idcode_to_name_plural(GS(id->name)),
885
MEM_freeN((void *)path);
887
else { /* cant find, print something sane */
888
ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
889
BKE_idcode_to_name_plural(GS(id->name)),
891
RNA_struct_identifier(self->ptr.type));
900
static PyObject *pyrna_prop_str(BPy_PropertyRNA *self)
442
const char *type_id= NULL;
443
char type_fmt[64]= "";
444
int type= RNA_property_type(self->prop);
446
if(RNA_enum_id_from_value(property_type_items, type, &type_id)==0) {
447
PyErr_SetString(PyExc_SystemError, "could not use property type, internal error"); /* should never happen */
905
const char *type_id = NULL;
906
char type_fmt[64] = "";
909
PYRNA_PROP_CHECK_OBJ(self);
911
type = RNA_property_type(self->prop);
913
if (RNA_enum_id_from_value(property_type_items, type, &type_id) == 0) {
914
PyErr_SetString(PyExc_RuntimeError, "could not use property type, internal error"); /* should never happen */
451
918
/* this should never fail */
455
while ( (*c++= tolower(*type_id++)) ) {} ;
457
if(type==PROP_COLLECTION) {
458
len= pyrna_prop_collection_length(self);
459
} else if (RNA_property_array_check(&self->ptr, self->prop)) {
460
len= pyrna_prop_array_length(self);
922
while ((*c++ = tolower(*type_id++))) {}
924
if (type == PROP_COLLECTION) {
925
len = pyrna_prop_collection_length(self);
927
else if (RNA_property_array_check(self->prop)) {
928
len = pyrna_prop_array_length((BPy_PropertyArrayRNA *)self);
464
932
sprintf(--c, "[%d]", len);
467
935
/* if a pointer, try to print name of pointer target too */
468
if(RNA_property_type(self->prop) == PROP_POINTER) {
469
ptr= RNA_property_pointer_get(&self->ptr, self->prop);
470
name= RNA_struct_name_get_alloc(&ptr, NULL, FALSE);
473
pyob= PyUnicode_FromFormat( "<bpy_%.200s, %.200s.%.200s(\"%.200s\")>", type_fmt, RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
479
return PyUnicode_FromFormat( "<bpy_%.200s, %.200s.%.200s>", type_fmt, RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
482
static long pyrna_struct_hash( BPy_StructRNA *self )
936
if (type == PROP_POINTER) {
937
ptr = RNA_property_pointer_get(&self->ptr, self->prop);
938
name = RNA_struct_name_get_alloc(&ptr, NULL, 0, NULL);
941
ret = PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s(\"%.200s\")>",
943
RNA_struct_identifier(self->ptr.type),
944
RNA_property_identifier(self->prop),
946
MEM_freeN((void *)name);
950
if (type == PROP_COLLECTION) {
952
if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
953
return PyUnicode_FromFormat("<bpy_%.200s, %.200s>",
955
RNA_struct_identifier(r_ptr.type));
959
return PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s>",
961
RNA_struct_identifier(self->ptr.type),
962
RNA_property_identifier(self->prop));
965
static PyObject *pyrna_prop_repr(BPy_PropertyRNA *self)
967
ID *id = self->ptr.id.data;
972
PYRNA_PROP_CHECK_OBJ(self);
975
return pyrna_prop_str(self); /* fallback */
977
tmp_str = PyUnicode_FromString(id->name + 2);
979
path = RNA_path_from_ID_to_property(&self->ptr, self->prop);
981
if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
982
ret = PyUnicode_FromFormat("bpy.data...%s",
986
ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
987
BKE_idcode_to_name_plural(GS(id->name)),
992
MEM_freeN((void *)path);
994
else { /* cant find, print something sane */
995
ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
996
BKE_idcode_to_name_plural(GS(id->name)),
998
RNA_property_identifier(self->prop));
1007
static PyObject *pyrna_func_repr(BPy_FunctionRNA *self)
1009
return PyUnicode_FromFormat("<%.200s %.200s.%.200s()>",
1010
Py_TYPE(self)->tp_name,
1011
RNA_struct_identifier(self->ptr.type),
1012
RNA_function_identifier(self->func));
1016
static Py_hash_t pyrna_struct_hash(BPy_StructRNA *self)
484
1018
return _Py_HashPointer(self->ptr.data);
487
1021
/* from python's meth_hash v3.1.2 */
488
1022
static long pyrna_prop_hash(BPy_PropertyRNA *self)
491
1025
if (self->ptr.data == NULL)
799
if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
1461
if (error_val == 0 && totkw > 0) { /* some keywords were given that were not used :/ */
800
1462
PyObject *key, *value;
801
1463
Py_ssize_t pos = 0;
803
1465
while (PyDict_Next(kw, &pos, &key, &value)) {
804
arg_name= _PyUnicode_AsString(key);
1466
arg_name = _PyUnicode_AsString(key);
805
1467
if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
809
PyErr_Format( PyExc_TypeError, "%.200s: keyword \"%.200s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1471
PyErr_Format(PyExc_TypeError,
1472
"%.200s: keyword \"%.200s\" unrecognized",
1473
error_prefix, arg_name ? arg_name : "<UNKNOWN>");
813
1477
return error_val;
816
static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw);
818
static PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func)
1481
static PyObject *pyrna_func_to_py(PointerRNA *ptr, FunctionRNA *func)
820
static PyMethodDef func_meth = {"<generic rna function>", (PyCFunction)pyrna_func_call, METH_VARARGS|METH_KEYWORDS, "python rna function"};
825
PyErr_Format( PyExc_RuntimeError, "%.200s: type attempted to get NULL function", RNA_struct_identifier(pyrna->ptr.type));
829
self= PyTuple_New(2);
831
PyTuple_SET_ITEM(self, 0, (PyObject *)pyrna);
834
PyTuple_SET_ITEM(self, 1, PyCapsule_New((void *)func, NULL, NULL));
836
ret= PyCFunction_New(&func_meth, self);
1483
BPy_FunctionRNA *pyfunc = (BPy_FunctionRNA *) PyObject_NEW(BPy_FunctionRNA, &pyrna_func_Type);
1485
pyfunc->func = func;
1486
return (PyObject *)pyfunc;
844
int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, ParameterList *parms, void *data, PyObject *value, const char *error_prefix)
1490
static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix)
846
1492
/* XXX hard limits should be checked here */
847
int type = RNA_property_type(prop);
850
if (RNA_property_array_check(ptr, prop)) {
852
/* char error_str[512]; */
856
if(MatrixObject_Check(value)) {
857
MatrixObject *mat = (MatrixObject*)value;
858
if(!BaseMath_ReadCallback(mat))
860
} else /* continue... */
862
if (!PySequence_Check(value)) {
863
PyErr_Format(PyExc_TypeError, "%.200s RNA array assignment to %.200s.%.200s expected a sequence instead of %.200s instance.", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1493
const int type = RNA_property_type(prop);
1496
if (RNA_property_array_check(prop)) {
866
1497
/* done getting the length */
867
ok= pyrna_py_to_array(ptr, prop, parms, data, value, error_prefix);
870
/* PyErr_Format(PyExc_AttributeError, "%.200s %s", error_prefix, error_str); */
1498
if (pyrna_py_to_array(ptr, prop, data, value, error_prefix) == -1) {
875
1503
/* Normal Property (not an array) */
877
1505
/* see if we can coorce into a python type - PropertyType */
882
/* prefer not to have an exception here
883
* however so many poll functions return None or a valid Object.
884
* its a hassle to convert these into a bool before returning, */
885
if(RNA_property_flag(prop) & PROP_OUTPUT)
886
param = PyObject_IsTrue( value );
888
param = PyLong_AsSsize_t( value );
890
if( param < 0 || param > 1) {
891
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected True/False or 0/1", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
894
if(data) *((int*)data)= param;
895
else RNA_property_boolean_set(ptr, prop, param);
901
int param = PyLong_AsSsize_t(value);
902
if (param==-1 && PyErr_Occurred()) {
903
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected an int type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
906
RNA_property_int_clamp(ptr, prop, ¶m);
907
if(data) *((int*)data)= param;
908
else RNA_property_int_set(ptr, prop, param);
914
float param = PyFloat_AsDouble(value);
915
if (PyErr_Occurred()) {
916
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a float type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
919
RNA_property_float_clamp(ptr, prop, (float *)¶m);
920
if(data) *((float*)data)= param;
921
else RNA_property_float_set(ptr, prop, param);
927
char *param = _PyUnicode_AsString(value);
930
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a string type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
934
if(data) *((char**)data)= param;
935
else RNA_property_string_set(ptr, prop, param);
943
if (PyUnicode_Check(value)) {
944
if (!pyrna_string_to_enum(value, ptr, prop, &val, error_prefix))
947
else if (PyAnySet_Check(value)) {
948
if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1510
/* prefer not to have an exception here
1511
* however so many poll functions return None or a valid Object.
1512
* its a hassle to convert these into a bool before returning, */
1513
if (RNA_property_flag(prop) & PROP_OUTPUT)
1514
param = PyObject_IsTrue(value);
1516
param = PyLong_AsLong(value);
1519
PyErr_Format(PyExc_TypeError,
1520
"%.200s %.200s.%.200s expected True/False or 0/1, not %.200s",
1521
error_prefix, RNA_struct_identifier(ptr->type),
1522
RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1526
if (data) *((int *)data) = param;
1527
else RNA_property_boolean_set(ptr, prop, param);
1534
long param = PyLong_AsLongAndOverflow(value, &overflow);
1535
if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
1536
PyErr_Format(PyExc_ValueError,
1537
"%.200s %.200s.%.200s value not in 'int' range "
1538
"(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
1539
error_prefix, RNA_struct_identifier(ptr->type),
1540
RNA_property_identifier(prop));
1543
else if (param == -1 && PyErr_Occurred()) {
1544
PyErr_Format(PyExc_TypeError,
1545
"%.200s %.200s.%.200s expected an int type, not %.200s",
1546
error_prefix, RNA_struct_identifier(ptr->type),
1547
RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1551
int param_i = (int)param;
1552
RNA_property_int_clamp(ptr, prop, ¶m_i);
1553
if (data) *((int *)data) = param_i;
1554
else RNA_property_int_set(ptr, prop, param_i);
1560
float param = PyFloat_AsDouble(value);
1561
if (PyErr_Occurred()) {
1562
PyErr_Format(PyExc_TypeError,
1563
"%.200s %.200s.%.200s expected a float type, not %.200s",
1564
error_prefix, RNA_struct_identifier(ptr->type),
1565
RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1569
RNA_property_float_clamp(ptr, prop, (float *)¶m);
1570
if (data) *((float *)data) = param;
1571
else RNA_property_float_set(ptr, prop, param);
1577
const int subtype = RNA_property_subtype(prop);
1580
if (subtype == PROP_BYTESTRING) {
1584
param = PyBytes_AsString(value);
1586
if (param == NULL) {
1587
if (PyBytes_Check(value)) {
1588
/* there was an error assigning a string type,
1589
* rather than setting a new error, prefix the existing one
1591
PyC_Err_Format_Prefix(PyExc_TypeError,
1592
"%.200s %.200s.%.200s error assigning bytes",
1593
error_prefix, RNA_struct_identifier(ptr->type),
1594
RNA_property_identifier(prop));
1597
PyErr_Format(PyExc_TypeError,
1598
"%.200s %.200s.%.200s expected a bytes type, not %.200s",
1599
error_prefix, RNA_struct_identifier(ptr->type),
1600
RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1606
/* same as unicode */
1607
if (data) *((char **)data) = (char *)param; /*XXX, this is suspect but needed for function calls, need to see if theres a better way */
1608
else RNA_property_string_set(ptr, prop, param);
1613
/* Unicode String */
1615
#ifdef USE_STRING_COERCE
1616
PyObject *value_coerce = NULL;
1617
if (ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1618
/* TODO, get size */
1619
param = PyC_UnicodeAsByte(value, &value_coerce);
1622
param = _PyUnicode_AsString(value);
1623
#ifdef WITH_INTERNATIONAL
1624
if (subtype == PROP_TRANSLATE) {
1625
param = IFACE_(param);
1627
#endif // WITH_INTERNATIONAL
1630
#else // USE_STRING_COERCE
1631
param = _PyUnicode_AsString(value);
1632
#endif // USE_STRING_COERCE
1634
if (param == NULL) {
1635
if (PyUnicode_Check(value)) {
1636
/* there was an error assigning a string type,
1637
* rather than setting a new error, prefix the existing one
1639
PyC_Err_Format_Prefix(PyExc_TypeError,
1640
"%.200s %.200s.%.200s error assigning string",
1641
error_prefix, RNA_struct_identifier(ptr->type),
1642
RNA_property_identifier(prop));
1645
PyErr_Format(PyExc_TypeError,
1646
"%.200s %.200s.%.200s expected a string type, not %.200s",
1647
error_prefix, RNA_struct_identifier(ptr->type),
1648
RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1655
if (data) *((char **)data) = (char *)param; /*XXX, this is suspect but needed for function calls, need to see if theres a better way */
1656
else RNA_property_string_set(ptr, prop, param);
1658
#ifdef USE_STRING_COERCE
1659
Py_XDECREF(value_coerce);
1660
#endif // USE_STRING_COERCE
1668
/* type checkins is done by each function */
1669
if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
949
1670
/* set of enum items, concatenate all values with OR */
950
if(pyrna_prop_to_enum_bitfield(ptr, prop, value, &val, error_prefix) < 0)
954
PyErr_Format(PyExc_TypeError, "%.200s, %.200s.%.200s is not a bitflag enum type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
959
char *enum_str= pyrna_enum_as_string(ptr, prop);
960
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a string enum or a set of strings in (%.2000s)", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), enum_str);
965
if(data) *((int*)data)= val;
966
else RNA_property_enum_set(ptr, prop, val);
972
StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
973
int flag = RNA_property_flag(prop);
975
/* if property is an OperatorProperties pointer and value is a map, forward back to pyrna_pydict_to_props */
976
if (RNA_struct_is_a(ptype, &RNA_OperatorProperties) && PyDict_Check(value)) {
977
PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
978
return pyrna_pydict_to_props(&opptr, value, 0, error_prefix);
981
if(!BPy_StructRNA_Check(value) && value != Py_None) {
982
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), RNA_struct_identifier(ptype));
984
} else if((flag & PROP_NEVER_NULL) && value == Py_None) {
985
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s does not support a 'None' assignment %.200s type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), RNA_struct_identifier(ptype));
987
} else if(value != Py_None && ((flag & PROP_ID_SELF_CHECK) && ptr->id.data == ((BPy_StructRNA*)value)->ptr.id.data)) {
988
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s ID type does not support assignment to its self", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
991
BPy_StructRNA *param= (BPy_StructRNA*)value;
992
int raise_error= FALSE;
995
if(flag & PROP_RNAPTR) {
997
memset(data, 0, sizeof(PointerRNA));
999
*((PointerRNA*)data)= param->ptr;
1001
else if(value == Py_None) {
1002
*((void**)data)= NULL;
1004
else if(RNA_struct_is_a(param->ptr.type, ptype)) {
1005
*((void**)data)= param->ptr.data;
1012
/* data==NULL, assign to RNA */
1013
if(value == Py_None) {
1014
PointerRNA valueptr;
1015
memset(&valueptr, 0, sizeof(valueptr));
1016
RNA_property_pointer_set(ptr, prop, valueptr);
1018
else if(RNA_struct_is_a(param->ptr.type, ptype)) {
1019
RNA_property_pointer_set(ptr, prop, param->ptr);
1671
if (pyrna_prop_to_enum_bitfield(ptr, prop, value, &val, error_prefix) < 0) {
1676
/* simple enum string */
1677
if (pyrna_string_to_enum(value, ptr, prop, &val, error_prefix) < 0) {
1682
if (data) *((int *)data) = val;
1683
else RNA_property_enum_set(ptr, prop, val);
1689
PyObject *value_new = NULL;
1691
StructRNA *ptr_type = RNA_property_pointer_type(ptr, prop);
1692
int flag = RNA_property_flag(prop);
1694
/* this is really nasty!, so we can fake the operator having direct properties eg:
1695
* layout.prop(self, "filepath")
1696
* ... which infact should be
1697
* layout.prop(self.properties, "filepath")
1699
* we need to do this trick.
1700
* if the prop is not an operator type and the pyobject is an operator,
1701
* use its properties in place of its self.
1703
* this is so bad that its almost a good reason to do away with fake 'self.properties -> self' class mixing
1704
* if this causes problems in the future it should be removed.
1706
if ((ptr_type == &RNA_AnyType) &&
1707
(BPy_StructRNA_Check(value)) &&
1708
(RNA_struct_is_a(((BPy_StructRNA *)value)->ptr.type, &RNA_Operator)))
1710
value = PyObject_GetAttrString(value, "properties");
1715
/* if property is an OperatorProperties pointer and value is a map,
1716
* forward back to pyrna_pydict_to_props */
1717
if (RNA_struct_is_a(ptr_type, &RNA_OperatorProperties) && PyDict_Check(value)) {
1718
PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
1719
return pyrna_pydict_to_props(&opptr, value, 0, error_prefix);
1722
/* another exception, allow to pass a collection as an RNA property */
1723
if (Py_TYPE(value) == &pyrna_prop_collection_Type) { /* ok to ignore idprop collections */
1725
BPy_PropertyRNA *value_prop = (BPy_PropertyRNA *)value;
1726
if (RNA_property_collection_type_get(&value_prop->ptr, value_prop->prop, &c_ptr)) {
1727
value = pyrna_struct_CreatePyObject(&c_ptr);
1731
PyErr_Format(PyExc_TypeError,
1732
"%.200s %.200s.%.200s collection has no type, "
1733
"cant be used as a %.200s type",
1734
error_prefix, RNA_struct_identifier(ptr->type),
1735
RNA_property_identifier(prop), RNA_struct_identifier(ptr_type));
1740
if (!BPy_StructRNA_Check(value) && value != Py_None) {
1741
PyErr_Format(PyExc_TypeError,
1742
"%.200s %.200s.%.200s expected a %.200s type, not %.200s",
1743
error_prefix, RNA_struct_identifier(ptr->type),
1744
RNA_property_identifier(prop), RNA_struct_identifier(ptr_type),
1745
Py_TYPE(value)->tp_name);
1746
Py_XDECREF(value_new); return -1;
1748
else if ((flag & PROP_NEVER_NULL) && value == Py_None) {
1749
PyErr_Format(PyExc_TypeError,
1750
"%.200s %.200s.%.200s does not support a 'None' assignment %.200s type",
1751
error_prefix, RNA_struct_identifier(ptr->type),
1752
RNA_property_identifier(prop), RNA_struct_identifier(ptr_type));
1753
Py_XDECREF(value_new); return -1;
1755
else if ((value != Py_None) &&
1756
((flag & PROP_ID_SELF_CHECK) && ptr->id.data == ((BPy_StructRNA *)value)->ptr.id.data))
1758
PyErr_Format(PyExc_TypeError,
1759
"%.200s %.200s.%.200s ID type does not support assignment to its self",
1760
error_prefix, RNA_struct_identifier(ptr->type),
1761
RNA_property_identifier(prop));
1762
Py_XDECREF(value_new); return -1;
1765
BPy_StructRNA *param = (BPy_StructRNA *)value;
1766
int raise_error = FALSE;
1769
if (flag & PROP_RNAPTR) {
1770
if (value == Py_None)
1771
memset(data, 0, sizeof(PointerRNA));
1773
*((PointerRNA *)data) = param->ptr;
1775
else if (value == Py_None) {
1776
*((void **)data) = NULL;
1778
else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
1779
*((void **)data) = param->ptr.data;
1786
/* data == NULL, assign to RNA */
1787
if (value == Py_None) {
1788
PointerRNA valueptr = {{NULL}};
1789
RNA_property_pointer_set(ptr, prop, valueptr);
1791
else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
1792
RNA_property_pointer_set(ptr, prop, param->ptr);
1796
RNA_pointer_create(NULL, ptr_type, NULL, &tmp);
1797
PyErr_Format(PyExc_TypeError,
1798
"%.200s %.200s.%.200s expected a %.200s type. not %.200s",
1799
error_prefix, RNA_struct_identifier(ptr->type),
1800
RNA_property_identifier(prop), RNA_struct_identifier(tmp.type),
1801
RNA_struct_identifier(param->ptr.type));
1802
Py_XDECREF(value_new); return -1;
1022
1807
PointerRNA tmp;
1023
RNA_pointer_create(NULL, ptype, NULL, &tmp);
1024
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), RNA_struct_identifier(tmp.type));
1031
RNA_pointer_create(NULL, ptype, NULL, &tmp);
1032
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), RNA_struct_identifier(tmp.type));
1038
case PROP_COLLECTION:
1044
CollectionPointerLink *link;
1046
lb= (data)? (ListBase*)data: NULL;
1048
/* convert a sequence of dict's into a collection */
1049
if(!PySequence_Check(value)) {
1050
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a sequence for an RNA collection, found a '%.200s' instead", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1054
seq_len = PySequence_Length(value);
1055
for(i=0; i<seq_len; i++) {
1056
item= PySequence_GetItem(value, i);
1059
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s failed to get sequence index '%d' for an RNA collection", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), i);
1064
if(PyDict_Check(item)==0) {
1065
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a each sequence member to be a dict for an RNA collection, found a '%.200s' instead", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), Py_TYPE(item)->tp_name);
1071
link= MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
1073
BLI_addtail(lb, link);
1076
RNA_property_collection_add(ptr, prop, &itemptr);
1078
if(pyrna_pydict_to_props(&itemptr, item, 1, "Converting a python list to an RNA collection")==-1) {
1079
PyObject *msg= BPY_exception_buffer();
1080
char *msg_char= _PyUnicode_AsString(msg);
1082
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s error converting a member of a collection from a dicts into an RNA collection, failed with: %s", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), msg_char);
1808
RNA_pointer_create(NULL, ptr_type, NULL, &tmp);
1809
PyErr_Format(PyExc_TypeError,
1810
"%.200s %.200s.%.200s expected a %.200s type, not %.200s",
1811
error_prefix, RNA_struct_identifier(ptr->type),
1812
RNA_property_identifier(prop), RNA_struct_identifier(tmp.type),
1813
RNA_struct_identifier(param->ptr.type));
1814
Py_XDECREF(value_new); return -1;
1818
Py_XDECREF(value_new);
1822
case PROP_COLLECTION:
1824
Py_ssize_t seq_len, i;
1828
CollectionPointerLink *link;
1830
lb = (data) ? (ListBase *)data : NULL;
1832
/* convert a sequence of dict's into a collection */
1833
if (!PySequence_Check(value)) {
1834
PyErr_Format(PyExc_TypeError,
1835
"%.200s %.200s.%.200s expected a sequence for an RNA collection, not %.200s",
1836
error_prefix, RNA_struct_identifier(ptr->type),
1837
RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1841
seq_len = PySequence_Size(value);
1842
for (i = 0; i < seq_len; i++) {
1843
item = PySequence_GetItem(value, i);
1846
PyErr_Format(PyExc_TypeError,
1847
"%.200s %.200s.%.200s failed to get sequence index '%d' for an RNA collection",
1848
error_prefix, RNA_struct_identifier(ptr->type),
1849
RNA_property_identifier(prop), i);
1854
if (PyDict_Check(item) == 0) {
1855
PyErr_Format(PyExc_TypeError,
1856
"%.200s %.200s.%.200s expected a each sequence "
1857
"member to be a dict for an RNA collection, not %.200s",
1858
error_prefix, RNA_struct_identifier(ptr->type),
1859
RNA_property_identifier(prop), Py_TYPE(item)->tp_name);
1865
link = MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
1866
link->ptr = itemptr;
1867
BLI_addtail(lb, link);
1870
RNA_property_collection_add(ptr, prop, &itemptr);
1872
if (pyrna_pydict_to_props(&itemptr, item, 1, "Converting a python list to an RNA collection") == -1) {
1873
PyObject *msg = PyC_ExceptionBuffer();
1874
const char *msg_char = _PyUnicode_AsString(msg);
1876
PyErr_Format(PyExc_TypeError,
1877
"%.200s %.200s.%.200s error converting a member of a collection "
1878
"from a dicts into an RNA collection, failed with: %s",
1879
error_prefix, RNA_struct_identifier(ptr->type),
1880
RNA_property_identifier(prop), msg_char);
1084
1886
Py_DECREF(item);
1094
PyErr_Format(PyExc_AttributeError, "%.200s %.200s.%.200s unknown property type (pyrna_py_to_prop)", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
1892
PyErr_Format(PyExc_AttributeError,
1893
"%.200s %.200s.%.200s unknown property type (pyrna_py_to_prop)",
1894
error_prefix, RNA_struct_identifier(ptr->type),
1895
RNA_property_identifier(prop));
1100
1901
/* Run rna property functions */
1101
RNA_property_update(BPy_GetContext(), ptr, prop);
1902
if (RNA_property_update_check(prop)) {
1903
RNA_property_update(BPy_GetContext(), ptr, prop);
1106
static PyObject * pyrna_prop_to_py_index(BPy_PropertyRNA *self, int index)
1909
static PyObject *pyrna_prop_array_to_py_index(BPy_PropertyArrayRNA *self, int index)
1911
PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
1108
1912
return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
1111
static int pyrna_py_to_prop_index(BPy_PropertyRNA *self, int index, PyObject *value)
1915
static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, PyObject *value)
1115
PointerRNA *ptr= &self->ptr;
1116
PropertyRNA *prop= self->prop;
1117
int type = RNA_property_type(prop);
1918
PointerRNA *ptr = &self->ptr;
1919
PropertyRNA *prop = self->prop;
1119
totdim= RNA_property_array_dimension(ptr, prop, NULL);
1921
const int totdim = RNA_property_array_dimension(ptr, prop, NULL);
1121
1923
if (totdim > 1) {
1122
1924
/* char error_str[512]; */
1123
if (!pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "")) {
1124
/* PyErr_SetString(PyExc_AttributeError, error_str); */
1925
if (pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "") == -1) {
1129
/* see if we can coorce into a python type - PropertyType */
1931
/* see if we can coerce into a python type - PropertyType */
1932
switch (RNA_property_type(prop)) {
1133
int param = PyLong_AsSsize_t( value );
1135
if( param < 0 || param > 1) {
1935
int param = PyLong_AsLong(value);
1937
if (param < 0 || param > 1) {
1136
1938
PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
1139
1942
RNA_property_boolean_set_index(ptr, prop, index, param);
1145
int param = PyLong_AsSsize_t(value);
1146
if (param==-1 && PyErr_Occurred()) {
1948
int param = PyLong_AsLong(value);
1949
if (param == -1 && PyErr_Occurred()) {
1147
1950
PyErr_SetString(PyExc_TypeError, "expected an int type");
1150
1954
RNA_property_int_clamp(ptr, prop, ¶m);
1151
1955
RNA_property_int_set_index(ptr, prop, index, param);
1157
1961
float param = PyFloat_AsDouble(value);
1158
1962
if (PyErr_Occurred()) {
1159
1963
PyErr_SetString(PyExc_TypeError, "expected a float type");
1162
1967
RNA_property_float_clamp(ptr, prop, ¶m);
1163
1968
RNA_property_float_set_index(ptr, prop, index, param);
1168
PyErr_SetString(PyExc_AttributeError, "not an array type");
1973
PyErr_SetString(PyExc_AttributeError, "not an array type");
1174
1979
/* Run rna property functions */
1175
RNA_property_update(BPy_GetContext(), ptr, prop);
1980
if (RNA_property_update_check(prop)) {
1981
RNA_property_update(BPy_GetContext(), ptr, prop);
1180
1987
//---------------sequence-------------------------------------------
1181
static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self)
1988
static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self)
1990
PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
1183
1992
if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1)
1184
1993
return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
1186
1995
return RNA_property_array_length(&self->ptr, self->prop);
1189
static Py_ssize_t pyrna_prop_collection_length( BPy_PropertyRNA *self )
1998
static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self)
2000
PYRNA_PROP_CHECK_INT(self);
1191
2002
return RNA_property_collection_length(&self->ptr, self->prop);
2005
/* bool functions are for speed, so we can avoid getting the length
2006
* of 1000's of items in a linked list for eg. */
2007
static int pyrna_prop_array_bool(BPy_PropertyRNA *self)
2009
PYRNA_PROP_CHECK_INT(self);
2011
return RNA_property_array_length(&self->ptr, self->prop) ? 1 : 0;
2014
static int pyrna_prop_collection_bool(BPy_PropertyRNA *self)
2016
/* no callback defined, just iterate and find the nth item */
2017
CollectionPropertyIterator iter;
2020
PYRNA_PROP_CHECK_INT(self);
2022
RNA_property_collection_begin(&self->ptr, self->prop, &iter);
2024
RNA_property_collection_end(&iter);
2029
/* notice getting the length of the collection is avoided unless negative
2030
* index is used or to detect internal error with a valid index.
2031
* This is done for faster lookups. */
2032
#define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err) \
2034
keynum_abs += RNA_property_collection_length(&self->ptr, self->prop); \
2035
if (keynum_abs < 0) { \
2036
PyErr_Format(PyExc_IndexError, \
2037
"bpy_prop_collection[%d]: out of range.", keynum); \
1194
2043
/* internal use only */
1195
2044
static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
1197
2046
PointerRNA newptr;
1198
int len= RNA_property_collection_length(&self->ptr, self->prop);
1200
if(keynum < 0) keynum += len;
1202
if(keynum >= 0 && keynum < len) {
1203
if(RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr)) {
1204
return pyrna_struct_CreatePyObject(&newptr);
1206
PyErr_Format(PyExc_IndexError, "bpy_prop_collection[index]: index %d could not be found", keynum);
2047
Py_ssize_t keynum_abs = keynum;
2049
PYRNA_PROP_CHECK_OBJ(self);
2051
PYRNA_PROP_COLLECTION_ABS_INDEX(NULL);
2053
if (RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum_abs, &newptr)) {
2054
return pyrna_struct_CreatePyObject(&newptr);
2057
const int len = RNA_property_collection_length(&self->ptr, self->prop);
2058
if (keynum_abs >= len) {
2059
PyErr_Format(PyExc_IndexError,
2060
"bpy_prop_collection[index]: "
2061
"index %d out of range, size %d", keynum, len);
2064
PyErr_Format(PyExc_RuntimeError,
2065
"bpy_prop_collection[index]: internal error, "
2066
"valid index %d given in %d sized collection but value not found",
1209
PyErr_Format(PyExc_IndexError, "bpy_prop_collection[index]: index %d out of range", keynum);
1213
static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyRNA *self, int keynum)
1215
int len= pyrna_prop_array_length(self);
1217
if(keynum < 0) keynum += len;
1219
if(keynum >= 0 && keynum < len)
1220
return pyrna_prop_to_py_index(self, keynum);
1222
PyErr_Format(PyExc_IndexError, "bpy_prop_array[index]: index %d out of range", keynum);
1226
static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, char *keyname)
2074
/* values type must have been already checked */
2075
static int pyrna_prop_collection_ass_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum, PyObject *value)
2077
Py_ssize_t keynum_abs = keynum;
2078
const PointerRNA *ptr = (value == Py_None) ? (&PointerRNA_NULL) : &((BPy_StructRNA *)value)->ptr;
2080
PYRNA_PROP_CHECK_INT(self);
2082
PYRNA_PROP_COLLECTION_ABS_INDEX(-1);
2084
if (RNA_property_collection_assign_int(&self->ptr, self->prop, keynum_abs, ptr) == 0) {
2085
const int len = RNA_property_collection_length(&self->ptr, self->prop);
2086
if (keynum_abs >= len) {
2087
PyErr_Format(PyExc_IndexError,
2088
"bpy_prop_collection[index] = value: "
2089
"index %d out of range, size %d", keynum, len);
2093
PyErr_Format(PyExc_IndexError,
2094
"bpy_prop_collection[index] = value: "
2095
"failed assignment (unknown reason)", keynum);
2103
static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
2107
PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2109
len = pyrna_prop_array_length(self);
2111
if (keynum < 0) keynum += len;
2113
if (keynum >= 0 && keynum < len)
2114
return pyrna_prop_array_to_py_index(self, keynum);
2116
PyErr_Format(PyExc_IndexError,
2117
"bpy_prop_array[index]: index %d out of range", keynum);
2121
static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, const char *keyname)
1228
2123
PointerRNA newptr;
1229
if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
2125
PYRNA_PROP_CHECK_OBJ(self);
2127
if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
1230
2128
return pyrna_struct_CreatePyObject(&newptr);
1232
2130
PyErr_Format(PyExc_KeyError, "bpy_prop_collection[key]: key \"%.200s\" not found", keyname);
1235
2133
/* static PyObject *pyrna_prop_array_subscript_str(BPy_PropertyRNA *self, char *keyname) */
1237
static PyObject *pyrna_prop_collection_subscript_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
1240
PyObject *list = PyList_New(stop - start);
1243
start = MIN2(start,stop); /* values are clamped from */
1245
for(count = start; count < stop; count++) {
1246
if(RNA_property_collection_lookup_int(ptr, prop, count - start, &newptr)) {
1247
PyList_SET_ITEM(list, count - start, pyrna_struct_CreatePyObject(&newptr));
1252
PyErr_SetString(PyExc_RuntimeError, "error getting an rna struct from a collection");
2135
/* special case: bpy.data.objects["some_id_name", "//some_lib_name.blend"]
2136
* also for: bpy.data.objects.get(("some_id_name", "//some_lib_name.blend"), fallback)
2139
* error codes since this is not to be called directly from python,
2140
* this matches pythons __contains__ values capi.
2144
int pyrna_prop_collection_subscript_str_lib_pair_ptr(BPy_PropertyRNA *self, PyObject *key,
2145
const char *err_prefix, const short err_not_found,
2151
/* first validate the args, all we know is that they are a tuple */
2152
if (PyTuple_GET_SIZE(key) != 2) {
2153
PyErr_Format(PyExc_KeyError,
2154
"%s: tuple key must be a pair, not size %d",
2155
err_prefix, PyTuple_GET_SIZE(key));
2158
else if (self->ptr.type != &RNA_BlendData) {
2159
PyErr_Format(PyExc_KeyError,
2160
"%s: is only valid for bpy.data collections, not %.200s",
2161
err_prefix, RNA_struct_identifier(self->ptr.type));
2164
else if ((keyname = _PyUnicode_AsString(PyTuple_GET_ITEM(key, 0))) == NULL) {
2165
PyErr_Format(PyExc_KeyError,
2166
"%s: id must be a string, not %.200s",
2167
err_prefix, Py_TYPE(PyTuple_GET_ITEM(key, 0))->tp_name);
2171
PyObject *keylib = PyTuple_GET_ITEM(key, 1);
2175
if (keylib == Py_None) {
2178
else if (PyUnicode_Check(keylib)) {
2179
Main *bmain = self->ptr.data;
2180
const char *keylib_str = _PyUnicode_AsString(keylib);
2181
lib = BLI_findstring(&bmain->library, keylib_str, offsetof(Library, name));
2183
if (err_not_found) {
2184
PyErr_Format(PyExc_KeyError,
2185
"%s: lib name '%.240s' "
2186
"does not reference a valid library",
2187
err_prefix, keylib_str);
2197
PyErr_Format(PyExc_KeyError,
2198
"%s: lib must be a sting or None, not %.200s",
2199
err_prefix, Py_TYPE(keylib)->tp_name);
2203
/* lib is either a valid poniter or NULL,
2204
* either way can do direct comparison with id.lib */
2206
RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2207
ID *id = itemptr.data; /* always an ID */
2208
if (id->lib == lib && (strncmp(keyname, id->name + 2, sizeof(id->name) - 2) == 0)) {
2218
/* we may want to fail silently as with collection.get() */
2219
if ((found == FALSE) && err_not_found) {
2220
/* only runs for getitem access so use fixed string */
2221
PyErr_SetString(PyExc_KeyError,
2222
"bpy_prop_collection[key, lib]: not found");
2226
return found; /* 1 / 0, no exception */
2231
static PyObject *pyrna_prop_collection_subscript_str_lib_pair(BPy_PropertyRNA *self, PyObject *key,
2232
const char *err_prefix, const short err_not_found)
2235
const int contains = pyrna_prop_collection_subscript_str_lib_pair_ptr(self, key, err_prefix, err_not_found, &ptr);
2237
if (contains == 1) {
2238
return pyrna_struct_CreatePyObject(&ptr);
2246
static PyObject *pyrna_prop_collection_subscript_slice(BPy_PropertyRNA *self, Py_ssize_t start, Py_ssize_t stop)
2248
CollectionPropertyIterator rna_macro_iter;
2254
PYRNA_PROP_CHECK_OBJ(self);
2256
list = PyList_New(0);
2258
/* first loop up-until the start */
2259
for (RNA_property_collection_begin(&self->ptr, self->prop, &rna_macro_iter);
2260
rna_macro_iter.valid;
2261
RNA_property_collection_next(&rna_macro_iter))
2263
/* PointerRNA itemptr = rna_macro_iter.ptr; */
2264
if (count == start) {
2270
/* add items until stop */
2271
for (; rna_macro_iter.valid;
2272
RNA_property_collection_next(&rna_macro_iter))
2274
item = pyrna_struct_CreatePyObject(&rna_macro_iter.ptr);
2275
PyList_Append(list, item);
2279
if (count == stop) {
2284
RNA_property_collection_end(&rna_macro_iter);
1260
2289
/* TODO - dimensions
1261
* note: could also use pyrna_prop_to_py_index(self, count) in a loop but its a lot slower
2290
* note: could also use pyrna_prop_array_to_py_index(self, count) in a loop but its a lot slower
1262
2291
* since at the moment it reads (and even allocates) the entire array for each index.
1264
static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
2293
static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop,
2294
Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length)
1266
2296
int count, totdim;
1268
PyObject *list = PyList_New(stop - start);
2299
PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2301
tuple = PyTuple_New(stop - start);
2303
/* PYRNA_PROP_CHECK_OBJ(self); isn't needed, internal use only */
1270
2305
totdim = RNA_property_array_dimension(ptr, prop, NULL);
1272
2307
if (totdim > 1) {
1273
2308
for (count = start; count < stop; count++)
1274
PyList_SET_ITEM(list, count - start, pyrna_prop_to_py_index(self, count));
2309
PyTuple_SET_ITEM(tuple, count - start, pyrna_prop_array_to_py_index(self, count));
1277
2312
switch (RNA_property_type(prop)) {
1280
2315
float values_stack[PYRNA_STACK_ARRAY];
1282
if(length > PYRNA_STACK_ARRAY) { values= PyMem_MALLOC(sizeof(float) * length); }
1283
else { values= values_stack; }
2317
if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(float) * length); }
2318
else { values = values_stack; }
1284
2319
RNA_property_float_get_array(ptr, prop, values);
1286
for(count=start; count<stop; count++)
1287
PyList_SET_ITEM(list, count-start, PyFloat_FromDouble(values[count]));
1289
if(values != values_stack) {
2321
for (count = start; count < stop; count++)
2322
PyTuple_SET_ITEM(tuple, count - start, PyFloat_FromDouble(values[count]));
2324
if (values != values_stack) {
1290
2325
PyMem_FREE(values);
1296
2331
int values_stack[PYRNA_STACK_ARRAY];
1298
if(length > PYRNA_STACK_ARRAY) { values= PyMem_MALLOC(sizeof(int) * length); }
1299
else { values= values_stack; }
2333
if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(int) * length); }
2334
else { values = values_stack; }
1301
2336
RNA_property_boolean_get_array(ptr, prop, values);
1302
for(count=start; count<stop; count++)
1303
PyList_SET_ITEM(list, count-start, PyBool_FromLong(values[count]));
2337
for (count = start; count < stop; count++)
2338
PyTuple_SET_ITEM(tuple, count - start, PyBool_FromLong(values[count]));
1305
if(values != values_stack) {
2340
if (values != values_stack) {
1306
2341
PyMem_FREE(values);
1312
2347
int values_stack[PYRNA_STACK_ARRAY];
1314
if(length > PYRNA_STACK_ARRAY) { values= PyMem_MALLOC(sizeof(int) * length); }
1315
else { values= values_stack; }
2349
if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(int) * length); }
2350
else { values = values_stack; }
1317
2352
RNA_property_int_get_array(ptr, prop, values);
1318
for(count=start; count<stop; count++)
1319
PyList_SET_ITEM(list, count-start, PyLong_FromSsize_t(values[count]));
2353
for (count = start; count < stop; count++)
2354
PyTuple_SET_ITEM(tuple, count - start, PyLong_FromSsize_t(values[count]));
1321
if(values != values_stack) {
2356
if (values != values_stack) {
1322
2357
PyMem_FREE(values);
1327
/* probably will never happen */
1328
PyErr_SetString(PyExc_TypeError, "not an array type");
2362
BLI_assert(!"Invalid array type");
2364
PyErr_SetString(PyExc_TypeError, "not an array type");
1336
2372
static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
2374
PYRNA_PROP_CHECK_OBJ(self);
1338
2376
if (PyUnicode_Check(key)) {
1339
2377
return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
1346
2384
return pyrna_prop_collection_subscript_int(self, i);
1348
2386
else if (PySlice_Check(key)) {
1349
int len= RNA_property_collection_length(&self->ptr, self->prop);
1350
Py_ssize_t start, stop, step, slicelength;
1352
if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1355
if (slicelength <= 0) {
1356
return PyList_New(0);
1358
else if (step == 1) {
1359
return pyrna_prop_collection_subscript_slice(&self->ptr, self->prop, start, stop, len);
2387
PySliceObject *key_slice = (PySliceObject *)key;
2388
Py_ssize_t step = 1;
2390
if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2393
else if (step != 1) {
2394
PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2397
else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2398
return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
1362
PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported with rna");
2401
Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2403
/* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2404
if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2405
if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) return NULL;
2407
if (start < 0 || stop < 0) {
2408
/* only get the length for negative values */
2409
Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2410
if (start < 0) start += len;
2411
if (stop < 0) start += len;
2414
if (stop - start <= 0) {
2415
return PyList_New(0);
2418
return pyrna_prop_collection_subscript_slice(self, start, stop);
2422
else if (PyTuple_Check(key)) {
2423
/* special case, for ID datablocks we */
2424
return pyrna_prop_collection_subscript_str_lib_pair(self, key,
2425
"bpy_prop_collection[id, lib]", TRUE);
1367
PyErr_Format(PyExc_TypeError, "bpy_prop_collection[key]: invalid key, must be a string or an int instead of %.200s instance.", Py_TYPE(key)->tp_name);
2428
PyErr_Format(PyExc_TypeError,
2429
"bpy_prop_collection[key]: invalid key, "
2430
"must be a string or an int, not %.200s",
2431
Py_TYPE(key)->tp_name);
1372
static PyObject *pyrna_prop_array_subscript(BPy_PropertyRNA *self, PyObject *key)
1374
/*if (PyUnicode_Check(key)) {
2436
/* generic check to see if a PyObject is compatible with a collection
2437
* -1 on failure, 0 on success, sets the error */
2438
static int pyrna_prop_collection_type_check(BPy_PropertyRNA *self, PyObject *value)
2440
StructRNA *prop_srna;
2442
if (value == Py_None) {
2443
if (RNA_property_flag(self->prop) & PROP_NEVER_NULL) {
2444
PyErr_Format(PyExc_TypeError,
2445
"bpy_prop_collection[key] = value: invalid, "
2446
"this collection doesn't support None assignment");
2450
return 0; /* None is OK */
2453
else if (BPy_StructRNA_Check(value) == 0) {
2454
PyErr_Format(PyExc_TypeError,
2455
"bpy_prop_collection[key] = value: invalid, "
2456
"expected a StructRNA type or None, not a %.200s",
2457
Py_TYPE(value)->tp_name);
2460
else if ((prop_srna = RNA_property_pointer_type(&self->ptr, self->prop))) {
2461
StructRNA *value_srna = ((BPy_StructRNA *)value)->ptr.type;
2462
if (RNA_struct_is_a(value_srna, prop_srna) == 0) {
2463
PyErr_Format(PyExc_TypeError,
2464
"bpy_prop_collection[key] = value: invalid, "
2465
"expected a '%.200s' type or None, not a '%.200s'",
2466
RNA_struct_identifier(prop_srna),
2467
RNA_struct_identifier(value_srna)
2472
return 0; /* OK, this is the correct type!*/
2476
PyErr_Format(PyExc_TypeError,
2477
"bpy_prop_collection[key] = value: internal error, "
2478
"failed to get the collection type");
2482
/* note: currently this is a copy of 'pyrna_prop_collection_subscript' with
2483
* large blocks commented, we may support slice/key indices later */
2484
static int pyrna_prop_collection_ass_subscript(BPy_PropertyRNA *self, PyObject *key, PyObject *value)
2486
PYRNA_PROP_CHECK_INT(self);
2488
/* validate the assigned value */
2489
if (value == NULL) {
2490
PyErr_SetString(PyExc_TypeError,
2491
"del bpy_prop_collection[key]: not supported");
2494
else if (pyrna_prop_collection_type_check(self, value) == -1) {
2495
return -1; /* exception is set */
2499
if (PyUnicode_Check(key)) {
2500
return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
2504
if (PyIndex_Check(key)) {
2505
Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2506
if (i == -1 && PyErr_Occurred())
2509
return pyrna_prop_collection_ass_subscript_int(self, i, value);
2511
#if 0 /* TODO, fake slice assignment */
2512
else if (PySlice_Check(key)) {
2513
PySliceObject *key_slice = (PySliceObject *)key;
2514
Py_ssize_t step = 1;
2516
if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2519
else if (step != 1) {
2520
PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2523
else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2524
return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2527
Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2529
/* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2530
if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2531
if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) return NULL;
2533
if (start < 0 || stop < 0) {
2534
/* only get the length for negative values */
2535
Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2536
if (start < 0) start += len;
2537
if (stop < 0) start += len;
2540
if (stop - start <= 0) {
2541
return PyList_New(0);
2544
return pyrna_prop_collection_subscript_slice(self, start, stop);
2550
PyErr_Format(PyExc_TypeError,
2551
"bpy_prop_collection[key]: invalid key, "
2552
"must be a string or an int, not %.200s",
2553
Py_TYPE(key)->tp_name);
2558
static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
2560
PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2563
if (PyUnicode_Check(key)) {
1375
2564
return pyrna_prop_array_subscript_str(self, _PyUnicode_AsString(key));
1377
2568
if (PyIndex_Check(key)) {
1378
2569
Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1379
2570
if (i == -1 && PyErr_Occurred())
1381
return pyrna_prop_array_subscript_int(self, PyLong_AsSsize_t(key));
2572
return pyrna_prop_array_subscript_int(self, PyLong_AsLong(key));
1383
2574
else if (PySlice_Check(key)) {
1384
Py_ssize_t start, stop, step, slicelength;
1385
int len = pyrna_prop_array_length(self);
1387
if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1390
if (slicelength <= 0) {
1391
return PyList_New(0);
1393
else if (step == 1) {
1394
return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, start, stop, len);
2575
Py_ssize_t step = 1;
2576
PySliceObject *key_slice = (PySliceObject *)key;
2578
if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2581
else if (step != 1) {
2582
PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported");
2585
else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2586
/* note, no significant advantage with optimizing [:] slice as with collections
2587
* but include here for consistency with collection slice func */
2588
Py_ssize_t len = (Py_ssize_t)pyrna_prop_array_length(self);
2589
return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len);
1397
PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported with rna");
2592
int len = pyrna_prop_array_length(self);
2593
Py_ssize_t start, stop, slicelength;
2595
if (PySlice_GetIndicesEx((void *)key, len, &start, &stop, &step, &slicelength) < 0)
2598
if (slicelength <= 0) {
2599
return PyTuple_New(0);
2602
return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, start, stop, len);
1572
2791
/* for slice only */
1573
2792
static PyMappingMethods pyrna_prop_array_as_mapping = {
1574
( lenfunc ) pyrna_prop_array_length, /* mp_length */
1575
( binaryfunc ) pyrna_prop_array_subscript, /* mp_subscript */
1576
( objobjargproc ) pyrna_prop_array_ass_subscript, /* mp_ass_subscript */
2793
(lenfunc) pyrna_prop_array_length, /* mp_length */
2794
(binaryfunc) pyrna_prop_array_subscript, /* mp_subscript */
2795
(objobjargproc) pyrna_prop_array_ass_subscript, /* mp_ass_subscript */
1579
2798
static PyMappingMethods pyrna_prop_collection_as_mapping = {
1580
( lenfunc ) pyrna_prop_collection_length, /* mp_length */
1581
( binaryfunc ) pyrna_prop_collection_subscript, /* mp_subscript */
1582
( objobjargproc ) NULL, /* mp_ass_subscript */
2799
(lenfunc) pyrna_prop_collection_length, /* mp_length */
2800
(binaryfunc) pyrna_prop_collection_subscript, /* mp_subscript */
2801
(objobjargproc) pyrna_prop_collection_ass_subscript, /* mp_ass_subscript */
2804
/* only for fast bool's, large structs, assign nb_bool on init */
2805
static PyNumberMethods pyrna_prop_array_as_number = {
2807
NULL, /* nb_subtract */
2808
NULL, /* nb_multiply */
2809
NULL, /* nb_remainder */
2810
NULL, /* nb_divmod */
2811
NULL, /* nb_power */
2812
NULL, /* nb_negative */
2813
NULL, /* nb_positive */
2814
NULL, /* nb_absolute */
2815
(inquiry) pyrna_prop_array_bool, /* nb_bool */
2817
static PyNumberMethods pyrna_prop_collection_as_number = {
2819
NULL, /* nb_subtract */
2820
NULL, /* nb_multiply */
2821
NULL, /* nb_remainder */
2822
NULL, /* nb_divmod */
2823
NULL, /* nb_power */
2824
NULL, /* nb_negative */
2825
NULL, /* nb_positive */
2826
NULL, /* nb_absolute */
2827
(inquiry) pyrna_prop_collection_bool, /* nb_bool */
1586
2830
static int pyrna_prop_array_contains(BPy_PropertyRNA *self, PyObject *value)
1588
2832
return pyrna_array_contains_py(&self->ptr, self->prop, value);
1591
static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *value)
2835
static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *key)
1593
2837
PointerRNA newptr; /* not used, just so RNA_property_collection_lookup_string runs */
1595
/* key in dict style check */
1596
char *keyname = _PyUnicode_AsString(value);
1599
PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.__contains__: expected a string");
1603
if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
2839
if (PyTuple_Check(key)) {
2840
/* special case, for ID datablocks we */
2841
return pyrna_prop_collection_subscript_str_lib_pair_ptr(self, key,
2842
"(id, lib) in bpy_prop_collection", FALSE, NULL);
2846
/* key in dict style check */
2847
const char *keyname = _PyUnicode_AsString(key);
2849
if (keyname == NULL) {
2850
PyErr_SetString(PyExc_TypeError,
2851
"bpy_prop_collection.__contains__: expected a string or a tuple of strings");
2855
if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
1609
2862
static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
1611
2864
IDProperty *group;
1612
char *name = _PyUnicode_AsString(value);
2865
const char *name = _PyUnicode_AsString(value);
2867
PYRNA_STRUCT_CHECK_INT(self);
1615
PyErr_SetString( PyExc_TypeError, "bpy_struct.__contains__: expected a string");
1619
if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1620
PyErr_SetString( PyExc_TypeError, "bpy_struct: this type doesnt support IDProperties");
1624
group= RNA_struct_idproperties(&self->ptr, 0);
2870
PyErr_SetString(PyExc_TypeError, "bpy_struct.__contains__: expected a string");
2874
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
2875
PyErr_SetString(PyExc_TypeError, "bpy_struct: this type doesn't support IDProperties");
2879
group = RNA_struct_idprops(&self->ptr, 0);
1629
return IDP_GetPropertyFromGroup(group, name) ? 1:0;
2884
return IDP_GetPropertyFromGroup(group, name) ? 1 : 0;
1632
2887
static PySequenceMethods pyrna_prop_array_as_sequence = {
1633
(lenfunc)pyrna_prop_array_length, /* Cant set the len otherwise it can evaluate as false */
1634
NULL, /* sq_concat */
1635
NULL, /* sq_repeat */
2888
(lenfunc)pyrna_prop_array_length, /* Cant set the len otherwise it can evaluate as false */
2889
NULL, /* sq_concat */
2890
NULL, /* sq_repeat */
1636
2891
(ssizeargfunc)pyrna_prop_array_subscript_int, /* sq_item */ /* Only set this so PySequence_Check() returns True */
1637
NULL, /* sq_slice */
1638
(ssizeobjargproc)prop_subscript_ass_array_int, /* sq_ass_item */
1639
NULL, /* *was* sq_ass_slice */
1640
(objobjproc)pyrna_prop_array_contains, /* sq_contains */
2892
NULL, /* sq_slice */
2893
(ssizeobjargproc)prop_subscript_ass_array_int, /* sq_ass_item */
2894
NULL, /* *was* sq_ass_slice */
2895
(objobjproc)pyrna_prop_array_contains, /* sq_contains */
1641
2896
(binaryfunc) NULL, /* sq_inplace_concat */
1642
2897
(ssizeargfunc) NULL, /* sq_inplace_repeat */
1645
2900
static PySequenceMethods pyrna_prop_collection_as_sequence = {
1646
(lenfunc)pyrna_prop_collection_length, /* Cant set the len otherwise it can evaluate as false */
1647
NULL, /* sq_concat */
1648
NULL, /* sq_repeat */
2901
(lenfunc)pyrna_prop_collection_length, /* Cant set the len otherwise it can evaluate as false */
2902
NULL, /* sq_concat */
2903
NULL, /* sq_repeat */
1649
2904
(ssizeargfunc)pyrna_prop_collection_subscript_int, /* sq_item */ /* Only set this so PySequence_Check() returns True */
1650
NULL, /* *was* sq_slice */
1651
NULL, /* sq_ass_item */
1652
NULL, /* *was* sq_ass_slice */
1653
(objobjproc)pyrna_prop_collection_contains, /* sq_contains */
2905
NULL, /* *was* sq_slice */
2906
(ssizeobjargproc)/* pyrna_prop_collection_ass_subscript_int */ NULL /* let mapping take this one */, /* sq_ass_item */
2907
NULL, /* *was* sq_ass_slice */
2908
(objobjproc)pyrna_prop_collection_contains, /* sq_contains */
1654
2909
(binaryfunc) NULL, /* sq_inplace_concat */
1655
2910
(ssizeargfunc) NULL, /* sq_inplace_repeat */
1658
2913
static PySequenceMethods pyrna_struct_as_sequence = {
1659
NULL, /* Cant set the len otherwise it can evaluate as false */
1660
NULL, /* sq_concat */
1661
NULL, /* sq_repeat */
1662
NULL, /* sq_item */ /* Only set this so PySequence_Check() returns True */
1663
NULL, /* *was* sq_slice */
1664
NULL, /* sq_ass_item */
1665
NULL, /* *was* sq_ass_slice */
1666
(objobjproc)pyrna_struct_contains, /* sq_contains */
2914
NULL, /* Cant set the len otherwise it can evaluate as false */
2915
NULL, /* sq_concat */
2916
NULL, /* sq_repeat */
2917
NULL, /* sq_item */ /* Only set this so PySequence_Check() returns True */
2918
NULL, /* *was* sq_slice */
2919
NULL, /* sq_ass_item */
2920
NULL, /* *was* sq_ass_slice */
2921
(objobjproc)pyrna_struct_contains, /* sq_contains */
1667
2922
(binaryfunc) NULL, /* sq_inplace_concat */
1668
2923
(ssizeargfunc) NULL, /* sq_inplace_repeat */
1671
static PyObject *pyrna_struct_subscript( BPy_StructRNA *self, PyObject *key )
2926
static PyObject *pyrna_struct_subscript(BPy_StructRNA *self, PyObject *key)
1673
2928
/* mostly copied from BPy_IDGroup_Map_GetItem */
1674
2929
IDProperty *group, *idprop;
1675
char *name= _PyUnicode_AsString(key);
1677
if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1678
PyErr_SetString( PyExc_TypeError, "this type doesn't support IDProperties");
1683
PyErr_SetString( PyExc_TypeError, "bpy_struct[key]: only strings are allowed as keys of ID properties");
1687
group= RNA_struct_idproperties(&self->ptr, 0);
1690
PyErr_Format( PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
1694
idprop= IDP_GetPropertyFromGroup(group, name);
1697
PyErr_Format( PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
1701
return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
2930
const char *name = _PyUnicode_AsString(key);
2932
PYRNA_STRUCT_CHECK_OBJ(self);
2934
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
2935
PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
2940
PyErr_SetString(PyExc_TypeError, "bpy_struct[key]: only strings are allowed as keys of ID properties");
2944
group = RNA_struct_idprops(&self->ptr, 0);
2946
if (group == NULL) {
2947
PyErr_Format(PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
2951
idprop = IDP_GetPropertyFromGroup(group, name);
2953
if (idprop == NULL) {
2954
PyErr_Format(PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
2958
return BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
1704
static int pyrna_struct_ass_subscript( BPy_StructRNA *self, PyObject *key, PyObject *value )
2961
static int pyrna_struct_ass_subscript(BPy_StructRNA *self, PyObject *key, PyObject *value)
1706
IDProperty *group= RNA_struct_idproperties(&self->ptr, 1);
2965
PYRNA_STRUCT_CHECK_INT(self);
2967
group = RNA_struct_idprops(&self->ptr, 1);
2969
#ifdef USE_PEDANTIC_WRITE
2970
if (rna_disallow_writes && rna_id_write_error(&self->ptr, key)) {
2973
#endif // USE_PEDANTIC_WRITE
2975
if (group == NULL) {
1709
2976
PyErr_SetString(PyExc_TypeError, "bpy_struct[key] = val: id properties not supported for this type");
1716
2983
static PyMappingMethods pyrna_struct_as_mapping = {
1717
( lenfunc ) NULL, /* mp_length */
1718
( binaryfunc ) pyrna_struct_subscript, /* mp_subscript */
1719
( objobjargproc ) pyrna_struct_ass_subscript, /* mp_ass_subscript */
2984
(lenfunc) NULL, /* mp_length */
2985
(binaryfunc) pyrna_struct_subscript, /* mp_subscript */
2986
(objobjargproc) pyrna_struct_ass_subscript, /* mp_ass_subscript */
1722
static char pyrna_struct_keys_doc[] =
2989
PyDoc_STRVAR(pyrna_struct_keys_doc,
1723
2990
".. method:: keys()\n"
1725
" Returns the keys of this objects custom properties (matches pythons dictionary function of the same name).\n"
2992
" Returns the keys of this objects custom properties (matches pythons\n"
2993
" dictionary function of the same name).\n"
1727
2995
" :return: custom property keys.\n"
1728
2996
" :rtype: list of strings\n"
1730
" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
2998
BPY_DOC_ID_PROP_TYPE_NOTE
1732
3000
static PyObject *pyrna_struct_keys(BPy_PropertyRNA *self)
1734
3002
IDProperty *group;
1736
if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1737
PyErr_SetString( PyExc_TypeError, "bpy_struct.keys(): this type doesn't support IDProperties");
3004
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3005
PyErr_SetString(PyExc_TypeError, "bpy_struct.keys(): this type doesn't support IDProperties");
1741
group= RNA_struct_idproperties(&self->ptr, 0);
3009
group = RNA_struct_idprops(&self->ptr, 0);
1744
3012
return PyList_New(0);
1746
3014
return BPy_Wrap_GetKeys(group);
1749
static char pyrna_struct_items_doc[] =
3017
PyDoc_STRVAR(pyrna_struct_items_doc,
1750
3018
".. method:: items()\n"
1752
" Returns the items of this objects custom properties (matches pythons dictionary function of the same name).\n"
3020
" Returns the items of this objects custom properties (matches pythons\n"
3021
" dictionary function of the same name).\n"
1754
3023
" :return: custom property key, value pairs.\n"
1755
3024
" :rtype: list of key, value tuples\n"
1757
" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
3026
BPY_DOC_ID_PROP_TYPE_NOTE
1759
3028
static PyObject *pyrna_struct_items(BPy_PropertyRNA *self)
1761
3030
IDProperty *group;
1763
if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1764
PyErr_SetString( PyExc_TypeError, "bpy_struct.items(): this type doesn't support IDProperties");
3032
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3033
PyErr_SetString(PyExc_TypeError, "bpy_struct.items(): this type doesn't support IDProperties");
1768
group= RNA_struct_idproperties(&self->ptr, 0);
3037
group = RNA_struct_idprops(&self->ptr, 0);
1771
3040
return PyList_New(0);
1773
3042
return BPy_Wrap_GetItems(self->ptr.id.data, group);
1776
static char pyrna_struct_values_doc[] =
3045
PyDoc_STRVAR(pyrna_struct_values_doc,
1777
3046
".. method:: values()\n"
1779
" Returns the values of this objects custom properties (matches pythons dictionary function of the same name).\n"
3048
" Returns the values of this objects custom properties (matches pythons\n"
3049
" dictionary function of the same name).\n"
1781
3051
" :return: custom property values.\n"
1782
3052
" :rtype: list\n"
1784
" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
3054
BPY_DOC_ID_PROP_TYPE_NOTE
1786
3056
static PyObject *pyrna_struct_values(BPy_PropertyRNA *self)
1788
3058
IDProperty *group;
1790
if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1791
PyErr_SetString( PyExc_TypeError, "bpy_struct.values(): this type doesn't support IDProperties");
3060
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3061
PyErr_SetString(PyExc_TypeError, "bpy_struct.values(): this type doesn't support IDProperties");
1795
group= RNA_struct_idproperties(&self->ptr, 0);
3065
group = RNA_struct_idprops(&self->ptr, 0);
1798
3068
return PyList_New(0);
1800
3070
return BPy_Wrap_GetValues(self->ptr.id.data, group);
1803
/* for keyframes and drivers */
1804
static int pyrna_struct_anim_args_parse(PointerRNA *ptr, const char *error_prefix, const char *path,
1805
char **path_full, int *index)
1809
if (ptr->data==NULL) {
1810
PyErr_Format(PyExc_TypeError, "%.200s this struct has no data, can't be animated", error_prefix);
1814
prop = RNA_struct_find_property(ptr, path);
1817
PyErr_Format( PyExc_TypeError, "%.200s property \"%s\" not found", error_prefix, path);
1821
if (!RNA_property_animateable(ptr, prop)) {
1822
PyErr_Format(PyExc_TypeError, "%.200s property \"%s\" not animatable", error_prefix, path);
1826
if(RNA_property_array_check(ptr, prop) == 0) {
1827
if((*index) == -1) {
1831
PyErr_Format(PyExc_TypeError, "%.200s index %d was given while property \"%s\" is not an array", error_prefix, *index, path);
1836
int array_len= RNA_property_array_length(ptr, prop);
1837
if((*index) < -1 || (*index) >= array_len) {
1838
PyErr_Format( PyExc_TypeError, "%.200s index out of range \"%s\", given %d, array length is %d", error_prefix, path, *index, array_len);
1843
*path_full= RNA_path_from_ID_to_property(ptr, prop);
1845
if (*path_full==NULL) {
1846
PyErr_Format( PyExc_TypeError, "%.200s could not make path to \"%s\"", error_prefix, path);
1853
/* internal use for insert and delete */
1854
static int pyrna_struct_keyframe_parse(PointerRNA *ptr, PyObject *args, PyObject *kw, const char *parse_str, const char *error_prefix,
1855
char **path_full, int *index, float *cfra, char **group_name) /* return values */
1857
static char *kwlist[] = {"data_path", "index", "frame", "group", NULL};
1860
/* note, parse_str MUST start with 's|ifs' */
1861
if (!PyArg_ParseTupleAndKeywords(args, kw, parse_str, (char **)kwlist, &path, index, cfra, group_name))
1864
if(pyrna_struct_anim_args_parse(ptr, error_prefix, path, path_full, index) < 0)
1868
*cfra= CTX_data_scene(BPy_GetContext())->r.cfra;
1870
return 0; /* success */
1873
static char pyrna_struct_keyframe_insert_doc[] =
1874
".. method:: keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group=\"\")\n"
1876
" Insert a keyframe on the property given, adding fcurves and animation data when necessary.\n"
1878
" :arg data_path: path to the property to key, analogous to the fcurve's data path.\n"
1879
" :type data_path: string\n"
1880
" :arg index: array index of the property to key. Defaults to -1 which will key all indicies or a single channel if the property is not an array.\n"
1881
" :type index: int\n"
1882
" :arg frame: The frame on which the keyframe is inserted, defaulting to the current frame.\n"
1883
" :type frame: float\n"
1884
" :arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.\n"
1885
" :type group: str\n"
1886
" :return: Success of keyframe insertion.\n"
1889
static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args, PyObject *kw)
1892
/* args, pyrna_struct_keyframe_parse handles these */
1893
char *path_full= NULL;
1895
float cfra= FLT_MAX;
1896
char *group_name= NULL;
1898
if(pyrna_struct_keyframe_parse(&self->ptr, args, kw, "s|ifs:bpy_struct.keyframe_insert()", "bpy_struct.keyframe_insert()", &path_full, &index, &cfra, &group_name) == -1)
1901
result= PyBool_FromLong(insert_keyframe((ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0));
1902
MEM_freeN(path_full);
1907
static char pyrna_struct_keyframe_delete_doc[] =
1908
".. method:: keyframe_delete(data_path, index=-1, frame=bpy.context.scene.frame_current, group=\"\")\n"
1910
" Remove a keyframe from this properties fcurve.\n"
1912
" :arg data_path: path to the property to remove a key, analogous to the fcurve's data path.\n"
1913
" :type data_path: string\n"
1914
" :arg index: array index of the property to remove a key. Defaults to -1 removing all indicies or a single channel if the property is not an array.\n"
1915
" :type index: int\n"
1916
" :arg frame: The frame on which the keyframe is deleted, defaulting to the current frame.\n"
1917
" :type frame: float\n"
1918
" :arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.\n"
1919
" :type group: str\n"
1920
" :return: Success of keyframe deleation.\n"
1923
static PyObject *pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *args, PyObject *kw)
1926
/* args, pyrna_struct_keyframe_parse handles these */
1927
char *path_full= NULL;
1929
float cfra= FLT_MAX;
1930
char *group_name= NULL;
1932
if(pyrna_struct_keyframe_parse(&self->ptr, args, kw, "s|ifs:bpy_struct.keyframe_delete()", "bpy_struct.keyframe_insert()", &path_full, &index, &cfra, &group_name) == -1)
1935
result= PyBool_FromLong(delete_keyframe((ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0));
1936
MEM_freeN(path_full);
1941
static char pyrna_struct_driver_add_doc[] =
1942
".. method:: driver_add(path, index=-1)\n"
1944
" Adds driver(s) to the given property\n"
1946
" :arg path: path to the property to drive, analogous to the fcurve's data path.\n"
1947
" :type path: string\n"
1948
" :arg index: array index of the property drive. Defaults to -1 for all indicies or a single channel if the property is not an array.\n"
1949
" :type index: int\n"
1950
" :return: The driver(s) added.\n"
1951
" :rtype: :class:`FCurve` or list if index is -1 with an array property.";
1953
static PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args)
1955
char *path, *path_full;
1959
if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index))
1962
if(pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_add():", path, &path_full, &index) < 0)
1965
if(ANIM_add_driver((ID *)self->ptr.id.data, path_full, index, 0, DRIVER_TYPE_PYTHON)) {
1966
ID *id= self->ptr.id.data;
1967
AnimData *adt= BKE_animdata_from_id(id);
1973
if(index == -1) { /* all, use a list */
1976
while((fcu= list_find_fcurve(&adt->drivers, path_full, i++))) {
1977
RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr);
1978
item= pyrna_struct_CreatePyObject(&tptr);
1979
PyList_Append(ret, item);
1984
fcu= list_find_fcurve(&adt->drivers, path_full, index);
1985
RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr);
1986
ret= pyrna_struct_CreatePyObject(&tptr);
1990
PyErr_SetString(PyExc_TypeError, "bpy_struct.driver_add(): failed because of an internal error");
1994
MEM_freeN(path_full);
2000
static char pyrna_struct_driver_remove_doc[] =
2001
".. method:: driver_remove(path, index=-1)\n"
2003
" Remove driver(s) from the given property\n"
2005
" :arg path: path to the property to drive, analogous to the fcurve's data path.\n"
2006
" :type path: string\n"
2007
" :arg index: array index of the property drive. Defaults to -1 for all indicies or a single channel if the property is not an array.\n"
2008
" :type index: int\n"
2009
" :return: Success of driver removal.\n"
2012
static PyObject *pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args)
2014
char *path, *path_full;
2018
if (!PyArg_ParseTuple(args, "s|i:driver_remove", &path, &index))
2021
if(pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_remove():", path, &path_full, &index) < 0)
2024
ret= PyBool_FromLong(ANIM_remove_driver((ID *)self->ptr.id.data, path_full, index, 0));
2026
MEM_freeN(path_full);
2032
static char pyrna_struct_is_property_set_doc[] =
3074
PyDoc_STRVAR(pyrna_struct_is_property_set_doc,
2033
3075
".. method:: is_property_set(property)\n"
2035
3077
" Check if a property is set, use for testing operator properties.\n"
2037
3079
" :return: True when the property has been set.\n"
3080
" :rtype: boolean\n"
2040
3082
static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args)
3087
PYRNA_STRUCT_CHECK_OBJ(self);
2044
3089
if (!PyArg_ParseTuple(args, "s:is_property_set", &name))
2047
return PyBool_FromLong(RNA_property_is_set(&self->ptr, name));
3092
if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3093
PyErr_Format(PyExc_TypeError,
3094
"%.200s.is_property_set(\"%.200s\") not found",
3095
RNA_struct_identifier(self->ptr.type), name);
3099
return PyBool_FromLong(RNA_property_is_set(&self->ptr, prop));
2050
static char pyrna_struct_is_property_hidden_doc[] =
3102
PyDoc_STRVAR(pyrna_struct_is_property_hidden_doc,
2051
3103
".. method:: is_property_hidden(property)\n"
2053
3105
" Check if a property is hidden.\n"
2055
3107
" :return: True when the property is hidden.\n"
3108
" :rtype: boolean\n"
2058
3110
static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *args)
2060
3112
PropertyRNA *prop;
3115
PYRNA_STRUCT_CHECK_OBJ(self);
2064
3117
if (!PyArg_ParseTuple(args, "s:is_property_hidden", &name))
2067
prop= RNA_struct_find_property(&self->ptr, name);
2068
hidden= (prop)? (RNA_property_flag(prop) & PROP_HIDDEN): 1;
2070
return PyBool_FromLong(hidden);
3120
if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3121
PyErr_Format(PyExc_TypeError,
3122
"%.200s.is_property_hidden(\"%.200s\") not found",
3123
RNA_struct_identifier(self->ptr.type), name);
3127
return PyBool_FromLong(RNA_property_flag(prop) & PROP_HIDDEN);
2073
static char pyrna_struct_path_resolve_doc[] =
2074
".. method:: path_resolve(path)\n"
2076
" Returns the property from the path given or None if the property is not found.";
2078
static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value)
3130
PyDoc_STRVAR(pyrna_struct_path_resolve_doc,
3131
".. method:: path_resolve(path, coerce=True)\n"
3133
" Returns the property from the path, raise an exception when not found.\n"
3135
" :arg path: path which this property resolves.\n"
3136
" :type path: string\n"
3137
" :arg coerce: optional argument, when True, the property will be converted\n"
3138
" into its python representation.\n"
3139
" :type coerce: boolean\n"
3141
static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *args)
2080
char *path= _PyUnicode_AsString(value);
3144
PyObject *coerce = Py_True;
2081
3145
PointerRNA r_ptr;
2082
3146
PropertyRNA *r_prop;
2085
PyErr_SetString(PyExc_TypeError, "bpy_struct.path_resolve(): accepts only a single string argument");
2089
if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop))
2090
return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
3149
PYRNA_STRUCT_CHECK_OBJ(self);
3151
if (!PyArg_ParseTuple(args, "s|O!:path_resolve", &path, &PyBool_Type, &coerce))
3154
if (RNA_path_resolve_full(&self->ptr, path, &r_ptr, &r_prop, &index)) {
3157
if (index >= RNA_property_array_length(&r_ptr, r_prop) || index < 0) {
3158
PyErr_Format(PyExc_IndexError,
3159
"%.200s.path_resolve(\"%.200s\") index out of range",
3160
RNA_struct_identifier(self->ptr.type), path);
3164
return pyrna_array_index(&r_ptr, r_prop, index);
3168
if (coerce == Py_False) {
3169
return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
3172
return pyrna_prop_to_py(&r_ptr, r_prop);
3177
return pyrna_struct_CreatePyObject(&r_ptr);
3181
PyErr_Format(PyExc_ValueError,
3182
"%.200s.path_resolve(\"%.200s\") could not be resolved",
3183
RNA_struct_identifier(self->ptr.type), path);
2095
static char pyrna_struct_path_from_id_doc[] =
3188
PyDoc_STRVAR(pyrna_struct_path_from_id_doc,
2096
3189
".. method:: path_from_id(property=\"\")\n"
2098
3191
" Returns the data path from the ID to this object (string).\n"
2100
" :arg property: Optional property name which can be used if the path is to a property of this object.\n"
3193
" :arg property: Optional property name which can be used if the path is\n"
3194
" to a property of this object.\n"
2101
3195
" :type property: string\n"
2102
" :return: The path from :class:`bpy_struct.id_data` to this struct and property (when given).\n"
3196
" :return: The path from :class:`bpy.types.bpy_struct.id_data`\n"
3197
" to this struct and property (when given).\n"
2105
3200
static PyObject *pyrna_struct_path_from_id(BPy_StructRNA *self, PyObject *args)
3202
const char *name = NULL;
2109
3204
PropertyRNA *prop;
3207
PYRNA_STRUCT_CHECK_OBJ(self);
2112
3209
if (!PyArg_ParseTuple(args, "|s:path_from_id", &name))
2116
prop= RNA_struct_find_property(&self->ptr, name);
2118
PyErr_Format(PyExc_TypeError, "%.200s.path_from_id(\"%.200s\") not found", RNA_struct_identifier(self->ptr.type), name);
3213
prop = RNA_struct_find_property(&self->ptr, name);
3215
PyErr_Format(PyExc_AttributeError,
3216
"%.200s.path_from_id(\"%.200s\") not found",
3217
RNA_struct_identifier(self->ptr.type), name);
2122
path= RNA_path_from_ID_to_property(&self->ptr, prop);
3221
path = RNA_path_from_ID_to_property(&self->ptr, prop);
2125
path= RNA_path_from_ID_to_struct(&self->ptr);
3224
path = RNA_path_from_ID_to_struct(&self->ptr);
2129
if(name) PyErr_Format(PyExc_TypeError, "%.200s.path_from_id(\"%s\") found but does not support path creation", RNA_struct_identifier(self->ptr.type), name);
2130
else PyErr_Format(PyExc_TypeError, "%.200s.path_from_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type));
3229
PyErr_Format(PyExc_ValueError,
3230
"%.200s.path_from_id(\"%s\") found but does not support path creation",
3231
RNA_struct_identifier(self->ptr.type), name);
3234
PyErr_Format(PyExc_ValueError,
3235
"%.200s.path_from_id() does not support path creation for this type",
3236
RNA_struct_identifier(self->ptr.type));
2134
ret= PyUnicode_FromString(path);
3241
ret = PyUnicode_FromString(path);
3242
MEM_freeN((void *)path);
2140
static char pyrna_prop_path_from_id_doc[] =
3247
PyDoc_STRVAR(pyrna_prop_path_from_id_doc,
2141
3248
".. method:: path_from_id()\n"
2143
3250
" Returns the data path from the ID to this property (string).\n"
2145
" :return: The path from :class:`bpy_struct.id_data` to this property.\n"
3252
" :return: The path from :class:`bpy.types.bpy_struct.id_data` to this property.\n"
2148
3255
static PyObject *pyrna_prop_path_from_id(BPy_PropertyRNA *self)
2151
3258
PropertyRNA *prop = self->prop;
2154
path= RNA_path_from_ID_to_property(&self->ptr, self->prop);
3261
path = RNA_path_from_ID_to_property(&self->ptr, self->prop);
2157
PyErr_Format(PyExc_TypeError, "%.200s.%.200s.path_from_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(prop));
3264
PyErr_Format(PyExc_ValueError,
3265
"%.200s.%.200s.path_from_id() does not support path creation for this type",
3266
RNA_struct_identifier(self->ptr.type), RNA_property_identifier(prop));
2161
ret= PyUnicode_FromString(path);
3270
ret = PyUnicode_FromString(path);
3271
MEM_freeN((void *)path);
2167
static char pyrna_struct_recast_type_doc[] =
2168
".. method:: recast_type()\n"
3276
PyDoc_STRVAR(pyrna_struct_type_recast_doc,
3277
".. method:: type_recast()\n"
2170
" Return a new instance, this is needed because types such as textures can be changed at runtime.\n"
3279
" Return a new instance, this is needed because types\n"
3280
" such as textures can be changed at runtime.\n"
2172
3282
" :return: a new instance of this object with the type initialized again.\n"
2173
" :rtype: subclass of :class:`bpy_struct`";
2175
static PyObject *pyrna_struct_recast_type(BPy_StructRNA *self, PyObject *args)
3283
" :rtype: subclass of :class:`bpy.types.bpy_struct`\n"
3285
static PyObject *pyrna_struct_type_recast(BPy_StructRNA *self)
2177
3287
PointerRNA r_ptr;
3289
PYRNA_STRUCT_CHECK_OBJ(self);
2178
3291
RNA_pointer_recast(&self->ptr, &r_ptr);
2179
3292
return pyrna_struct_CreatePyObject(&r_ptr);
3295
static void pyrna_dir_members_py__add_keys(PyObject *list, PyObject *dict)
3299
list_tmp = PyDict_Keys(dict);
3300
PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
3301
Py_DECREF(list_tmp);
2182
3304
static void pyrna_dir_members_py(PyObject *list, PyObject *self)
2184
3306
PyObject *dict;
2185
3307
PyObject **dict_ptr;
2188
dict_ptr= _PyObject_GetDictPtr((PyObject *)self);
2190
if(dict_ptr && (dict=*dict_ptr)) {
2191
list_tmp = PyDict_Keys(dict);
2192
PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
2193
Py_DECREF(list_tmp);
2196
dict= ((PyTypeObject *)Py_TYPE(self))->tp_dict;
2198
list_tmp = PyDict_Keys(dict);
2199
PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
2200
Py_DECREF(list_tmp);
3309
dict_ptr = _PyObject_GetDictPtr((PyObject *)self);
3311
if (dict_ptr && (dict = *dict_ptr)) {
3312
pyrna_dir_members_py__add_keys(list, dict);
3315
dict = ((PyTypeObject *)Py_TYPE(self))->tp_dict;
3317
pyrna_dir_members_py__add_keys(list, dict);
3320
/* since this is least common case, handle it last */
3321
if (BPy_PropertyRNA_Check(self)) {
3322
BPy_PropertyRNA *self_prop = (BPy_PropertyRNA *)self;
3323
if (RNA_property_type(self_prop->prop) == PROP_COLLECTION) {
3326
if (RNA_property_collection_type_get(&self_prop->ptr, self_prop->prop, &r_ptr)) {
3327
PyObject *cls = pyrna_struct_Subtype(&r_ptr); /* borrows */
3328
dict = ((PyTypeObject *)cls)->tp_dict;
3329
pyrna_dir_members_py__add_keys(list, dict);
2649
static PyObject *pyrna_prop_values(BPy_PropertyRNA *self)
4047
PyDoc_STRVAR(pyrna_prop_collection_values_doc,
4048
".. method:: values()\n"
4050
" Return the values of collection\n"
4051
" (matching pythons dict.values() functionality).\n"
4053
" :return: the members of this collection.\n"
4056
static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self)
2651
PyObject *ret= PyList_New(0);
2654
RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2655
item = pyrna_struct_CreatePyObject(&itemptr);
2656
PyList_Append(ret, item);
4059
return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2664
static char pyrna_struct_get_doc[] =
4062
PyDoc_STRVAR(pyrna_struct_get_doc,
2665
4063
".. method:: get(key, default=None)\n"
2667
" Returns the value of the custom property assigned to key or default when not found (matches pythons dictionary function of the same name).\n"
4065
" Returns the value of the custom property assigned to key or default\n"
4066
" when not found (matches pythons dictionary function of the same name).\n"
2669
" :arg key: The key assosiated with the custom property.\n"
4068
" :arg key: The key associated with the custom property.\n"
2670
4069
" :type key: string\n"
2671
" :arg default: Optional argument for the value to return if *key* is not found.\n"
2672
// " :type default: Undefined\n"
4070
" :arg default: Optional argument for the value to return if\n"
4071
" *key* is not found.\n"
4072
" :type default: Undefined\n"
2674
" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
4074
BPY_DOC_ID_PROP_TYPE_NOTE
2676
4076
static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
2678
4078
IDProperty *group, *idprop;
2681
PyObject* def = Py_None;
4081
PyObject *def = Py_None;
4083
PYRNA_STRUCT_CHECK_OBJ(self);
2683
4085
if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
2686
4088
/* mostly copied from BPy_IDGroup_Map_GetItem */
2687
if(RNA_struct_idproperties_check(self->ptr.type)==0) {
2688
PyErr_SetString( PyExc_TypeError, "this type doesn't support IDProperties");
4089
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
4090
PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
2692
group= RNA_struct_idproperties(&self->ptr, 0);
2694
idprop= IDP_GetPropertyFromGroup(group, key);
4094
group = RNA_struct_idprops(&self->ptr, 0);
4096
idprop = IDP_GetPropertyFromGroup(group, key);
2697
return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
4099
return BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
4103
return Py_INCREF(def), def;
2704
static char pyrna_struct_as_pointer_doc[] =
4106
PyDoc_STRVAR(pyrna_struct_as_pointer_doc,
2705
4107
".. method:: as_pointer()\n"
2707
" Returns capsule which holds a pointer to blenders internal data\n"
2709
" :return: capsule with a name set from the struct type.\n"
2710
" :rtype: PyCapsule\n"
2712
" .. note:: This is intended only for advanced script writers who need to pass blender data to their own C/Python modules.\n";
4109
" Returns the memory address which holds a pointer to blenders internal data\n"
4111
" :return: int (memory address).\n"
4114
" .. note:: This is intended only for advanced script writers who need to\n"
4115
" pass blender data to their own C/Python modules.\n"
2714
4117
static PyObject *pyrna_struct_as_pointer(BPy_StructRNA *self)
2717
return PyCapsule_New(self->ptr.data, RNA_struct_identifier(self->ptr.type), NULL);
4119
return PyLong_FromVoidPtr(self->ptr.data);
2722
static PyObject *pyrna_prop_get(BPy_PropertyRNA *self, PyObject *args)
4122
PyDoc_STRVAR(pyrna_prop_collection_get_doc,
4123
".. method:: get(key, default=None)\n"
4125
" Returns the value of the item assigned to key or default when not found\n"
4126
" (matches pythons dictionary function of the same name).\n"
4128
" :arg key: The identifier for the collection member.\n"
4129
" :type key: string\n"
4130
" :arg default: Optional argument for the value to return if\n"
4131
" *key* is not found.\n"
4132
" :type default: Undefined\n"
4134
static PyObject *pyrna_prop_collection_get(BPy_PropertyRNA *self, PyObject *args)
2724
4136
PointerRNA newptr;
2727
PyObject* def = Py_None;
2729
if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
4139
PyObject *def = Py_None;
4141
PYRNA_PROP_CHECK_OBJ(self);
4143
if (!PyArg_ParseTuple(args, "O|O:get", &key_ob, &def))
2732
if(RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
2733
return pyrna_struct_CreatePyObject(&newptr);
2739
static void foreach_attr_type( BPy_PropertyRNA *self, char *attr,
2740
/* values to assign */
2741
RawPropertyType *raw_type, int *attr_tot, int *attr_signed )
4146
if (PyUnicode_Check(key_ob)) {
4147
const char *key = _PyUnicode_AsString(key_ob);
4149
if (RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
4150
return pyrna_struct_CreatePyObject(&newptr);
4152
else if (PyTuple_Check(key_ob)) {
4153
PyObject *ret = pyrna_prop_collection_subscript_str_lib_pair(self, key_ob,
4154
"bpy_prop_collection.get((id, lib))", FALSE);
4160
PyErr_Format(PyExc_KeyError,
4161
"bpy_prop_collection.get(key, ...): key must be a string or tuple, not %.200s",
4162
Py_TYPE(key_ob)->tp_name);
4165
return Py_INCREF(def), def;
4168
PyDoc_STRVAR(pyrna_prop_collection_find_doc,
4169
".. method:: find(key)\n"
4171
" Returns the index of a key in a collection or -1 when not found\n"
4172
" (matches pythons string find function of the same name).\n"
4174
" :arg key: The identifier for the collection member.\n"
4175
" :type key: string\n"
4176
" :return: index of the key.\n"
4179
static PyObject *pyrna_prop_collection_find(BPy_PropertyRNA *self, PyObject *key_ob)
4181
Py_ssize_t key_len_ssize_t;
4182
const char *key = _PyUnicode_AsStringAndSize(key_ob, &key_len_ssize_t);
4183
const int key_len = (int)key_len_ssize_t; /* comare with same type */
4185
char name[256], *nameptr;
4190
PYRNA_PROP_CHECK_OBJ(self);
4192
RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
4193
nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
4196
if ((key_len == namelen) && memcmp(nameptr, key, key_len) == 0) {
4201
if (name != nameptr) {
4210
return PyLong_FromSsize_t(index);
4213
static void foreach_attr_type(BPy_PropertyRNA *self, const char *attr,
4214
/* values to assign */
4215
RawPropertyType *raw_type, int *attr_tot, int *attr_signed)
2743
4217
PropertyRNA *prop;
2744
*raw_type= PROP_RAW_UNSET;
2746
*attr_signed= FALSE;
4218
*raw_type = PROP_RAW_UNSET;
4220
*attr_signed = FALSE;
2748
/* note: this is fail with zero length lists, so dont let this get caled in that case */
4222
/* note: this is fail with zero length lists, so don't let this get caled in that case */
2749
4223
RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2750
4224
prop = RNA_struct_find_property(&itemptr, attr);
2751
*raw_type= RNA_property_raw_type(prop);
4225
*raw_type = RNA_property_raw_type(prop);
2752
4226
*attr_tot = RNA_property_array_length(&itemptr, prop);
2753
*attr_signed= (RNA_property_subtype(prop)==PROP_UNSIGNED) ? FALSE:TRUE;
4227
*attr_signed = (RNA_property_subtype(prop) == PROP_UNSIGNED) ? FALSE : TRUE;
2759
/* pyrna_prop_foreach_get/set both use this */
2760
static int foreach_parse_args(
2761
BPy_PropertyRNA *self, PyObject *args,
4233
/* pyrna_prop_collection_foreach_get/set both use this */
4234
static int foreach_parse_args(BPy_PropertyRNA *self, PyObject *args,
2763
/*values to assign */
2764
char **attr, PyObject **seq, int *tot, int *size, RawPropertyType *raw_type, int *attr_tot, int *attr_signed)
4236
/* values to assign */
4237
const char **attr, PyObject **seq, int *tot, int *size,
4238
RawPropertyType *raw_type, int *attr_tot, int *attr_signed
2768
4243
int target_tot;
2771
*size= *attr_tot= *attr_signed= FALSE;
2772
*raw_type= PROP_RAW_UNSET;
4246
*size = *attr_tot = *attr_signed = FALSE;
4247
*raw_type = PROP_RAW_UNSET;
2774
if(!PyArg_ParseTuple(args, "sO", attr, seq) || (!PySequence_Check(*seq) && PyObject_CheckBuffer(*seq))) {
2775
PyErr_SetString( PyExc_TypeError, "foreach_get(attr, sequence) expects a string and a sequence" );
4249
if (!PyArg_ParseTuple(args, "sO", attr, seq) || (!PySequence_Check(*seq) && PyObject_CheckBuffer(*seq))) {
4250
PyErr_SetString(PyExc_TypeError, "foreach_get(attr, sequence) expects a string and a sequence");
2779
*tot= PySequence_Length(*seq); // TODO - buffer may not be a sequence! array.array() is tho.
4254
*tot = PySequence_Size(*seq); // TODO - buffer may not be a sequence! array.array() is tho.
2782
4257
foreach_attr_type(self, *attr, raw_type, attr_tot, attr_signed);
2783
*size= RNA_raw_type_sizeof(*raw_type);
4258
*size = RNA_raw_type_sizeof(*raw_type);
2785
#if 0 // works fine but not strictly needed, we could allow RNA_property_collection_raw_* to do the checks
4260
#if 0 // works fine but not strictly needed, we could allow RNA_property_collection_raw_* to do the checks
4261
if ((*attr_tot) < 1)
2789
4264
if (RNA_property_type(self->prop) == PROP_COLLECTION)
2790
4265
array_tot = RNA_property_collection_length(&self->ptr, self->prop);
3062
4582
static struct PyMethodDef pyrna_prop_array_methods[] = {
3063
{"foreach_get", (PyCFunction)pyrna_prop_foreach_get, METH_VARARGS, NULL},
3064
{"foreach_set", (PyCFunction)pyrna_prop_foreach_set, METH_VARARGS, NULL},
3065
4583
{NULL, NULL, 0, NULL}
3068
4586
static struct PyMethodDef pyrna_prop_collection_methods[] = {
3069
{"foreach_get", (PyCFunction)pyrna_prop_foreach_get, METH_VARARGS, NULL},
3070
{"foreach_set", (PyCFunction)pyrna_prop_foreach_set, METH_VARARGS, NULL},
3072
{"keys", (PyCFunction)pyrna_prop_keys, METH_NOARGS, NULL},
3073
{"items", (PyCFunction)pyrna_prop_items, METH_NOARGS,NULL},
3074
{"values", (PyCFunction)pyrna_prop_values, METH_NOARGS, NULL},
3076
{"get", (PyCFunction)pyrna_prop_get, METH_VARARGS, NULL},
3078
/* moved into a getset */
3079
{"add", (PyCFunction)pyrna_prop_add, METH_NOARGS, NULL},
3080
{"remove", (PyCFunction)pyrna_prop_remove, METH_O, NULL},
3081
{"move", (PyCFunction)pyrna_prop_move, METH_VARARGS, NULL},
4587
{"foreach_get", (PyCFunction)pyrna_prop_collection_foreach_get, METH_VARARGS, pyrna_prop_collection_foreach_get_doc},
4588
{"foreach_set", (PyCFunction)pyrna_prop_collection_foreach_set, METH_VARARGS, pyrna_prop_collection_foreach_set_doc},
4590
{"keys", (PyCFunction)pyrna_prop_collection_keys, METH_NOARGS, pyrna_prop_collection_keys_doc},
4591
{"items", (PyCFunction)pyrna_prop_collection_items, METH_NOARGS, pyrna_prop_collection_items_doc},
4592
{"values", (PyCFunction)pyrna_prop_collection_values, METH_NOARGS, pyrna_prop_collection_values_doc},
4594
{"get", (PyCFunction)pyrna_prop_collection_get, METH_VARARGS, pyrna_prop_collection_get_doc},
4595
{"find", (PyCFunction)pyrna_prop_collection_find, METH_O, pyrna_prop_collection_find_doc},
4596
{NULL, NULL, 0, NULL}
4599
static struct PyMethodDef pyrna_prop_collection_idprop_methods[] = {
4600
{"add", (PyCFunction)pyrna_prop_collection_idprop_add, METH_NOARGS, NULL},
4601
{"remove", (PyCFunction)pyrna_prop_collection_idprop_remove, METH_O, NULL},
4602
{"clear", (PyCFunction)pyrna_prop_collection_idprop_clear, METH_NOARGS, NULL},
4603
{"move", (PyCFunction)pyrna_prop_collection_idprop_move, METH_VARARGS, NULL},
3082
4604
{NULL, NULL, 0, NULL}
3085
4607
/* only needed for subtyping, so a new class gets a valid BPy_StructRNA
3086
4608
* todo - also accept useful args */
3087
static PyObject * pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
4609
static PyObject *pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *UNUSED(kwds))
4611
if (PyTuple_GET_SIZE(args) == 1) {
4612
BPy_StructRNA *base = (BPy_StructRNA *)PyTuple_GET_ITEM(args, 0);
4613
if (Py_TYPE(base) == type) {
4615
return (PyObject *)base;
4617
else if (PyType_IsSubtype(Py_TYPE(base), &pyrna_struct_Type)) {
4618
/* this almost never runs, only when using user defined subclasses of built-in object.
4619
* this isn't common since its NOT related to registerable subclasses. eg:
4621
* >>> class MyObSubclass(bpy.types.Object):
4622
* ... def test_func(self):
4625
* >>> myob = MyObSubclass(bpy.context.object)
4626
* >>> myob.test_func()
4629
* Keep this since it could be useful.
4632
if ((ret = (BPy_StructRNA *)type->tp_alloc(type, 0))) {
4633
ret->ptr = base->ptr;
4635
/* pass on exception & NULL if tp_alloc fails */
4636
return (PyObject *)ret;
3089
BPy_StructRNA *base = NULL;
3091
if (!PyArg_ParseTuple(args, "O!:bpy_struct.__new__", &pyrna_struct_Type, &base))
3094
if (type == &pyrna_struct_Type) {
3095
return pyrna_struct_CreatePyObject(&base->ptr);
3097
BPy_StructRNA *ret = (BPy_StructRNA *) type->tp_alloc(type, 0);
3098
ret->ptr = base->ptr;
3099
return (PyObject *)ret;
4639
/* error, invalid type given */
4640
PyErr_Format(PyExc_TypeError,
4641
"bpy_struct.__new__(type): type '%.200s' is not a subtype of bpy_struct",
4646
PyErr_Format(PyExc_TypeError,
4647
"bpy_struct.__new__(type): expected a single argument");
3103
4652
/* only needed for subtyping, so a new class gets a valid BPy_StructRNA
3104
4653
* todo - also accept useful args */
3105
static PyObject * pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
4654
static PyObject *pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *UNUSED(kwds))
4656
BPy_PropertyRNA *base;
3107
BPy_PropertyRNA *base = NULL;
3109
if (!PyArg_ParseTuple(args, "O!:Base BPy_PropertyRNA", &pyrna_prop_Type, &base))
4658
if (!PyArg_ParseTuple(args, "O!:bpy_prop.__new__", &pyrna_prop_Type, &base))
3112
if (ELEM3(type, &pyrna_prop_Type, &pyrna_prop_array_Type, &pyrna_prop_collection_Type)) {
3113
return pyrna_prop_CreatePyObject(&base->ptr, base->prop);
4661
if (type == Py_TYPE(base)) {
4663
return (PyObject *)base;
4665
else if (PyType_IsSubtype(type, &pyrna_prop_Type)) {
3115
4666
BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0);
3116
4667
ret->ptr = base->ptr;
3117
4668
ret->prop = base->prop;
3118
4669
return (PyObject *)ret;
4672
PyErr_Format(PyExc_TypeError,
4673
"bpy_prop.__new__(type): type '%.200s' is not a subtype of bpy_prop",
3122
PyObject *pyrna_param_to_py(PointerRNA *ptr, ParameterList *parms, PropertyRNA *prop, void *data)
4679
static PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
3125
int type = RNA_property_type(prop);
3126
int flag = RNA_property_flag(prop);
4682
const int type = RNA_property_type(prop);
4683
const int flag = RNA_property_flag(prop);
3129
if(RNA_property_array_check(ptr, prop)) {
4685
if (RNA_property_array_check(prop)) {
3132
4688
if (flag & PROP_DYNAMIC) {
3133
len= RNA_parameter_length_get_data(parms, prop, data);
3135
data= *((void **)data);
4689
ParameterDynAlloc *data_alloc = data;
4690
len = data_alloc->array_tot;
4691
data = data_alloc->array;
3138
len= RNA_property_array_length(ptr, prop);
4694
len = RNA_property_array_length(ptr, prop);
3140
4696
/* resolve the array from a new pytype */
3142
4698
/* kazanbas: TODO make multidim sequences here */
3144
4700
switch (type) {
3146
ret = PyTuple_New(len);
3147
for(a=0; a<len; a++)
3148
PyTuple_SET_ITEM(ret, a, PyBool_FromLong( ((int*)data)[a] ));
3151
ret = PyTuple_New(len);
3152
for(a=0; a<len; a++)
3153
PyTuple_SET_ITEM(ret, a, PyLong_FromSsize_t( (Py_ssize_t)((int*)data)[a] ));
3156
switch(RNA_property_subtype(prop)) {
3157
case PROP_ALL_VECTOR_SUBTYPES:
3158
ret= newVectorObject(data, len, Py_NEW, NULL);
3162
ret= newMatrixObject(data, 4, 4, Py_NEW, NULL);
3166
ret= newMatrixObject(data, 3, 3, Py_NEW, NULL);
3171
ret = PyTuple_New(len);
3172
for(a=0; a<len; a++)
3173
PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble( ((float*)data)[a] ));
4702
ret = PyTuple_New(len);
4703
for (a = 0; a < len; a++)
4704
PyTuple_SET_ITEM(ret, a, PyBool_FromLong(((int *)data)[a]));
4707
ret = PyTuple_New(len);
4708
for (a = 0; a < len; a++)
4709
PyTuple_SET_ITEM(ret, a, PyLong_FromSsize_t((Py_ssize_t)((int *)data)[a]));
4712
switch (RNA_property_subtype(prop)) {
4713
#ifdef USE_MATHUTILS
4714
case PROP_ALL_VECTOR_SUBTYPES:
4715
ret = Vector_CreatePyObject(data, len, Py_NEW, NULL);
4719
ret = Matrix_CreatePyObject(data, 4, 4, Py_NEW, NULL);
4722
else if (len == 9) {
4723
ret = Matrix_CreatePyObject(data, 3, 3, Py_NEW, NULL);
4729
ret = PyTuple_New(len);
4730
for (a = 0; a < len; a++)
4731
PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble(((float *)data)[a]));
3178
PyErr_Format(PyExc_TypeError, "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)", type);
4736
PyErr_Format(PyExc_TypeError,
4737
"RNA Error: unknown array type \"%d\" (pyrna_param_to_py)",
3184
4744
/* see if we can coorce into a python type - PropertyType */
3185
4745
switch (type) {
3187
ret = PyBool_FromLong( *(int*)data );
3190
ret = PyLong_FromSsize_t( (Py_ssize_t)*(int*)data );
3193
ret = PyFloat_FromDouble( *(float*)data );
3197
if(flag & PROP_THICK_WRAP)
3198
ret = PyUnicode_FromString( (char*)data );
3200
ret = PyUnicode_FromString( *(char**)data );
3205
ret= pyrna_enum_to_py(ptr, prop, *(int*)data);
3211
StructRNA *type= RNA_property_pointer_type(ptr, prop);
3213
if(flag & PROP_RNAPTR) {
3214
/* in this case we get the full ptr */
3215
newptr= *(PointerRNA*)data;
3218
if(RNA_struct_is_ID(type)) {
3219
RNA_id_pointer_create(*(void**)data, &newptr);
3221
/* note: this is taken from the function's ID pointer
3222
* and will break if a function returns a pointer from
3223
* another ID block, watch this! - it should at least be
3224
* easy to debug since they are all ID's */
3225
RNA_pointer_create(ptr->id.data, type, *(void**)data, &newptr);
3230
ret = pyrna_struct_CreatePyObject(&newptr);
3237
case PROP_COLLECTION:
3239
ListBase *lb= (ListBase*)data;
3240
CollectionPointerLink *link;
3243
ret = PyList_New(0);
3245
for(link=lb->first; link; link=link->next) {
3246
linkptr= pyrna_struct_CreatePyObject(&link->ptr);
3247
PyList_Append(ret, linkptr);
3254
PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_param_to_py)", type);
4747
ret = PyBool_FromLong(*(int *)data);
4750
ret = PyLong_FromSsize_t((Py_ssize_t)*(int *)data);
4753
ret = PyFloat_FromDouble(*(float *)data);
4758
PyObject *value_coerce = NULL;
4759
const int subtype = RNA_property_subtype(prop);
4761
if (flag & PROP_THICK_WRAP)
4762
data_ch = (char *)data;
4764
data_ch = *(char **)data;
4766
#ifdef USE_STRING_COERCE
4767
if (subtype == PROP_BYTESTRING) {
4768
ret = PyBytes_FromString(data_ch);
4770
else if (ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
4771
ret = PyC_UnicodeFromByte(data_ch);
4774
ret = PyUnicode_FromString(data_ch);
4777
if (subtype == PROP_BYTESTRING) {
4778
ret = PyBytes_FromString(buf);
4781
ret = PyUnicode_FromString(data_ch);
4785
#ifdef USE_STRING_COERCE
4786
Py_XDECREF(value_coerce);
4793
ret = pyrna_enum_to_py(ptr, prop, *(int *)data);
4799
StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
4801
if (flag & PROP_RNAPTR) {
4802
/* in this case we get the full ptr */
4803
newptr = *(PointerRNA *)data;
4806
if (RNA_struct_is_ID(ptype)) {
4807
RNA_id_pointer_create(*(void **)data, &newptr);
4810
/* note: this is taken from the function's ID pointer
4811
* and will break if a function returns a pointer from
4812
* another ID block, watch this! - it should at least be
4813
* easy to debug since they are all ID's */
4814
RNA_pointer_create(ptr->id.data, ptype, *(void **)data, &newptr);
4819
ret = pyrna_struct_CreatePyObject(&newptr);
4827
case PROP_COLLECTION:
4829
ListBase *lb = (ListBase *)data;
4830
CollectionPointerLink *link;
4833
ret = PyList_New(0);
4835
for (link = lb->first; link; link = link->next) {
4836
linkptr = pyrna_struct_CreatePyObject(&link->ptr);
4837
PyList_Append(ret, linkptr);
4844
PyErr_Format(PyExc_TypeError,
4845
"RNA Error: unknown type \"%d\" (pyrna_param_to_py)",
3263
static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw)
4855
/* Use to replace PyDict_GetItemString() when the overhead of converting a
4856
* string into a python unicode is higher than a non hash lookup.
4857
* works on small dict's such as keyword args. */
4858
static PyObject *small_dict_get_item_string(PyObject *dict, const char *key_lookup)
4860
PyObject *key = NULL;
4862
PyObject *value = NULL;
4864
while (PyDict_Next(dict, &pos, &key, &value)) {
4865
if (PyUnicode_Check(key)) {
4866
if (strcmp(key_lookup, _PyUnicode_AsString(key)) == 0) {
4875
static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject *kw)
3265
4877
/* Note, both BPy_StructRNA and BPy_PropertyRNA can be used here */
3266
PointerRNA *self_ptr= &(((BPy_DummyPointerRNA *)PyTuple_GET_ITEM(self, 0))->ptr);
3267
FunctionRNA *self_func= PyCapsule_GetPointer(PyTuple_GET_ITEM(self, 1), NULL);
4878
PointerRNA *self_ptr = &self->ptr;
4879
FunctionRNA *self_func = self->func;
3269
4881
PointerRNA funcptr;
3270
4882
ParameterList parms;
3271
4883
ParameterIterator iter;
3272
4884
PropertyRNA *parm;
3273
4885
PyObject *ret, *item;
3274
int i, pyargs_len, pykw_len, parms_len, ret_len, flag, err= 0, kw_tot= 0, kw_arg;
3275
const char *parm_id;
3277
PropertyRNA *pret_single= NULL;
3278
void *retdata_single= NULL;
4886
int i, pyargs_len, pykw_len, parms_len, ret_len, flag, err = 0, kw_tot = 0, kw_arg;
4888
PropertyRNA *pret_single = NULL;
4889
void *retdata_single = NULL;
4891
/* enable this so all strings are copied and freed after calling.
4892
* this exposes bugs where the pointer to the string is held and re-used */
4893
// #define DEBUG_STRING_FREE
4895
#ifdef DEBUG_STRING_FREE
4896
PyObject *string_free_ls = PyList_New(0);
3280
4899
/* Should never happen but it does in rare cases */
3281
if(self_ptr==NULL) {
4900
BLI_assert(self_ptr != NULL);
4902
if (self_ptr == NULL) {
3282
4903
PyErr_SetString(PyExc_RuntimeError, "rna functions internal rna pointer is NULL, this is a bug. aborting");
3286
if(self_func==NULL) {
3287
PyErr_Format(PyExc_RuntimeError, "%.200s.<unknown>(): rna function internal function is NULL, this is a bug. aborting", RNA_struct_identifier(self_ptr->type));
4907
if (self_func == NULL) {
4908
PyErr_Format(PyExc_RuntimeError,
4909
"%.200s.<unknown>(): rna function internal function is NULL, this is a bug. aborting",
4910
RNA_struct_identifier(self_ptr->type));
4919
PyC_FileAndNum(&fn, &lineno);
4920
printf("pyrna_func_call > %.200s.%.200s : %.200s:%d\n",
4921
RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), fn, lineno);
3291
4925
/* include the ID pointer for pyrna_param_to_py() so we can include the
3292
4926
* ID pointer on return values, this only works when returned values have
3293
4927
* the same ID as the functions. */
3294
4928
RNA_pointer_create(self_ptr->id.data, &RNA_Function, self_func, &funcptr);
3296
pyargs_len= PyTuple_GET_SIZE(args);
3297
pykw_len= kw ? PyDict_Size(kw) : 0;
4930
pyargs_len = PyTuple_GET_SIZE(args);
4931
pykw_len = kw ? PyDict_Size(kw) : 0;
3299
4933
RNA_parameter_list_create(&parms, self_ptr, self_func);
3300
4934
RNA_parameter_list_begin(&parms, &iter);
3301
parms_len= RNA_parameter_list_arg_count(&parms);
4935
parms_len = RNA_parameter_list_arg_count(&parms);
3304
if(pyargs_len + pykw_len > parms_len) {
4938
if (pyargs_len + pykw_len > parms_len) {
3305
4939
RNA_parameter_list_end(&iter);
3306
PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): takes at most %d arguments, got %d", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parms_len, pyargs_len + pykw_len);
4940
PyErr_Format(PyExc_TypeError,
4941
"%.200s.%.200s(): takes at most %d arguments, got %d",
4942
RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func),
4943
parms_len, pyargs_len + pykw_len);
3310
4947
/* parse function parameters */
3311
for (i= 0; iter.valid && err==0; RNA_parameter_list_next(&iter)) {
3313
flag= RNA_property_flag(parm);
4948
for (i = 0; iter.valid && err == 0; RNA_parameter_list_next(&iter)) {
4950
flag = RNA_property_flag(parm);
3315
4952
/* only useful for single argument returns, we'll need another list loop for multiple */
3316
4953
if (flag & PROP_OUTPUT) {
3318
if (pret_single==NULL) {
3320
retdata_single= iter.data;
4955
if (pret_single == NULL) {
4957
retdata_single = iter.data;
3326
parm_id= RNA_property_identifier(parm);
3329
4965
if (i < pyargs_len) {
3330
item= PyTuple_GET_ITEM(args, i);
4966
item = PyTuple_GET_ITEM(args, i);
3335
4969
else if (kw != NULL) {
3336
item= PyDict_GetItemString(kw, parm_id); /* borrow ref */
3338
kw_tot++; /* make sure invalid keywords are not given */
4971
item = PyDict_GetItemString(kw, RNA_property_identifier(parm)); /* borrow ref */
4973
item = small_dict_get_item_string(kw, RNA_property_identifier(parm)); /* borrow ref */
4976
kw_tot++; /* make sure invalid keywords are not given */
3344
if(flag & PROP_REQUIRED) {
3345
PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): required parameter \"%.200s\" not specified", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parm_id);
4981
i++; /* current argument */
4984
if (flag & PROP_REQUIRED) {
4985
PyErr_Format(PyExc_TypeError,
4986
"%.200s.%.200s(): required parameter \"%.200s\" not specified",
4987
RNA_struct_identifier(self_ptr->type),
4988
RNA_function_identifier(self_func),
4989
RNA_property_identifier(parm));
3349
else /* PyDict_GetItemString wont raise an error */
4993
else { /* PyDict_GetItemString wont raise an error */
3353
err= pyrna_py_to_prop(&funcptr, parm, &parms, iter.data, item, "");
3356
/* the error generated isnt that useful, so generate it again with a useful prefix
4998
#ifdef DEBUG_STRING_FREE
5000
if (PyUnicode_Check(item)) {
5001
item = PyUnicode_FromString(_PyUnicode_AsString(item));
5002
PyList_Append(string_free_ls, item);
5007
err = pyrna_py_to_prop(&funcptr, parm, iter.data, item, "");
5010
/* the error generated isn't that useful, so generate it again with a useful prefix
3357
5011
* could also write a function to prepend to error messages */
3358
5012
char error_prefix[512];
3359
5013
PyErr_Clear(); /* re-raise */
3362
snprintf(error_prefix, sizeof(error_prefix), "%s.%s(): error with keyword argument \"%s\" - ", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parm_id);
5016
BLI_snprintf(error_prefix, sizeof(error_prefix),
5017
"%.200s.%.200s(): error with keyword argument \"%.200s\" - ",
5018
RNA_struct_identifier(self_ptr->type),
5019
RNA_function_identifier(self_func),
5020
RNA_property_identifier(parm));
3364
snprintf(error_prefix, sizeof(error_prefix), "%s.%s(): error with argument %d, \"%s\" - ", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), i, parm_id);
5022
BLI_snprintf(error_prefix, sizeof(error_prefix),
5023
"%.200s.%.200s(): error with argument %d, \"%.200s\" - ",
5024
RNA_struct_identifier(self_ptr->type),
5025
RNA_function_identifier(self_func),
5027
RNA_property_identifier(parm));
3366
pyrna_py_to_prop(&funcptr, parm, &parms, iter.data, item, error_prefix);
5029
pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix);
3372
5035
RNA_parameter_list_end(&iter);
3374
/* Check if we gave args that dont exist in the function
5037
/* Check if we gave args that don't exist in the function
3375
5038
* printing the error is slow but it should only happen when developing.
3376
5039
* the if below is quick, checking if it passed less keyword args then we gave.
3377
* (Dont overwrite the error if we have one, otherwise can skip important messages and confuse with args)
5040
* (Don't overwrite the error if we have one, otherwise can skip important messages and confuse with args)
3379
if(err == 0 && kw && (pykw_len > kw_tot)) {
5042
if (err == 0 && kw && (pykw_len > kw_tot)) {
3380
5043
PyObject *key, *value;
3381
5044
Py_ssize_t pos = 0;
3383
DynStr *bad_args= BLI_dynstr_new();
3384
DynStr *good_args= BLI_dynstr_new();
5046
DynStr *bad_args = BLI_dynstr_new();
5047
DynStr *good_args = BLI_dynstr_new();
3386
char *arg_name, *bad_args_str, *good_args_str;
3387
int found= FALSE, first= TRUE;
5049
const char *arg_name, *bad_args_str, *good_args_str;
5050
int found = FALSE, first = TRUE;
3389
5052
while (PyDict_Next(kw, &pos, &key, &value)) {
3391
arg_name= _PyUnicode_AsString(key);
5054
arg_name = _PyUnicode_AsString(key);
3394
if(arg_name==NULL) { /* unlikely the argname is not a string but ignore if it is*/
5057
if (arg_name == NULL) { /* unlikely the argname is not a string but ignore if it is*/
3398
5061
/* Search for arg_name */
3399
5062
RNA_parameter_list_begin(&parms, &iter);
3400
for(; iter.valid; RNA_parameter_list_next(&iter)) {
3402
if (strcmp(arg_name, RNA_property_identifier(parm))==0) {
5063
for (; iter.valid; RNA_parameter_list_next(&iter)) {
5065
if (strcmp(arg_name, RNA_property_identifier(parm)) == 0) {
3408
5071
RNA_parameter_list_end(&iter);
5073
if (found == FALSE) {
3411
5074
BLI_dynstr_appendf(bad_args, first ? "%s" : ", %s", arg_name);
3417
5080
/* list good args */
3420
5083
RNA_parameter_list_begin(&parms, &iter);
3421
for(; iter.valid; RNA_parameter_list_next(&iter)) {
3423
if(RNA_property_flag(parm) & PROP_OUTPUT)
5084
for (; iter.valid; RNA_parameter_list_next(&iter)) {
5086
if (RNA_property_flag(parm) & PROP_OUTPUT)
3426
5089
BLI_dynstr_appendf(good_args, first ? "%s" : ", %s", RNA_property_identifier(parm));
3429
5092
RNA_parameter_list_end(&iter);
3432
bad_args_str= BLI_dynstr_get_cstring(bad_args);
3433
good_args_str= BLI_dynstr_get_cstring(good_args);
5095
bad_args_str = BLI_dynstr_get_cstring(bad_args);
5096
good_args_str = BLI_dynstr_get_cstring(good_args);
3435
PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): was called with invalid keyword arguments(s) (%s), expected (%s)", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), bad_args_str, good_args_str);
5098
PyErr_Format(PyExc_TypeError,
5099
"%.200s.%.200s(): was called with invalid keyword arguments(s) (%s), expected (%s)",
5100
RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func),
5101
bad_args_str, good_args_str);
3437
5103
BLI_dynstr_free(bad_args);
3438
5104
BLI_dynstr_free(good_args);
3439
MEM_freeN(bad_args_str);
3440
MEM_freeN(good_args_str);
5105
MEM_freeN((void *)bad_args_str);
5106
MEM_freeN((void *)good_args_str);
3447
5113
/* call function */
3448
5114
ReportList reports;
3449
bContext *C= BPy_GetContext();
5115
bContext *C = BPy_GetContext();
3451
5117
BKE_reports_init(&reports, RPT_STORE);
3452
5118
RNA_function_call(C, &reports, self_ptr, self_func, &parms);
3454
err= (BPy_reports_to_error(&reports))? -1: 0;
3455
BKE_reports_clear(&reports);
5120
err = (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE));
3457
5122
/* return value */
3459
5124
if (ret_len > 0) {
3460
5125
if (ret_len > 1) {
3461
ret= PyTuple_New(ret_len);
3462
i= 0; /* arg index */
5126
ret = PyTuple_New(ret_len);
5127
i = 0; /* arg index */
3464
5129
RNA_parameter_list_begin(&parms, &iter);
3466
for(; iter.valid; RNA_parameter_list_next(&iter)) {
3468
flag= RNA_property_flag(parm);
5131
for (; iter.valid; RNA_parameter_list_next(&iter)) {
5133
flag = RNA_property_flag(parm);
3470
5135
if (flag & PROP_OUTPUT)
3471
PyTuple_SET_ITEM(ret, i++, pyrna_param_to_py(&funcptr, &parms, parm, iter.data));
5136
PyTuple_SET_ITEM(ret, i++, pyrna_param_to_py(&funcptr, parm, iter.data));
3474
5139
RNA_parameter_list_end(&iter);
3477
ret= pyrna_param_to_py(&funcptr, &parms, pret_single, retdata_single);
5142
ret = pyrna_param_to_py(&funcptr, pret_single, retdata_single);
3479
5144
/* possible there is an error in conversion */
5152
#ifdef DEBUG_STRING_FREE
5154
if (PyList_GET_SIZE(string_free_ls)) {
5155
printf("%.200s.%.200s(): has %d strings\n",
5156
RNA_struct_identifier(self_ptr->type),
5157
RNA_function_identifier(self_func),
5158
(int)PyList_GET_SIZE(string_free_ls));
5161
Py_DECREF(string_free_ls);
5162
#undef DEBUG_STRING_FREE
3487
5166
RNA_parameter_list_end(&iter);
3488
5167
RNA_parameter_list_free(&parms);
3496
5175
Py_RETURN_NONE;
5178
static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *UNUSED(closure))
5183
args = RNA_function_as_string_keywords(NULL, self->func, NULL, TRUE, TRUE);
5185
ret = PyUnicode_FromFormat("%.200s.%.200s(%.200s)\n%s",
5186
RNA_struct_identifier(self->ptr.type),
5187
RNA_function_identifier(self->func),
5188
args, RNA_function_ui_description(self->func));
5195
/* subclasses of pyrna_struct_Type which support idprop definitions use this as a metaclass */
5196
/* note: tp_base member is set to &PyType_Type on init */
5197
PyTypeObject pyrna_struct_meta_idprop_Type = {
5198
PyVarObject_HEAD_INIT(NULL, 0)
5199
"bpy_struct_meta_idprop", /* tp_name */
5201
/* NOTE! would be PyTypeObject, but subtypes of Type must be PyHeapTypeObject's */
5202
sizeof(PyHeapTypeObject), /* tp_basicsize */
5204
0, /* tp_itemsize */
5206
NULL, /* tp_dealloc */
5207
NULL, /* printfunc tp_print; */
5208
NULL, /* getattrfunc tp_getattr; */
5209
NULL, /* setattrfunc tp_setattr; */
5210
NULL, /* tp_compare */ /* deprecated in python 3.0! */
5213
/* Method suites for standard classes */
5214
NULL, /* PyNumberMethods *tp_as_number; */
5215
NULL, /* PySequenceMethods *tp_as_sequence; */
5216
NULL, /* PyMappingMethods *tp_as_mapping; */
5218
/* More standard operations (here for binary compatibility) */
5219
NULL, /* hashfunc tp_hash; */
5220
NULL, /* ternaryfunc tp_call; */
5221
NULL, /* reprfunc tp_str; */
5222
NULL /*(getattrofunc) pyrna_struct_meta_idprop_getattro*/, /* getattrofunc tp_getattro; */
5223
(setattrofunc) pyrna_struct_meta_idprop_setattro, /* setattrofunc tp_setattro; */
5225
/* Functions to access object as input/output buffer */
5226
NULL, /* PyBufferProcs *tp_as_buffer; */
5228
/*** Flags to define presence of optional/expanded features ***/
5229
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
5231
NULL, /* char *tp_doc; Documentation string */
5232
/*** Assigned meaning in release 2.0 ***/
5233
/* call function for all accessible objects */
5234
NULL, /* traverseproc tp_traverse; */
5236
/* delete references to contained objects */
5237
NULL, /* inquiry tp_clear; */
5239
/*** Assigned meaning in release 2.1 ***/
5240
/*** rich comparisons ***/
5241
NULL, /* richcmpfunc tp_richcompare; */
5243
/*** weak reference enabler ***/
5244
0, /* long tp_weaklistoffset; */
5246
/*** Added in release 2.2 ***/
5248
NULL, /* getiterfunc tp_iter; */
5249
NULL, /* iternextfunc tp_iternext; */
5251
/*** Attribute descriptor and subclassing stuff ***/
5252
NULL, /* struct PyMethodDef *tp_methods; */
5253
NULL, /* struct PyMemberDef *tp_members; */
5254
NULL, /* struct PyGetSetDef *tp_getset; */
5255
#if defined(_MSC_VER) || defined(FREE_WINDOWS)
5256
NULL, /* defer assignment */
5258
&PyType_Type, /* struct _typeobject *tp_base; */
5260
NULL, /* PyObject *tp_dict; */
5261
NULL, /* descrgetfunc tp_descr_get; */
5262
NULL, /* descrsetfunc tp_descr_set; */
5263
0, /* long tp_dictoffset; */
5264
NULL, /* initproc tp_init; */
5265
NULL, /* allocfunc tp_alloc; */
5266
NULL, /* newfunc tp_new; */
5267
/* Low-level free-memory routine */
5268
NULL, /* freefunc tp_free; */
5269
/* For PyObject_IS_GC */
5270
NULL, /* inquiry tp_is_gc; */
5271
NULL, /* PyObject *tp_bases; */
5272
/* method resolution order */
5273
NULL, /* PyObject *tp_mro; */
5274
NULL, /* PyObject *tp_cache; */
5275
NULL, /* PyObject *tp_subclasses; */
5276
NULL, /* PyObject *tp_weaklist; */
3499
5281
/*-----------------------BPy_StructRNA method def------------------------------*/
3500
5282
PyTypeObject pyrna_struct_Type = {
3501
5283
PyVarObject_HEAD_INIT(NULL, 0)
3502
"bpy_struct", /* tp_name */
3503
sizeof( BPy_StructRNA ), /* tp_basicsize */
3504
0, /* tp_itemsize */
5284
"bpy_struct", /* tp_name */
5285
sizeof(BPy_StructRNA), /* tp_basicsize */
5286
0, /* tp_itemsize */
3506
( destructor ) pyrna_struct_dealloc,/* tp_dealloc */
5288
(destructor) pyrna_struct_dealloc, /* tp_dealloc */
3507
5289
NULL, /* printfunc tp_print; */
3508
NULL, /* getattrfunc tp_getattr; */
3509
NULL, /* setattrfunc tp_setattr; */
3510
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
3511
( reprfunc ) pyrna_struct_repr, /* tp_repr */
5290
NULL, /* getattrfunc tp_getattr; */
5291
NULL, /* setattrfunc tp_setattr; */
5292
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
5293
(reprfunc) pyrna_struct_repr, /* tp_repr */
3513
5295
/* Method suites for standard classes */
3515
5297
NULL, /* PyNumberMethods *tp_as_number; */
3516
&pyrna_struct_as_sequence, /* PySequenceMethods *tp_as_sequence; */
3517
&pyrna_struct_as_mapping, /* PyMappingMethods *tp_as_mapping; */
5298
&pyrna_struct_as_sequence, /* PySequenceMethods *tp_as_sequence; */
5299
&pyrna_struct_as_mapping, /* PyMappingMethods *tp_as_mapping; */
3519
5301
/* More standard operations (here for binary compatibility) */
3521
( hashfunc )pyrna_struct_hash, /* hashfunc tp_hash; */
3522
NULL, /* ternaryfunc tp_call; */
3523
NULL, /* reprfunc tp_str; */
3524
( getattrofunc ) pyrna_struct_getattro, /* getattrofunc tp_getattro; */
3525
( setattrofunc ) pyrna_struct_setattro, /* setattrofunc tp_setattro; */
5303
(hashfunc) pyrna_struct_hash, /* hashfunc tp_hash; */
5304
NULL, /* ternaryfunc tp_call; */
5305
(reprfunc) pyrna_struct_str, /* reprfunc tp_str; */
5306
(getattrofunc) pyrna_struct_getattro, /* getattrofunc tp_getattro; */
5307
(setattrofunc) pyrna_struct_setattro, /* setattrofunc tp_setattro; */
3527
5309
/* Functions to access object as input/output buffer */
3528
5310
NULL, /* PyBufferProcs *tp_as_buffer; */
3530
/*** Flags to define presence of optional/expanded features ***/
3531
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
5312
/*** Flags to define presence of optional/expanded features ***/
5313
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* long tp_flags; */
3533
NULL, /* char *tp_doc; Documentation string */
3534
/*** Assigned meaning in release 2.0 ***/
5315
NULL, /* char *tp_doc; Documentation string */
5316
/*** Assigned meaning in release 2.0 ***/
3535
5317
/* call function for all accessible objects */
5318
#ifdef USE_PYRNA_STRUCT_REFERENCE
5319
(traverseproc) pyrna_struct_traverse, /* traverseproc tp_traverse; */
5321
/* delete references to contained objects */
5322
(inquiry)pyrna_struct_clear, /* inquiry tp_clear; */
3536
5324
NULL, /* traverseproc tp_traverse; */
3538
/* delete references to contained objects */
5326
/* delete references to contained objects */
3539
5327
NULL, /* inquiry tp_clear; */
3541
/*** Assigned meaning in release 2.1 ***/
3542
/*** rich comparisons ***/
3543
(richcmpfunc)pyrna_struct_richcmp, /* richcmpfunc tp_richcompare; */
3545
/*** weak reference enabler ***/
3546
0, /* long tp_weaklistoffset; */
3548
/*** Added in release 2.2 ***/
5328
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
5330
/*** Assigned meaning in release 2.1 ***/
5331
/*** rich comparisons ***/
5332
(richcmpfunc)pyrna_struct_richcmp, /* richcmpfunc tp_richcompare; */
5334
/*** weak reference enabler ***/
5336
offsetof(BPy_StructRNA, in_weakreflist), /* long tp_weaklistoffset; */
5340
/*** Added in release 2.2 ***/
3549
5341
/* Iterators */
3550
5342
NULL, /* getiterfunc tp_iter; */
3551
5343
NULL, /* iternextfunc tp_iternext; */
3553
/*** Attribute descriptor and subclassing stuff ***/
3554
pyrna_struct_methods, /* struct PyMethodDef *tp_methods; */
5345
/*** Attribute descriptor and subclassing stuff ***/
5346
pyrna_struct_methods, /* struct PyMethodDef *tp_methods; */
3555
5347
NULL, /* struct PyMemberDef *tp_members; */
3556
pyrna_struct_getseters, /* struct PyGetSetDef *tp_getset; */
5348
pyrna_struct_getseters, /* struct PyGetSetDef *tp_getset; */
3557
5349
NULL, /* struct _typeobject *tp_base; */
3558
5350
NULL, /* PyObject *tp_dict; */
3559
5351
NULL, /* descrgetfunc tp_descr_get; */
3739
5538
PyTypeObject pyrna_prop_collection_Type = {
3740
5539
PyVarObject_HEAD_INIT(NULL, 0)
3741
"bpy_prop_collection", /* tp_name */
3742
sizeof( BPy_PropertyRNA ), /* tp_basicsize */
3743
0, /* tp_itemsize */
3745
NULL, /* tp_dealloc */
3746
NULL, /* printfunc tp_print; */
3747
NULL, /* getattrfunc tp_getattr; */
3748
NULL, /* setattrfunc tp_setattr; */
3749
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
3750
NULL, /* subclassed */ /* tp_repr */
3752
/* Method suites for standard classes */
3754
NULL, /* PyNumberMethods *tp_as_number; */
3755
&pyrna_prop_collection_as_sequence, /* PySequenceMethods *tp_as_sequence; */
3756
&pyrna_prop_collection_as_mapping, /* PyMappingMethods *tp_as_mapping; */
3758
/* More standard operations (here for binary compatibility) */
3760
NULL, /* hashfunc tp_hash; */
3761
NULL, /* ternaryfunc tp_call; */
3762
NULL, /* reprfunc tp_str; */
3764
/* will only use these if this is a subtype of a py class */
3765
( getattrofunc ) pyrna_prop_collection_getattro, /* getattrofunc tp_getattro; */
3766
( setattrofunc ) pyrna_prop_collection_setattro, /* setattrofunc tp_setattro; */
3768
/* Functions to access object as input/output buffer */
3769
NULL, /* PyBufferProcs *tp_as_buffer; */
3771
/*** Flags to define presence of optional/expanded features ***/
3772
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
3774
NULL, /* char *tp_doc; Documentation string */
3775
/*** Assigned meaning in release 2.0 ***/
3776
/* call function for all accessible objects */
3777
NULL, /* traverseproc tp_traverse; */
3779
/* delete references to contained objects */
3780
NULL, /* inquiry tp_clear; */
3782
/*** Assigned meaning in release 2.1 ***/
3783
/*** rich comparisons ***/
3784
NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
3786
/*** weak reference enabler ***/
3787
0, /* long tp_weaklistoffset; */
3789
/*** Added in release 2.2 ***/
3791
(getiterfunc)pyrna_prop_collection_iter, /* getiterfunc tp_iter; */
3792
NULL, /* iternextfunc tp_iternext; */
3794
/*** Attribute descriptor and subclassing stuff ***/
3795
pyrna_prop_collection_methods, /* struct PyMethodDef *tp_methods; */
3796
NULL, /* struct PyMemberDef *tp_members; */
3797
NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
3798
&pyrna_prop_Type, /* struct _typeobject *tp_base; */
3799
NULL, /* PyObject *tp_dict; */
3800
NULL, /* descrgetfunc tp_descr_get; */
3801
NULL, /* descrsetfunc tp_descr_set; */
3802
0, /* long tp_dictoffset; */
3803
NULL, /* initproc tp_init; */
3804
NULL, /* allocfunc tp_alloc; */
3805
NULL, /* newfunc tp_new; */
3806
/* Low-level free-memory routine */
3807
NULL, /* freefunc tp_free; */
3808
/* For PyObject_IS_GC */
3809
NULL, /* inquiry tp_is_gc; */
3810
NULL, /* PyObject *tp_bases; */
3811
/* method resolution order */
3812
NULL, /* PyObject *tp_mro; */
3813
NULL, /* PyObject *tp_cache; */
3814
NULL, /* PyObject *tp_subclasses; */
3815
NULL, /* PyObject *tp_weaklist; */
3819
static struct PyMethodDef pyrna_struct_subtype_methods[] = {
3820
{"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
3821
{"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
3822
{"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntProperty_doc},
3823
{"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntVectorProperty_doc},
3824
{"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatProperty_doc},
3825
{"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatVectorProperty_doc},
3826
{"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, BPy_StringProperty_doc},
3827
{"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
3828
{"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
3829
{"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
3831
{"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc},
3833
// {"__get_rna", (PyCFunction)BPy_GetStructRNA, METH_NOARGS, ""},
3834
{NULL, NULL, 0, NULL}
5540
"bpy_prop_collection", /* tp_name */
5541
sizeof(BPy_PropertyRNA), /* tp_basicsize */
5542
0, /* tp_itemsize */
5544
(destructor)pyrna_prop_dealloc, /* tp_dealloc */
5545
NULL, /* printfunc tp_print; */
5546
NULL, /* getattrfunc tp_getattr; */
5547
NULL, /* setattrfunc tp_setattr; */
5548
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
5549
NULL, /* subclassed */ /* tp_repr */
5551
/* Method suites for standard classes */
5553
&pyrna_prop_collection_as_number, /* PyNumberMethods *tp_as_number; */
5554
&pyrna_prop_collection_as_sequence, /* PySequenceMethods *tp_as_sequence; */
5555
&pyrna_prop_collection_as_mapping, /* PyMappingMethods *tp_as_mapping; */
5557
/* More standard operations (here for binary compatibility) */
5559
NULL, /* hashfunc tp_hash; */
5560
NULL, /* ternaryfunc tp_call; */
5561
NULL, /* reprfunc tp_str; */
5563
/* will only use these if this is a subtype of a py class */
5564
(getattrofunc) pyrna_prop_collection_getattro, /* getattrofunc tp_getattro; */
5565
(setattrofunc) pyrna_prop_collection_setattro, /* setattrofunc tp_setattro; */
5567
/* Functions to access object as input/output buffer */
5568
NULL, /* PyBufferProcs *tp_as_buffer; */
5570
/*** Flags to define presence of optional/expanded features ***/
5571
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
5573
NULL, /* char *tp_doc; Documentation string */
5574
/*** Assigned meaning in release 2.0 ***/
5575
/* call function for all accessible objects */
5576
NULL, /* traverseproc tp_traverse; */
5578
/* delete references to contained objects */
5579
NULL, /* inquiry tp_clear; */
5581
/*** Assigned meaning in release 2.1 ***/
5582
/*** rich comparisons ***/
5583
NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
5585
/*** weak reference enabler ***/
5587
offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
5592
/*** Added in release 2.2 ***/
5594
(getiterfunc)pyrna_prop_collection_iter, /* getiterfunc tp_iter; */
5595
NULL, /* iternextfunc tp_iternext; */
5597
/*** Attribute descriptor and subclassing stuff ***/
5598
pyrna_prop_collection_methods, /* struct PyMethodDef *tp_methods; */
5599
NULL, /* struct PyMemberDef *tp_members; */
5600
NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
5601
&pyrna_prop_Type, /* struct _typeobject *tp_base; */
5602
NULL, /* PyObject *tp_dict; */
5603
NULL, /* descrgetfunc tp_descr_get; */
5604
NULL, /* descrsetfunc tp_descr_set; */
5605
0, /* long tp_dictoffset; */
5606
NULL, /* initproc tp_init; */
5607
NULL, /* allocfunc tp_alloc; */
5608
NULL, /* newfunc tp_new; */
5609
/* Low-level free-memory routine */
5610
NULL, /* freefunc tp_free; */
5611
/* For PyObject_IS_GC */
5612
NULL, /* inquiry tp_is_gc; */
5613
NULL, /* PyObject *tp_bases; */
5614
/* method resolution order */
5615
NULL, /* PyObject *tp_mro; */
5616
NULL, /* PyObject *tp_cache; */
5617
NULL, /* PyObject *tp_subclasses; */
5618
NULL, /* PyObject *tp_weaklist; */
5622
/* only for add/remove/move methods */
5623
static PyTypeObject pyrna_prop_collection_idprop_Type = {
5624
PyVarObject_HEAD_INIT(NULL, 0)
5625
"bpy_prop_collection_idprop", /* tp_name */
5626
sizeof(BPy_PropertyRNA), /* tp_basicsize */
5627
0, /* tp_itemsize */
5629
(destructor)pyrna_prop_dealloc, /* tp_dealloc */
5630
NULL, /* printfunc tp_print; */
5631
NULL, /* getattrfunc tp_getattr; */
5632
NULL, /* setattrfunc tp_setattr; */
5633
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
5634
NULL, /* subclassed */ /* tp_repr */
5636
/* Method suites for standard classes */
5638
NULL, /* PyNumberMethods *tp_as_number; */
5639
NULL, /* PySequenceMethods *tp_as_sequence; */
5640
NULL, /* PyMappingMethods *tp_as_mapping; */
5642
/* More standard operations (here for binary compatibility) */
5644
NULL, /* hashfunc tp_hash; */
5645
NULL, /* ternaryfunc tp_call; */
5646
NULL, /* reprfunc tp_str; */
5648
/* will only use these if this is a subtype of a py class */
5649
NULL, /* getattrofunc tp_getattro; */
5650
NULL, /* setattrofunc tp_setattro; */
5652
/* Functions to access object as input/output buffer */
5653
NULL, /* PyBufferProcs *tp_as_buffer; */
5655
/*** Flags to define presence of optional/expanded features ***/
5656
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
5658
NULL, /* char *tp_doc; Documentation string */
5659
/*** Assigned meaning in release 2.0 ***/
5660
/* call function for all accessible objects */
5661
NULL, /* traverseproc tp_traverse; */
5663
/* delete references to contained objects */
5664
NULL, /* inquiry tp_clear; */
5666
/*** Assigned meaning in release 2.1 ***/
5667
/*** rich comparisons ***/
5668
NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
5670
/*** weak reference enabler ***/
5672
offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
5677
/*** Added in release 2.2 ***/
5679
NULL, /* getiterfunc tp_iter; */
5680
NULL, /* iternextfunc tp_iternext; */
5682
/*** Attribute descriptor and subclassing stuff ***/
5683
pyrna_prop_collection_idprop_methods, /* struct PyMethodDef *tp_methods; */
5684
NULL, /* struct PyMemberDef *tp_members; */
5685
NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
5686
&pyrna_prop_collection_Type, /* struct _typeobject *tp_base; */
5687
NULL, /* PyObject *tp_dict; */
5688
NULL, /* descrgetfunc tp_descr_get; */
5689
NULL, /* descrsetfunc tp_descr_set; */
5690
0, /* long tp_dictoffset; */
5691
NULL, /* initproc tp_init; */
5692
NULL, /* allocfunc tp_alloc; */
5693
NULL, /* newfunc tp_new; */
5694
/* Low-level free-memory routine */
5695
NULL, /* freefunc tp_free; */
5696
/* For PyObject_IS_GC */
5697
NULL, /* inquiry tp_is_gc; */
5698
NULL, /* PyObject *tp_bases; */
5699
/* method resolution order */
5700
NULL, /* PyObject *tp_mro; */
5701
NULL, /* PyObject *tp_cache; */
5702
NULL, /* PyObject *tp_subclasses; */
5703
NULL, /* PyObject *tp_weaklist; */
5707
/*-----------------------BPy_PropertyRNA method def------------------------------*/
5708
PyTypeObject pyrna_func_Type = {
5709
PyVarObject_HEAD_INIT(NULL, 0)
5710
"bpy_func", /* tp_name */
5711
sizeof(BPy_FunctionRNA), /* tp_basicsize */
5712
0, /* tp_itemsize */
5714
NULL, /* tp_dealloc */
5715
NULL, /* printfunc tp_print; */
5716
NULL, /* getattrfunc tp_getattr; */
5717
NULL, /* setattrfunc tp_setattr; */
5718
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
5719
(reprfunc) pyrna_func_repr, /* tp_repr */
5721
/* Method suites for standard classes */
5723
NULL, /* PyNumberMethods *tp_as_number; */
5724
NULL, /* PySequenceMethods *tp_as_sequence; */
5725
NULL, /* PyMappingMethods *tp_as_mapping; */
5727
/* More standard operations (here for binary compatibility) */
5729
NULL, /* hashfunc tp_hash; */
5730
(ternaryfunc)pyrna_func_call, /* ternaryfunc tp_call; */
5731
NULL, /* reprfunc tp_str; */
5733
/* will only use these if this is a subtype of a py class */
5734
NULL, /* getattrofunc tp_getattro; */
5735
NULL, /* setattrofunc tp_setattro; */
5737
/* Functions to access object as input/output buffer */
5738
NULL, /* PyBufferProcs *tp_as_buffer; */
5740
/*** Flags to define presence of optional/expanded features ***/
5741
Py_TPFLAGS_DEFAULT, /* long tp_flags; */
5743
NULL, /* char *tp_doc; Documentation string */
5744
/*** Assigned meaning in release 2.0 ***/
5745
/* call function for all accessible objects */
5746
NULL, /* traverseproc tp_traverse; */
5748
/* delete references to contained objects */
5749
NULL, /* inquiry tp_clear; */
5751
/*** Assigned meaning in release 2.1 ***/
5752
/*** rich comparisons ***/
5753
NULL, /* richcmpfunc tp_richcompare; */
5755
/*** weak reference enabler ***/
5757
offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
5762
/*** Added in release 2.2 ***/
5764
NULL, /* getiterfunc tp_iter; */
5765
NULL, /* iternextfunc tp_iternext; */
5767
/*** Attribute descriptor and subclassing stuff ***/
5768
NULL, /* struct PyMethodDef *tp_methods; */
5769
NULL, /* struct PyMemberDef *tp_members; */
5770
pyrna_func_getseters, /* struct PyGetSetDef *tp_getset; */
5771
NULL, /* struct _typeobject *tp_base; */
5772
NULL, /* PyObject *tp_dict; */
5773
NULL, /* descrgetfunc tp_descr_get; */
5774
NULL, /* descrsetfunc tp_descr_set; */
5775
0, /* long tp_dictoffset; */
5776
NULL, /* initproc tp_init; */
5777
NULL, /* allocfunc tp_alloc; */
5778
NULL, /* newfunc tp_new; */
5779
/* Low-level free-memory routine */
5780
NULL, /* freefunc tp_free; */
5781
/* For PyObject_IS_GC */
5782
NULL, /* inquiry tp_is_gc; */
5783
NULL, /* PyObject *tp_bases; */
5784
/* method resolution order */
5785
NULL, /* PyObject *tp_mro; */
5786
NULL, /* PyObject *tp_cache; */
5787
NULL, /* PyObject *tp_subclasses; */
5788
NULL, /* PyObject *tp_weaklist; */
5792
#ifdef USE_PYRNA_ITER
5793
/* --- collection iterator: start --- */
5794
/* wrap rna collection iterator functions */
5796
* RNA_property_collection_begin(...)
5797
* RNA_property_collection_next(...)
5798
* RNA_property_collection_end(...)
5801
static void pyrna_prop_collection_iter_dealloc(BPy_PropertyCollectionIterRNA *self);
5802
static PyObject *pyrna_prop_collection_iter_next(BPy_PropertyCollectionIterRNA *self);
5804
PyTypeObject pyrna_prop_collection_iter_Type = {
5805
PyVarObject_HEAD_INIT(NULL, 0)
5806
"bpy_prop_collection_iter", /* tp_name */
5807
sizeof(BPy_PropertyCollectionIterRNA), /* tp_basicsize */
5808
0, /* tp_itemsize */
5810
(destructor)pyrna_prop_collection_iter_dealloc, /* tp_dealloc */
5811
NULL, /* printfunc tp_print; */
5812
NULL, /* getattrfunc tp_getattr; */
5813
NULL, /* setattrfunc tp_setattr; */
5814
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
5815
NULL, /* subclassed */ /* tp_repr */
5817
/* Method suites for standard classes */
5819
NULL, /* PyNumberMethods *tp_as_number; */
5820
NULL, /* PySequenceMethods *tp_as_sequence; */
5821
NULL, /* PyMappingMethods *tp_as_mapping; */
5823
/* More standard operations (here for binary compatibility) */
5825
NULL, /* hashfunc tp_hash; */
5826
NULL, /* ternaryfunc tp_call; */
5827
NULL, /* reprfunc tp_str; */
5829
/* will only use these if this is a subtype of a py class */
5830
#if defined(_MSC_VER) || defined(FREE_WINDOWS)
5831
NULL, /* defer assignment */
5833
PyObject_GenericGetAttr, /* getattrofunc tp_getattro; */
5835
NULL, /* setattrofunc tp_setattro; */
5837
/* Functions to access object as input/output buffer */
5838
NULL, /* PyBufferProcs *tp_as_buffer; */
5840
/*** Flags to define presence of optional/expanded features ***/
5841
Py_TPFLAGS_DEFAULT, /* long tp_flags; */
5843
NULL, /* char *tp_doc; Documentation string */
5844
/*** Assigned meaning in release 2.0 ***/
5845
/* call function for all accessible objects */
5846
NULL, /* traverseproc tp_traverse; */
5848
/* delete references to contained objects */
5849
NULL, /* inquiry tp_clear; */
5851
/*** Assigned meaning in release 2.1 ***/
5852
/*** rich comparisons ***/
5853
NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
5855
/*** weak reference enabler ***/
5857
offsetof(BPy_PropertyCollectionIterRNA, in_weakreflist), /* long tp_weaklistoffset; */
5861
/*** Added in release 2.2 ***/
5863
#if defined(_MSC_VER) || defined(FREE_WINDOWS)
5864
NULL, /* defer assignment */
5866
PyObject_SelfIter, /* getiterfunc tp_iter; */
5868
(iternextfunc) pyrna_prop_collection_iter_next, /* iternextfunc tp_iternext; */
5870
/*** Attribute descriptor and subclassing stuff ***/
5871
NULL, /* struct PyMethodDef *tp_methods; */
5872
NULL, /* struct PyMemberDef *tp_members; */
5873
NULL, /* struct PyGetSetDef *tp_getset; */
5874
NULL, /* struct _typeobject *tp_base; */
5875
NULL, /* PyObject *tp_dict; */
5876
NULL, /* descrgetfunc tp_descr_get; */
5877
NULL, /* descrsetfunc tp_descr_set; */
5878
0, /* long tp_dictoffset; */
5879
NULL, /* initproc tp_init; */
5880
NULL, /* allocfunc tp_alloc; */
5881
NULL, /* newfunc tp_new; */
5882
/* Low-level free-memory routine */
5883
NULL, /* freefunc tp_free; */
5884
/* For PyObject_IS_GC */
5885
NULL, /* inquiry tp_is_gc; */
5886
NULL, /* PyObject *tp_bases; */
5887
/* method resolution order */
5888
NULL, /* PyObject *tp_mro; */
5889
NULL, /* PyObject *tp_cache; */
5890
NULL, /* PyObject *tp_subclasses; */
5891
NULL, /* PyObject *tp_weaklist; */
5895
PyObject *pyrna_prop_collection_iter_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
5897
BPy_PropertyCollectionIterRNA *self = PyObject_New(BPy_PropertyCollectionIterRNA, &pyrna_prop_collection_iter_Type);
5900
self->in_weakreflist = NULL;
5903
RNA_property_collection_begin(ptr, prop, &self->iter);
5905
return (PyObject *)self;
5908
static PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self)
5910
return pyrna_prop_collection_iter_CreatePyObject(&self->ptr, self->prop);
5913
static PyObject *pyrna_prop_collection_iter_next(BPy_PropertyCollectionIterRNA *self)
5915
if (self->iter.valid == FALSE) {
5916
PyErr_SetString(PyExc_StopIteration, "pyrna_prop_collection_iter stop");
5920
BPy_StructRNA *pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&self->iter.ptr);
5922
#ifdef USE_PYRNA_STRUCT_REFERENCE
5923
if (pyrna) { /* unlikely but may fail */
5924
if ((PyObject *)pyrna != Py_None) {
5925
/* hold a reference to the iterator since it may have
5926
* allocated memory 'pyrna' needs. eg: introspecting dynamic enum's */
5927
/* TODO, we could have an api call to know if this is needed since most collections don't */
5928
pyrna_struct_reference_set(pyrna, (PyObject *)self);
5931
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
5933
RNA_property_collection_next(&self->iter);
5935
return (PyObject *)pyrna;
5940
static void pyrna_prop_collection_iter_dealloc(BPy_PropertyCollectionIterRNA *self)
5943
if (self->in_weakreflist != NULL) {
5944
PyObject_ClearWeakRefs((PyObject *)self);
5948
RNA_property_collection_end(&self->iter);
5953
/* --- collection iterator: end --- */
5954
#endif /* !USE_PYRNA_ITER */
3837
5957
static void pyrna_subtype_set_rna(PyObject *newclass, StructRNA *srna)
3839
5959
PointerRNA ptr;
3840
5960
PyObject *item;
3842
5962
Py_INCREF(newclass);
3844
5964
if (RNA_struct_py_type_get(srna))
3845
PyObSpit("RNA WAS SET - ", RNA_struct_py_type_get(srna));
5965
PyC_ObSpit("RNA WAS SET - ", RNA_struct_py_type_get(srna));
3847
5967
Py_XDECREF(((PyObject *)RNA_struct_py_type_get(srna)));
3849
5969
RNA_struct_py_type_set(srna, (void *)newclass); /* Store for later use */
3851
5971
/* Not 100% needed but useful,
3852
5972
* having an instance within a type looks wrong however this instance IS an rna type */
3854
/* python deals with the curcular ref */
5974
/* python deals with the circular ref */
3855
5975
RNA_pointer_create(NULL, &RNA_Struct, srna, &ptr);
3856
5976
item = pyrna_struct_CreatePyObject(&ptr);
3858
//item = PyCapsule_New(srna, NULL, NULL);
3859
PyDict_SetItemString(((PyTypeObject *)newclass)->tp_dict, "bl_rna", item);
5978
/* note, must set the class not the __dict__ else the internal slots are not updated correctly */
5979
PyObject_SetAttr(newclass, bpy_intern_str_bl_rna, item);
3860
5980
Py_DECREF(item);
3861
5982
/* done with rna instance */
3863
/* attach functions into the class
3864
* so you can do... bpy.types.Scene.SomeFunction()
3869
for(ml= pyrna_struct_subtype_methods; ml->ml_name; ml++){
3870
PyObject_SetAttrString(newclass, ml->ml_name, PyCFunction_New(ml, newclass));
3875
static PyObject* pyrna_srna_Subtype(StructRNA *srna);
5985
static PyObject *pyrna_srna_Subtype(StructRNA *srna);
3877
5987
/* return a borrowed reference */
3878
static PyObject* pyrna_srna_PyBase(StructRNA *srna) //, PyObject *bpy_types_dict)
5988
static PyObject *pyrna_srna_PyBase(StructRNA *srna) //, PyObject *bpy_types_dict)
3880
5990
/* Assume RNA_struct_py_type_get(srna) was already checked */
3881
5991
StructRNA *base;
3883
PyObject *py_base= NULL;
5993
PyObject *py_base = NULL;
3885
5995
/* get the base type */
3886
base= RNA_struct_base(srna);
5996
base = RNA_struct_base(srna);
3888
if(base && base != srna) {
3889
/*/printf("debug subtype %s %p\n", RNA_struct_identifier(srna), srna); */
3890
py_base= pyrna_srna_Subtype(base); //, bpy_types_dict);
5998
if (base && base != srna) {
5999
/* printf("debug subtype %s %p\n", RNA_struct_identifier(srna), srna); */
6000
py_base = pyrna_srna_Subtype(base); //, bpy_types_dict);
3891
6001
Py_DECREF(py_base); /* srna owns, this is only to pass as an arg */
3895
py_base= (PyObject *)&pyrna_struct_Type;
6004
if (py_base == NULL) {
6005
py_base = (PyObject *)&pyrna_struct_Type;
3898
6008
return py_base;
4014
6144
/* always returns a new ref, be sure to decref when done */
4015
static PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
6145
static PyObject *pyrna_struct_Subtype(PointerRNA *ptr)
4017
6147
return pyrna_srna_Subtype(srna_from_ptr(ptr));
4020
6150
/*-----------------------CreatePyObject---------------------------------*/
4021
PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
6151
PyObject *pyrna_struct_CreatePyObject(PointerRNA *ptr)
4023
BPy_StructRNA *pyrna= NULL;
6153
BPy_StructRNA *pyrna = NULL;
4025
6155
/* note: don't rely on this to return None since NULL data with a valid type can often crash */
4026
if (ptr->data==NULL && ptr->type==NULL) { /* Operator RNA has NULL data */
6156
if (ptr->data == NULL && ptr->type == NULL) { /* Operator RNA has NULL data */
4027
6157
Py_RETURN_NONE;
4030
6160
PyTypeObject *tp = (PyTypeObject *)pyrna_struct_Subtype(ptr);
4033
6163
pyrna = (BPy_StructRNA *) tp->tp_alloc(tp, 0);
4034
6164
Py_DECREF(tp); /* srna owns, cant hold a ref */
4037
fprintf(stderr, "Could not make type\n");
4038
pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
6167
fprintf(stderr, "%s: could not make type\n", __func__);
6168
pyrna = (BPy_StructRNA *) PyObject_GC_New(BPy_StructRNA, &pyrna_struct_Type);
6170
pyrna->in_weakreflist = NULL;
4043
PyErr_SetString( PyExc_MemoryError, "couldn't create bpy_struct object" );
6175
if (pyrna == NULL) {
6176
PyErr_SetString(PyExc_MemoryError, "couldn't create bpy_struct object");
4048
pyrna->freeptr= FALSE;
4050
// PyObSpit("NewStructRNA: ", (PyObject *)pyrna);
4052
return ( PyObject * ) pyrna;
6181
#ifdef PYRNA_FREE_SUPPORT
6182
pyrna->freeptr = FALSE;
6185
#ifdef USE_PYRNA_STRUCT_REFERENCE
6186
pyrna->reference = NULL;
6189
// PyC_ObSpit("NewStructRNA: ", (PyObject *)pyrna);
6191
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
6193
id_weakref_pool_add(ptr->id.data, (BPy_DummyPointerRNA *)pyrna);
6196
return (PyObject *)pyrna;
4055
PyObject *pyrna_prop_CreatePyObject( PointerRNA *ptr, PropertyRNA *prop )
6199
PyObject *pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
4057
6201
BPy_PropertyRNA *pyrna;
4059
if (RNA_property_type(prop) == PROP_COLLECTION) type= &pyrna_prop_collection_Type;
4060
else if (RNA_property_array_check(ptr, prop)) type= &pyrna_prop_array_Type;
4061
else type= &pyrna_prop_Type;
4063
pyrna = ( BPy_PropertyRNA * ) PyObject_NEW(BPy_PropertyRNA, type);
4066
PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_rna object" );
6203
if (RNA_property_array_check(prop) == 0) {
6206
if (RNA_property_type(prop) != PROP_COLLECTION) {
6207
type = &pyrna_prop_Type;
6210
if ((RNA_property_flag(prop) & PROP_IDPROPERTY) == 0) {
6211
type = &pyrna_prop_collection_Type;
6214
type = &pyrna_prop_collection_idprop_Type;
6218
pyrna = (BPy_PropertyRNA *) PyObject_NEW(BPy_PropertyRNA, type);
6220
pyrna->in_weakreflist = NULL;
6224
pyrna = (BPy_PropertyRNA *) PyObject_NEW(BPy_PropertyArrayRNA, &pyrna_prop_array_Type);
6225
((BPy_PropertyArrayRNA *)pyrna)->arraydim = 0;
6226
((BPy_PropertyArrayRNA *)pyrna)->arrayoffset = 0;
6228
((BPy_PropertyArrayRNA *)pyrna)->in_weakreflist = NULL;
6232
if (pyrna == NULL) {
6233
PyErr_SetString(PyExc_MemoryError, "couldn't create BPy_rna object");
4070
6237
pyrna->ptr = *ptr;
4071
6238
pyrna->prop = prop;
4074
pyrna->arrayoffset= 0;
4076
return ( PyObject * ) pyrna;
6240
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
6242
id_weakref_pool_add(ptr->id.data, (BPy_DummyPointerRNA *)pyrna);
6246
return (PyObject *)pyrna;
6249
/* utility func to be used by external modules, *sneaky!* */
6250
PyObject *pyrna_id_CreatePyObject(ID *id)
6254
RNA_id_pointer_create(id, &ptr);
6255
return pyrna_struct_CreatePyObject(&ptr);
6262
int pyrna_id_FromPyObject(PyObject *obj, ID **id)
6264
if (BPy_StructRNA_Check(obj) && (RNA_struct_is_ID(((BPy_StructRNA *)obj)->ptr.type))) {
6265
*id = ((BPy_StructRNA *)obj)->ptr.id.data;
4079
6274
void BPY_rna_init(void)
4081
6276
#ifdef USE_MATHUTILS // register mathutils callbacks, ok to run more then once.
4082
mathutils_rna_array_cb_index= Mathutils_RegisterCallback(&mathutils_rna_array_cb);
4083
mathutils_rna_matrix_cb_index= Mathutils_RegisterCallback(&mathutils_rna_matrix_cb);
4086
if( PyType_Ready( &pyrna_struct_Type ) < 0 )
4089
if( PyType_Ready( &pyrna_prop_Type ) < 0 )
4092
if( PyType_Ready( &pyrna_prop_array_Type ) < 0 )
4095
if( PyType_Ready( &pyrna_prop_collection_Type ) < 0 )
6277
mathutils_rna_array_cb_index = Mathutils_RegisterCallback(&mathutils_rna_array_cb);
6278
mathutils_rna_matrix_cb_index = Mathutils_RegisterCallback(&mathutils_rna_matrix_cb);
6281
/* for some reason MSVC complains of these */
6282
#if defined(_MSC_VER) || defined(FREE_WINDOWS)
6283
pyrna_struct_meta_idprop_Type.tp_base = &PyType_Type;
6285
pyrna_prop_collection_iter_Type.tp_iter = PyObject_SelfIter;
6286
pyrna_prop_collection_iter_Type.tp_getattro = PyObject_GenericGetAttr;
6290
if (PyType_Ready(&pyrna_struct_meta_idprop_Type) < 0)
6293
if (PyType_Ready(&pyrna_struct_Type) < 0)
6296
if (PyType_Ready(&pyrna_prop_Type) < 0)
6299
if (PyType_Ready(&pyrna_prop_array_Type) < 0)
6302
if (PyType_Ready(&pyrna_prop_collection_Type) < 0)
6305
if (PyType_Ready(&pyrna_prop_collection_idprop_Type) < 0)
6308
if (PyType_Ready(&pyrna_func_Type) < 0)
6311
#ifdef USE_PYRNA_ITER
6312
if (PyType_Ready(&pyrna_prop_collection_iter_Type) < 0)
4099
6317
/* bpy.data from python */
4100
static PointerRNA *rna_module_ptr= NULL;
6318
static PointerRNA *rna_module_ptr = NULL;
4101
6319
PyObject *BPY_rna_module(void)
4103
6321
BPy_StructRNA *pyrna;
4104
6322
PointerRNA ptr;
4106
/* for now, return the base RNA type rather then a real module */
6324
/* for now, return the base RNA type rather than a real module */
4107
6325
RNA_main_pointer_create(G.main, &ptr);
4108
pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
4110
rna_module_ptr= &pyrna->ptr;
6326
pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
6328
rna_module_ptr = &pyrna->ptr;
4111
6329
return (PyObject *)pyrna;
4114
6332
void BPY_update_rna_module(void)
4116
6335
RNA_main_pointer_create(G.main, rna_module_ptr);
6337
rna_module_ptr->data = G.main; /* just set data is enough */
4120
6342
/* This is a way we can access docstrings for RNA types
4121
6343
* without having the datatypes in blender */
4122
PyObject *BPY_rna_doc( void )
6344
PyObject *BPY_rna_doc(void)
4124
6346
PointerRNA ptr;
4126
/* for now, return the base RNA type rather then a real module */
6348
/* for now, return the base RNA type rather than a real module */
4127
6349
RNA_blender_rna_pointer_create(&ptr);
4129
6351
return pyrna_struct_CreatePyObject(&ptr);
4134
/* pyrna_basetype_* - BPy_BaseTypeRNA is just a BPy_PropertyRNA struct with a differnt type
6356
/* pyrna_basetype_* - BPy_BaseTypeRNA is just a BPy_PropertyRNA struct with a different type
4135
6357
* the self->ptr and self->prop are always set to the "structs" collection */
4136
6358
//---------------getattr--------------------------------------------
4137
static PyObject *pyrna_basetype_getattro( BPy_BaseTypeRNA *self, PyObject *pyname )
6359
static PyObject *pyrna_basetype_getattro(BPy_BaseTypeRNA *self, PyObject *pyname)
4139
6361
PointerRNA newptr;
4141
char *name= _PyUnicode_AsString(pyname);
4143
if(strcmp(name, "register")==0) {
4144
/* this is called so often, make an exception and save a full lookup on all types */
4145
ret= PyObject_GenericGetAttr((PyObject *)self, pyname);
6363
const char *name = _PyUnicode_AsString(pyname);
6366
PyErr_SetString(PyExc_AttributeError, "bpy.types: __getattr__ must be a string");
4147
6369
else if (RNA_property_collection_lookup_string(&self->ptr, self->prop, name, &newptr)) {
4148
ret= pyrna_struct_Subtype(&newptr);
4150
PyErr_Format(PyExc_SystemError, "bpy.types.%.200s subtype could not be generated, this is a bug!", _PyUnicode_AsString(pyname));
6370
ret = pyrna_struct_Subtype(&newptr);
6372
PyErr_Format(PyExc_RuntimeError,
6373
"bpy.types.%.200s subtype could not be generated, this is a bug!",
6374
_PyUnicode_AsString(pyname));
4155
PyErr_Format(PyExc_AttributeError, "bpy.types.%.200s RNA_Struct does not exist", _PyUnicode_AsString(pyname));
6379
PyErr_Format(PyExc_AttributeError,
6380
"bpy.types.%.200s RNA_Struct does not exist",
6381
_PyUnicode_AsString(pyname));
4158
6384
/* The error raised here will be displayed */
4159
ret= PyObject_GenericGetAttr((PyObject *)self, pyname);
6385
ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
4165
6391
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self);
6392
static PyObject *pyrna_register_class(PyObject *self, PyObject *py_class);
6393
static PyObject *pyrna_unregister_class(PyObject *self, PyObject *py_class);
4166
6395
static struct PyMethodDef pyrna_basetype_methods[] = {
4167
6396
{"__dir__", (PyCFunction)pyrna_basetype_dir, METH_NOARGS, ""},
4168
{"register", (PyCFunction)pyrna_basetype_register, METH_O, ""},
4169
{"unregister", (PyCFunction)pyrna_basetype_unregister, METH_O, ""},
4170
6397
{NULL, NULL, 0, NULL}
6400
/* used to call ..._keys() direct, but we need to filter out operator subclasses */
4173
6402
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
4175
PyObject *list, *name;
4176
6407
PyMethodDef *meth;
4178
list= pyrna_prop_keys(self); /* like calling structs.keys(), avoids looping here */
4180
for(meth=pyrna_basetype_methods; meth->ml_name; meth++) {
6410
list = pyrna_prop_collection_keys(self); /* like calling structs.keys(), avoids looping here */
6412
#if 0 /* for now only contains __dir__ */
6413
for (meth = pyrna_basetype_methods; meth->ml_name; meth++) {
4181
6414
name = PyUnicode_FromString(meth->ml_name);
4182
6415
PyList_Append(list, name);
4183
6416
Py_DECREF(name);
4189
PyTypeObject pyrna_basetype_Type = BLANK_PYTHON_TYPE;
6424
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
6426
PyObject *ret = PyList_New(0);
6429
RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
6430
StructRNA *srna = itemptr.data;
6431
StructRNA *srna_base = RNA_struct_base(itemptr.data);
6432
/* skip own operators, these double up [#29666] */
6433
if (srna_base == &RNA_Operator) {
6437
/* add to python list */
6438
item = PyUnicode_FromString(RNA_struct_identifier(srna));
6439
PyList_Append(ret, item);
6450
static PyTypeObject pyrna_basetype_Type = BLANK_PYTHON_TYPE;
4191
6452
PyObject *BPY_rna_types(void)
4193
6454
BPy_BaseTypeRNA *self;
4195
if ((pyrna_basetype_Type.tp_flags & Py_TPFLAGS_READY)==0) {
6456
if ((pyrna_basetype_Type.tp_flags & Py_TPFLAGS_READY) == 0) {
4196
6457
pyrna_basetype_Type.tp_name = "RNA_Types";
4197
pyrna_basetype_Type.tp_basicsize = sizeof( BPy_BaseTypeRNA );
4198
pyrna_basetype_Type.tp_getattro = ( getattrofunc )pyrna_basetype_getattro;
6458
pyrna_basetype_Type.tp_basicsize = sizeof(BPy_BaseTypeRNA);
6459
pyrna_basetype_Type.tp_getattro = (getattrofunc) pyrna_basetype_getattro;
4199
6460
pyrna_basetype_Type.tp_flags = Py_TPFLAGS_DEFAULT;
4200
6461
pyrna_basetype_Type.tp_methods = pyrna_basetype_methods;
4202
if( PyType_Ready( &pyrna_basetype_Type ) < 0 )
6463
if (PyType_Ready(&pyrna_basetype_Type) < 0)
4206
self= (BPy_BaseTypeRNA *)PyObject_NEW( BPy_BaseTypeRNA, &pyrna_basetype_Type );
4207
self->arraydim = self->arrayoffset = 0; /* unused but better set */
6467
self = (BPy_BaseTypeRNA *)PyObject_NEW(BPy_BaseTypeRNA, &pyrna_basetype_Type);
4209
6469
/* avoid doing this lookup for every getattr */
4210
6470
RNA_blender_rna_pointer_create(&self->ptr);
4211
6471
self->prop = RNA_struct_find_property(&self->ptr, "structs");
6473
self->in_weakreflist = NULL;
4213
6475
return (PyObject *)self;
4515
extern void BPY_update_modules( void ); //XXX temp solution
4517
6868
/* TODO - multiple return values like with rna functions */
4518
static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *parms)
6869
static int bpy_class_call(bContext *C, PointerRNA *ptr, FunctionRNA *func, ParameterList *parms)
4520
6871
PyObject *args;
4521
PyObject *ret= NULL, *py_srna= NULL, *py_class, *py_class_instance= NULL, *parmitem;
4522
void **py_class_instance_store= NULL;
6872
PyObject *ret = NULL, *py_srna = NULL, *py_class_instance = NULL, *parmitem;
6873
PyTypeObject *py_class;
6874
void **py_class_instance_store = NULL;
4523
6875
PropertyRNA *parm;
4524
6876
ParameterIterator iter;
4525
6877
PointerRNA funcptr;
4526
int err= 0, i, flag, ret_len=0;
4528
PropertyRNA *pret_single= NULL;
4529
void *retdata_single= NULL;
6878
int err = 0, i, flag, ret_len = 0;
6879
const char is_static = (RNA_function_flag(func) & FUNC_NO_SELF) != 0;
6881
/* annoying!, need to check if the screen gets set to NULL which is a
6882
* hint that the file was actually re-loaded. */
6885
PropertyRNA *pret_single = NULL;
6886
void *retdata_single = NULL;
4531
6888
PyGILState_STATE gilstate;
4533
bContext *C= BPy_GetContext(); // XXX - NEEDS FIXING, QUITE BAD.
6890
#ifdef USE_PEDANTIC_WRITE
6891
const int is_operator = RNA_struct_is_a(ptr->type, &RNA_Operator);
6892
const char *func_id = RNA_function_identifier(func);
6893
/* testing, for correctness, not operator and not draw function */
6894
const short is_readonly = ((strncmp("draw", func_id, 4) == 0) || /* draw or draw_header */
6895
/*strstr("render", func_id) ||*/
6899
py_class = RNA_struct_py_type_get(ptr->type);
6900
/* rare case. can happen when registering subclasses */
6901
if (py_class == NULL) {
6902
fprintf(stderr, "%s: unable to get python class for rna struct '%.200s'\n",
6903
__func__, RNA_struct_identifier(ptr->type));
6907
/* XXX, this is needed because render engine calls without a context
6908
* this should be supported at some point but at the moment its not! */
6910
C = BPy_GetContext();
6912
is_valid_wm = (CTX_wm_manager(C) != NULL);
4534
6914
bpy_context_set(C, &gilstate);
4536
py_class= RNA_struct_py_type_get(ptr->type);
4538
/* exception, operators store their PyObjects for re-use */
4540
if(RNA_struct_is_a(ptr->type, &RNA_Operator)) {
4541
wmOperator *op= ptr->data;
4542
if(op->py_instance) {
4543
py_class_instance= op->py_instance;
6917
/* some datatypes (operator, render engine) can store PyObjects for re-use */
6919
void **instance = RNA_struct_instance(ptr);
6923
py_class_instance = *instance;
6924
Py_INCREF(py_class_instance);
6927
/* store the instance here once its created */
6928
py_class_instance_store = instance;
6934
if (py_class_instance == NULL)
6935
py_srna = pyrna_struct_CreatePyObject(ptr);
6937
if (py_class_instance) {
6938
/* special case, instance is cached */
6940
else if (py_srna == NULL) {
6941
py_class_instance = NULL;
6943
else if (py_srna == Py_None) { /* probably wont ever happen but possible */
6945
py_class_instance = NULL;
6949
/* Skip the code below and call init directly on the allocated 'py_srna'
6950
* otherwise __init__() always needs to take a second self argument, see pyrna_struct_new().
6951
* Although this is annoying to have to implement a part of pythons typeobject.c:type_call().
6953
if (py_class->tp_init) {
6954
#ifdef USE_PEDANTIC_WRITE
6955
const int prev_write = rna_disallow_writes;
6956
rna_disallow_writes = is_operator ? FALSE : TRUE; /* only operators can write on __init__ */
6959
/* true in most cases even when the class its self doesn't define an __init__ function. */
6960
args = PyTuple_New(0);
6961
if (py_class->tp_init(py_srna, args, NULL) < 0) {
6967
#ifdef USE_PEDANTIC_WRITE
6968
rna_disallow_writes = prev_write;
6971
py_class_instance = py_srna;
6974
const int prev_write = rna_disallow_writes;
6975
rna_disallow_writes = TRUE;
6977
/* 'almost' all the time calling the class isn't needed.
6978
* We could just do... */
6980
py_class_instance = py_srna;
6981
Py_INCREF(py_class_instance);
6984
* This would work fine but means __init__ functions wouldnt run.
6985
* none of blenders default scripts use __init__ but its nice to call it
6986
* for general correctness. just to note why this is here when it could be safely removed.
6988
args = PyTuple_New(1);
6989
PyTuple_SET_ITEM(args, 0, py_srna);
6990
py_class_instance = PyObject_Call(py_class, args, NULL);
6993
rna_disallow_writes = prev_write;
6997
if (py_class_instance == NULL) {
6998
err = -1; /* so the error is not overridden below */
7000
else if (py_class_instance_store) {
7001
*py_class_instance_store = py_class_instance;
4544
7002
Py_INCREF(py_class_instance);
4547
/* store the instance here once its created */
4548
py_class_instance_store= &op->py_instance;
4554
if(py_class_instance==NULL)
4555
py_srna= pyrna_struct_CreatePyObject(ptr);
4557
if(py_class_instance) {
4558
/* special case, instance is cached */
4560
else if(py_srna == NULL) {
4561
py_class_instance = NULL;
4563
else if(py_srna == Py_None) { /* probably wont ever happen but possible */
4565
py_class_instance = NULL;
4568
args = PyTuple_New(1);
4569
PyTuple_SET_ITEM(args, 0, py_srna);
4570
py_class_instance = PyObject_Call(py_class, args, NULL);
4573
if(py_class_instance_store) {
4574
*py_class_instance_store = py_class_instance;
4575
Py_INCREF(py_class_instance);
4579
if (py_class_instance) { /* Initializing the class worked, now run its invoke function */
4580
PyObject *item= PyObject_GetAttrString(py_class, RNA_function_identifier(func));
4581
// flag= RNA_function_flag(func);
7007
if (err != -1 && (is_static || py_class_instance)) { /* Initializing the class worked, now run its invoke function */
7008
PyObject *item = PyObject_GetAttrString((PyObject *)py_class, RNA_function_identifier(func));
7009
// flag = RNA_function_flag(func);
4584
7012
RNA_pointer_create(NULL, &RNA_Function, func, &funcptr);
4586
7014
args = PyTuple_New(rna_function_arg_count(func)); /* first arg is included in 'item' */
4587
PyTuple_SET_ITEM(args, 0, py_class_instance);
7020
PyTuple_SET_ITEM(args, 0, py_class_instance);
4589
7024
RNA_parameter_list_begin(parms, &iter);
4591
7026
/* parse function parameters */
4592
for (i= 1; iter.valid; RNA_parameter_list_next(&iter)) {
4594
flag= RNA_property_flag(parm);
7027
for (; iter.valid; RNA_parameter_list_next(&iter)) {
7029
flag = RNA_property_flag(parm);
4596
7031
/* only useful for single argument returns, we'll need another list loop for multiple */
4597
7032
if (flag & PROP_OUTPUT) {
4599
if (pret_single==NULL) {
4601
retdata_single= iter.data;
7034
if (pret_single == NULL) {
7036
retdata_single = iter.data;
4607
parmitem= pyrna_param_to_py(&funcptr, parms, parm, iter.data);
7042
parmitem = pyrna_param_to_py(&funcptr, parm, iter.data);
4608
7043
PyTuple_SET_ITEM(args, i, parmitem);
7047
#ifdef USE_PEDANTIC_WRITE
7048
rna_disallow_writes = is_readonly ? TRUE : FALSE;
7050
/* *** Main Caller *** */
4612
7052
ret = PyObject_Call(item, args, NULL);
7054
/* *** Done Calling *** */
7056
#ifdef USE_PEDANTIC_WRITE
7057
rna_disallow_writes = FALSE;
4614
7060
RNA_parameter_list_end(&iter);
4615
7061
Py_DECREF(item);
4616
7062
Py_DECREF(args);
4755
7262
* There is currently a bug where moving registering a python class does
4756
7263
* not properly manage refcounts from the python class, since the srna owns
4757
7264
* the python class this should not be so tricky but changing the references as
4758
* youd expect when changing ownership crashes blender on exit so I had to comment out
7265
* you'd expect when changing ownership crashes blender on exit so I had to comment out
4759
7266
* the decref. This is not so bad because the leak only happens when re-registering (hold F8)
4760
7267
* - Should still be fixed - Campbell
4763
PyObject *pyrna_basetype_register(PyObject *self, PyObject *py_class)
7269
PyDoc_STRVAR(pyrna_register_class_doc,
7270
".. method:: register_class(cls)\n"
7272
" Register a subclass of a blender type in (:class:`bpy.types.Panel`,\n"
7273
" :class:`bpy.types.Menu`, :class:`bpy.types.Header`, :class:`bpy.types.Operator`,\n"
7274
" :class:`bpy.types.KeyingSetInfo`, :class:`bpy.types.RenderEngine`).\n"
7276
" If the class has a *register* class method it will be called\n"
7277
" before registration.\n"
7281
" :exc:`ValueError` exception is raised if the class is not a\n"
7282
" subclass of a registerable blender class.\n"
7285
PyMethodDef meth_bpy_register_class = {"register_class", pyrna_register_class, METH_O, pyrna_register_class_doc};
7286
static PyObject *pyrna_register_class(PyObject *UNUSED(self), PyObject *py_class)
4766
7289
ReportList reports;
4767
7290
StructRegisterFunc reg;
4768
7291
StructRNA *srna;
4769
7292
StructRNA *srna_new;
4771
const char *identifier= "";
4773
if(PyDict_GetItemString(((PyTypeObject*)py_class)->tp_dict, "bl_rna")) {
4774
PyErr_SetString(PyExc_AttributeError, "bpy.types.register(...): already registered as a subclass.");
7293
const char *identifier;
7294
PyObject *py_cls_meth;
7296
if (!PyType_Check(py_class)) {
7297
PyErr_Format(PyExc_ValueError,
7298
"register_class(...): "
7299
"expected a class argument, not '%.200s'", Py_TYPE(py_class)->tp_name);
7303
if (PyDict_GetItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna)) {
7304
PyErr_SetString(PyExc_ValueError,
7305
"register_class(...): "
7306
"already registered as a subclass");
7310
if (!pyrna_write_check()) {
7311
PyErr_Format(PyExc_RuntimeError,
7312
"register_class(...): "
7313
"can't run in readonly state '%.200s'",
7314
((PyTypeObject *)py_class)->tp_name);
4778
7318
/* warning: gets parent classes srna, only for the register function */
4779
srna= pyrna_struct_as_srna(py_class, 1, "bpy.types.register(...):");
7319
srna = pyrna_struct_as_srna(py_class, 1, "register_class(...):");
7323
/* fails in cases, cant use this check but would like to :| */
7325
if (RNA_struct_py_type_get(srna)) {
7326
PyErr_Format(PyExc_ValueError,
7327
"register_class(...): %.200s's parent class %.200s is already registered, this is not allowed",
7328
((PyTypeObject *)py_class)->tp_name, RNA_struct_identifier(srna));
4783
7333
/* check that we have a register callback for this type */
4784
reg= RNA_struct_register(srna);
7334
reg = RNA_struct_register(srna);
4787
PyErr_SetString(PyExc_ValueError, "bpy.types.register(...): expected a Type subclassed from a registerable rna type (no register supported).");
7337
PyErr_Format(PyExc_ValueError,
7338
"register_class(...): expected a subclass of a registerable "
7339
"rna type (%.200s does not support registration)",
7340
RNA_struct_identifier(srna));
4791
7344
/* get the context, so register callback can do necessary refreshes */
4792
C= BPy_GetContext();
7345
C = BPy_GetContext();
4794
7347
/* call the register callback with reports & identifier */
4795
7348
BKE_reports_init(&reports, RPT_STORE);
4797
item= PyObject_GetAttrString(py_class, "__name__");
4800
identifier= _PyUnicode_AsString(item);
4801
Py_DECREF(item); /* no need to keep a ref, the class owns it */
4804
srna_new= reg(C, &reports, py_class, identifier, bpy_class_validate, bpy_class_call, bpy_class_free);
4807
BPy_reports_to_error(&reports);
4808
BKE_reports_clear(&reports);
4812
BKE_reports_clear(&reports);
7350
identifier = ((PyTypeObject *)py_class)->tp_name;
7352
srna_new = reg(CTX_data_main(C), &reports, py_class, identifier,
7353
bpy_class_validate, bpy_class_call, bpy_class_free);
7355
if (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE) == -1)
7358
/* python errors validating are not converted into reports so the check above will fail.
7359
* the cause for returning NULL will be printed as an error */
7360
if (srna_new == NULL)
4814
7363
pyrna_subtype_set_rna(py_class, srna_new); /* takes a ref to py_class */
4816
/* old srna still references us, keep the check incase registering somehow can free it */
4817
if(RNA_struct_py_type_get(srna)) {
7365
/* old srna still references us, keep the check in case registering somehow can free it */
7366
if (RNA_struct_py_type_get(srna)) {
4818
7367
RNA_struct_py_type_set(srna, NULL);
4819
// Py_DECREF(py_class); // shuld be able to do this XXX since the old rna adds a new ref.
7368
// Py_DECREF(py_class); // should be able to do this XXX since the old rna adds a new ref.
4822
7371
/* Can't use this because it returns a dict proxy
4824
* item= PyObject_GetAttrString(py_class, "__dict__");
7373
* item = PyObject_GetAttrString(py_class, "__dict__");
4826
item= ((PyTypeObject*)py_class)->tp_dict;
4828
if(pyrna_deferred_register_props(srna_new, item)!=0) {
7375
if (pyrna_deferred_register_class(srna_new, py_class) != 0)
7378
/* call classed register method () */
7379
py_cls_meth = PyObject_GetAttr(py_class, bpy_intern_str_register);
7380
if (py_cls_meth == NULL) {
7384
PyObject *ret = PyObject_CallObject(py_cls_meth, NULL);
4836
7393
Py_RETURN_NONE;
4839
PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *py_class)
7397
static int pyrna_srna_contains_pointer_prop_srna(StructRNA *srna_props, StructRNA *srna, const char **prop_identifier)
7402
/* verify properties */
7403
const ListBase *lb = RNA_struct_type_properties(srna);
7405
for (link = lb->first; link; link = link->next) {
7406
prop = (PropertyRNA *)link;
7407
if (RNA_property_type(prop) == PROP_POINTER && !(RNA_property_flag(prop) & PROP_BUILTIN)) {
7409
RNA_pointer_create(NULL, &RNA_Struct, srna_props, &tptr);
7411
if (RNA_property_pointer_type(&tptr, prop) == srna) {
7412
*prop_identifier = RNA_property_identifier(prop);
7421
PyDoc_STRVAR(pyrna_unregister_class_doc,
7422
".. method:: unregister_class(cls)\n"
7424
" Unload the python class from blender.\n"
7426
" If the class has an *unregister* class method it will be called\n"
7427
" before unregistering.\n"
7429
PyMethodDef meth_bpy_unregister_class = {
7430
"unregister_class", pyrna_unregister_class, METH_O, pyrna_unregister_class_doc
7432
static PyObject *pyrna_unregister_class(PyObject *UNUSED(self), PyObject *py_class)
4842
7435
StructUnregisterFunc unreg;
4843
7436
StructRNA *srna;
4845
/*if(PyDict_GetItemString(((PyTypeObject*)py_class)->tp_dict, "bl_rna")==NULL) {
4846
PyErr_SetString(PyExc_ValueError, "bpy.types.unregister(): not a registered as a subclass.");
4850
srna= pyrna_struct_as_srna(py_class, 0, "bpy.types.unregister(...):");
7437
PyObject *py_cls_meth;
7439
if (!PyType_Check(py_class)) {
7440
PyErr_Format(PyExc_ValueError,
7441
"register_class(...): "
7442
"expected a class argument, not '%.200s'", Py_TYPE(py_class)->tp_name);
7447
if (PyDict_GetItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna) == NULL) {
7449
PyErr_SetString(PyExc_ValueError, "unregister_class(): not a registered as a subclass");
7454
if (!pyrna_write_check()) {
7455
PyErr_Format(PyExc_RuntimeError,
7456
"unregister_class(...): "
7457
"can't run in readonly state '%.200s'",
7458
((PyTypeObject *)py_class)->tp_name);
7462
srna = pyrna_struct_as_srna(py_class, 0, "unregister_class(...):");
4854
7466
/* check that we have a unregister callback for this type */
4855
unreg= RNA_struct_unregister(srna);
7467
unreg = RNA_struct_unregister(srna);
4858
PyErr_SetString(PyExc_ValueError, "bpy.types.unregister(...): expected a Type subclassed from a registerable rna type (no unregister supported).");
7470
PyErr_SetString(PyExc_ValueError,
7471
"unregister_class(...): "
7472
"expected a Type subclassed from a registerable rna type (no unregister supported)");
7476
/* call classed unregister method */
7477
py_cls_meth = PyObject_GetAttr(py_class, bpy_intern_str_unregister);
7478
if (py_cls_meth == NULL) {
7482
PyObject *ret = PyObject_CallObject(py_cls_meth, NULL);
7491
/* should happen all the time but very slow */
7492
if (G.debug & G_DEBUG_PYTHON) {
7493
/* remove all properties using this class */
7494
StructRNA *srna_iter;
7496
PropertyRNA *prop_rna;
7497
const char *prop_identifier = NULL;
7499
RNA_blender_rna_pointer_create(&ptr_rna);
7500
prop_rna = RNA_struct_find_property(&ptr_rna, "structs");
7504
/* loop over all structs */
7505
RNA_PROP_BEGIN(&ptr_rna, itemptr, prop_rna) {
7506
srna_iter = itemptr.data;
7507
if (pyrna_srna_contains_pointer_prop_srna(srna_iter, srna, &prop_identifier)) {
7513
if (prop_identifier) {
7514
PyErr_Format(PyExc_RuntimeError,
7515
"unregister_class(...): can't unregister %s because %s.%s pointer property is using this",
7516
RNA_struct_identifier(srna), RNA_struct_identifier(srna_iter), prop_identifier);
4862
7521
/* get the context, so register callback can do necessary refreshes */
4863
C= BPy_GetContext();
7522
C = BPy_GetContext();
4865
7524
/* call unregister */
4866
unreg(C, srna); /* calls bpy_class_free, this decref's py_class */
7525
unreg(CTX_data_main(C), srna); /* calls bpy_class_free, this decref's py_class */
4868
PyDict_DelItemString(((PyTypeObject *)py_class)->tp_dict, "bl_rna");
4869
if(PyErr_Occurred())
4870
PyErr_Clear(); //return NULL;
7527
PyDict_DelItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna);
7528
if (PyErr_Occurred())
7529
PyErr_Clear(); //return NULL;
4872
7531
Py_RETURN_NONE;