~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/python/intern/bpy_rna.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * $Id: bpy_rna.c 30446 2010-07-17 18:08:14Z campbellbarton $
3
 
 *
 
1
/*
4
2
 * ***** BEGIN GPL LICENSE BLOCK *****
5
3
 *
6
4
 * This program is free software; you can redistribute it and/or
22
20
 * ***** END GPL LICENSE BLOCK *****
23
21
 */
24
22
 
 
23
/** \file blender/python/intern/bpy_rna.c
 
24
 *  \ingroup pythonintern
 
25
 *
 
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.
 
28
 *
 
29
 * The two main types are 'BPy_StructRNA' and 'BPy_PropertyRNA' - the base
 
30
 * classes for most of the data python accesses in blender.
 
31
 */
 
32
 
 
33
#include <Python.h>
 
34
 
 
35
#include <stddef.h>
 
36
#include <float.h> /* FLT_MIN/MAX */
 
37
 
 
38
#include "RNA_types.h"
 
39
 
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"
 
46
 
 
47
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
 
48
#include "MEM_guardedalloc.h"
 
49
#endif
 
50
 
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"
 
56
 
 
57
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
 
58
#include "BLI_ghash.h"
 
59
#endif
33
60
 
34
61
#include "RNA_enum_types.h"
 
62
#include "RNA_define.h" /* RNA_def_property_free_identifier */
 
63
#include "RNA_access.h"
35
64
 
36
65
#include "MEM_guardedalloc.h"
37
 
#include "BKE_utildefines.h"
 
66
 
 
67
#include "BKE_main.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"
41
73
 
42
74
#include "BKE_animsys.h"
43
75
#include "BKE_fcurve.h"
44
76
 
45
 
/* only for keyframing */
46
 
#include "DNA_scene_types.h"
47
 
#include "DNA_anim_types.h"
48
 
#include "ED_keyframing.h"
49
 
 
 
77
#include "../generic/idprop_py_api.h" /* for IDprop lookups */
 
78
#include "../generic/py_capi_utils.h"
 
79
 
 
80
#ifdef WITH_INTERNATIONAL
 
81
#include "BLF_translation.h"
 
82
#endif
 
83
 
 
84
#define USE_PEDANTIC_WRITE
50
85
#define USE_MATHUTILS
51
 
 
52
 
#ifdef USE_MATHUTILS
53
 
#include "../generic/mathutils.h" /* so we can have mathutils callbacks */
54
 
#include "../generic/IDProp.h" /* for IDprop lookups */
55
 
 
56
 
 
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
 
87
 
 
88
static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);
 
89
static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self);
 
90
 
 
91
#define BPY_DOC_ID_PROP_TYPE_NOTE                                             \
 
92
"   .. note::\n"                                                              \
 
93
"\n"                                                                          \
 
94
"      Only :class:`bpy.types.ID`, :class:`bpy.types.Bone` and \n"            \
 
95
"      :class:`bpy.types.PoseBone` classes support custom properties.\n"
 
96
 
 
97
 
 
98
int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
 
99
{
 
100
        if (pysrna->ptr.type) {
 
101
                return 0;
 
102
        }
 
103
        PyErr_Format(PyExc_ReferenceError,
 
104
                     "StructRNA of type %.200s has been removed",
 
105
                     Py_TYPE(pysrna)->tp_name);
 
106
        return -1;
 
107
}
 
108
 
 
109
int pyrna_prop_validity_check(BPy_PropertyRNA *self)
 
110
{
 
111
        if (self->ptr.type) {
 
112
                return 0;
 
113
        }
 
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));
 
117
        return -1;
 
118
}
 
119
 
 
120
#if defined(USE_PYRNA_INVALIDATE_GC) || defined(USE_PYRNA_INVALIDATE_WEAKREF)
 
121
static void pyrna_invalidate(BPy_DummyPointerRNA *self)
 
122
{
 
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 */
 
125
}
 
126
#endif
 
127
 
 
128
#ifdef USE_PYRNA_INVALIDATE_GC
 
129
#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g) + 1))
 
130
 
 
131
/* only for sizeof() */
 
132
struct gc_generation {
 
133
        PyGC_Head head;
 
134
        int threshold;
 
135
        int count;
 
136
} gc_generation;
 
137
 
 
138
static void id_release_gc(struct ID *id)
 
139
{
 
140
        unsigned int j;
 
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);
 
153
                                        // i++;
 
154
                                }
 
155
                        }
 
156
                }
 
157
        }
 
158
        // printf("id_release_gc freed '%s': %d\n", id->name, i);
 
159
}
 
160
#endif
 
161
 
 
162
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
 
163
//#define DEBUG_RNA_WEAKREF
 
164
 
 
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};
 
168
 
 
169
/* adds a reference to the list, remember to decref */
 
170
static GHash *id_weakref_pool_get(ID *id)
 
171
{
 
172
        GHash *weakinfo_hash = NULL;
 
173
 
 
174
        if (id_weakref_pool) {
 
175
                weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
 
176
        }
 
177
        else {
 
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;
 
181
        }
 
182
 
 
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);
 
187
        }
 
188
 
 
189
        return weakinfo_hash;
 
190
}
 
191
 
 
192
/* called from pyrna_struct_CreatePyObject() and pyrna_prop_CreatePyObject() */
 
193
void id_weakref_pool_add(ID *id, BPy_DummyPointerRNA *pyrna)
 
194
{
 
195
        PyObject *weakref;
 
196
        PyObject *weakref_capsule;
 
197
        PyObject *weakref_cb_py;
 
198
 
 
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 */
 
201
 
 
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);
 
205
 
 
206
        /* add weakref to weakinfo_hash list */
 
207
        weakref = PyWeakref_NewRef((PyObject *)pyrna, weakref_cb_py);
 
208
 
 
209
        Py_DECREF(weakref_cb_py); /* function owned by the weakref now */
 
210
 
 
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 */
 
214
 
 
215
}
 
216
 
 
217
/* workaround to get the last id without a lookup */
 
218
static ID *_id_tmp_ptr;
 
219
static void value_id_set(void *id)
 
220
{
 
221
        _id_tmp_ptr = (ID *)id;
 
222
}
 
223
 
 
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)
 
226
{
 
227
        /* important to search backwards */
 
228
        GHash *weakinfo_hash = PyCapsule_GetPointer(weakinfo_capsule, NULL);
 
229
 
 
230
 
 
231
        if (BLI_ghash_size(weakinfo_hash) > 1) {
 
232
                BLI_ghash_remove(weakinfo_hash, weakref, NULL, NULL);
 
233
        }
 
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);
 
237
        }
 
238
 
 
239
        Py_DECREF(weakref);
 
240
 
 
241
        Py_RETURN_NONE;
 
242
}
 
243
 
 
244
static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash)
 
245
{
 
246
        GHashIterator weakinfo_hash_iter;
 
247
 
 
248
        BLI_ghashIterator_init(&weakinfo_hash_iter, weakinfo_hash);
 
249
 
 
250
#ifdef DEBUG_RNA_WEAKREF
 
251
        fprintf(stdout, "id_release_weakref: '%s', %d items\n", id->name, BLI_ghash_size(weakinfo_hash));
 
252
#endif
 
253
 
 
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) {
 
258
 
 
259
#ifdef DEBUG_RNA_WEAKREF
 
260
                        PyC_ObSpit("id_release_weakref item ", item);
 
261
#endif
 
262
 
 
263
                        pyrna_invalidate((BPy_DummyPointerRNA *)item);
 
264
                }
 
265
 
 
266
                Py_DECREF(weakref);
 
267
 
 
268
                BLI_ghashIterator_step(&weakinfo_hash_iter);
 
269
        }
 
270
 
 
271
        BLI_ghash_remove(id_weakref_pool, (void *)id, NULL, NULL);
 
272
        BLI_ghash_free(weakinfo_hash, NULL, NULL);
 
273
 
 
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");
 
279
#endif
 
280
        }
 
281
}
 
282
 
 
283
static void id_release_weakref(struct ID *id)
 
284
{
 
285
        GHash *weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
 
286
        if (weakinfo_hash) {
 
287
                id_release_weakref_list(id, weakinfo_hash);
 
288
        }
 
289
}
 
290
 
 
291
#endif /* USE_PYRNA_INVALIDATE_WEAKREF */
 
292
 
 
293
void BPY_id_release(struct ID *id)
 
294
{
 
295
#ifdef USE_PYRNA_INVALIDATE_GC
 
296
        id_release_gc(id);
 
297
#endif
 
298
 
 
299
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
 
300
        if (id_weakref_pool) {
 
301
                PyGILState_STATE gilstate = PyGILState_Ensure();
 
302
 
 
303
                id_release_weakref(id);
 
304
 
 
305
                PyGILState_Release(gilstate);
 
306
        }
 
307
#endif /* USE_PYRNA_INVALIDATE_WEAKREF */
 
308
 
 
309
        (void)id;
 
310
}
 
311
 
 
312
#ifdef USE_PEDANTIC_WRITE
 
313
static short rna_disallow_writes = FALSE;
 
314
 
 
315
static int rna_id_write_error(PointerRNA *ptr, PyObject *key)
 
316
{
 
317
        ID *id = ptr->id.data;
 
318
        if (id) {
 
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);
 
322
                        const char *pyname;
 
323
                        if (key && PyUnicode_Check(key)) pyname = _PyUnicode_AsString(key);
 
324
                        else                             pyname = "<UNKNOWN>";
 
325
 
 
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);
 
332
 
 
333
                        return TRUE;
 
334
                }
 
335
        }
 
336
        return FALSE;
 
337
}
 
338
#endif // USE_PEDANTIC_WRITE
 
339
 
 
340
 
 
341
#ifdef USE_PEDANTIC_WRITE
 
342
int pyrna_write_check(void)
 
343
{
 
344
        return !rna_disallow_writes;
 
345
}
 
346
 
 
347
void pyrna_write_set(int val)
 
348
{
 
349
        rna_disallow_writes = !val;
 
350
}
 
351
#else // USE_PEDANTIC_WRITE
 
352
int pyrna_write_check(void)
 
353
{
 
354
        return TRUE;
 
355
}
 
356
void pyrna_write_set(int UNUSED(val))
 
357
{
 
358
        /* nothing */
 
359
}
 
360
#endif // USE_PEDANTIC_WRITE
 
361
 
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);
 
366
 
 
367
#ifdef USE_MATHUTILS
 
368
#include "../mathutils/mathutils.h" /* so we can have mathutils callbacks */
 
369
 
 
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);
61
373
 
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 */
64
376
 
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
70
382
 
71
383
static int mathutils_rna_generic_check(BaseMathObject *bmo)
72
384
{
73
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
74
 
        return self->prop ? 1:0;
 
385
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
 
386
 
 
387
        PYRNA_PROP_CHECK_INT(self);
 
388
 
 
389
        return self->prop ? 0 : -1;
75
390
}
76
391
 
77
392
static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
78
393
{
79
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
80
 
        if(self->prop==NULL)
81
 
                return 0;
82
 
        
 
394
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
 
395
 
 
396
        PYRNA_PROP_CHECK_INT(self);
 
397
 
 
398
        if (self->prop == NULL)
 
399
                return -1;
 
400
 
83
401
        RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
84
 
        
 
402
 
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);
90
408
        }
91
 
        
92
 
        return 1;
 
409
 
 
410
        return 0;
93
411
}
94
412
 
95
413
static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
96
414
{
97
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
 
415
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
98
416
        float min, max;
99
 
        if(self->prop==NULL)
100
 
                return 0;
 
417
 
 
418
        PYRNA_PROP_CHECK_INT(self);
 
419
 
 
420
        if (self->prop == NULL)
 
421
                return -1;
 
422
 
 
423
#ifdef USE_PEDANTIC_WRITE
 
424
        if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
 
425
                return -1;
 
426
        }
 
427
#endif // USE_PEDANTIC_WRITE
 
428
 
 
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));
 
433
                return -1;
 
434
        }
101
435
 
102
436
        RNA_property_float_range(&self->ptr, self->prop, &min, &max);
103
437
 
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);
108
442
                }
109
443
        }
110
444
 
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);
 
448
        }
113
449
 
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);
 
459
                        }
122
460
                }
123
461
        }
124
 
        return 1;
125
 
}
126
 
 
127
 
static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int subtype, int index)
128
 
{
129
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
130
 
 
131
 
        if(self->prop==NULL)
132
 
                return 0;
133
 
        
134
 
        bmo->data[index]= RNA_property_float_get_index(&self->ptr, self->prop, index);
135
 
        return 1;
136
 
}
137
 
 
138
 
static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int subtype, int index)
139
 
{
140
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
141
 
 
142
 
        if(self->prop==NULL)
143
 
                return 0;
 
462
        return 0;
 
463
}
 
464
 
 
465
static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
 
466
{
 
467
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
 
468
 
 
469
        PYRNA_PROP_CHECK_INT(self);
 
470
 
 
471
        if (self->prop == NULL)
 
472
                return -1;
 
473
 
 
474
        bmo->data[index] = RNA_property_float_get_index(&self->ptr, self->prop, index);
 
475
        return 0;
 
476
}
 
477
 
 
478
static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
 
479
{
 
480
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
 
481
 
 
482
        PYRNA_PROP_CHECK_INT(self);
 
483
 
 
484
        if (self->prop == NULL)
 
485
                return -1;
 
486
 
 
487
#ifdef USE_PEDANTIC_WRITE
 
488
        if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
 
489
                return -1;
 
490
        }
 
491
#endif // USE_PEDANTIC_WRITE
 
492
 
 
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));
 
497
                return -1;
 
498
        }
144
499
 
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);
148
 
        return 1;
 
502
 
 
503
        if (RNA_property_update_check(self->prop)) {
 
504
                RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
 
505
        }
 
506
 
 
507
        return 0;
149
508
}
150
509
 
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
157
516
};
158
517
 
159
518
 
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 */
162
521
 
163
 
static int mathutils_rna_matrix_get(BaseMathObject *bmo, int subtype)
 
522
static int mathutils_rna_matrix_get(BaseMathObject *bmo, int UNUSED(subtype))
164
523
{
165
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
166
 
 
167
 
        if(self->prop==NULL)
168
 
                return 0;
 
524
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
 
525
 
 
526
        PYRNA_PROP_CHECK_INT(self);
 
527
 
 
528
        if (self->prop == NULL)
 
529
                return -1;
169
530
 
170
531
        RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
171
 
        return 1;
 
532
        return 0;
172
533
}
173
534
 
174
 
static int mathutils_rna_matrix_set(BaseMathObject *bmo, int subtype)
 
535
static int mathutils_rna_matrix_set(BaseMathObject *bmo, int UNUSED(subtype))
175
536
{
176
 
        BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
177
 
        
178
 
        if(self->prop==NULL)
179
 
                return 0;
 
537
        BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
 
538
 
 
539
        PYRNA_PROP_CHECK_INT(self);
 
540
 
 
541
        if (self->prop == NULL)
 
542
                return -1;
 
543
 
 
544
#ifdef USE_PEDANTIC_WRITE
 
545
        if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
 
546
                return -1;
 
547
        }
 
548
#endif // USE_PEDANTIC_WRITE
 
549
 
 
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));
 
554
                return -1;
 
555
        }
 
556
 
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);
183
 
        return 1;
 
559
 
 
560
        if (RNA_property_update_check(self->prop)) {
 
561
                RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
 
562
        }
 
563
        return 0;
184
564
}
185
565
 
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,
191
571
        NULL
192
572
};
193
573
 
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)
196
575
{
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);
200
 
                MEM_freeN(enum_str);
201
 
                return -1;
 
576
        /* attempt to get order */
 
577
        if (*prop_eul_order == NULL)
 
578
                *prop_eul_order = RNA_struct_find_property(ptr, "rotation_mode");
 
579
 
 
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 */
 
583
                        return order;
202
584
        }
203
585
 
204
 
        return 0;
 
586
        return order_fallback;
205
587
}
206
588
 
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
 
590
 
 
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                                              \
 
594
                 PROP_COORDS:                                                         \
 
595
        case PROP_TRANSLATION:                                                    \
 
596
        case PROP_DIRECTION:                                                      \
 
597
        case PROP_VELOCITY:                                                       \
 
598
        case PROP_ACCELERATION:                                                   \
 
599
        case PROP_XYZ:                                                            \
 
600
        case PROP_XYZ_LENGTH                                                      \
 
601
 
208
602
 
209
603
PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
210
604
{
211
 
        PyObject *ret= NULL;
 
605
        PyObject *ret = NULL;
212
606
 
213
607
#ifdef USE_MATHUTILS
214
608
        int subtype, totdim;
215
609
        int len;
216
610
        int is_thick;
217
 
        int flag= RNA_property_flag(prop);
 
611
        const int flag = RNA_property_flag(prop);
218
612
 
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))
222
616
                return NULL;
223
617
 
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);
228
622
 
229
623
        if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) {
230
 
                if(!is_thick)
231
 
                        ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
232
 
 
233
 
                switch(RNA_property_subtype(prop)) {
234
 
                case PROP_ALL_VECTOR_SUBTYPES:
235
 
                        if(len>=2 && len <= 4) {
236
 
                                if(is_thick) {
237
 
                                        ret= newVectorObject(NULL, len, Py_NEW, NULL);
238
 
                                        RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
239
 
                                }
240
 
                                else {
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 */
244
 
                                }
245
 
                        }
246
 
                        break;
247
 
                case PROP_MATRIX:
248
 
                        if(len==16) {
249
 
                                if(is_thick) {
250
 
                                        ret= newMatrixObject(NULL, 4, 4, Py_NEW, NULL);
251
 
                                        RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->contigPtr);
252
 
                                }
253
 
                                else {
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 */
257
 
                                }
258
 
                        }
259
 
                        else if (len==9) {
260
 
                                if(is_thick) {
261
 
                                        ret= newMatrixObject(NULL, 3, 3, Py_NEW, NULL);
262
 
                                        RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->contigPtr);
263
 
                                }
264
 
                                else {
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 */
268
 
                                }
269
 
                        }
270
 
                        break;
271
 
                case PROP_EULER:
272
 
                case PROP_QUATERNION:
273
 
                        if(len==3) { /* euler */
274
 
                                if(is_thick) {
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);
278
 
 
279
 
                                        ret= newEulerObject(NULL, order, Py_NEW, NULL); // TODO, get order from RNA
280
 
                                        RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
281
 
                                }
282
 
                                else {
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 */
287
 
                                }
288
 
                        }
289
 
                        else if (len==4) {
290
 
                                if(is_thick) {
291
 
                                        ret= newQuaternionObject(NULL, Py_NEW, NULL);
292
 
                                        RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
293
 
                                }
294
 
                                else {
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 */
298
 
                                }
299
 
                        }
300
 
                        break;
301
 
                case PROP_COLOR:
302
 
                        if(len==3) { /* color */
303
 
                                if(is_thick) {
304
 
                                        ret= newColorObject(NULL, Py_NEW, NULL); // TODO, get order from RNA
305
 
                                        RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col);
306
 
                                }
307
 
                                else {
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 */
311
 
                                }
312
 
                        }
313
 
                default:
314
 
                        break;
 
624
                if (!is_thick)
 
625
                        ret = pyrna_prop_CreatePyObject(ptr, prop);  /* owned by the mathutils PyObject */
 
626
 
 
627
                switch (subtype) {
 
628
                        case PROP_ALL_VECTOR_SUBTYPES:
 
629
                                if (len >= 2 && len <= 4) {
 
630
                                        if (is_thick) {
 
631
                                                ret = Vector_CreatePyObject(NULL, len, Py_NEW, NULL);
 
632
                                                RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
 
633
                                        }
 
634
                                        else {
 
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 */
 
638
                                        }
 
639
                                }
 
640
                                break;
 
641
                        case PROP_MATRIX:
 
642
                                if (len == 16) {
 
643
                                        if (is_thick) {
 
644
                                                ret = Matrix_CreatePyObject(NULL, 4, 4, Py_NEW, NULL);
 
645
                                                RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
 
646
                                        }
 
647
                                        else {
 
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 */
 
651
                                        }
 
652
                                }
 
653
                                else if (len == 9) {
 
654
                                        if (is_thick) {
 
655
                                                ret = Matrix_CreatePyObject(NULL, 3, 3, Py_NEW, NULL);
 
656
                                                RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
 
657
                                        }
 
658
                                        else {
 
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 */
 
662
                                        }
 
663
                                }
 
664
                                break;
 
665
                        case PROP_EULER:
 
666
                        case PROP_QUATERNION:
 
667
                                if (len == 3) { /* euler */
 
668
                                        if (is_thick) {
 
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);
 
672
 
 
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);
 
675
                                        }
 
676
                                        else {
 
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 */
 
681
                                        }
 
682
                                }
 
683
                                else if (len == 4) {
 
684
                                        if (is_thick) {
 
685
                                                ret = Quaternion_CreatePyObject(NULL, Py_NEW, NULL);
 
686
                                                RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
 
687
                                        }
 
688
                                        else {
 
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 */
 
692
                                        }
 
693
                                }
 
694
                                break;
 
695
                        case PROP_COLOR:
 
696
                        case PROP_COLOR_GAMMA:
 
697
                                if (len == 3) { /* color */
 
698
                                        if (is_thick) {
 
699
                                                ret = Color_CreatePyObject(NULL, Py_NEW, NULL); // TODO, get order from RNA
 
700
                                                RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col);
 
701
                                        }
 
702
                                        else {
 
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 */
 
706
                                        }
 
707
                                }
 
708
                        default:
 
709
                                break;
315
710
                }
316
711
        }
317
712
 
318
 
        if(ret==NULL) {
319
 
                if(is_thick) {
 
713
        if (ret == NULL) {
 
714
                if (is_thick) {
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);
323
 
                } else {
324
 
                        ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
 
718
                }
 
719
                else {
 
720
                        ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the mathutils PyObject */
325
721
                }
326
722
        }
327
 
 
328
 
#endif
 
723
#else // USE_MATHUTILS
 
724
        (void)ptr;
 
725
        (void)prop;
 
726
#endif // USE_MATHUTILS
329
727
 
330
728
        return ret;
331
729
}
332
730
 
333
 
#endif
334
 
 
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)
336
733
{
337
 
        /* attempt to get order */
338
 
        if(*prop_eul_order==NULL)
339
 
                *prop_eul_order= RNA_struct_find_property(ptr, "rotation_mode");
340
 
 
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 */
344
 
                        return order;
 
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);
 
740
                return -1;
345
741
        }
346
742
 
347
 
        return order_fallback;
348
 
}
349
 
 
350
 
static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
351
 
{
352
 
        return (a->ptr.data==b->ptr.data) ? 0 : -1;
353
 
}
354
 
 
355
 
static int pyrna_prop_compare( BPy_PropertyRNA * a, BPy_PropertyRNA * b )
356
 
{
357
 
        return (a->prop==b->prop && a->ptr.data==b->ptr.data ) ? 0 : -1;
 
743
        return 0;
 
744
}
 
745
 
 
746
/* note on __cmp__:
 
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.
 
751
 *
 
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.
 
755
 * - campbell */
 
756
 
 
757
static int pyrna_struct_compare(BPy_StructRNA *a, BPy_StructRNA *b)
 
758
{
 
759
        return (((a->ptr.data == b->ptr.data) &&
 
760
                 (a->ptr.type == b->ptr.type)) ? 0 : -1);
 
761
}
 
762
 
 
763
static int pyrna_prop_compare(BPy_PropertyRNA *a, BPy_PropertyRNA *b)
 
764
{
 
765
        return (((a->prop == b->prop) &&
 
766
                 (a->ptr.data == b->ptr.data) &&
 
767
                 (a->ptr.type == b->ptr.type)) ? 0 : -1);
358
768
}
359
769
 
360
770
static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
361
771
{
362
772
        PyObject *res;
363
 
        int ok= -1; /* zero is true */
 
773
        int ok = -1; /* zero is true */
364
774
 
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);
367
777
 
368
778
        switch (op) {
369
 
        case Py_NE:
370
 
                ok = !ok; /* pass through */
371
 
        case Py_EQ:
372
 
                res = ok ? Py_False : Py_True;
373
 
                break;
 
779
                case Py_NE:
 
780
                        ok = !ok; /* pass through */
 
781
                case Py_EQ:
 
782
                        res = ok ? Py_False : Py_True;
 
783
                        break;
374
784
 
375
 
        case Py_LT:
376
 
        case Py_LE:
377
 
        case Py_GT:
378
 
        case Py_GE:
379
 
                res = Py_NotImplemented;
380
 
                break;
381
 
        default:
382
 
                PyErr_BadArgument();
383
 
                return NULL;
 
785
                case Py_LT:
 
786
                case Py_LE:
 
787
                case Py_GT:
 
788
                case Py_GE:
 
789
                        res = Py_NotImplemented;
 
790
                        break;
 
791
                default:
 
792
                        PyErr_BadArgument();
 
793
                        return NULL;
384
794
        }
385
795
 
386
 
        Py_INCREF(res);
387
 
        return res;
 
796
        return Py_INCREF(res), res;
388
797
}
389
798
 
390
799
static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
391
800
{
392
801
        PyObject *res;
393
 
        int ok= -1; /* zero is true */
 
802
        int ok = -1; /* zero is true */
394
803
 
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);
397
806
 
398
807
        switch (op) {
399
 
        case Py_NE:
400
 
                ok = !ok; /* pass through */
401
 
        case Py_EQ:
402
 
                res = ok ? Py_False : Py_True;
403
 
                break;
 
808
                case Py_NE:
 
809
                        ok = !ok; /* pass through */
 
810
                case Py_EQ:
 
811
                        res = ok ? Py_False : Py_True;
 
812
                        break;
404
813
 
405
 
        case Py_LT:
406
 
        case Py_LE:
407
 
        case Py_GT:
408
 
        case Py_GE:
409
 
                res = Py_NotImplemented;
410
 
                break;
411
 
        default:
412
 
                PyErr_BadArgument();
413
 
                return NULL;
 
814
                case Py_LT:
 
815
                case Py_LE:
 
816
                case Py_GT:
 
817
                case Py_GE:
 
818
                        res = Py_NotImplemented;
 
819
                        break;
 
820
                default:
 
821
                        PyErr_BadArgument();
 
822
                        return NULL;
414
823
        }
415
824
 
416
 
        Py_INCREF(res);
417
 
        return res;
 
825
        return Py_INCREF(res), res;
418
826
}
419
827
 
420
828
/*----------------------repr--------------------------------------------*/
421
 
static PyObject *pyrna_struct_repr( BPy_StructRNA *self )
 
829
static PyObject *pyrna_struct_str(BPy_StructRNA *self)
422
830
{
423
 
        PyObject *pyob;
424
 
        char *name;
 
831
        PyObject *ret;
 
832
        const char *name;
 
833
 
 
834
        if (!PYRNA_STRUCT_IS_VALID(self)) {
 
835
                return PyUnicode_FromFormat("<bpy_struct, %.200s dead>",
 
836
                                            Py_TYPE(self)->tp_name);
 
837
        }
425
838
 
426
839
        /* print name if available */
427
 
        name= RNA_struct_name_get_alloc(&self->ptr, NULL, FALSE);
428
 
        if(name) {
429
 
                pyob= PyUnicode_FromFormat( "<bpy_struct, %.200s(\"%.200s\")>", RNA_struct_identifier(self->ptr.type), name);
430
 
                MEM_freeN(name);
431
 
                return pyob;
432
 
        }
433
 
 
434
 
        return PyUnicode_FromFormat( "<bpy_struct, %.200s at %p>", RNA_struct_identifier(self->ptr.type), self->ptr.data);
435
 
}
436
 
 
437
 
static PyObject *pyrna_prop_repr( BPy_PropertyRNA *self )
438
 
{
439
 
        PyObject *pyob;
 
840
        name = RNA_struct_name_get_alloc(&self->ptr, NULL, 0, NULL);
 
841
        if (name) {
 
842
                ret = PyUnicode_FromFormat("<bpy_struct, %.200s(\"%.200s\")>",
 
843
                                           RNA_struct_identifier(self->ptr.type),
 
844
                                           name);
 
845
                MEM_freeN((void *)name);
 
846
                return ret;
 
847
        }
 
848
 
 
849
        return PyUnicode_FromFormat("<bpy_struct, %.200s at %p>",
 
850
                                    RNA_struct_identifier(self->ptr.type),
 
851
                                    self->ptr.data);
 
852
}
 
853
 
 
854
static PyObject *pyrna_struct_repr(BPy_StructRNA *self)
 
855
{
 
856
        ID *id = self->ptr.id.data;
 
857
        PyObject *tmp_str;
 
858
        PyObject *ret;
 
859
 
 
860
        if (id == NULL || !PYRNA_STRUCT_IS_VALID(self))
 
861
                return pyrna_struct_str(self);  /* fallback */
 
862
 
 
863
        tmp_str = PyUnicode_FromString(id->name + 2);
 
864
 
 
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)),
 
868
                                           tmp_str);
 
869
        }
 
870
        else {
 
871
                const char *path;
 
872
                path = RNA_path_from_ID_to_struct(&self->ptr);
 
873
                if (path) {
 
874
                        if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
 
875
                                ret = PyUnicode_FromFormat("bpy.data...%s",
 
876
                                                                                   path);
 
877
                        }
 
878
                        else {
 
879
                                ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
 
880
                                                           BKE_idcode_to_name_plural(GS(id->name)),
 
881
                                                           tmp_str,
 
882
                                                           path);
 
883
                        }
 
884
 
 
885
                        MEM_freeN((void *)path);
 
886
                }
 
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)),
 
890
                                                   tmp_str,
 
891
                                                   RNA_struct_identifier(self->ptr.type));
 
892
                }
 
893
        }
 
894
 
 
895
        Py_DECREF(tmp_str);
 
896
 
 
897
        return ret;
 
898
}
 
899
 
 
900
static PyObject *pyrna_prop_str(BPy_PropertyRNA *self)
 
901
{
 
902
        PyObject *ret;
440
903
        PointerRNA ptr;
441
 
        char *name;
442
 
        const char *type_id= NULL;
443
 
        char type_fmt[64]= "";
444
 
        int type= RNA_property_type(self->prop);
445
 
 
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 */
 
904
        const char *name;
 
905
        const char *type_id = NULL;
 
906
        char type_fmt[64] = "";
 
907
        int type;
 
908
 
 
909
        PYRNA_PROP_CHECK_OBJ(self);
 
910
 
 
911
        type = RNA_property_type(self->prop);
 
912
 
 
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 */
448
915
                return NULL;
449
916
        }
450
917
        else {
451
918
                /* this should never fail */
452
919
                int len = -1;
453
 
                char *c= type_fmt;
454
 
 
455
 
                while ( (*c++= tolower(*type_id++)) ) {} ;
456
 
 
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);
461
 
                }
462
 
 
463
 
                if(len != -1)
 
920
                char *c = type_fmt;
 
921
 
 
922
                while ((*c++ = tolower(*type_id++))) {}
 
923
 
 
924
                if (type == PROP_COLLECTION) {
 
925
                        len = pyrna_prop_collection_length(self);
 
926
                }
 
927
                else if (RNA_property_array_check(self->prop)) {
 
928
                        len = pyrna_prop_array_length((BPy_PropertyArrayRNA *)self);
 
929
                }
 
930
 
 
931
                if (len != -1)
464
932
                        sprintf(--c, "[%d]", len);
465
933
        }
466
934
 
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);
471
 
 
472
 
                if(name) {
473
 
                        pyob= PyUnicode_FromFormat( "<bpy_%.200s, %.200s.%.200s(\"%.200s\")>", type_fmt, RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
474
 
                        MEM_freeN(name);
475
 
                        return pyob;
476
 
                }
477
 
        }
478
 
 
479
 
        return PyUnicode_FromFormat( "<bpy_%.200s, %.200s.%.200s>", type_fmt, RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
480
 
}
481
 
 
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);
 
939
 
 
940
                if (name) {
 
941
                        ret = PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s(\"%.200s\")>",
 
942
                                                   type_fmt,
 
943
                                                   RNA_struct_identifier(self->ptr.type),
 
944
                                                   RNA_property_identifier(self->prop),
 
945
                                                   name);
 
946
                        MEM_freeN((void *)name);
 
947
                        return ret;
 
948
                }
 
949
        }
 
950
        if (type == PROP_COLLECTION) {
 
951
                PointerRNA r_ptr;
 
952
                if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
 
953
                        return PyUnicode_FromFormat("<bpy_%.200s, %.200s>",
 
954
                                                    type_fmt,
 
955
                                                    RNA_struct_identifier(r_ptr.type));
 
956
                }
 
957
        }
 
958
 
 
959
        return PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s>",
 
960
                                    type_fmt,
 
961
                                    RNA_struct_identifier(self->ptr.type),
 
962
                                    RNA_property_identifier(self->prop));
 
963
}
 
964
 
 
965
static PyObject *pyrna_prop_repr(BPy_PropertyRNA *self)
 
966
{
 
967
        ID *id = self->ptr.id.data;
 
968
        PyObject *tmp_str;
 
969
        PyObject *ret;
 
970
        const char *path;
 
971
 
 
972
        PYRNA_PROP_CHECK_OBJ(self);
 
973
 
 
974
        if (id == NULL)
 
975
                return pyrna_prop_str(self);  /* fallback */
 
976
 
 
977
        tmp_str = PyUnicode_FromString(id->name + 2);
 
978
 
 
979
        path = RNA_path_from_ID_to_property(&self->ptr, self->prop);
 
980
        if (path) {
 
981
                if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
 
982
                        ret = PyUnicode_FromFormat("bpy.data...%s",
 
983
                                                                           path);
 
984
                }
 
985
                else {
 
986
                        ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
 
987
                                                   BKE_idcode_to_name_plural(GS(id->name)),
 
988
                                                   tmp_str,
 
989
                                               path);
 
990
                }
 
991
 
 
992
                MEM_freeN((void *)path);
 
993
        }
 
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)),
 
997
                                           tmp_str,
 
998
                                           RNA_property_identifier(self->prop));
 
999
        }
 
1000
 
 
1001
        Py_DECREF(tmp_str);
 
1002
 
 
1003
        return ret;
 
1004
}
 
1005
 
 
1006
 
 
1007
static PyObject *pyrna_func_repr(BPy_FunctionRNA *self)
 
1008
{
 
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));
 
1013
}
 
1014
 
 
1015
 
 
1016
static Py_hash_t pyrna_struct_hash(BPy_StructRNA *self)
483
1017
{
484
1018
        return _Py_HashPointer(self->ptr.data);
485
1019
}
486
1020
 
487
1021
/* from python's meth_hash v3.1.2 */
488
1022
static long pyrna_prop_hash(BPy_PropertyRNA *self)
489
 
{       
490
 
        long x,y;
 
1023
{
 
1024
        long x, y;
491
1025
        if (self->ptr.data == NULL)
492
1026
                x = 0;
493
1027
        else {
495
1029
                if (x == -1)
496
1030
                        return -1;
497
1031
        }
498
 
        y = _Py_HashPointer((void*)(self->prop));
 
1032
        y = _Py_HashPointer((void *)(self->prop));
499
1033
        if (y == -1)
500
1034
                return -1;
501
1035
        x ^= y;
504
1038
        return x;
505
1039
}
506
1040
 
 
1041
#ifdef USE_PYRNA_STRUCT_REFERENCE
 
1042
static int pyrna_struct_traverse(BPy_StructRNA *self, visitproc visit, void *arg)
 
1043
{
 
1044
        Py_VISIT(self->reference);
 
1045
        return 0;
 
1046
}
 
1047
 
 
1048
static int pyrna_struct_clear(BPy_StructRNA *self)
 
1049
{
 
1050
        Py_CLEAR(self->reference);
 
1051
        return 0;
 
1052
}
 
1053
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
 
1054
 
507
1055
/* use our own dealloc so we can free a property if we use one */
508
 
static void pyrna_struct_dealloc( BPy_StructRNA *self )
 
1056
static void pyrna_struct_dealloc(BPy_StructRNA *self)
509
1057
{
 
1058
#ifdef PYRNA_FREE_SUPPORT
510
1059
        if (self->freeptr && self->ptr.data) {
511
1060
                IDP_FreeProperty(self->ptr.data);
512
1061
                MEM_freeN(self->ptr.data);
513
 
                self->ptr.data= NULL;
514
 
        }
515
 
 
516
 
        /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
517
 
        Py_TYPE(self)->tp_free(self);
518
 
        return;
519
 
}
520
 
 
521
 
static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
 
1062
                self->ptr.data = NULL;
 
1063
        }
 
1064
#endif /* PYRNA_FREE_SUPPORT */
 
1065
 
 
1066
#ifdef USE_WEAKREFS
 
1067
        if (self->in_weakreflist != NULL) {
 
1068
                PyObject_ClearWeakRefs((PyObject *)self);
 
1069
        }
 
1070
#endif
 
1071
 
 
1072
#ifdef USE_PYRNA_STRUCT_REFERENCE
 
1073
        if (self->reference) {
 
1074
                PyObject_GC_UnTrack(self);
 
1075
                pyrna_struct_clear(self);
 
1076
        }
 
1077
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
 
1078
 
 
1079
        /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
 
1080
        Py_TYPE(self)->tp_free(self);
 
1081
}
 
1082
 
 
1083
#ifdef USE_PYRNA_STRUCT_REFERENCE
 
1084
static void pyrna_struct_reference_set(BPy_StructRNA *self, PyObject *reference)
 
1085
{
 
1086
        if (self->reference) {
 
1087
//              PyObject_GC_UnTrack(self); /* INITIALIZED TRACKED ? */
 
1088
                pyrna_struct_clear(self);
 
1089
        }
 
1090
        /* reference is now NULL */
 
1091
 
 
1092
        if (reference) {
 
1093
                self->reference = reference;
 
1094
                Py_INCREF(reference);
 
1095
//              PyObject_GC_Track(self);  /* INITIALIZED TRACKED ? */
 
1096
        }
 
1097
}
 
1098
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
 
1099
 
 
1100
/* use our own dealloc so we can free a property if we use one */
 
1101
static void pyrna_prop_dealloc(BPy_PropertyRNA *self)
 
1102
{
 
1103
#ifdef USE_WEAKREFS
 
1104
        if (self->in_weakreflist != NULL) {
 
1105
                PyObject_ClearWeakRefs((PyObject *)self);
 
1106
        }
 
1107
#endif
 
1108
        /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
 
1109
        Py_TYPE(self)->tp_free(self);
 
1110
}
 
1111
 
 
1112
static void pyrna_prop_array_dealloc(BPy_PropertyRNA *self)
 
1113
{
 
1114
#ifdef USE_WEAKREFS
 
1115
        if (self->in_weakreflist != NULL) {
 
1116
                PyObject_ClearWeakRefs((PyObject *)self);
 
1117
        }
 
1118
#endif
 
1119
        /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
 
1120
        Py_TYPE(self)->tp_free(self);
 
1121
}
 
1122
 
 
1123
static const char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
522
1124
{
523
1125
        EnumPropertyItem *item;
524
 
        char *result;
525
 
        int free= FALSE;
526
 
        
 
1126
        const char *result;
 
1127
        int free = FALSE;
 
1128
 
527
1129
        RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
528
 
        if(item) {
529
 
                result= BPy_enum_as_string(item);
 
1130
        if (item) {
 
1131
                result = BPy_enum_as_string(item);
530
1132
        }
531
1133
        else {
532
 
                result= "";
 
1134
                result = "";
533
1135
        }
534
 
        
535
 
        if(free)
 
1136
 
 
1137
        if (free)
536
1138
                MEM_freeN(item);
537
 
        
 
1139
 
538
1140
        return result;
539
1141
}
540
1142
 
541
1143
 
542
1144
static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *val, const char *error_prefix)
543
1145
{
544
 
        char *param= _PyUnicode_AsString(item);
 
1146
        const char *param = _PyUnicode_AsString(item);
545
1147
 
546
 
        if (param==NULL) {
547
 
                char *enum_str= pyrna_enum_as_string(ptr, prop);
548
 
                PyErr_Format(PyExc_TypeError, "%.200s expected a string enum type in (%.200s)", error_prefix, enum_str);
549
 
                MEM_freeN(enum_str);
550
 
                return 0;
551
 
        } else {
 
1148
        if (param == NULL) {
 
1149
                PyErr_Format(PyExc_TypeError,
 
1150
                             "%.200s expected a string enum, not %.200s",
 
1151
                             error_prefix, Py_TYPE(item)->tp_name);
 
1152
                return -1;
 
1153
        }
 
1154
        else {
552
1155
                if (!RNA_property_enum_value(BPy_GetContext(), ptr, prop, param, val)) {
553
 
                        char *enum_str= pyrna_enum_as_string(ptr, prop);
554
 
                        PyErr_Format(PyExc_TypeError, "%.200s enum \"%.200s\" not found in (%.200s)", error_prefix, param, enum_str);
555
 
                        MEM_freeN(enum_str);
556
 
                        return 0;
 
1156
                        const char *enum_str = pyrna_enum_as_string(ptr, prop);
 
1157
                        PyErr_Format(PyExc_TypeError,
 
1158
                                     "%.200s enum \"%.200s\" not found in (%.200s)",
 
1159
                                     error_prefix, param, enum_str);
 
1160
                        MEM_freeN((void *)enum_str);
 
1161
                        return -1;
557
1162
                }
558
1163
        }
559
1164
 
560
 
        return 1;
 
1165
        return 0;
561
1166
}
562
1167
 
 
1168
/* 'value' _must_ be a set type, error check before calling */
563
1169
int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
564
1170
{
565
1171
        /* set of enum items, concatenate all values with OR */
566
 
        int ret, flag= 0;
 
1172
        int ret, flag = 0;
567
1173
 
568
1174
        /* set looping */
569
1175
        Py_ssize_t pos = 0;
 
1176
        Py_ssize_t hash = 0;
570
1177
        PyObject *key;
571
 
        long hash;
572
1178
 
573
 
        *r_value= 0;
 
1179
        *r_value = 0;
574
1180
 
575
1181
        while (_PySet_NextEntry(value, &pos, &key, &hash)) {
576
 
                char *param= _PyUnicode_AsString(key);
577
 
 
578
 
                if(param==NULL) {
579
 
                        PyErr_Format(PyExc_TypeError, "%.200s expected a string. found a %.200s", error_prefix, Py_TYPE(key)->tp_name);
580
 
                        return -1;
581
 
                }
582
 
                if(pyrna_enum_value_from_id(items, param, &ret, error_prefix) < 0)
583
 
                        return -1;
 
1182
                const char *param = _PyUnicode_AsString(key);
 
1183
 
 
1184
                if (param == NULL) {
 
1185
                        PyErr_Format(PyExc_TypeError,
 
1186
                                     "%.200s expected a string, not %.200s",
 
1187
                                     error_prefix, Py_TYPE(key)->tp_name);
 
1188
                        return -1;
 
1189
                }
 
1190
 
 
1191
                if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) < 0) {
 
1192
                        return -1;
 
1193
                }
584
1194
 
585
1195
                flag |= ret;
586
1196
        }
587
1197
 
588
 
        *r_value= flag;
 
1198
        *r_value = flag;
589
1199
        return 0;
590
1200
}
591
1201
 
593
1203
{
594
1204
        EnumPropertyItem *item;
595
1205
        int ret;
596
 
        int free= FALSE;
597
 
 
598
 
        *r_value= 0;
 
1206
        int free = FALSE;
 
1207
 
 
1208
        *r_value = 0;
 
1209
 
 
1210
        if (!PyAnySet_Check(value)) {
 
1211
                PyErr_Format(PyExc_TypeError,
 
1212
                             "%.200s, %.200s.%.200s expected a set, not a %.200s",
 
1213
                             error_prefix, RNA_struct_identifier(ptr->type),
 
1214
                             RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
 
1215
                return -1;
 
1216
        }
599
1217
 
600
1218
        RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
601
1219
 
602
 
        if(item) {
603
 
                ret= pyrna_set_to_enum_bitfield(item, value, r_value, error_prefix);
 
1220
        if (item) {
 
1221
                ret = pyrna_set_to_enum_bitfield(item, value, r_value, error_prefix);
604
1222
        }
605
1223
        else {
606
 
                if(PySet_GET_SIZE(value)) {
607
 
                        PyErr_Format(PyExc_TypeError, "%.200s: empty enum \"%.200s\" could not have any values assigned.", error_prefix, RNA_property_identifier(prop));
608
 
                        ret= -1;
 
1224
                if (PySet_GET_SIZE(value)) {
 
1225
                        PyErr_Format(PyExc_TypeError,
 
1226
                                     "%.200s: empty enum \"%.200s\" could not have any values assigned",
 
1227
                                     error_prefix, RNA_property_identifier(prop));
 
1228
                        ret = -1;
609
1229
                }
610
1230
                else {
611
 
                        ret= 0;
 
1231
                        ret = 0;
612
1232
                }
613
1233
        }
614
1234
 
615
 
        if(free)
 
1235
        if (free)
616
1236
                MEM_freeN(item);
617
1237
 
618
1238
        return ret;
620
1240
 
621
1241
PyObject *pyrna_enum_bitfield_to_py(EnumPropertyItem *items, int value)
622
1242
{
623
 
        PyObject *ret= PySet_New(NULL);
 
1243
        PyObject *ret = PySet_New(NULL);
624
1244
        const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
625
1245
 
626
 
        if(RNA_enum_bitflag_identifiers(items, value, identifier)) {
 
1246
        if (RNA_enum_bitflag_identifiers(items, value, identifier)) {
627
1247
                PyObject *item;
628
1248
                int index;
629
 
                for(index=0; identifier[index]; index++) {
630
 
                        item= PyUnicode_FromString(identifier[index]);
 
1249
                for (index = 0; identifier[index]; index++) {
 
1250
                        item = PyUnicode_FromString(identifier[index]);
631
1251
                        PySet_Add(ret, item);
632
1252
                        Py_DECREF(item);
633
1253
                }
638
1258
 
639
1259
static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val)
640
1260
{
641
 
        PyObject *item, *ret= NULL;
 
1261
        PyObject *item, *ret = NULL;
642
1262
 
643
 
        if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
 
1263
        if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
644
1264
                const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
645
1265
 
646
 
                ret= PySet_New(NULL);
 
1266
                ret = PySet_New(NULL);
647
1267
 
648
1268
                if (RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier)) {
649
1269
                        int index;
650
1270
 
651
 
                        for(index=0; identifier[index]; index++) {
652
 
                                item= PyUnicode_FromString(identifier[index]);
 
1271
                        for (index = 0; identifier[index]; index++) {
 
1272
                                item = PyUnicode_FromString(identifier[index]);
653
1273
                                PySet_Add(ret, item);
654
1274
                                Py_DECREF(item);
655
1275
                        }
660
1280
                const char *identifier;
661
1281
                if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) {
662
1282
                        ret = PyUnicode_FromString(identifier);
663
 
                } else {
664
 
                        EnumPropertyItem *item;
665
 
                        int free= FALSE;
 
1283
                }
 
1284
                else {
 
1285
                        EnumPropertyItem *enum_item;
 
1286
                        int free = FALSE;
666
1287
 
667
1288
                        /* don't throw error here, can't trust blender 100% to give the
668
1289
                         * right values, python code should not generate error for that */
669
 
                        RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
670
 
                        if(item && item->identifier) {
671
 
                                ret= PyUnicode_FromString(item->identifier);
 
1290
                        RNA_property_enum_items(BPy_GetContext(), ptr, prop, &enum_item, NULL, &free);
 
1291
                        if (enum_item && enum_item->identifier) {
 
1292
                                ret = PyUnicode_FromString(enum_item->identifier);
672
1293
                        }
673
1294
                        else {
674
 
                                char *ptr_name= RNA_struct_name_get_alloc(ptr, NULL, FALSE);
 
1295
                                const char *ptr_name = RNA_struct_name_get_alloc(ptr, NULL, 0, NULL);
675
1296
 
676
 
                                /* prefer not fail silently incase of api errors, maybe disable it later */
677
 
                                printf("RNA Warning: Current value \"%d\" matches no enum in '%s', '%s', '%s'\n", val, RNA_struct_identifier(ptr->type), ptr_name, RNA_property_identifier(prop));
 
1297
                                /* prefer not fail silently in case of api errors, maybe disable it later */
 
1298
                                printf("RNA Warning: Current value \"%d\" "
 
1299
                                       "matches no enum in '%s', '%s', '%s'\n",
 
1300
                                       val, RNA_struct_identifier(ptr->type),
 
1301
                                       ptr_name, RNA_property_identifier(prop));
678
1302
 
679
1303
#if 0           // gives python decoding errors while generating docs :(
680
1304
                                char error_str[256];
681
 
                                snprintf(error_str, sizeof(error_str), "RNA Warning: Current value \"%d\" matches no enum in '%s', '%s', '%s'", val, RNA_struct_identifier(ptr->type), ptr_name, RNA_property_identifier(prop));
 
1305
                                BLI_snprintf(error_str, sizeof(error_str),
 
1306
                                             "RNA Warning: Current value \"%d\" "
 
1307
                                             "matches no enum in '%s', '%s', '%s'",
 
1308
                                             val, RNA_struct_identifier(ptr->type),
 
1309
                                             ptr_name, RNA_property_identifier(prop));
 
1310
 
682
1311
                                PyErr_Warn(PyExc_RuntimeWarning, error_str);
683
1312
#endif
684
1313
 
685
 
                                if(ptr_name)
686
 
                                        MEM_freeN(ptr_name);
 
1314
                                if (ptr_name)
 
1315
                                        MEM_freeN((void *)ptr_name);
687
1316
 
688
 
                                ret = PyUnicode_FromString( "" );
 
1317
                                ret = PyUnicode_FromString("");
689
1318
                        }
690
1319
 
691
 
                        if(free)
692
 
                                MEM_freeN(item);
693
 
 
694
 
                        /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
695
 
                        ret = NULL;*/
 
1320
                        if (free)
 
1321
                                MEM_freeN(enum_item);
 
1322
#if 0
 
1323
                        PyErr_Format(PyExc_AttributeError,
 
1324
                                     "RNA Error: Current value \"%d\" matches no enum", val);
 
1325
                        ret = NULL;
 
1326
#endif
696
1327
                }
697
1328
        }
698
1329
 
699
1330
        return ret;
700
1331
}
701
1332
 
702
 
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
 
1333
PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
703
1334
{
704
1335
        PyObject *ret;
705
 
        int type = RNA_property_type(prop);
 
1336
        const int type = RNA_property_type(prop);
706
1337
 
707
 
        if (RNA_property_array_check(ptr, prop)) {
 
1338
        if (RNA_property_array_check(prop)) {
708
1339
                return pyrna_py_from_array(ptr, prop);
709
1340
        }
710
 
        
 
1341
 
711
1342
        /* see if we can coorce into a python type - PropertyType */
712
1343
        switch (type) {
713
 
        case PROP_BOOLEAN:
714
 
                ret = PyBool_FromLong( RNA_property_boolean_get(ptr, prop) );
715
 
                break;
716
 
        case PROP_INT:
717
 
                ret = PyLong_FromSsize_t( (Py_ssize_t)RNA_property_int_get(ptr, prop) );
718
 
                break;
719
 
        case PROP_FLOAT:
720
 
                ret = PyFloat_FromDouble( RNA_property_float_get(ptr, prop) );
721
 
                break;
722
 
        case PROP_STRING:
723
 
        {
724
 
                char *buf;
725
 
                buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1);
726
 
                ret = PyUnicode_FromString( buf );
727
 
                MEM_freeN(buf);
728
 
                break;
729
 
        }
730
 
        case PROP_ENUM:
731
 
        {
732
 
                ret= pyrna_enum_to_py(ptr, prop, RNA_property_enum_get(ptr, prop));
733
 
                break;
734
 
        }
735
 
        case PROP_POINTER:
736
 
        {
737
 
                PointerRNA newptr;
738
 
                newptr= RNA_property_pointer_get(ptr, prop);
739
 
                if (newptr.data) {
740
 
                        ret = pyrna_struct_CreatePyObject(&newptr);
741
 
                } else {
742
 
                        ret = Py_None;
743
 
                        Py_INCREF(ret);
744
 
                }
745
 
                break;
746
 
        }
747
 
        case PROP_COLLECTION:
748
 
                ret = pyrna_prop_CreatePyObject(ptr, prop);
749
 
                break;
750
 
        default:
751
 
                PyErr_Format(PyExc_TypeError, "bpy_struct internal error: unknown type \"%d\" (pyrna_prop_to_py)", type);
752
 
                ret = NULL;
753
 
                break;
754
 
        }
755
 
        
 
1344
                case PROP_BOOLEAN:
 
1345
                        ret = PyBool_FromLong(RNA_property_boolean_get(ptr, prop));
 
1346
                        break;
 
1347
                case PROP_INT:
 
1348
                        ret = PyLong_FromSsize_t((Py_ssize_t)RNA_property_int_get(ptr, prop));
 
1349
                        break;
 
1350
                case PROP_FLOAT:
 
1351
                        ret = PyFloat_FromDouble(RNA_property_float_get(ptr, prop));
 
1352
                        break;
 
1353
                case PROP_STRING:
 
1354
                {
 
1355
                        const int subtype = RNA_property_subtype(prop);
 
1356
                        const char *buf;
 
1357
                        int buf_len;
 
1358
                        char buf_fixed[32];
 
1359
 
 
1360
                        buf = RNA_property_string_get_alloc(ptr, prop, buf_fixed, sizeof(buf_fixed), &buf_len);
 
1361
#ifdef USE_STRING_COERCE
 
1362
                        /* only file paths get special treatment, they may contain non utf-8 chars */
 
1363
                        if (subtype == PROP_BYTESTRING) {
 
1364
                                ret = PyBytes_FromStringAndSize(buf, buf_len);
 
1365
                        }
 
1366
                        else if (ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
 
1367
                                ret = PyC_UnicodeFromByteAndSize(buf, buf_len);
 
1368
                        }
 
1369
                        else {
 
1370
                                ret = PyUnicode_FromStringAndSize(buf, buf_len);
 
1371
                        }
 
1372
#else // USE_STRING_COERCE
 
1373
                        if (subtype == PROP_BYTESTRING) {
 
1374
                                ret = PyBytes_FromStringAndSize(buf, buf_len);
 
1375
                        }
 
1376
                        else {
 
1377
                                ret = PyUnicode_FromStringAndSize(buf, buf_len);
 
1378
                        }
 
1379
#endif // USE_STRING_COERCE
 
1380
                        if (buf_fixed != buf) {
 
1381
                                MEM_freeN((void *)buf);
 
1382
                        }
 
1383
                        break;
 
1384
                }
 
1385
                case PROP_ENUM:
 
1386
                {
 
1387
                        ret = pyrna_enum_to_py(ptr, prop, RNA_property_enum_get(ptr, prop));
 
1388
                        break;
 
1389
                }
 
1390
                case PROP_POINTER:
 
1391
                {
 
1392
                        PointerRNA newptr;
 
1393
                        newptr = RNA_property_pointer_get(ptr, prop);
 
1394
                        if (newptr.data) {
 
1395
                                ret = pyrna_struct_CreatePyObject(&newptr);
 
1396
                        }
 
1397
                        else {
 
1398
                                ret = Py_None;
 
1399
                                Py_INCREF(ret);
 
1400
                        }
 
1401
                        break;
 
1402
                }
 
1403
                case PROP_COLLECTION:
 
1404
                        ret = pyrna_prop_CreatePyObject(ptr, prop);
 
1405
                        break;
 
1406
                default:
 
1407
                        PyErr_Format(PyExc_TypeError,
 
1408
                                     "bpy_struct internal error: unknown type '%d' (pyrna_prop_to_py)", type);
 
1409
                        ret = NULL;
 
1410
                        break;
 
1411
        }
 
1412
 
756
1413
        return ret;
757
1414
}
758
1415
 
762
1419
{
763
1420
        int error_val = 0;
764
1421
        int totkw;
765
 
        const char *arg_name= NULL;
 
1422
        const char *arg_name = NULL;
766
1423
        PyObject *item;
767
1424
 
768
 
        totkw = kw ? PyDict_Size(kw):0;
 
1425
        totkw = kw ? PyDict_Size(kw) : 0;
769
1426
 
770
1427
        RNA_STRUCT_BEGIN(ptr, prop) {
771
 
                arg_name= RNA_property_identifier(prop);
772
 
 
773
 
                if (strcmp(arg_name, "rna_type")==0) continue;
774
 
 
775
 
                if (kw==NULL) {
776
 
                        PyErr_Format( PyExc_TypeError, "%.200s: no keywords, expected \"%.200s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
777
 
                        error_val= -1;
 
1428
                arg_name = RNA_property_identifier(prop);
 
1429
 
 
1430
                if (strcmp(arg_name, "rna_type") == 0) continue;
 
1431
 
 
1432
                if (kw == NULL) {
 
1433
                        PyErr_Format(PyExc_TypeError,
 
1434
                                     "%.200s: no keywords, expected \"%.200s\"",
 
1435
                                     error_prefix, arg_name ? arg_name : "<UNKNOWN>");
 
1436
                        error_val = -1;
778
1437
                        break;
779
1438
                }
780
1439
 
781
 
                item= PyDict_GetItemString(kw, arg_name); /* wont set an error */
 
1440
                item = PyDict_GetItemString(kw, arg_name); /* wont set an error */
782
1441
 
783
1442
                if (item == NULL) {
784
 
                        if(all_args) {
785
 
                                PyErr_Format( PyExc_TypeError, "%.200s: keyword \"%.200s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
 
1443
                        if (all_args) {
 
1444
                                PyErr_Format(PyExc_TypeError,
 
1445
                                             "%.200s: keyword \"%.200s\" missing",
 
1446
                                             error_prefix, arg_name ? arg_name : "<UNKNOWN>");
786
1447
                                error_val = -1; /* pyrna_py_to_prop sets the error */
787
1448
                                break;
788
1449
                        }
789
 
                } else {
790
 
                        if (pyrna_py_to_prop(ptr, prop, NULL, NULL, item, error_prefix)) {
791
 
                                error_val= -1;
 
1450
                }
 
1451
                else {
 
1452
                        if (pyrna_py_to_prop(ptr, prop, NULL, item, error_prefix)) {
 
1453
                                error_val = -1;
792
1454
                                break;
793
1455
                        }
794
1456
                        totkw--;
796
1458
        }
797
1459
        RNA_STRUCT_END;
798
1460
 
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;
802
1464
 
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;
806
 
                        arg_name= NULL;
 
1468
                        arg_name = NULL;
807
1469
                }
808
1470
 
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>");
810
1474
                error_val = -1;
811
1475
        }
812
1476
 
813
1477
        return error_val;
814
1478
}
815
1479
 
816
 
static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw);
817
1480
 
818
 
static PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func)
 
1481
static PyObject *pyrna_func_to_py(PointerRNA *ptr, FunctionRNA *func)
819
1482
{
820
 
        static PyMethodDef func_meth = {"<generic rna function>", (PyCFunction)pyrna_func_call, METH_VARARGS|METH_KEYWORDS, "python rna function"};
821
 
        PyObject *self;
822
 
        PyObject *ret;
823
 
        
824
 
        if(func==NULL) {
825
 
                PyErr_Format( PyExc_RuntimeError, "%.200s: type attempted to get NULL function", RNA_struct_identifier(pyrna->ptr.type));
826
 
                return NULL;
827
 
        }
828
 
 
829
 
        self= PyTuple_New(2);
830
 
        
831
 
        PyTuple_SET_ITEM(self, 0, (PyObject *)pyrna);
832
 
        Py_INCREF(pyrna);
833
 
 
834
 
        PyTuple_SET_ITEM(self, 1, PyCapsule_New((void *)func, NULL, NULL));
835
 
        
836
 
        ret= PyCFunction_New(&func_meth, self);
837
 
        Py_DECREF(self);
838
 
        
839
 
        return ret;
 
1483
        BPy_FunctionRNA *pyfunc = (BPy_FunctionRNA *) PyObject_NEW(BPy_FunctionRNA, &pyrna_func_Type);
 
1484
        pyfunc->ptr = *ptr;
 
1485
        pyfunc->func = func;
 
1486
        return (PyObject *)pyfunc;
840
1487
}
841
1488
 
842
1489
 
843
 
 
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)
845
1491
{
846
1492
        /* XXX hard limits should be checked here */
847
 
        int type = RNA_property_type(prop);
848
 
        
849
 
 
850
 
        if (RNA_property_array_check(ptr, prop)) {
851
 
 
852
 
                /* char error_str[512]; */
853
 
                int ok= 1;
854
 
 
855
 
#ifdef USE_MATHUTILS
856
 
                if(MatrixObject_Check(value)) {
857
 
                        MatrixObject *mat = (MatrixObject*)value;
858
 
                        if(!BaseMath_ReadCallback(mat))
859
 
                                return -1;
860
 
                } else /* continue... */
861
 
#endif
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);
864
 
                        return -1;
865
 
                }
 
1493
        const int type = RNA_property_type(prop);
 
1494
 
 
1495
 
 
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);
868
 
 
869
 
                if (!ok) {
870
 
                        /* PyErr_Format(PyExc_AttributeError, "%.200s %s", error_prefix, error_str); */
 
1498
                if (pyrna_py_to_array(ptr, prop, data, value, error_prefix) == -1) {
871
1499
                        return -1;
872
1500
                }
873
1501
        }
874
1502
        else {
875
1503
                /* Normal Property (not an array) */
876
 
                
 
1504
 
877
1505
                /* see if we can coorce into a python type - PropertyType */
878
1506
                switch (type) {
879
 
                case PROP_BOOLEAN:
880
 
                {
881
 
                        int param;
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 );
887
 
                        else
888
 
                                param = PyLong_AsSsize_t( value );
889
 
                        
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));
892
 
                                return -1;
893
 
                        } else {
894
 
                                if(data)        *((int*)data)= param;
895
 
                                else            RNA_property_boolean_set(ptr, prop, param);
896
 
                        }
897
 
                        break;
898
 
                }
899
 
                case PROP_INT:
900
 
                {
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));
904
 
                                return -1;
905
 
                        } else {
906
 
                                RNA_property_int_clamp(ptr, prop, &param);
907
 
                                if(data)        *((int*)data)= param;
908
 
                                else            RNA_property_int_set(ptr, prop, param);
909
 
                        }
910
 
                        break;
911
 
                }
912
 
                case PROP_FLOAT:
913
 
                {
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));
917
 
                                return -1;
918
 
                        } else {
919
 
                                RNA_property_float_clamp(ptr, prop, (float *)&param);
920
 
                                if(data)        *((float*)data)= param;
921
 
                                else            RNA_property_float_set(ptr, prop, param);
922
 
                        }
923
 
                        break;
924
 
                }
925
 
                case PROP_STRING:
926
 
                {
927
 
                        char *param = _PyUnicode_AsString(value);
928
 
 
929
 
                        if (param==NULL) {
930
 
                                PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a string type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
931
 
                                return -1;
932
 
                        }
933
 
                        else {
934
 
                                if(data)        *((char**)data)= param;
935
 
                                else            RNA_property_string_set(ptr, prop, param);
936
 
                        }
937
 
                        break;
938
 
                }
939
 
                case PROP_ENUM:
940
 
                {
941
 
                        int val= 0;
942
 
 
943
 
                        if (PyUnicode_Check(value)) {
944
 
                                if (!pyrna_string_to_enum(value, ptr, prop, &val, error_prefix))
945
 
                                        return -1;
946
 
                        }
947
 
                        else if (PyAnySet_Check(value)) {
948
 
                                if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
 
1507
                        case PROP_BOOLEAN:
 
1508
                        {
 
1509
                                int param;
 
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);
 
1515
                                else
 
1516
                                        param = PyLong_AsLong(value);
 
1517
 
 
1518
                                if (param < 0) {
 
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);
 
1523
                                        return -1;
 
1524
                                }
 
1525
                                else {
 
1526
                                        if (data) *((int *)data) = param;
 
1527
                                        else RNA_property_boolean_set(ptr, prop, param);
 
1528
                                }
 
1529
                                break;
 
1530
                        }
 
1531
                        case PROP_INT:
 
1532
                        {
 
1533
                                int overflow;
 
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));
 
1541
                                        return -1;
 
1542
                                }
 
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);
 
1548
                                        return -1;
 
1549
                                }
 
1550
                                else {
 
1551
                                        int param_i = (int)param;
 
1552
                                        RNA_property_int_clamp(ptr, prop, &param_i);
 
1553
                                        if (data) *((int *)data) = param_i;
 
1554
                                        else RNA_property_int_set(ptr, prop, param_i);
 
1555
                                }
 
1556
                                break;
 
1557
                        }
 
1558
                        case PROP_FLOAT:
 
1559
                        {
 
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);
 
1566
                                        return -1;
 
1567
                                }
 
1568
                                else {
 
1569
                                        RNA_property_float_clamp(ptr, prop, (float *)&param);
 
1570
                                        if (data) *((float *)data) = param;
 
1571
                                        else RNA_property_float_set(ptr, prop, param);
 
1572
                                }
 
1573
                                break;
 
1574
                        }
 
1575
                        case PROP_STRING:
 
1576
                        {
 
1577
                                const int subtype = RNA_property_subtype(prop);
 
1578
                                const char *param;
 
1579
 
 
1580
                                if (subtype == PROP_BYTESTRING) {
 
1581
 
 
1582
                                        /* Byte String */
 
1583
 
 
1584
                                        param = PyBytes_AsString(value);
 
1585
 
 
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
 
1590
                                                         */
 
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));
 
1595
                                                }
 
1596
                                                else {
 
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);
 
1601
                                                }
 
1602
 
 
1603
                                                return -1;
 
1604
                                        }
 
1605
                                        else {
 
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);
 
1609
                                        }
 
1610
                                }
 
1611
                                else {
 
1612
 
 
1613
                                        /* Unicode String */
 
1614
 
 
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);
 
1620
                                        }
 
1621
                                        else {
 
1622
                                                param = _PyUnicode_AsString(value);
 
1623
#ifdef WITH_INTERNATIONAL
 
1624
                                                if (subtype == PROP_TRANSLATE) {
 
1625
                                                        param = IFACE_(param);
 
1626
                                                }
 
1627
#endif // WITH_INTERNATIONAL
 
1628
 
 
1629
                                        }
 
1630
#else // USE_STRING_COERCE
 
1631
                                        param = _PyUnicode_AsString(value);
 
1632
#endif // USE_STRING_COERCE
 
1633
 
 
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
 
1638
                                                         */
 
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));
 
1643
                                                }
 
1644
                                                else {
 
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);
 
1649
                                                }
 
1650
 
 
1651
                                                return -1;
 
1652
                                        }
 
1653
                                        else {
 
1654
                                                /* same as bytes */
 
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);
 
1657
                                        }
 
1658
#ifdef USE_STRING_COERCE
 
1659
                                        Py_XDECREF(value_coerce);
 
1660
#endif // USE_STRING_COERCE
 
1661
                                }
 
1662
                                break;
 
1663
                        }
 
1664
                        case PROP_ENUM:
 
1665
                        {
 
1666
                                int val = 0;
 
1667
 
 
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)
951
 
                                                return -1;
952
 
                                }
953
 
                                else {
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));
955
 
                                        return -1;
956
 
                                }
957
 
                        }
958
 
                        else {
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);
961
 
                                MEM_freeN(enum_str);
962
 
                                return -1;
963
 
                        }
964
 
 
965
 
                        if(data)        *((int*)data)= val;
966
 
                        else            RNA_property_enum_set(ptr, prop, val);
967
 
                        
968
 
                        break;
969
 
                }
970
 
                case PROP_POINTER:
971
 
                {
972
 
                        StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
973
 
                        int flag = RNA_property_flag(prop);
974
 
 
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);
979
 
                        }
980
 
 
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));
983
 
                                return -1;
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));
986
 
                                return -1;
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));
989
 
                                return -1;
990
 
                        } else {
991
 
                                BPy_StructRNA *param= (BPy_StructRNA*)value;
992
 
                                int raise_error= FALSE;
993
 
                                if(data) {
994
 
 
995
 
                                        if(flag & PROP_RNAPTR) {
996
 
                                                if(value == Py_None)
997
 
                                                        memset(data, 0, sizeof(PointerRNA));
998
 
                                                else
999
 
                                                        *((PointerRNA*)data)= param->ptr;
1000
 
                                        }
1001
 
                                        else if(value == Py_None) {
1002
 
                                                *((void**)data)= NULL;
1003
 
                                        }
1004
 
                                        else if(RNA_struct_is_a(param->ptr.type, ptype)) {
1005
 
                                                *((void**)data)= param->ptr.data;
1006
 
                                        }
1007
 
                                        else {
1008
 
                                                raise_error= TRUE;
1009
 
                                        }
1010
 
                                }
1011
 
                                else {
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);
1017
 
                                        }
1018
 
                                        else if(RNA_struct_is_a(param->ptr.type, ptype)) {
1019
 
                                                RNA_property_pointer_set(ptr, prop, param->ptr);
1020
 
                                        }
1021
 
                                        else {
 
1671
                                        if (pyrna_prop_to_enum_bitfield(ptr, prop, value, &val, error_prefix) < 0) {
 
1672
                                                return -1;
 
1673
                                        }
 
1674
                                }
 
1675
                                else {
 
1676
                                        /* simple enum string */
 
1677
                                        if (pyrna_string_to_enum(value, ptr, prop, &val, error_prefix) < 0) {
 
1678
                                                return -1;
 
1679
                                        }
 
1680
                                }
 
1681
 
 
1682
                                if (data) *((int *)data) = val;
 
1683
                                else RNA_property_enum_set(ptr, prop, val);
 
1684
 
 
1685
                                break;
 
1686
                        }
 
1687
                        case PROP_POINTER:
 
1688
                        {
 
1689
                                PyObject *value_new = NULL;
 
1690
 
 
1691
                                StructRNA *ptr_type = RNA_property_pointer_type(ptr, prop);
 
1692
                                int flag = RNA_property_flag(prop);
 
1693
 
 
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")
 
1698
                                 *
 
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.
 
1702
                                 *
 
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.
 
1705
                                 */
 
1706
                                if ((ptr_type == &RNA_AnyType) &&
 
1707
                                    (BPy_StructRNA_Check(value)) &&
 
1708
                                    (RNA_struct_is_a(((BPy_StructRNA *)value)->ptr.type, &RNA_Operator)))
 
1709
                                {
 
1710
                                        value = PyObject_GetAttrString(value, "properties");
 
1711
                                        value_new = value;
 
1712
                                }
 
1713
 
 
1714
 
 
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);
 
1720
                                }
 
1721
 
 
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 */
 
1724
                                        PointerRNA c_ptr;
 
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);
 
1728
                                                value_new = value;
 
1729
                                        }
 
1730
                                        else {
 
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));
 
1736
                                                return -1;
 
1737
                                        }
 
1738
                                }
 
1739
 
 
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;
 
1747
                                }
 
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;
 
1754
                                }
 
1755
                                else if ((value != Py_None) &&
 
1756
                                         ((flag & PROP_ID_SELF_CHECK) && ptr->id.data == ((BPy_StructRNA *)value)->ptr.id.data))
 
1757
                                {
 
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;
 
1763
                                }
 
1764
                                else {
 
1765
                                        BPy_StructRNA *param = (BPy_StructRNA *)value;
 
1766
                                        int raise_error = FALSE;
 
1767
                                        if (data) {
 
1768
 
 
1769
                                                if (flag & PROP_RNAPTR) {
 
1770
                                                        if (value == Py_None)
 
1771
                                                                memset(data, 0, sizeof(PointerRNA));
 
1772
                                                        else
 
1773
                                                                *((PointerRNA *)data) = param->ptr;
 
1774
                                                }
 
1775
                                                else if (value == Py_None) {
 
1776
                                                        *((void **)data) = NULL;
 
1777
                                                }
 
1778
                                                else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
 
1779
                                                        *((void **)data) = param->ptr.data;
 
1780
                                                }
 
1781
                                                else {
 
1782
                                                        raise_error = TRUE;
 
1783
                                                }
 
1784
                                        }
 
1785
                                        else {
 
1786
                                                /* data == NULL, assign to RNA */
 
1787
                                                if (value == Py_None) {
 
1788
                                                        PointerRNA valueptr = {{NULL}};
 
1789
                                                        RNA_property_pointer_set(ptr, prop, valueptr);
 
1790
                                                }
 
1791
                                                else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
 
1792
                                                        RNA_property_pointer_set(ptr, prop, param->ptr);
 
1793
                                                }
 
1794
                                                else {
 
1795
                                                        PointerRNA tmp;
 
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;
 
1803
                                                }
 
1804
                                        }
 
1805
 
 
1806
                                        if (raise_error) {
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));
1025
 
                                                return -1;
1026
 
                                        }
1027
 
                                }
1028
 
                                
1029
 
                                if(raise_error) {
1030
 
                                        PointerRNA tmp;
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));
1033
 
                                        return -1;
1034
 
                                }
1035
 
                        }
1036
 
                        break;
1037
 
                }
1038
 
                case PROP_COLLECTION:
1039
 
                {
1040
 
                        int seq_len, i;
1041
 
                        PyObject *item;
1042
 
                        PointerRNA itemptr;
1043
 
                        ListBase *lb;
1044
 
                        CollectionPointerLink *link;
1045
 
 
1046
 
                        lb= (data)? (ListBase*)data: NULL;
1047
 
                        
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);
1051
 
                                return -1;
1052
 
                        }
1053
 
 
1054
 
                        seq_len = PySequence_Length(value);
1055
 
                        for(i=0; i<seq_len; i++) {
1056
 
                                item= PySequence_GetItem(value, i);
1057
 
 
1058
 
                                if(item==NULL) {
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);
1060
 
                                        Py_XDECREF(item);
1061
 
                                        return -1;
1062
 
                                }
1063
 
 
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);
1066
 
                                        Py_XDECREF(item);
1067
 
                                        return -1;
1068
 
                                }
1069
 
 
1070
 
                                if(lb) {
1071
 
                                        link= MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
1072
 
                                        link->ptr= itemptr;
1073
 
                                        BLI_addtail(lb, link);
1074
 
                                }
1075
 
                                else
1076
 
                                        RNA_property_collection_add(ptr, prop, &itemptr);
1077
 
 
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);
1081
 
 
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);
1083
 
 
 
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;
 
1815
                                        }
 
1816
                                }
 
1817
 
 
1818
                                Py_XDECREF(value_new);
 
1819
 
 
1820
                                break;
 
1821
                        }
 
1822
                        case PROP_COLLECTION:
 
1823
                        {
 
1824
                                Py_ssize_t seq_len, i;
 
1825
                                PyObject *item;
 
1826
                                PointerRNA itemptr;
 
1827
                                ListBase *lb;
 
1828
                                CollectionPointerLink *link;
 
1829
 
 
1830
                                lb = (data) ? (ListBase *)data : NULL;
 
1831
 
 
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);
 
1838
                                        return -1;
 
1839
                                }
 
1840
 
 
1841
                                seq_len = PySequence_Size(value);
 
1842
                                for (i = 0; i < seq_len; i++) {
 
1843
                                        item = PySequence_GetItem(value, i);
 
1844
 
 
1845
                                        if (item == NULL) {
 
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);
 
1850
                                                Py_XDECREF(item);
 
1851
                                                return -1;
 
1852
                                        }
 
1853
 
 
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);
 
1860
                                                Py_XDECREF(item);
 
1861
                                                return -1;
 
1862
                                        }
 
1863
 
 
1864
                                        if (lb) {
 
1865
                                                link = MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
 
1866
                                                link->ptr = itemptr;
 
1867
                                                BLI_addtail(lb, link);
 
1868
                                        }
 
1869
                                        else
 
1870
                                                RNA_property_collection_add(ptr, prop, &itemptr);
 
1871
 
 
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);
 
1875
 
 
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);
 
1881
 
 
1882
                                                Py_DECREF(item);
 
1883
                                                Py_DECREF(msg);
 
1884
                                                return -1;
 
1885
                                        }
1084
1886
                                        Py_DECREF(item);
1085
 
                                        Py_DECREF(msg);
1086
 
                                        return -1;
1087
1887
                                }
1088
 
                                Py_DECREF(item);
 
1888
 
 
1889
                                break;
1089
1890
                        }
1090
 
                        
1091
 
                        break;
1092
 
                }
1093
 
                default:
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));
1095
 
                        return -1;
1096
 
                        break;
 
1891
                        default:
 
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));
 
1896
                                return -1;
 
1897
                                break;
1097
1898
                }
1098
1899
        }
1099
1900
 
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);
 
1904
        }
1102
1905
 
1103
1906
        return 0;
1104
1907
}
1105
1908
 
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)
1107
1910
{
 
1911
        PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
1108
1912
        return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
1109
1913
}
1110
1914
 
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)
1112
1916
{
1113
1917
        int ret = 0;
1114
 
        int totdim;
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;
1118
1920
 
1119
 
        totdim= RNA_property_array_dimension(ptr, prop, NULL);
 
1921
        const int totdim = RNA_property_array_dimension(ptr, prop, NULL);
1120
1922
 
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); */
1125
 
                        ret= -1;
 
1925
                if (pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "") == -1) {
 
1926
                        /* error is set */
 
1927
                        ret = -1;
1126
1928
                }
1127
1929
        }
1128
1930
        else {
1129
 
                /* see if we can coorce into a python type - PropertyType */
1130
 
                switch (type) {
1131
 
                case PROP_BOOLEAN:
 
1931
                /* see if we can coerce into a python type - PropertyType */
 
1932
                switch (RNA_property_type(prop)) {
 
1933
                        case PROP_BOOLEAN:
1132
1934
                        {
1133
 
                                int param = PyLong_AsSsize_t( value );
1134
 
                
1135
 
                                if( param < 0 || param > 1) {
 
1935
                                int param = PyLong_AsLong(value);
 
1936
 
 
1937
                                if (param < 0 || param > 1) {
1136
1938
                                        PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
1137
1939
                                        ret = -1;
1138
 
                                } else {
 
1940
                                }
 
1941
                                else {
1139
1942
                                        RNA_property_boolean_set_index(ptr, prop, index, param);
1140
1943
                                }
1141
1944
                                break;
1142
1945
                        }
1143
 
                case PROP_INT:
 
1946
                        case PROP_INT:
1144
1947
                        {
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");
1148
1951
                                        ret = -1;
1149
 
                                } else {
 
1952
                                }
 
1953
                                else {
1150
1954
                                        RNA_property_int_clamp(ptr, prop, &param);
1151
1955
                                        RNA_property_int_set_index(ptr, prop, index, param);
1152
1956
                                }
1153
1957
                                break;
1154
1958
                        }
1155
 
                case PROP_FLOAT:
 
1959
                        case PROP_FLOAT:
1156
1960
                        {
1157
1961
                                float param = PyFloat_AsDouble(value);
1158
1962
                                if (PyErr_Occurred()) {
1159
1963
                                        PyErr_SetString(PyExc_TypeError, "expected a float type");
1160
1964
                                        ret = -1;
1161
 
                                } else {
 
1965
                                }
 
1966
                                else {
1162
1967
                                        RNA_property_float_clamp(ptr, prop, &param);
1163
1968
                                        RNA_property_float_set_index(ptr, prop, index, param);
1164
1969
                                }
1165
1970
                                break;
1166
1971
                        }
1167
 
                default:
1168
 
                        PyErr_SetString(PyExc_AttributeError, "not an array type");
1169
 
                        ret = -1;
1170
 
                        break;
 
1972
                        default:
 
1973
                                PyErr_SetString(PyExc_AttributeError, "not an array type");
 
1974
                                ret = -1;
 
1975
                                break;
1171
1976
                }
1172
1977
        }
1173
1978
 
1174
1979
        /* Run rna property functions */
1175
 
        RNA_property_update(BPy_GetContext(), ptr, prop);
1176
 
        
 
1980
        if (RNA_property_update_check(prop)) {
 
1981
                RNA_property_update(BPy_GetContext(), ptr, prop);
 
1982
        }
 
1983
 
1177
1984
        return ret;
1178
1985
}
1179
1986
 
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)
1182
1989
{
 
1990
        PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
 
1991
 
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);
1185
1994
        else
1186
1995
                return RNA_property_array_length(&self->ptr, self->prop);
1187
1996
}
1188
1997
 
1189
 
static Py_ssize_t pyrna_prop_collection_length( BPy_PropertyRNA *self )
 
1998
static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self)
1190
1999
{
 
2000
        PYRNA_PROP_CHECK_INT(self);
 
2001
 
1191
2002
        return RNA_property_collection_length(&self->ptr, self->prop);
1192
2003
}
1193
2004
 
 
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)
 
2008
{
 
2009
        PYRNA_PROP_CHECK_INT(self);
 
2010
 
 
2011
        return RNA_property_array_length(&self->ptr, self->prop) ? 1 : 0;
 
2012
}
 
2013
 
 
2014
static int pyrna_prop_collection_bool(BPy_PropertyRNA *self)
 
2015
{
 
2016
        /* no callback defined, just iterate and find the nth item */
 
2017
        CollectionPropertyIterator iter;
 
2018
        int test;
 
2019
 
 
2020
        PYRNA_PROP_CHECK_INT(self);
 
2021
 
 
2022
        RNA_property_collection_begin(&self->ptr, self->prop, &iter);
 
2023
        test = iter.valid;
 
2024
        RNA_property_collection_end(&iter);
 
2025
        return test;
 
2026
}
 
2027
 
 
2028
 
 
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)                              \
 
2033
        if (keynum < 0) {                                                         \
 
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);   \
 
2038
                        return ret_err;                                                   \
 
2039
                }                                                                     \
 
2040
        }                                                                         \
 
2041
 
 
2042
 
1194
2043
/* internal use only */
1195
2044
static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
1196
2045
{
1197
2046
        PointerRNA newptr;
1198
 
        int len= RNA_property_collection_length(&self->ptr, self->prop);
1199
 
 
1200
 
        if(keynum < 0) keynum += len;
1201
 
 
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);
1205
 
                }
1206
 
                PyErr_Format(PyExc_IndexError, "bpy_prop_collection[index]: index %d could not be found", keynum);
 
2047
        Py_ssize_t keynum_abs = keynum;
 
2048
 
 
2049
        PYRNA_PROP_CHECK_OBJ(self);
 
2050
 
 
2051
        PYRNA_PROP_COLLECTION_ABS_INDEX(NULL);
 
2052
 
 
2053
        if (RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum_abs, &newptr)) {
 
2054
                return pyrna_struct_CreatePyObject(&newptr);
 
2055
        }
 
2056
        else {
 
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);
 
2062
                }
 
2063
                else {
 
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",
 
2067
                                     keynum_abs, len);
 
2068
                }
 
2069
 
1207
2070
                return NULL;
1208
2071
        }
1209
 
        PyErr_Format(PyExc_IndexError, "bpy_prop_collection[index]: index %d out of range", keynum);
1210
 
        return NULL;
1211
 
}
1212
 
 
1213
 
static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyRNA *self, int keynum)
1214
 
{
1215
 
        int len= pyrna_prop_array_length(self);
1216
 
 
1217
 
        if(keynum < 0) keynum += len;
1218
 
 
1219
 
        if(keynum >= 0 && keynum < len)
1220
 
                return pyrna_prop_to_py_index(self, keynum);
1221
 
 
1222
 
        PyErr_Format(PyExc_IndexError, "bpy_prop_array[index]: index %d out of range", keynum);
1223
 
        return NULL;
1224
 
}
1225
 
 
1226
 
static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, char *keyname)
 
2072
}
 
2073
 
 
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)
 
2076
{
 
2077
        Py_ssize_t keynum_abs = keynum;
 
2078
        const PointerRNA *ptr = (value == Py_None) ? (&PointerRNA_NULL) : &((BPy_StructRNA *)value)->ptr;
 
2079
 
 
2080
        PYRNA_PROP_CHECK_INT(self);
 
2081
 
 
2082
        PYRNA_PROP_COLLECTION_ABS_INDEX(-1);
 
2083
 
 
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);
 
2090
                }
 
2091
                else {
 
2092
 
 
2093
                        PyErr_Format(PyExc_IndexError,
 
2094
                                     "bpy_prop_collection[index] = value: "
 
2095
                                     "failed assignment (unknown reason)", keynum);
 
2096
                }
 
2097
                return -1;
 
2098
        }
 
2099
 
 
2100
        return 0;
 
2101
}
 
2102
 
 
2103
static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
 
2104
{
 
2105
        int len;
 
2106
 
 
2107
        PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
 
2108
 
 
2109
        len = pyrna_prop_array_length(self);
 
2110
 
 
2111
        if (keynum < 0) keynum += len;
 
2112
 
 
2113
        if (keynum >= 0 && keynum < len)
 
2114
                return pyrna_prop_array_to_py_index(self, keynum);
 
2115
 
 
2116
        PyErr_Format(PyExc_IndexError,
 
2117
                     "bpy_prop_array[index]: index %d out of range", keynum);
 
2118
        return NULL;
 
2119
}
 
2120
 
 
2121
static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, const char *keyname)
1227
2122
{
1228
2123
        PointerRNA newptr;
1229
 
        if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
 
2124
 
 
2125
        PYRNA_PROP_CHECK_OBJ(self);
 
2126
 
 
2127
        if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
1230
2128
                return pyrna_struct_CreatePyObject(&newptr);
1231
2129
 
1232
2130
        PyErr_Format(PyExc_KeyError, "bpy_prop_collection[key]: key \"%.200s\" not found", keyname);
1234
2132
}
1235
2133
/* static PyObject *pyrna_prop_array_subscript_str(BPy_PropertyRNA *self, char *keyname) */
1236
2134
 
1237
 
static PyObject *pyrna_prop_collection_subscript_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
1238
 
{
1239
 
        PointerRNA newptr;
1240
 
        PyObject *list = PyList_New(stop - start);
1241
 
        int count;
1242
 
 
1243
 
        start = MIN2(start,stop); /* values are clamped from  */
1244
 
 
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));
1248
 
                }
1249
 
                else {
1250
 
                        Py_DECREF(list);
1251
 
 
1252
 
                        PyErr_SetString(PyExc_RuntimeError, "error getting an rna struct from a collection");
1253
 
                        return NULL;
1254
 
                }
1255
 
        }
 
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)
 
2137
 *
 
2138
 * note:
 
2139
 * error codes since this is not to be called directly from python,
 
2140
 * this matches pythons __contains__ values capi.
 
2141
 * -1: exception set
 
2142
 *  0: not found
 
2143
 *  1: found */
 
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,
 
2146
                                                     PointerRNA *r_ptr
 
2147
                                                     )
 
2148
{
 
2149
        char *keyname;
 
2150
 
 
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));
 
2156
                return -1;
 
2157
        }
 
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));
 
2162
                return -1;
 
2163
        }
 
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);
 
2168
                return -1;
 
2169
        }
 
2170
        else {
 
2171
                PyObject *keylib = PyTuple_GET_ITEM(key, 1);
 
2172
                Library *lib;
 
2173
                int found = FALSE;
 
2174
 
 
2175
                if (keylib == Py_None) {
 
2176
                        lib = NULL;
 
2177
                }
 
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));
 
2182
                        if (lib == NULL) {
 
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);
 
2188
                                        return -1;
 
2189
                                }
 
2190
                                else {
 
2191
                                        return 0;
 
2192
                                }
 
2193
 
 
2194
                        }
 
2195
                }
 
2196
                else {
 
2197
                        PyErr_Format(PyExc_KeyError,
 
2198
                                     "%s: lib must be a sting or None, not %.200s",
 
2199
                                     err_prefix, Py_TYPE(keylib)->tp_name);
 
2200
                        return -1;
 
2201
                }
 
2202
 
 
2203
                /* lib is either a valid poniter or NULL,
 
2204
                 * either way can do direct comparison with id.lib */
 
2205
 
 
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)) {
 
2209
                                found = TRUE;
 
2210
                                if (r_ptr) {
 
2211
                                        *r_ptr = itemptr;
 
2212
                                }
 
2213
                                break;
 
2214
                        }
 
2215
                }
 
2216
                RNA_PROP_END;
 
2217
 
 
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");
 
2223
                        return -1;
 
2224
                }
 
2225
                else {
 
2226
                        return found; /* 1 / 0, no exception */
 
2227
                }
 
2228
        }
 
2229
}
 
2230
 
 
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)
 
2233
{
 
2234
        PointerRNA ptr;
 
2235
        const int contains = pyrna_prop_collection_subscript_str_lib_pair_ptr(self, key, err_prefix, err_not_found, &ptr);
 
2236
 
 
2237
        if (contains == 1) {
 
2238
                return pyrna_struct_CreatePyObject(&ptr);
 
2239
        }
 
2240
        else {
 
2241
                return NULL;
 
2242
        }
 
2243
}
 
2244
 
 
2245
 
 
2246
static PyObject *pyrna_prop_collection_subscript_slice(BPy_PropertyRNA *self, Py_ssize_t start, Py_ssize_t stop)
 
2247
{
 
2248
        CollectionPropertyIterator rna_macro_iter;
 
2249
        int count = 0;
 
2250
 
 
2251
        PyObject *list;
 
2252
        PyObject *item;
 
2253
 
 
2254
        PYRNA_PROP_CHECK_OBJ(self);
 
2255
 
 
2256
        list = PyList_New(0);
 
2257
 
 
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))
 
2262
        {
 
2263
                /* PointerRNA itemptr = rna_macro_iter.ptr; */
 
2264
                if (count == start) {
 
2265
                        break;
 
2266
                }
 
2267
                count++;
 
2268
        }
 
2269
 
 
2270
        /* add items until stop */
 
2271
        for (; rna_macro_iter.valid;
 
2272
             RNA_property_collection_next(&rna_macro_iter))
 
2273
        {
 
2274
                item = pyrna_struct_CreatePyObject(&rna_macro_iter.ptr);
 
2275
                PyList_Append(list, item);
 
2276
                Py_DECREF(item);
 
2277
 
 
2278
                count++;
 
2279
                if (count == stop) {
 
2280
                        break;
 
2281
                }
 
2282
        }
 
2283
 
 
2284
        RNA_property_collection_end(&rna_macro_iter);
1256
2285
 
1257
2286
        return list;
1258
2287
}
1259
2288
 
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.
1263
2292
 */
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)
1265
2295
{
1266
2296
        int count, totdim;
1267
 
 
1268
 
        PyObject *list = PyList_New(stop - start);
 
2297
        PyObject *tuple;
 
2298
 
 
2299
        PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
 
2300
 
 
2301
        tuple = PyTuple_New(stop - start);
 
2302
 
 
2303
        /* PYRNA_PROP_CHECK_OBJ(self); isn't needed, internal use only */
1269
2304
 
1270
2305
        totdim = RNA_property_array_dimension(ptr, prop, NULL);
1271
2306
 
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));
1275
2310
        }
1276
2311
        else {
1277
2312
                switch (RNA_property_type(prop)) {
1278
 
                case PROP_FLOAT:
 
2313
                        case PROP_FLOAT:
1279
2314
                        {
1280
2315
                                float values_stack[PYRNA_STACK_ARRAY];
1281
2316
                                float *values;
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);
1285
 
                        
1286
 
                                for(count=start; count<stop; count++)
1287
 
                                        PyList_SET_ITEM(list, count-start, PyFloat_FromDouble(values[count]));
1288
 
 
1289
 
                                if(values != values_stack) {
 
2320
 
 
2321
                                for (count = start; count < stop; count++)
 
2322
                                        PyTuple_SET_ITEM(tuple, count - start, PyFloat_FromDouble(values[count]));
 
2323
 
 
2324
                                if (values != values_stack) {
1290
2325
                                        PyMem_FREE(values);
1291
2326
                                }
1292
2327
                                break;
1293
2328
                        }
1294
 
                case PROP_BOOLEAN:
 
2329
                        case PROP_BOOLEAN:
1295
2330
                        {
1296
2331
                                int values_stack[PYRNA_STACK_ARRAY];
1297
2332
                                int *values;
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; }
1300
2335
 
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]));
1304
2339
 
1305
 
                                if(values != values_stack) {
 
2340
                                if (values != values_stack) {
1306
2341
                                        PyMem_FREE(values);
1307
2342
                                }
1308
2343
                                break;
1309
2344
                        }
1310
 
                case PROP_INT:
 
2345
                        case PROP_INT:
1311
2346
                        {
1312
2347
                                int values_stack[PYRNA_STACK_ARRAY];
1313
2348
                                int *values;
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; }
1316
2351
 
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]));
1320
2355
 
1321
 
                                if(values != values_stack) {
 
2356
                                if (values != values_stack) {
1322
2357
                                        PyMem_FREE(values);
1323
2358
                                }
1324
2359
                                break;
1325
2360
                        }
1326
 
                default:
1327
 
                        /* probably will never happen */
1328
 
                        PyErr_SetString(PyExc_TypeError, "not an array type");
1329
 
                        Py_DECREF(list);
1330
 
                        list= NULL;
 
2361
                        default:
 
2362
                                BLI_assert(!"Invalid array type");
 
2363
 
 
2364
                                PyErr_SetString(PyExc_TypeError, "not an array type");
 
2365
                                Py_DECREF(tuple);
 
2366
                                tuple = NULL;
1331
2367
                }
1332
2368
        }
1333
 
        return list;
 
2369
        return tuple;
1334
2370
}
1335
2371
 
1336
2372
static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
1337
2373
{
 
2374
        PYRNA_PROP_CHECK_OBJ(self);
 
2375
 
1338
2376
        if (PyUnicode_Check(key)) {
1339
2377
                return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
1340
2378
        }
1346
2384
                return pyrna_prop_collection_subscript_int(self, i);
1347
2385
        }
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;
1351
 
 
1352
 
                if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1353
 
                        return NULL;
1354
 
 
1355
 
                if (slicelength <= 0) {
1356
 
                        return PyList_New(0);
1357
 
                }
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;
 
2389
 
 
2390
                if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
 
2391
                        return NULL;
 
2392
                }
 
2393
                else if (step != 1) {
 
2394
                        PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
 
2395
                        return NULL;
 
2396
                }
 
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);
1360
2399
                }
1361
2400
                else {
1362
 
                        PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported with rna");
1363
 
                        return NULL;
 
2401
                        Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
 
2402
 
 
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;
 
2406
 
 
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;
 
2412
                        }
 
2413
 
 
2414
                        if (stop - start <= 0) {
 
2415
                                return PyList_New(0);
 
2416
                        }
 
2417
                        else {
 
2418
                                return pyrna_prop_collection_subscript_slice(self, start, stop);
 
2419
                        }
1364
2420
                }
1365
2421
        }
 
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);
 
2426
        }
1366
2427
        else {
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);
1368
2432
                return NULL;
1369
2433
        }
1370
2434
}
1371
2435
 
1372
 
static PyObject *pyrna_prop_array_subscript(BPy_PropertyRNA *self, PyObject *key)
1373
 
{
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)
 
2439
{
 
2440
        StructRNA *prop_srna;
 
2441
 
 
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");
 
2447
                        return -1;
 
2448
                }
 
2449
                else {
 
2450
                        return 0; /* None is OK */
 
2451
                }
 
2452
        }
 
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);
 
2458
                return -1;
 
2459
        }
 
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)
 
2468
                                     );
 
2469
                        return -1;
 
2470
                }
 
2471
                else {
 
2472
                        return 0; /* OK, this is the correct type!*/
 
2473
                }
 
2474
        }
 
2475
 
 
2476
        PyErr_Format(PyExc_TypeError,
 
2477
                     "bpy_prop_collection[key] = value: internal error, "
 
2478
                     "failed to get the collection type");
 
2479
        return -1;
 
2480
}
 
2481
 
 
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)
 
2485
{
 
2486
        PYRNA_PROP_CHECK_INT(self);
 
2487
 
 
2488
        /* validate the assigned value */
 
2489
        if (value == NULL) {
 
2490
                PyErr_SetString(PyExc_TypeError,
 
2491
                                "del bpy_prop_collection[key]: not supported");
 
2492
                return -1;
 
2493
        }
 
2494
        else if (pyrna_prop_collection_type_check(self, value) == -1) {
 
2495
                return -1; /* exception is set */
 
2496
        }
 
2497
 
 
2498
#if 0
 
2499
        if (PyUnicode_Check(key)) {
 
2500
                return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
 
2501
        }
 
2502
        else
 
2503
#endif
 
2504
        if (PyIndex_Check(key)) {
 
2505
                Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
 
2506
                if (i == -1 && PyErr_Occurred())
 
2507
                        return -1;
 
2508
 
 
2509
                return pyrna_prop_collection_ass_subscript_int(self, i, value);
 
2510
        }
 
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;
 
2515
 
 
2516
                if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
 
2517
                        return NULL;
 
2518
                }
 
2519
                else if (step != 1) {
 
2520
                        PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
 
2521
                        return NULL;
 
2522
                }
 
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);
 
2525
                }
 
2526
                else {
 
2527
                        Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
 
2528
 
 
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;
 
2532
 
 
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;
 
2538
                        }
 
2539
 
 
2540
                        if (stop - start <= 0) {
 
2541
                                return PyList_New(0);
 
2542
                        }
 
2543
                        else {
 
2544
                                return pyrna_prop_collection_subscript_slice(self, start, stop);
 
2545
                        }
 
2546
                }
 
2547
        }
 
2548
#endif
 
2549
        else {
 
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);
 
2554
                return -1;
 
2555
        }
 
2556
}
 
2557
 
 
2558
static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
 
2559
{
 
2560
        PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
 
2561
 
 
2562
#if 0
 
2563
        if (PyUnicode_Check(key)) {
1375
2564
                return pyrna_prop_array_subscript_str(self, _PyUnicode_AsString(key));
1376
 
        } else*/
 
2565
        }
 
2566
        else
 
2567
#endif
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())
1380
2571
                        return NULL;
1381
 
                return pyrna_prop_array_subscript_int(self, PyLong_AsSsize_t(key));
 
2572
                return pyrna_prop_array_subscript_int(self, PyLong_AsLong(key));
1382
2573
        }
1383
2574
        else if (PySlice_Check(key)) {
1384
 
                Py_ssize_t start, stop, step, slicelength;
1385
 
                int len = pyrna_prop_array_length(self);
1386
 
 
1387
 
                if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1388
 
                        return NULL;
1389
 
 
1390
 
                if (slicelength <= 0) {
1391
 
                        return PyList_New(0);
1392
 
                }
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;
 
2577
 
 
2578
                if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
 
2579
                        return NULL;
 
2580
                }
 
2581
                else if (step != 1) {
 
2582
                        PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported");
 
2583
                        return NULL;
 
2584
                }
 
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);
1395
2590
                }
1396
2591
                else {
1397
 
                        PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported with rna");
1398
 
                        return NULL;
 
2592
                        int len = pyrna_prop_array_length(self);
 
2593
                        Py_ssize_t start, stop, slicelength;
 
2594
 
 
2595
                        if (PySlice_GetIndicesEx((void *)key, len, &start, &stop, &step, &slicelength) < 0)
 
2596
                                return NULL;
 
2597
 
 
2598
                        if (slicelength <= 0) {
 
2599
                                return PyTuple_New(0);
 
2600
                        }
 
2601
                        else {
 
2602
                                return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, start, stop, len);
 
2603
                        }
1399
2604
                }
1400
2605
        }
1401
2606
        else {
1404
2609
        }
1405
2610
}
1406
2611
 
1407
 
/* could call (pyrna_py_to_prop_index(self, i, value) in a loop but it is slow */
1408
 
static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length, PyObject *value_orig)
 
2612
/* could call (pyrna_py_to_prop_array_index(self, i, value) in a loop but it is slow */
 
2613
static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop,
 
2614
                                          int start, int stop, int length, PyObject *value_orig)
1409
2615
{
1410
2616
        PyObject *value;
1411
2617
        int count;
1412
 
        void *values_alloc= NULL;
1413
 
        int ret= 0;
1414
 
 
1415
 
        if(value_orig == NULL) {
1416
 
                PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice] = value: deleting with list types is not supported by bpy_struct.");
1417
 
                return -1;
1418
 
        }
1419
 
 
1420
 
        if(!(value=PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: assignment is not a sequence type"))) {
1421
 
                return -1;
1422
 
        }
1423
 
 
1424
 
        if(PySequence_Fast_GET_SIZE(value) != stop-start) {
 
2618
        void *values_alloc = NULL;
 
2619
        int ret = 0;
 
2620
 
 
2621
        if (value_orig == NULL) {
 
2622
                PyErr_SetString(PyExc_TypeError,
 
2623
                                "bpy_prop_array[slice] = value: deleting with list types is not supported by bpy_struct");
 
2624
                return -1;
 
2625
        }
 
2626
 
 
2627
        if (!(value = PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: assignment is not a sequence type"))) {
 
2628
                return -1;
 
2629
        }
 
2630
 
 
2631
        if (PySequence_Fast_GET_SIZE(value) != stop - start) {
1425
2632
                Py_DECREF(value);
1426
 
                PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice] = value: resizing bpy_struct arrays isn't supported.");
 
2633
                PyErr_SetString(PyExc_TypeError,
 
2634
                                "bpy_prop_array[slice] = value: re-sizing bpy_struct arrays isn't supported");
1427
2635
                return -1;
1428
2636
        }
1429
2637
 
1436
2644
                        float min, max;
1437
2645
                        RNA_property_float_range(ptr, prop, &min, &max);
1438
2646
 
1439
 
                        if(length > PYRNA_STACK_ARRAY)  {       values= values_alloc= PyMem_MALLOC(sizeof(float) * length); }
1440
 
                        else                                                    {       values= values_stack; }
1441
 
                        if(start != 0 || stop != length) /* partial assignment? - need to get the array */
 
2647
                        if (length > PYRNA_STACK_ARRAY) { values = values_alloc = PyMem_MALLOC(sizeof(float) * length); }
 
2648
                        else                            { values = values_stack; }
 
2649
                        if (start != 0 || stop != length) /* partial assignment? - need to get the array */
1442
2650
                                RNA_property_float_get_array(ptr, prop, values);
1443
 
                        
1444
 
                        for(count=start; count<stop; count++) {
1445
 
                                fval = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, count-start));
 
2651
 
 
2652
                        for (count = start; count < stop; count++) {
 
2653
                                fval = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, count - start));
1446
2654
                                CLAMP(fval, min, max);
1447
2655
                                values[count] = fval;
1448
2656
                        }
1449
2657
 
1450
 
                        if(PyErr_Occurred())    ret= -1;
1451
 
                        else                                    RNA_property_float_set_array(ptr, prop, values);
 
2658
                        if (PyErr_Occurred()) ret = -1;
 
2659
                        else                  RNA_property_float_set_array(ptr, prop, values);
1452
2660
                        break;
1453
2661
                }
1454
2662
                case PROP_BOOLEAN:
1455
2663
                {
1456
2664
                        int values_stack[PYRNA_STACK_ARRAY];
1457
2665
                        int *values;
1458
 
                        if(length > PYRNA_STACK_ARRAY)  {       values= values_alloc= PyMem_MALLOC(sizeof(int) * length); }
1459
 
                        else                                                    {       values= values_stack; }
 
2666
                        if (length > PYRNA_STACK_ARRAY) { values = values_alloc = PyMem_MALLOC(sizeof(int) * length); }
 
2667
                        else                            { values = values_stack; }
1460
2668
 
1461
 
                        if(start != 0 || stop != length) /* partial assignment? - need to get the array */
 
2669
                        if (start != 0 || stop != length) /* partial assignment? - need to get the array */
1462
2670
                                RNA_property_boolean_get_array(ptr, prop, values);
1463
 
        
1464
 
                        for(count=start; count<stop; count++)
1465
 
                                values[count] = PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, count-start));
1466
 
 
1467
 
                        if(PyErr_Occurred())    ret= -1;
1468
 
                        else                                    RNA_property_boolean_set_array(ptr, prop, values);
 
2671
 
 
2672
                        for (count = start; count < stop; count++)
 
2673
                                values[count] = PyLong_AsLong(PySequence_Fast_GET_ITEM(value, count - start));
 
2674
 
 
2675
                        if (PyErr_Occurred()) ret = -1;
 
2676
                        else                  RNA_property_boolean_set_array(ptr, prop, values);
1469
2677
                        break;
1470
2678
                }
1471
2679
                case PROP_INT:
1476
2684
                        int min, max;
1477
2685
                        RNA_property_int_range(ptr, prop, &min, &max);
1478
2686
 
1479
 
                        if(length > PYRNA_STACK_ARRAY)  {       values= values_alloc= PyMem_MALLOC(sizeof(int) * length); }
1480
 
                        else                                                    {       values= values_stack; }
 
2687
                        if (length > PYRNA_STACK_ARRAY) { values = values_alloc = PyMem_MALLOC(sizeof(int) * length); }
 
2688
                        else                            { values = values_stack; }
1481
2689
 
1482
 
                        if(start != 0 || stop != length) /* partial assignment? - need to get the array */
 
2690
                        if (start != 0 || stop != length) /* partial assignment? - need to get the array */
1483
2691
                                RNA_property_int_get_array(ptr, prop, values);
1484
2692
 
1485
 
                        for(count=start; count<stop; count++) {
1486
 
                                ival = PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, count-start));
 
2693
                        for (count = start; count < stop; count++) {
 
2694
                                ival = PyLong_AsLong(PySequence_Fast_GET_ITEM(value, count - start));
1487
2695
                                CLAMP(ival, min, max);
1488
2696
                                values[count] = ival;
1489
2697
                        }
1490
2698
 
1491
 
                        if(PyErr_Occurred())    ret= -1;
1492
 
                        else                                    RNA_property_int_set_array(ptr, prop, values);
 
2699
                        if (PyErr_Occurred()) ret = -1;
 
2700
                        else                  RNA_property_int_set_array(ptr, prop, values);
1493
2701
                        break;
1494
2702
                }
1495
2703
                default:
1496
2704
                        PyErr_SetString(PyExc_TypeError, "not an array type");
1497
 
                        ret= -1;
 
2705
                        ret = -1;
1498
2706
        }
1499
2707
 
1500
2708
        Py_DECREF(value);
1501
 
        
1502
 
        if(values_alloc) {
 
2709
 
 
2710
        if (values_alloc) {
1503
2711
                PyMem_FREE(values_alloc);
1504
2712
        }
1505
 
        
 
2713
 
1506
2714
        return ret;
1507
2715
 
1508
2716
}
1509
2717
 
1510
 
static int prop_subscript_ass_array_int(BPy_PropertyRNA *self, Py_ssize_t keynum, PyObject *value)
 
2718
static int prop_subscript_ass_array_int(BPy_PropertyArrayRNA *self, Py_ssize_t keynum, PyObject *value)
1511
2719
{
1512
 
        int len= pyrna_prop_array_length(self);
1513
 
 
1514
 
        if(keynum < 0) keynum += len;
1515
 
 
1516
 
        if(keynum >= 0 && keynum < len)
1517
 
                return pyrna_py_to_prop_index(self, keynum, value);
1518
 
 
1519
 
        PyErr_SetString(PyExc_IndexError, "bpy_prop_array[index] = value: index out of range");
 
2720
        int len;
 
2721
 
 
2722
        PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
 
2723
 
 
2724
        len = pyrna_prop_array_length(self);
 
2725
 
 
2726
        if (keynum < 0) keynum += len;
 
2727
 
 
2728
        if (keynum >= 0 && keynum < len)
 
2729
                return pyrna_py_to_prop_array_index(self, keynum, value);
 
2730
 
 
2731
        PyErr_SetString(PyExc_IndexError,
 
2732
                        "bpy_prop_array[index] = value: index out of range");
1520
2733
        return -1;
1521
2734
}
1522
2735
 
1523
 
static int pyrna_prop_array_ass_subscript( BPy_PropertyRNA *self, PyObject *key, PyObject *value )
 
2736
static int pyrna_prop_array_ass_subscript(BPy_PropertyArrayRNA *self, PyObject *key, PyObject *value)
1524
2737
{
1525
2738
        /* char *keyname = NULL; */ /* not supported yet */
1526
 
        int ret= -1;
 
2739
        int ret = -1;
 
2740
 
 
2741
        PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
1527
2742
 
1528
2743
        if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
1529
 
                PyErr_Format(PyExc_AttributeError, "bpy_prop_collection: attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
1530
 
                ret= -1;
 
2744
                PyErr_Format(PyExc_AttributeError,
 
2745
                             "bpy_prop_collection: attribute \"%.200s\" from \"%.200s\" is read-only",
 
2746
                             RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type));
 
2747
                ret = -1;
1531
2748
        }
1532
2749
 
1533
2750
        else if (PyIndex_Check(key)) {
1534
2751
                Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1535
2752
                if (i == -1 && PyErr_Occurred()) {
1536
 
                        ret= -1;
 
2753
                        ret = -1;
1537
2754
                }
1538
2755
                else {
1539
 
                        ret= prop_subscript_ass_array_int(self, i, value);
 
2756
                        ret = prop_subscript_ass_array_int(self, i, value);
1540
2757
                }
1541
2758
        }
1542
2759
        else if (PySlice_Check(key)) {
1543
 
                int len= RNA_property_array_length(&self->ptr, self->prop);
 
2760
                int len = RNA_property_array_length(&self->ptr, self->prop);
1544
2761
                Py_ssize_t start, stop, step, slicelength;
1545
2762
 
1546
 
                if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0) {
1547
 
                        ret= -1;
 
2763
                if (PySlice_GetIndicesEx((void *)key, len, &start, &stop, &step, &slicelength) < 0) {
 
2764
                        ret = -1;
1548
2765
                }
1549
2766
                else if (slicelength <= 0) {
1550
 
                        ret= 0; /* do nothing */
 
2767
                        ret = 0; /* do nothing */
1551
2768
                }
1552
2769
                else if (step == 1) {
1553
 
                        ret= prop_subscript_ass_array_slice(&self->ptr, self->prop, start, stop, len, value);
 
2770
                        ret = prop_subscript_ass_array_slice(&self->ptr, self->prop, start, stop, len, value);
1554
2771
                }
1555
2772
                else {
1556
2773
                        PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
1557
 
                        ret= -1;
 
2774
                        ret = -1;
1558
2775
                }
1559
2776
        }
1560
2777
        else {
1561
2778
                PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
1562
 
                ret= -1;
 
2779
                ret = -1;
1563
2780
        }
1564
2781
 
1565
 
        if(ret != -1) {
1566
 
                RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
 
2782
        if (ret != -1) {
 
2783
                if (RNA_property_update_check(self->prop)) {
 
2784
                        RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
 
2785
                }
1567
2786
        }
1568
2787
 
1569
2788
        return ret;
1571
2790
 
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 */
1577
2796
};
1578
2797
 
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 */
1583
2802
};
1584
2803
 
 
2804
/* only for fast bool's, large structs, assign nb_bool on init */
 
2805
static PyNumberMethods pyrna_prop_array_as_number = {
 
2806
        NULL, /* nb_add */
 
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 */
 
2816
};
 
2817
static PyNumberMethods pyrna_prop_collection_as_number = {
 
2818
        NULL, /* nb_add */
 
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 */
 
2828
};
1585
2829
 
1586
2830
static int pyrna_prop_array_contains(BPy_PropertyRNA *self, PyObject *value)
1587
2831
{
1588
2832
        return pyrna_array_contains_py(&self->ptr, self->prop, value);
1589
2833
}
1590
2834
 
1591
 
static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *value)
 
2835
static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *key)
1592
2836
{
1593
2837
        PointerRNA newptr; /* not used, just so RNA_property_collection_lookup_string runs */
1594
2838
 
1595
 
        /* key in dict style check */
1596
 
        char *keyname = _PyUnicode_AsString(value);
1597
 
 
1598
 
        if(keyname==NULL) {
1599
 
                PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.__contains__: expected a string");
1600
 
                return -1;
1601
 
        }
1602
 
 
1603
 
        if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
1604
 
                return 1;
1605
 
 
1606
 
        return 0;
 
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);
 
2843
        }
 
2844
        else {
 
2845
 
 
2846
                /* key in dict style check */
 
2847
                const char *keyname = _PyUnicode_AsString(key);
 
2848
 
 
2849
                if (keyname == NULL) {
 
2850
                        PyErr_SetString(PyExc_TypeError,
 
2851
                                        "bpy_prop_collection.__contains__: expected a string or a tuple of strings");
 
2852
                        return -1;
 
2853
                }
 
2854
 
 
2855
                if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
 
2856
                        return 1;
 
2857
 
 
2858
                return 0;
 
2859
        }
1607
2860
}
1608
2861
 
1609
2862
static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
1610
2863
{
1611
2864
        IDProperty *group;
1612
 
        char *name = _PyUnicode_AsString(value);
 
2865
        const char *name = _PyUnicode_AsString(value);
 
2866
 
 
2867
        PYRNA_STRUCT_CHECK_INT(self);
1613
2868
 
1614
2869
        if (!name) {
1615
 
                PyErr_SetString( PyExc_TypeError, "bpy_struct.__contains__: expected a string");
1616
 
                return -1;
1617
 
        }
1618
 
 
1619
 
        if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1620
 
                PyErr_SetString( PyExc_TypeError, "bpy_struct: this type doesnt support IDProperties");
1621
 
                return -1;
1622
 
        }
1623
 
 
1624
 
        group= RNA_struct_idproperties(&self->ptr, 0);
1625
 
        
1626
 
        if(!group)
 
2870
                PyErr_SetString(PyExc_TypeError, "bpy_struct.__contains__: expected a string");
 
2871
                return -1;
 
2872
        }
 
2873
 
 
2874
        if (RNA_struct_idprops_check(self->ptr.type) == 0) {
 
2875
                PyErr_SetString(PyExc_TypeError, "bpy_struct: this type doesn't support IDProperties");
 
2876
                return -1;
 
2877
        }
 
2878
 
 
2879
        group = RNA_struct_idprops(&self->ptr, 0);
 
2880
 
 
2881
        if (!group)
1627
2882
                return 0;
1628
 
        
1629
 
        return IDP_GetPropertyFromGroup(group, name) ? 1:0;
 
2883
 
 
2884
        return IDP_GetPropertyFromGroup(group, name) ? 1 : 0;
1630
2885
}
1631
2886
 
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 */
1643
2898
};
1644
2899
 
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 */
1656
2911
};
1657
2912
 
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 */
1669
2924
};
1670
2925
 
1671
 
static PyObject *pyrna_struct_subscript( BPy_StructRNA *self, PyObject *key )
 
2926
static PyObject *pyrna_struct_subscript(BPy_StructRNA *self, PyObject *key)
1672
2927
{
1673
2928
        /* mostly copied from BPy_IDGroup_Map_GetItem */
1674
2929
        IDProperty *group, *idprop;
1675
 
        char *name= _PyUnicode_AsString(key);
1676
 
 
1677
 
        if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1678
 
                PyErr_SetString( PyExc_TypeError, "this type doesn't support IDProperties");
1679
 
                return NULL;
1680
 
        }
1681
 
 
1682
 
        if(name==NULL) {
1683
 
                PyErr_SetString( PyExc_TypeError, "bpy_struct[key]: only strings are allowed as keys of ID properties");
1684
 
                return NULL;
1685
 
        }
1686
 
 
1687
 
        group= RNA_struct_idproperties(&self->ptr, 0);
1688
 
 
1689
 
        if(group==NULL) {
1690
 
                PyErr_Format( PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
1691
 
                return NULL;
1692
 
        }
1693
 
 
1694
 
        idprop= IDP_GetPropertyFromGroup(group, name);
1695
 
 
1696
 
        if(idprop==NULL) {
1697
 
                PyErr_Format( PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
1698
 
                return NULL;
1699
 
        }
1700
 
 
1701
 
        return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
 
2930
        const char *name = _PyUnicode_AsString(key);
 
2931
 
 
2932
        PYRNA_STRUCT_CHECK_OBJ(self);
 
2933
 
 
2934
        if (RNA_struct_idprops_check(self->ptr.type) == 0) {
 
2935
                PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
 
2936
                return NULL;
 
2937
        }
 
2938
 
 
2939
        if (name == NULL) {
 
2940
                PyErr_SetString(PyExc_TypeError, "bpy_struct[key]: only strings are allowed as keys of ID properties");
 
2941
                return NULL;
 
2942
        }
 
2943
 
 
2944
        group = RNA_struct_idprops(&self->ptr, 0);
 
2945
 
 
2946
        if (group == NULL) {
 
2947
                PyErr_Format(PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
 
2948
                return NULL;
 
2949
        }
 
2950
 
 
2951
        idprop = IDP_GetPropertyFromGroup(group, name);
 
2952
 
 
2953
        if (idprop == NULL) {
 
2954
                PyErr_Format(PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
 
2955
                return NULL;
 
2956
        }
 
2957
 
 
2958
        return BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
1702
2959
}
1703
2960
 
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)
1705
2962
{
1706
 
        IDProperty *group= RNA_struct_idproperties(&self->ptr, 1);
1707
 
 
1708
 
        if(group==NULL) {
 
2963
        IDProperty *group;
 
2964
 
 
2965
        PYRNA_STRUCT_CHECK_INT(self);
 
2966
 
 
2967
        group = RNA_struct_idprops(&self->ptr, 1);
 
2968
 
 
2969
#ifdef USE_PEDANTIC_WRITE
 
2970
        if (rna_disallow_writes && rna_id_write_error(&self->ptr, key)) {
 
2971
                return -1;
 
2972
        }
 
2973
#endif // USE_PEDANTIC_WRITE
 
2974
 
 
2975
        if (group == NULL) {
1709
2976
                PyErr_SetString(PyExc_TypeError, "bpy_struct[key] = val: id properties not supported for this type");
1710
2977
                return -1;
1711
2978
        }
1714
2981
}
1715
2982
 
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 */
1720
2987
};
1721
2988
 
1722
 
static char pyrna_struct_keys_doc[] =
 
2989
PyDoc_STRVAR(pyrna_struct_keys_doc,
1723
2990
".. method:: keys()\n"
1724
2991
"\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"
1726
2994
"\n"
1727
2995
"   :return: custom property keys.\n"
1728
2996
"   :rtype: list of strings\n"
1729
2997
"\n"
1730
 
"   .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
1731
 
 
 
2998
BPY_DOC_ID_PROP_TYPE_NOTE
 
2999
);
1732
3000
static PyObject *pyrna_struct_keys(BPy_PropertyRNA *self)
1733
3001
{
1734
3002
        IDProperty *group;
1735
3003
 
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");
1738
3006
                return NULL;
1739
3007
        }
1740
3008
 
1741
 
        group= RNA_struct_idproperties(&self->ptr, 0);
 
3009
        group = RNA_struct_idprops(&self->ptr, 0);
1742
3010
 
1743
 
        if(group==NULL)
 
3011
        if (group == NULL)
1744
3012
                return PyList_New(0);
1745
3013
 
1746
3014
        return BPy_Wrap_GetKeys(group);
1747
3015
}
1748
3016
 
1749
 
static char pyrna_struct_items_doc[] =
 
3017
PyDoc_STRVAR(pyrna_struct_items_doc,
1750
3018
".. method:: items()\n"
1751
3019
"\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"
1753
3022
"\n"
1754
3023
"   :return: custom property key, value pairs.\n"
1755
3024
"   :rtype: list of key, value tuples\n"
1756
3025
"\n"
1757
 
"   .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
1758
 
 
 
3026
BPY_DOC_ID_PROP_TYPE_NOTE
 
3027
);
1759
3028
static PyObject *pyrna_struct_items(BPy_PropertyRNA *self)
1760
3029
{
1761
3030
        IDProperty *group;
1762
3031
 
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");
1765
3034
                return NULL;
1766
3035
        }
1767
3036
 
1768
 
        group= RNA_struct_idproperties(&self->ptr, 0);
 
3037
        group = RNA_struct_idprops(&self->ptr, 0);
1769
3038
 
1770
 
        if(group==NULL)
 
3039
        if (group == NULL)
1771
3040
                return PyList_New(0);
1772
3041
 
1773
3042
        return BPy_Wrap_GetItems(self->ptr.id.data, group);
1774
3043
}
1775
3044
 
1776
 
static char pyrna_struct_values_doc[] =
 
3045
PyDoc_STRVAR(pyrna_struct_values_doc,
1777
3046
".. method:: values()\n"
1778
3047
"\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"
1780
3050
"\n"
1781
3051
"   :return: custom property values.\n"
1782
3052
"   :rtype: list\n"
1783
3053
"\n"
1784
 
"   .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
1785
 
 
 
3054
BPY_DOC_ID_PROP_TYPE_NOTE
 
3055
);
1786
3056
static PyObject *pyrna_struct_values(BPy_PropertyRNA *self)
1787
3057
{
1788
3058
        IDProperty *group;
1789
3059
 
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");
1792
3062
                return NULL;
1793
3063
        }
1794
3064
 
1795
 
        group= RNA_struct_idproperties(&self->ptr, 0);
 
3065
        group = RNA_struct_idprops(&self->ptr, 0);
1796
3066
 
1797
 
        if(group==NULL)
 
3067
        if (group == NULL)
1798
3068
                return PyList_New(0);
1799
3069
 
1800
3070
        return BPy_Wrap_GetValues(self->ptr.id.data, group);
1801
3071
}
1802
3072
 
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)
1806
 
{
1807
 
        PropertyRNA *prop;
1808
 
        
1809
 
        if (ptr->data==NULL) {
1810
 
                PyErr_Format(PyExc_TypeError, "%.200s this struct has no data, can't be animated", error_prefix);
1811
 
                return -1;
1812
 
        }
1813
 
        
1814
 
        prop = RNA_struct_find_property(ptr, path);
1815
 
        
1816
 
        if (prop==NULL) {
1817
 
                PyErr_Format( PyExc_TypeError, "%.200s property \"%s\" not found", error_prefix, path);
1818
 
                return -1;
1819
 
        }
1820
 
 
1821
 
        if (!RNA_property_animateable(ptr, prop)) {
1822
 
                PyErr_Format(PyExc_TypeError, "%.200s property \"%s\" not animatable", error_prefix, path);
1823
 
                return -1;
1824
 
        }
1825
 
 
1826
 
        if(RNA_property_array_check(ptr, prop) == 0) {
1827
 
                if((*index) == -1) {
1828
 
                        *index= 0;
1829
 
                }
1830
 
                else {
1831
 
                        PyErr_Format(PyExc_TypeError, "%.200s index %d was given while property \"%s\" is not an array", error_prefix, *index, path);
1832
 
                        return -1;
1833
 
                }
1834
 
        }
1835
 
        else {
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);
1839
 
                        return -1;
1840
 
                }
1841
 
        }
1842
 
        
1843
 
        *path_full= RNA_path_from_ID_to_property(ptr, prop);
1844
 
 
1845
 
        if (*path_full==NULL) {
1846
 
                PyErr_Format( PyExc_TypeError, "%.200s could not make path to \"%s\"", error_prefix, path);
1847
 
                return -1;
1848
 
        }
1849
 
 
1850
 
        return 0;
1851
 
}
1852
 
 
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 */
1856
 
{
1857
 
        static char *kwlist[] = {"data_path", "index", "frame", "group", NULL};
1858
 
        char *path;
1859
 
 
1860
 
        /* note, parse_str MUST start with 's|ifs' */
1861
 
        if (!PyArg_ParseTupleAndKeywords(args, kw, parse_str, (char **)kwlist, &path, index, cfra, group_name))
1862
 
                return -1;
1863
 
 
1864
 
        if(pyrna_struct_anim_args_parse(ptr, error_prefix, path,  path_full, index) < 0) 
1865
 
                return -1;
1866
 
        
1867
 
        if(*cfra==FLT_MAX)
1868
 
                *cfra= CTX_data_scene(BPy_GetContext())->r.cfra;
1869
 
 
1870
 
        return 0; /* success */
1871
 
}
1872
 
 
1873
 
static char pyrna_struct_keyframe_insert_doc[] =
1874
 
".. method:: keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group=\"\")\n"
1875
 
"\n"
1876
 
"   Insert a keyframe on the property given, adding fcurves and animation data when necessary.\n"
1877
 
"\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"
1887
 
"   :rtype: boolean";
1888
 
 
1889
 
static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args, PyObject *kw)
1890
 
{
1891
 
        PyObject *result;
1892
 
        /* args, pyrna_struct_keyframe_parse handles these */
1893
 
        char *path_full= NULL;
1894
 
        int index= -1;
1895
 
        float cfra= FLT_MAX;
1896
 
        char *group_name= NULL;
1897
 
 
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)
1899
 
                return NULL;
1900
 
 
1901
 
        result= PyBool_FromLong(insert_keyframe((ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0));
1902
 
        MEM_freeN(path_full);
1903
 
 
1904
 
        return result;
1905
 
}
1906
 
 
1907
 
static char pyrna_struct_keyframe_delete_doc[] =
1908
 
".. method:: keyframe_delete(data_path, index=-1, frame=bpy.context.scene.frame_current, group=\"\")\n"
1909
 
"\n"
1910
 
"   Remove a keyframe from this properties fcurve.\n"
1911
 
"\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"
1921
 
"   :rtype: boolean";
1922
 
 
1923
 
static PyObject *pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *args, PyObject *kw)
1924
 
{
1925
 
        PyObject *result;
1926
 
        /* args, pyrna_struct_keyframe_parse handles these */
1927
 
        char *path_full= NULL;
1928
 
        int index= -1;
1929
 
        float cfra= FLT_MAX;
1930
 
        char *group_name= NULL;
1931
 
 
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)
1933
 
                return NULL;
1934
 
 
1935
 
        result= PyBool_FromLong(delete_keyframe((ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0));
1936
 
        MEM_freeN(path_full);
1937
 
 
1938
 
        return result;
1939
 
}
1940
 
 
1941
 
static char pyrna_struct_driver_add_doc[] =
1942
 
".. method:: driver_add(path, index=-1)\n"
1943
 
"\n"
1944
 
"   Adds driver(s) to the given property\n"
1945
 
"\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.";
1952
 
 
1953
 
static PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args)
1954
 
{
1955
 
        char *path, *path_full;
1956
 
        int index= -1;
1957
 
        PyObject *ret;
1958
 
 
1959
 
        if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index))
1960
 
                return NULL;
1961
 
 
1962
 
        if(pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_add():", path,  &path_full, &index) < 0) 
1963
 
                return NULL;
1964
 
 
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);
1968
 
                FCurve *fcu;
1969
 
 
1970
 
                PointerRNA tptr;
1971
 
                PyObject *item;
1972
 
 
1973
 
                if(index == -1) { /* all, use a list */
1974
 
                        int i= 0;
1975
 
                        ret= PyList_New(0);
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);
1980
 
                                Py_DECREF(item);
1981
 
                        }
1982
 
                }
1983
 
                else {
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);
1987
 
                }
1988
 
        }
1989
 
        else {
1990
 
                PyErr_SetString(PyExc_TypeError, "bpy_struct.driver_add(): failed because of an internal error");
1991
 
                return NULL;
1992
 
        }
1993
 
 
1994
 
        MEM_freeN(path_full);
1995
 
 
1996
 
        return ret;
1997
 
}
1998
 
 
1999
 
 
2000
 
static char pyrna_struct_driver_remove_doc[] =
2001
 
".. method:: driver_remove(path, index=-1)\n"
2002
 
"\n"
2003
 
"   Remove driver(s) from the given property\n"
2004
 
"\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"
2010
 
"   :rtype: boolean";
2011
 
 
2012
 
static PyObject *pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args)
2013
 
{
2014
 
        char *path, *path_full;
2015
 
        int index= -1;
2016
 
        PyObject *ret;
2017
 
 
2018
 
        if (!PyArg_ParseTuple(args, "s|i:driver_remove", &path, &index))
2019
 
                return NULL;
2020
 
 
2021
 
        if(pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_remove():", path,  &path_full, &index) < 0) 
2022
 
                return NULL;
2023
 
 
2024
 
        ret= PyBool_FromLong(ANIM_remove_driver((ID *)self->ptr.id.data, path_full, index, 0));
2025
 
 
2026
 
        MEM_freeN(path_full);
2027
 
 
2028
 
        return ret;
2029
 
}
2030
 
 
2031
 
 
2032
 
static char pyrna_struct_is_property_set_doc[] =
 
3073
 
 
3074
PyDoc_STRVAR(pyrna_struct_is_property_set_doc,
2033
3075
".. method:: is_property_set(property)\n"
2034
3076
"\n"
2035
3077
"   Check if a property is set, use for testing operator properties.\n"
2036
3078
"\n"
2037
3079
"   :return: True when the property has been set.\n"
2038
 
"   :rtype: boolean";
2039
 
 
 
3080
"   :rtype: boolean\n"
 
3081
);
2040
3082
static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args)
2041
3083
{
2042
 
        char *name;
 
3084
        PropertyRNA *prop;
 
3085
        const char *name;
 
3086
 
 
3087
        PYRNA_STRUCT_CHECK_OBJ(self);
2043
3088
 
2044
3089
        if (!PyArg_ParseTuple(args, "s:is_property_set", &name))
2045
3090
                return NULL;
2046
3091
 
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);
 
3096
                return NULL;
 
3097
        }
 
3098
 
 
3099
        return PyBool_FromLong(RNA_property_is_set(&self->ptr, prop));
2048
3100
}
2049
3101
 
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"
2052
3104
"\n"
2053
3105
"   Check if a property is hidden.\n"
2054
3106
"\n"
2055
3107
"   :return: True when the property is hidden.\n"
2056
 
"   :rtype: boolean";
2057
 
 
 
3108
"   :rtype: boolean\n"
 
3109
);
2058
3110
static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *args)
2059
3111
{
2060
3112
        PropertyRNA *prop;
2061
 
        char *name;
2062
 
        int hidden;
 
3113
        const char *name;
 
3114
 
 
3115
        PYRNA_STRUCT_CHECK_OBJ(self);
2063
3116
 
2064
3117
        if (!PyArg_ParseTuple(args, "s:is_property_hidden", &name))
2065
3118
                return NULL;
2066
 
        
2067
 
        prop= RNA_struct_find_property(&self->ptr, name);
2068
 
        hidden= (prop)? (RNA_property_flag(prop) & PROP_HIDDEN): 1;
2069
 
 
2070
 
        return PyBool_FromLong(hidden);
 
3119
 
 
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);
 
3124
                return NULL;
 
3125
        }
 
3126
 
 
3127
        return PyBool_FromLong(RNA_property_flag(prop) & PROP_HIDDEN);
2071
3128
}
2072
3129
 
2073
 
static char pyrna_struct_path_resolve_doc[] =
2074
 
".. method:: path_resolve(path)\n"
2075
 
"\n"
2076
 
"   Returns the property from the path given or None if the property is not found.";
2077
 
 
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"
 
3132
"\n"
 
3133
"   Returns the property from the path, raise an exception when not found.\n"
 
3134
"\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"
 
3140
);
 
3141
static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *args)
2079
3142
{
2080
 
        char *path= _PyUnicode_AsString(value);
 
3143
        const char *path;
 
3144
        PyObject *coerce = Py_True;
2081
3145
        PointerRNA r_ptr;
2082
3146
        PropertyRNA *r_prop;
2083
 
 
2084
 
        if(path==NULL) {
2085
 
                PyErr_SetString(PyExc_TypeError, "bpy_struct.path_resolve(): accepts only a single string argument");
2086
 
                return NULL;
2087
 
        }
2088
 
 
2089
 
        if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop))
2090
 
                return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
2091
 
 
2092
 
        Py_RETURN_NONE;
 
3147
        int index = -1;
 
3148
 
 
3149
        PYRNA_STRUCT_CHECK_OBJ(self);
 
3150
 
 
3151
        if (!PyArg_ParseTuple(args, "s|O!:path_resolve", &path, &PyBool_Type, &coerce))
 
3152
                return NULL;
 
3153
 
 
3154
        if (RNA_path_resolve_full(&self->ptr, path, &r_ptr, &r_prop, &index)) {
 
3155
                if (r_prop) {
 
3156
                        if (index != -1) {
 
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);
 
3161
                                        return NULL;
 
3162
                                }
 
3163
                                else {
 
3164
                                        return pyrna_array_index(&r_ptr, r_prop, index);
 
3165
                                }
 
3166
                        }
 
3167
                        else {
 
3168
                                if (coerce == Py_False) {
 
3169
                                        return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
 
3170
                                }
 
3171
                                else {
 
3172
                                        return pyrna_prop_to_py(&r_ptr, r_prop);
 
3173
                                }
 
3174
                        }
 
3175
                }
 
3176
                else {
 
3177
                        return pyrna_struct_CreatePyObject(&r_ptr);
 
3178
                }
 
3179
        }
 
3180
        else {
 
3181
                PyErr_Format(PyExc_ValueError,
 
3182
                             "%.200s.path_resolve(\"%.200s\") could not be resolved",
 
3183
                             RNA_struct_identifier(self->ptr.type), path);
 
3184
                return NULL;
 
3185
        }
2093
3186
}
2094
3187
 
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"
2097
3190
"\n"
2098
3191
"   Returns the data path from the ID to this object (string).\n"
2099
3192
"\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"
2103
 
"   :rtype: str";
2104
 
 
 
3196
"   :return: The path from :class:`bpy.types.bpy_struct.id_data`\n"
 
3197
"      to this struct and property (when given).\n"
 
3198
"   :rtype: str\n"
 
3199
);
2105
3200
static PyObject *pyrna_struct_path_from_id(BPy_StructRNA *self, PyObject *args)
2106
3201
{
2107
 
        char *name= NULL;
2108
 
        char *path;
 
3202
        const char *name = NULL;
 
3203
        const char *path;
2109
3204
        PropertyRNA *prop;
2110
3205
        PyObject *ret;
2111
3206
 
 
3207
        PYRNA_STRUCT_CHECK_OBJ(self);
 
3208
 
2112
3209
        if (!PyArg_ParseTuple(args, "|s:path_from_id", &name))
2113
3210
                return NULL;
2114
3211
 
2115
 
        if(name) {
2116
 
                prop= RNA_struct_find_property(&self->ptr, name);
2117
 
                if(prop==NULL) {
2118
 
                        PyErr_Format(PyExc_TypeError, "%.200s.path_from_id(\"%.200s\") not found", RNA_struct_identifier(self->ptr.type), name);
 
3212
        if (name) {
 
3213
                prop = RNA_struct_find_property(&self->ptr, name);
 
3214
                if (prop == NULL) {
 
3215
                        PyErr_Format(PyExc_AttributeError,
 
3216
                                     "%.200s.path_from_id(\"%.200s\") not found",
 
3217
                                     RNA_struct_identifier(self->ptr.type), name);
2119
3218
                        return NULL;
2120
3219
                }
2121
3220
 
2122
 
                path= RNA_path_from_ID_to_property(&self->ptr, prop);
 
3221
                path = RNA_path_from_ID_to_property(&self->ptr, prop);
2123
3222
        }
2124
3223
        else {
2125
 
                path= RNA_path_from_ID_to_struct(&self->ptr);
 
3224
                path = RNA_path_from_ID_to_struct(&self->ptr);
2126
3225
        }
2127
3226
 
2128
 
        if(path==NULL) {
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));
 
3227
        if (path == NULL) {
 
3228
                if (name) {
 
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);
 
3232
                }
 
3233
                else {
 
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));
 
3237
                }
2131
3238
                return NULL;
2132
3239
        }
2133
3240
 
2134
 
        ret= PyUnicode_FromString(path);
2135
 
        MEM_freeN(path);
 
3241
        ret = PyUnicode_FromString(path);
 
3242
        MEM_freeN((void *)path);
2136
3243
 
2137
3244
        return ret;
2138
3245
}
2139
3246
 
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"
2142
3249
"\n"
2143
3250
"   Returns the data path from the ID to this property (string).\n"
2144
3251
"\n"
2145
 
"   :return: The path from :class:`bpy_struct.id_data` to this property.\n"
2146
 
"   :rtype: str";
2147
 
 
 
3252
"   :return: The path from :class:`bpy.types.bpy_struct.id_data` to this property.\n"
 
3253
"   :rtype: str\n"
 
3254
);
2148
3255
static PyObject *pyrna_prop_path_from_id(BPy_PropertyRNA *self)
2149
3256
{
2150
 
        char *path;
 
3257
        const char *path;
2151
3258
        PropertyRNA *prop = self->prop;
2152
3259
        PyObject *ret;
2153
3260
 
2154
 
        path= RNA_path_from_ID_to_property(&self->ptr, self->prop);
 
3261
        path = RNA_path_from_ID_to_property(&self->ptr, self->prop);
2155
3262
 
2156
 
        if(path==NULL) {
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));
 
3263
        if (path == NULL) {
 
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));
2158
3267
                return NULL;
2159
3268
        }
2160
3269
 
2161
 
        ret= PyUnicode_FromString(path);
2162
 
        MEM_freeN(path);
 
3270
        ret = PyUnicode_FromString(path);
 
3271
        MEM_freeN((void *)path);
2163
3272
 
2164
3273
        return ret;
2165
3274
}
2166
3275
 
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"
2169
3278
"\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"
2171
3281
"\n"
2172
3282
"   :return: a new instance of this object with the type initialized again.\n"
2173
 
"   :rtype: subclass of :class:`bpy_struct`";
2174
 
 
2175
 
static PyObject *pyrna_struct_recast_type(BPy_StructRNA *self, PyObject *args)
 
3283
"   :rtype: subclass of :class:`bpy.types.bpy_struct`\n"
 
3284
);
 
3285
static PyObject *pyrna_struct_type_recast(BPy_StructRNA *self)
2176
3286
{
2177
3287
        PointerRNA r_ptr;
 
3288
 
 
3289
        PYRNA_STRUCT_CHECK_OBJ(self);
 
3290
 
2178
3291
        RNA_pointer_recast(&self->ptr, &r_ptr);
2179
3292
        return pyrna_struct_CreatePyObject(&r_ptr);
2180
3293
}
2181
3294
 
 
3295
static void pyrna_dir_members_py__add_keys(PyObject *list, PyObject *dict)
 
3296
{
 
3297
        PyObject *list_tmp;
 
3298
 
 
3299
        list_tmp = PyDict_Keys(dict);
 
3300
        PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
 
3301
        Py_DECREF(list_tmp);
 
3302
}
 
3303
 
2182
3304
static void pyrna_dir_members_py(PyObject *list, PyObject *self)
2183
3305
{
2184
3306
        PyObject *dict;
2185
3307
        PyObject **dict_ptr;
2186
 
        PyObject *list_tmp;
2187
 
 
2188
 
        dict_ptr= _PyObject_GetDictPtr((PyObject *)self);
2189
 
 
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);
2194
 
        }
2195
 
 
2196
 
        dict= ((PyTypeObject *)Py_TYPE(self))->tp_dict;
2197
 
        if(dict) {
2198
 
                list_tmp = PyDict_Keys(dict);
2199
 
                PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
2200
 
                Py_DECREF(list_tmp);
 
3308
 
 
3309
        dict_ptr = _PyObject_GetDictPtr((PyObject *)self);
 
3310
 
 
3311
        if (dict_ptr && (dict = *dict_ptr)) {
 
3312
                pyrna_dir_members_py__add_keys(list, dict);
 
3313
        }
 
3314
 
 
3315
        dict = ((PyTypeObject *)Py_TYPE(self))->tp_dict;
 
3316
        if (dict) {
 
3317
                pyrna_dir_members_py__add_keys(list, dict);
 
3318
        }
 
3319
 
 
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) {
 
3324
                        PointerRNA r_ptr;
 
3325
 
 
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);
 
3330
                                Py_DECREF(cls);
 
3331
                        }
 
3332
                }
2201
3333
        }
2202
3334
}
2203
3335
 
2212
3344
 
2213
3345
        {
2214
3346
                RNA_pointer_create(NULL, &RNA_Struct, ptr->type, &tptr);
2215
 
                iterprop= RNA_struct_find_property(&tptr, "functions");
 
3347
                iterprop = RNA_struct_find_property(&tptr, "functions");
2216
3348
 
2217
3349
                RNA_PROP_BEGIN(&tptr, itemptr, iterprop) {
2218
 
                        idname= RNA_function_identifier(itemptr.data);
 
3350
                        idname = RNA_function_identifier(itemptr.data);
2219
3351
 
2220
3352
                        pystring = PyUnicode_FromString(idname);
2221
3353
                        PyList_Append(list, pystring);
2229
3361
                 * Collect RNA attributes
2230
3362
                 */
2231
3363
                char name[256], *nameptr;
 
3364
                int namelen;
2232
3365
 
2233
 
                iterprop= RNA_struct_iterator_property(ptr->type);
 
3366
                iterprop = RNA_struct_iterator_property(ptr->type);
2234
3367
 
2235
3368
                RNA_PROP_BEGIN(ptr, itemptr, iterprop) {
2236
 
                        nameptr= RNA_struct_name_get_alloc(&itemptr, name, sizeof(name));
 
3369
                        nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
2237
3370
 
2238
 
                        if(nameptr) {
2239
 
                                pystring = PyUnicode_FromString(nameptr);
 
3371
                        if (nameptr) {
 
3372
                                pystring = PyUnicode_FromStringAndSize(nameptr, namelen);
2240
3373
                                PyList_Append(list, pystring);
2241
3374
                                Py_DECREF(pystring);
2242
3375
 
2243
 
                                if(name != nameptr)
 
3376
                                if (name != nameptr) {
2244
3377
                                        MEM_freeN(nameptr);
 
3378
                                }
2245
3379
                        }
2246
3380
                }
2247
3381
                RNA_PROP_END;
2254
3388
        PyObject *ret;
2255
3389
        PyObject *pystring;
2256
3390
 
2257
 
        /* Include this incase this instance is a subtype of a python class
 
3391
        PYRNA_STRUCT_CHECK_OBJ(self);
 
3392
 
 
3393
        /* Include this in case this instance is a subtype of a python class
2258
3394
         * In these instances we may want to return a function or variable provided by the subtype
2259
3395
         * */
2260
3396
        ret = PyList_New(0);
2264
3400
 
2265
3401
        pyrna_dir_members_rna(ret, &self->ptr);
2266
3402
 
2267
 
        if(self->ptr.type == &RNA_Context) {
 
3403
        if (self->ptr.type == &RNA_Context) {
2268
3404
                ListBase lb = CTX_data_dir_get(self->ptr.data);
2269
3405
                LinkData *link;
2270
3406
 
2271
 
                for(link=lb.first; link; link=link->next) {
 
3407
                for (link = lb.first; link; link = link->next) {
2272
3408
                        pystring = PyUnicode_FromString(link->data);
2273
3409
                        PyList_Append(ret, pystring);
2274
3410
                        Py_DECREF(pystring);
2276
3412
 
2277
3413
                BLI_freelistN(&lb);
2278
3414
        }
 
3415
 
 
3416
        {
 
3417
                /* set(), this is needed to remove-doubles because the deferred
 
3418
                 * register-props will be in both the python __dict__ and accessed as RNA */
 
3419
 
 
3420
                PyObject *set = PySet_New(ret);
 
3421
 
 
3422
                Py_DECREF(ret);
 
3423
                ret = PySequence_List(set);
 
3424
                Py_DECREF(set);
 
3425
        }
 
3426
 
2279
3427
        return ret;
2280
3428
}
2281
3429
 
2282
3430
//---------------getattr--------------------------------------------
2283
 
static PyObject *pyrna_struct_getattro( BPy_StructRNA *self, PyObject *pyname )
 
3431
static PyObject *pyrna_struct_getattro(BPy_StructRNA *self, PyObject *pyname)
2284
3432
{
2285
 
        char *name = _PyUnicode_AsString(pyname);
 
3433
        const char *name = _PyUnicode_AsString(pyname);
2286
3434
        PyObject *ret;
2287
3435
        PropertyRNA *prop;
2288
3436
        FunctionRNA *func;
2289
 
        
2290
 
        if(name[0]=='_') { // rna can't start with a "_", so for __dict__ and similar we can skip using rna lookups
 
3437
 
 
3438
        PYRNA_STRUCT_CHECK_OBJ(self);
 
3439
 
 
3440
        if (name == NULL) {
 
3441
                PyErr_SetString(PyExc_AttributeError, "bpy_struct: __getattr__ must be a string");
 
3442
                ret = NULL;
 
3443
        }
 
3444
        else if (name[0] == '_') { // rna can't start with a "_", so for __dict__ and similar we can skip using rna lookups
2291
3445
                /* annoying exception, maybe we need to have different types for this... */
2292
 
                if((strcmp(name, "__getitem__")==0 || strcmp(name, "__setitem__")==0) && !RNA_struct_idproperties_check(self->ptr.type)) {
 
3446
                if ((strcmp(name, "__getitem__") == 0 || strcmp(name, "__setitem__") == 0) && !RNA_struct_idprops_check(self->ptr.type)) {
2293
3447
                        PyErr_SetString(PyExc_AttributeError, "bpy_struct: no __getitem__ support for this type");
2294
3448
                        ret = NULL;
2295
3449
                }
2298
3452
                }
2299
3453
        }
2300
3454
        else if ((prop = RNA_struct_find_property(&self->ptr, name))) {
2301
 
                  ret = pyrna_prop_to_py(&self->ptr, prop);
 
3455
                ret = pyrna_prop_to_py(&self->ptr, prop);
2302
3456
        }
2303
3457
        /* RNA function only if callback is declared (no optional functions) */
2304
3458
        else if ((func = RNA_struct_find_function(&self->ptr, name)) && RNA_function_defined(func)) {
2305
 
                ret = pyrna_func_to_py((BPy_DummyPointerRNA *)self, func);
 
3459
                ret = pyrna_func_to_py(&self->ptr, func);
2306
3460
        }
2307
3461
        else if (self->ptr.type == &RNA_Context) {
2308
3462
                bContext *C = self->ptr.data;
2309
 
                if(C==NULL) {
2310
 
                        PyErr_Format( PyExc_AttributeError, "bpy_struct: Context is 'NULL', can't get \"%.200s\" from context", name);
2311
 
                        ret= NULL;
 
3463
                if (C == NULL) {
 
3464
                        PyErr_Format(PyExc_AttributeError,
 
3465
                                     "bpy_struct: Context is 'NULL', can't get \"%.200s\" from context",
 
3466
                                     name);
 
3467
                        ret = NULL;
2312
3468
                }
2313
3469
                else {
2314
3470
                        PointerRNA newptr;
2315
3471
                        ListBase newlb;
2316
3472
                        short newtype;
2317
3473
 
2318
 
                        int done= CTX_data_get(C, name, &newptr, &newlb, &newtype);
 
3474
                        int done = CTX_data_get(C, name, &newptr, &newlb, &newtype);
2319
3475
 
2320
 
                        if(done==1) { /* found */
2321
 
                                switch(newtype) {
2322
 
                                case CTX_DATA_TYPE_POINTER:
2323
 
                                        if(newptr.data == NULL) {
2324
 
                                                ret= Py_None;
2325
 
                                                Py_INCREF(ret);
2326
 
                                        }
2327
 
                                        else {
2328
 
                                                ret= pyrna_struct_CreatePyObject(&newptr);
2329
 
                                        }
2330
 
                                        break;
2331
 
                                case CTX_DATA_TYPE_COLLECTION:
 
3476
                        if (done == 1) { /* found */
 
3477
                                switch (newtype) {
 
3478
                                        case CTX_DATA_TYPE_POINTER:
 
3479
                                                if (newptr.data == NULL) {
 
3480
                                                        ret = Py_None;
 
3481
                                                        Py_INCREF(ret);
 
3482
                                                }
 
3483
                                                else {
 
3484
                                                        ret = pyrna_struct_CreatePyObject(&newptr);
 
3485
                                                }
 
3486
                                                break;
 
3487
                                        case CTX_DATA_TYPE_COLLECTION:
2332
3488
                                        {
2333
3489
                                                CollectionPointerLink *link;
2334
3490
                                                PyObject *linkptr;
2335
 
        
 
3491
 
2336
3492
                                                ret = PyList_New(0);
2337
 
        
2338
 
                                                for(link=newlb.first; link; link=link->next) {
2339
 
                                                        linkptr= pyrna_struct_CreatePyObject(&link->ptr);
 
3493
 
 
3494
                                                for (link = newlb.first; link; link = link->next) {
 
3495
                                                        linkptr = pyrna_struct_CreatePyObject(&link->ptr);
2340
3496
                                                        PyList_Append(ret, linkptr);
2341
3497
                                                        Py_DECREF(linkptr);
2342
3498
                                                }
2343
3499
                                        }
2344
3500
                                        break;
2345
 
                                default:
2346
 
                                        /* should never happen */
2347
 
                                        PyErr_Format(PyExc_AttributeError, "bpy_struct: Context type invalid %d, can't get \"%.200s\" from context", newtype, name);
2348
 
                                        ret= NULL;
 
3501
                                        default:
 
3502
                                                /* should never happen */
 
3503
                                                BLI_assert(!"Invalid context type");
 
3504
 
 
3505
                                                PyErr_Format(PyExc_AttributeError,
 
3506
                                                             "bpy_struct: Context type invalid %d, can't get \"%.200s\" from context",
 
3507
                                                             newtype, name);
 
3508
                                                ret = NULL;
2349
3509
                                }
2350
3510
                        }
2351
 
                        else if (done==-1) { /* found but not set */
 
3511
                        else if (done == -1) { /* found but not set */
2352
3512
                                ret = Py_None;
2353
3513
                                Py_INCREF(ret);
2354
3514
                        }
2362
3522
        }
2363
3523
        else {
2364
3524
#if 0
2365
 
                PyErr_Format( PyExc_AttributeError, "bpy_struct: attribute \"%.200s\" not found", name);
 
3525
                PyErr_Format(PyExc_AttributeError,
 
3526
                             "bpy_struct: attribute \"%.200s\" not found",
 
3527
                             name);
2366
3528
                ret = NULL;
2367
3529
#endif
2368
 
                /* Include this incase this instance is a subtype of a python class
 
3530
                /* Include this in case this instance is a subtype of a python class
2369
3531
                 * In these instances we may want to return a function or variable provided by the subtype
2370
3532
                 *
2371
3533
                 * Also needed to return methods when its not a subtype
2374
3536
                /* The error raised here will be displayed */
2375
3537
                ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
2376
3538
        }
2377
 
        
 
3539
 
2378
3540
        return ret;
2379
3541
}
2380
3542
 
2381
3543
#if 0
2382
3544
static int pyrna_struct_pydict_contains(PyObject *self, PyObject *pyname)
2383
3545
{
2384
 
         PyObject *dict= *(_PyObject_GetDictPtr((PyObject *)self));
2385
 
         if (dict==NULL) /* unlikely */
2386
 
                 return 0;
 
3546
        PyObject *dict = *(_PyObject_GetDictPtr((PyObject *)self));
 
3547
        if (dict == NULL) /* unlikely */
 
3548
                return 0;
2387
3549
 
2388
3550
        return PyDict_Contains(dict, pyname);
2389
3551
}
2390
3552
#endif
2391
3553
 
2392
3554
//--------------- setattr-------------------------------------------
2393
 
static int pyrna_struct_setattro( BPy_StructRNA *self, PyObject *pyname, PyObject *value )
2394
 
{
2395
 
        char *name = _PyUnicode_AsString(pyname);
2396
 
        PropertyRNA *prop= NULL;
2397
 
 
2398
 
        if (name[0] != '_' && (prop= RNA_struct_find_property(&self->ptr, name))) {
 
3555
static int pyrna_is_deferred_prop(const PyObject *value)
 
3556
{
 
3557
        return PyTuple_CheckExact(value) &&
 
3558
               PyTuple_GET_SIZE(value) == 2 &&
 
3559
               PyCFunction_Check(PyTuple_GET_ITEM(value, 0)) &&
 
3560
               PyDict_CheckExact(PyTuple_GET_ITEM(value, 1));
 
3561
}
 
3562
 
 
3563
#if 0
 
3564
static PyObject *pyrna_struct_meta_idprop_getattro(PyObject *cls, PyObject *attr)
 
3565
{
 
3566
        PyObject *ret = PyType_Type.tp_getattro(cls, attr);
 
3567
 
 
3568
        /* Allows:
 
3569
         * >>> bpy.types.Scene.foo = BoolProperty()
 
3570
         * >>> bpy.types.Scene.foo
 
3571
         * <bpy_struct, BoolProperty("foo")>
 
3572
         * ...rather than returning the deferred class register tuple as checked by pyrna_is_deferred_prop()
 
3573
         *
 
3574
         * Disable for now, this is faking internal behavior in a way thats too tricky to maintain well. */
 
3575
#if 0
 
3576
        if (ret == NULL) { // || pyrna_is_deferred_prop(ret)
 
3577
                StructRNA *srna = srna_from_self(cls, "StructRNA.__getattr__");
 
3578
                if (srna) {
 
3579
                        PropertyRNA *prop = RNA_struct_type_find_property(srna, _PyUnicode_AsString(attr));
 
3580
                        if (prop) {
 
3581
                                PointerRNA tptr;
 
3582
                                PyErr_Clear(); /* clear error from tp_getattro */
 
3583
                                RNA_pointer_create(NULL, &RNA_Property, prop, &tptr);
 
3584
                                ret = pyrna_struct_CreatePyObject(&tptr);
 
3585
                        }
 
3586
                }
 
3587
        }
 
3588
#endif
 
3589
 
 
3590
        return ret;
 
3591
}
 
3592
#endif
 
3593
 
 
3594
static int pyrna_struct_meta_idprop_setattro(PyObject *cls, PyObject *attr, PyObject *value)
 
3595
{
 
3596
        StructRNA *srna = srna_from_self(cls, "StructRNA.__setattr__");
 
3597
        const int is_deferred_prop = (value && pyrna_is_deferred_prop(value));
 
3598
        const char *attr_str = _PyUnicode_AsString(attr);
 
3599
 
 
3600
        if (srna && !pyrna_write_check() && (is_deferred_prop || RNA_struct_type_find_property(srna, attr_str))) {
 
3601
                PyErr_Format(PyExc_AttributeError,
 
3602
                             "pyrna_struct_meta_idprop_setattro() "
 
3603
                             "can't set in readonly state '%.200s.%S'",
 
3604
                             ((PyTypeObject *)cls)->tp_name, attr);
 
3605
                return -1;
 
3606
        }
 
3607
 
 
3608
        if (srna == NULL) {
 
3609
                /* allow setting on unregistered classes which can be registered later on */
 
3610
#if 0
 
3611
                if (value && is_deferred_prop) {
 
3612
                        PyErr_Format(PyExc_AttributeError,
 
3613
                                     "pyrna_struct_meta_idprop_setattro() unable to get srna from class '%.200s'",
 
3614
                                     ((PyTypeObject *)cls)->tp_name);
 
3615
                        return -1;
 
3616
                }
 
3617
#endif
 
3618
                /* srna_from_self may set an error */
 
3619
                PyErr_Clear();
 
3620
                return PyType_Type.tp_setattro(cls, attr, value);
 
3621
        }
 
3622
 
 
3623
        if (value) {
 
3624
                /* check if the value is a property */
 
3625
                if (is_deferred_prop) {
 
3626
                        int ret = deferred_register_prop(srna, attr, value);
 
3627
                        if (ret == -1) {
 
3628
                                /* error set */
 
3629
                                return ret;
 
3630
                        }
 
3631
 
 
3632
                        /* pass through and assign to the classes __dict__ as well
 
3633
                         * when the value isn't assigned it still creates the RNA property
 
3634
                         * but gets confusing from script writers POV if the assigned value cant be read back. */
 
3635
                }
 
3636
                else {
 
3637
                        /* remove existing property if its set or we also end up with confusion */
 
3638
                        RNA_def_property_free_identifier(srna, attr_str); /* ignore on failure */
 
3639
                }
 
3640
        }
 
3641
        else { /* __delattr__ */
 
3642
                /* first find if this is a registered property */
 
3643
                const int ret = RNA_def_property_free_identifier(srna, attr_str);
 
3644
                if (ret == -1) {
 
3645
                        PyErr_Format(PyExc_TypeError,
 
3646
                                     "struct_meta_idprop.detattr(): '%s' not a dynamic property",
 
3647
                                     attr_str);
 
3648
                        return -1;
 
3649
                }
 
3650
        }
 
3651
 
 
3652
        /* fallback to standard py, delattr/setattr */
 
3653
        return PyType_Type.tp_setattro(cls, attr, value);
 
3654
}
 
3655
 
 
3656
static int pyrna_struct_setattro(BPy_StructRNA *self, PyObject *pyname, PyObject *value)
 
3657
{
 
3658
        const char *name = _PyUnicode_AsString(pyname);
 
3659
        PropertyRNA *prop = NULL;
 
3660
 
 
3661
        PYRNA_STRUCT_CHECK_INT(self);
 
3662
 
 
3663
#ifdef USE_PEDANTIC_WRITE
 
3664
        if (rna_disallow_writes && rna_id_write_error(&self->ptr, pyname)) {
 
3665
                return -1;
 
3666
        }
 
3667
#endif // USE_PEDANTIC_WRITE
 
3668
 
 
3669
        if (name == NULL) {
 
3670
                PyErr_SetString(PyExc_AttributeError, "bpy_struct: __setattr__ must be a string");
 
3671
                return -1;
 
3672
        }
 
3673
        else if (name[0] != '_' && (prop = RNA_struct_find_property(&self->ptr, name))) {
2399
3674
                if (!RNA_property_editable_flag(&self->ptr, prop)) {
2400
 
                        PyErr_Format( PyExc_AttributeError, "bpy_struct: attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type) );
 
3675
                        PyErr_Format(PyExc_AttributeError,
 
3676
                                     "bpy_struct: attribute \"%.200s\" from \"%.200s\" is read-only",
 
3677
                                     RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type));
2401
3678
                        return -1;
2402
3679
                }
2403
3680
        }
2404
3681
        else if (self->ptr.type == &RNA_Context) {
2405
3682
                /* code just raises correct error, context prop's cant be set, unless its apart of the py class */
2406
3683
                bContext *C = self->ptr.data;
2407
 
                if(C==NULL) {
2408
 
                        PyErr_Format(PyExc_AttributeError, "bpy_struct: Context is 'NULL', can't set \"%.200s\" from context", name);
 
3684
                if (C == NULL) {
 
3685
                        PyErr_Format(PyExc_AttributeError,
 
3686
                                     "bpy_struct: Context is 'NULL', can't set \"%.200s\" from context",
 
3687
                                     name);
2409
3688
                        return -1;
2410
3689
                }
2411
3690
                else {
2413
3692
                        ListBase newlb;
2414
3693
                        short newtype;
2415
3694
 
2416
 
                        int done= CTX_data_get(C, name, &newptr, &newlb, &newtype);
 
3695
                        int done = CTX_data_get(C, name, &newptr, &newlb, &newtype);
2417
3696
 
2418
 
                        if(done==1) {
2419
 
                                PyErr_Format(PyExc_AttributeError, "bpy_struct: Context property \"%.200s\" is read-only", name);
 
3697
                        if (done == 1) {
 
3698
                                PyErr_Format(PyExc_AttributeError,
 
3699
                                             "bpy_struct: Context property \"%.200s\" is read-only",
 
3700
                                             name);
2420
3701
                                BLI_freelistN(&newlb);
2421
3702
                                return -1;
2422
3703
                        }
2426
3707
        }
2427
3708
 
2428
3709
        /* pyrna_py_to_prop sets its own exceptions */
2429
 
        if(prop)
2430
 
                return pyrna_py_to_prop(&self->ptr, prop, NULL, NULL, value, "bpy_struct: item.attr = val:");
2431
 
        else
 
3710
        if (prop) {
 
3711
                if (value == NULL) {
 
3712
                        PyErr_SetString(PyExc_AttributeError, "bpy_struct: del not supported");
 
3713
                        return -1;
 
3714
                }
 
3715
                return pyrna_py_to_prop(&self->ptr, prop, NULL, value, "bpy_struct: item.attr = val:");
 
3716
        }
 
3717
        else {
2432
3718
                return PyObject_GenericSetAttr((PyObject *)self, pyname, value);
 
3719
        }
2433
3720
}
2434
3721
 
2435
3722
static PyObject *pyrna_prop_dir(BPy_PropertyRNA *self)
2437
3724
        PyObject *ret;
2438
3725
        PointerRNA r_ptr;
2439
3726
 
2440
 
        /* Include this incase this instance is a subtype of a python class
 
3727
        /* Include this in case this instance is a subtype of a python class
2441
3728
         * In these instances we may want to return a function or variable provided by the subtype
2442
3729
         * */
2443
3730
        ret = PyList_New(0);
2444
3731
 
2445
 
        if (!BPy_PropertyRNA_CheckExact(self))
 
3732
        if (!BPy_PropertyRNA_CheckExact(self)) {
2446
3733
                pyrna_dir_members_py(ret, (PyObject *)self);
 
3734
        }
2447
3735
 
2448
 
        if(RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr))
2449
 
                pyrna_dir_members_rna(ret, &r_ptr);
 
3736
        if (RNA_property_type(self->prop) == PROP_COLLECTION) {
 
3737
                if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
 
3738
                        pyrna_dir_members_rna(ret, &r_ptr);
 
3739
                }
 
3740
        }
2450
3741
 
2451
3742
        return ret;
2452
3743
}
2453
3744
 
2454
3745
 
2455
 
static PyObject *pyrna_prop_array_getattro( BPy_PropertyRNA *self, PyObject *pyname )
 
3746
static PyObject *pyrna_prop_array_getattro(BPy_PropertyRNA *self, PyObject *pyname)
2456
3747
{
2457
3748
        return PyObject_GenericGetAttr((PyObject *)self, pyname);
2458
3749
}
2459
3750
 
2460
 
static PyObject *pyrna_prop_collection_getattro( BPy_PropertyRNA *self, PyObject *pyname )
 
3751
static PyObject *pyrna_prop_collection_getattro(BPy_PropertyRNA *self, PyObject *pyname)
2461
3752
{
2462
 
        char *name = _PyUnicode_AsString(pyname);
 
3753
        const char *name = _PyUnicode_AsString(pyname);
2463
3754
 
2464
 
        if(name[0] != '_') {
 
3755
        if (name == NULL) {
 
3756
                PyErr_SetString(PyExc_AttributeError, "bpy_prop_collection: __getattr__ must be a string");
 
3757
                return NULL;
 
3758
        }
 
3759
        else if (name[0] != '_') {
2465
3760
                PyObject *ret;
2466
3761
                PropertyRNA *prop;
2467
3762
                FunctionRNA *func;
2468
3763
 
2469
3764
                PointerRNA r_ptr;
2470
 
                if(RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
 
3765
                if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
2471
3766
                        if ((prop = RNA_struct_find_property(&r_ptr, name))) {
2472
3767
                                ret = pyrna_prop_to_py(&r_ptr, prop);
2473
3768
 
2474
3769
                                return ret;
2475
3770
                        }
2476
3771
                        else if ((func = RNA_struct_find_function(&r_ptr, name))) {
2477
 
                                PyObject *self_collection= pyrna_struct_CreatePyObject(&r_ptr);
2478
 
                                ret = pyrna_func_to_py((BPy_DummyPointerRNA *)self_collection, func);
 
3772
                                PyObject *self_collection = pyrna_struct_CreatePyObject(&r_ptr);
 
3773
                                ret = pyrna_func_to_py(&((BPy_DummyPointerRNA *)self_collection)->ptr, func);
2479
3774
                                Py_DECREF(self_collection);
2480
3775
 
2481
3776
                                return ret;
2483
3778
                }
2484
3779
        }
2485
3780
 
2486
 
        /* The error raised here will be displayed */
 
3781
#if 0
2487
3782
        return PyObject_GenericGetAttr((PyObject *)self, pyname);
 
3783
#else
 
3784
        {
 
3785
                /* Could just do this except for 1 awkward case.
 
3786
                 * PyObject_GenericGetAttr((PyObject *)self, pyname);
 
3787
                 * so as to support 'bpy.data.library.load()'
 
3788
                 * note, this _only_ supports static methods */
 
3789
 
 
3790
                PyObject *ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
 
3791
 
 
3792
                if (ret == NULL && name[0] != '_') { /* avoid inheriting __call__ and similar */
 
3793
                        /* since this is least common case, handle it last */
 
3794
                        PointerRNA r_ptr;
 
3795
                        if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
 
3796
                                PyObject *cls;
 
3797
 
 
3798
                                PyObject *error_type, *error_value, *error_traceback;
 
3799
                                PyErr_Fetch(&error_type, &error_value, &error_traceback);
 
3800
                                PyErr_Clear();
 
3801
 
 
3802
                                cls = pyrna_struct_Subtype(&r_ptr);
 
3803
                                ret = PyObject_GenericGetAttr(cls, pyname);
 
3804
                                Py_DECREF(cls);
 
3805
 
 
3806
                                /* restore the original error */
 
3807
                                if (ret == NULL) {
 
3808
                                        PyErr_Restore(error_type, error_value, error_traceback);
 
3809
                                }
 
3810
                        }
 
3811
                }
 
3812
 
 
3813
                return ret;
 
3814
        }
 
3815
#endif
2488
3816
}
2489
3817
 
2490
3818
//--------------- setattr-------------------------------------------
2491
 
static int pyrna_prop_collection_setattro( BPy_PropertyRNA *self, PyObject *pyname, PyObject *value )
 
3819
static int pyrna_prop_collection_setattro(BPy_PropertyRNA *self, PyObject *pyname, PyObject *value)
2492
3820
{
2493
 
        char *name = _PyUnicode_AsString(pyname);
 
3821
        const char *name = _PyUnicode_AsString(pyname);
2494
3822
        PropertyRNA *prop;
2495
3823
        PointerRNA r_ptr;
2496
3824
 
2497
 
        if(RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
 
3825
#ifdef USE_PEDANTIC_WRITE
 
3826
        if (rna_disallow_writes && rna_id_write_error(&self->ptr, pyname)) {
 
3827
                return -1;
 
3828
        }
 
3829
#endif // USE_PEDANTIC_WRITE
 
3830
 
 
3831
        if (name == NULL) {
 
3832
                PyErr_SetString(PyExc_AttributeError, "bpy_prop: __setattr__ must be a string");
 
3833
                return -1;
 
3834
        }
 
3835
        else if (value == NULL) {
 
3836
                PyErr_SetString(PyExc_AttributeError, "bpy_prop: del not supported");
 
3837
                return -1;
 
3838
        }
 
3839
        else if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
2498
3840
                if ((prop = RNA_struct_find_property(&r_ptr, name))) {
2499
3841
                        /* pyrna_py_to_prop sets its own exceptions */
2500
 
                        return pyrna_py_to_prop(&r_ptr, prop, NULL, NULL, value, "BPy_PropertyRNA - Attribute (setattr):");
 
3842
                        return pyrna_py_to_prop(&r_ptr, prop, NULL, value, "BPy_PropertyRNA - Attribute (setattr):");
2501
3843
                }
2502
3844
        }
2503
3845
 
2504
 
        PyErr_Format( PyExc_AttributeError, "bpy_prop_collection: attribute \"%.200s\" not found", name);
 
3846
        PyErr_Format(PyExc_AttributeError,
 
3847
                     "bpy_prop_collection: attribute \"%.200s\" not found",
 
3848
                     name);
2505
3849
        return -1;
2506
3850
}
2507
3851
 
2508
3852
/* odd case, we need to be able return a python method from a tp_getset */
2509
 
static PyObject *pyrna_prop_add(BPy_PropertyRNA *self)
 
3853
static PyObject *pyrna_prop_collection_idprop_add(BPy_PropertyRNA *self)
2510
3854
{
2511
3855
        PointerRNA r_ptr;
2512
3856
 
2513
3857
        RNA_property_collection_add(&self->ptr, self->prop, &r_ptr);
2514
 
        if(!r_ptr.data) {
2515
 
                PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.add(): not supported for this collection");
 
3858
        if (!r_ptr.data) {
 
3859
                PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.add(): not supported for this collection");
2516
3860
                return NULL;
2517
3861
        }
2518
3862
        else {
2520
3864
        }
2521
3865
}
2522
3866
 
2523
 
static PyObject *pyrna_prop_remove(BPy_PropertyRNA *self, PyObject *value)
2524
 
{
2525
 
        PyObject *ret;
2526
 
        int key= PyLong_AsSsize_t(value);
2527
 
 
2528
 
        if (key==-1 && PyErr_Occurred()) {
2529
 
                PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.remove(): expected one int argument");
2530
 
                return NULL;
2531
 
        }
2532
 
 
2533
 
        if(!RNA_property_collection_remove(&self->ptr, self->prop, key)) {
2534
 
                PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.remove() not supported for this collection");
2535
 
                return NULL;
2536
 
        }
2537
 
 
2538
 
        ret = Py_None;
2539
 
        Py_INCREF(ret);
2540
 
 
2541
 
        return ret;
2542
 
}
2543
 
 
2544
 
static PyObject *pyrna_prop_move(BPy_PropertyRNA *self, PyObject *args)
2545
 
{
2546
 
        PyObject *ret;
2547
 
        int key=0, pos=0;
 
3867
static PyObject *pyrna_prop_collection_idprop_remove(BPy_PropertyRNA *self, PyObject *value)
 
3868
{
 
3869
        int key = PyLong_AsLong(value);
 
3870
 
 
3871
        if (key == -1 && PyErr_Occurred()) {
 
3872
                PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.remove(): expected one int argument");
 
3873
                return NULL;
 
3874
        }
 
3875
 
 
3876
        if (!RNA_property_collection_remove(&self->ptr, self->prop, key)) {
 
3877
                PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.remove() not supported for this collection");
 
3878
                return NULL;
 
3879
        }
 
3880
 
 
3881
        Py_RETURN_NONE;
 
3882
}
 
3883
 
 
3884
static PyObject *pyrna_prop_collection_idprop_clear(BPy_PropertyRNA *self)
 
3885
{
 
3886
        RNA_property_collection_clear(&self->ptr, self->prop);
 
3887
 
 
3888
        Py_RETURN_NONE;
 
3889
}
 
3890
 
 
3891
static PyObject *pyrna_prop_collection_idprop_move(BPy_PropertyRNA *self, PyObject *args)
 
3892
{
 
3893
        int key = 0, pos = 0;
2548
3894
 
2549
3895
        if (!PyArg_ParseTuple(args, "ii", &key, &pos)) {
2550
 
                PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.move(): expected two ints as arguments");
2551
 
                return NULL;
2552
 
        }
2553
 
 
2554
 
        if(!RNA_property_collection_move(&self->ptr, self->prop, key, pos)) {
2555
 
                PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.move() not supported for this collection");
2556
 
                return NULL;
2557
 
        }
2558
 
 
2559
 
        ret = Py_None;
2560
 
        Py_INCREF(ret);
2561
 
 
2562
 
        return ret;
 
3896
                PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.move(): expected two ints as arguments");
 
3897
                return NULL;
 
3898
        }
 
3899
 
 
3900
        if (!RNA_property_collection_move(&self->ptr, self->prop, key, pos)) {
 
3901
                PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.move() not supported for this collection");
 
3902
                return NULL;
 
3903
        }
 
3904
 
 
3905
        Py_RETURN_NONE;
2563
3906
}
2564
3907
 
2565
 
static PyObject *pyrna_struct_get_id_data(BPy_StructRNA *self)
 
3908
 
 
3909
PyDoc_STRVAR(pyrna_struct_get_id_data_doc,
 
3910
"The :class:`bpy.types.ID` object this datablock is from or None, (not available for all data types)"
 
3911
);
 
3912
static PyObject *pyrna_struct_get_id_data(BPy_DummyPointerRNA *self)
2566
3913
{
2567
 
        if(self->ptr.id.data) {
 
3914
        /* used for struct and pointer since both have a ptr */
 
3915
        if (self->ptr.id.data) {
2568
3916
                PointerRNA id_ptr;
2569
3917
                RNA_id_pointer_create((ID *)self->ptr.id.data, &id_ptr);
2570
3918
                return pyrna_struct_CreatePyObject(&id_ptr);
2573
3921
        Py_RETURN_NONE;
2574
3922
}
2575
3923
 
 
3924
PyDoc_STRVAR(pyrna_struct_get_data_doc,
 
3925
"The data this property is using, *type* :class:`bpy.types.bpy_struct`"
 
3926
);
 
3927
static PyObject *pyrna_struct_get_data(BPy_DummyPointerRNA *self)
 
3928
{
 
3929
        return pyrna_struct_CreatePyObject(&self->ptr);
 
3930
}
 
3931
 
 
3932
PyDoc_STRVAR(pyrna_struct_get_rna_type_doc,
 
3933
"The property type for introspection"
 
3934
);
 
3935
static PyObject *pyrna_struct_get_rna_type(BPy_PropertyRNA *self)
 
3936
{
 
3937
        PointerRNA tptr;
 
3938
        RNA_pointer_create(NULL, &RNA_Property, self->prop, &tptr);
 
3939
        return pyrna_struct_Subtype(&tptr);
 
3940
}
 
3941
 
 
3942
 
 
3943
 
2576
3944
/*****************************************************************************/
2577
3945
/* Python attributes get/set structure:                                      */
2578
3946
/*****************************************************************************/
2579
 
#if 0
 
3947
 
2580
3948
static PyGetSetDef pyrna_prop_getseters[] = {
2581
 
        {"active", (getter)pyrna_prop_get_active, (setter)pyrna_prop_set_active, "", NULL},
2582
 
        {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
 
3949
        {(char *)"id_data", (getter)pyrna_struct_get_id_data, (setter)NULL, (char *)pyrna_struct_get_id_data_doc, NULL},
 
3950
        {(char *)"data", (getter)pyrna_struct_get_data, (setter)NULL, (char *)pyrna_struct_get_data_doc, NULL},
 
3951
        {(char *)"rna_type", (getter)pyrna_struct_get_rna_type, (setter)NULL, (char *)pyrna_struct_get_rna_type_doc, NULL},
 
3952
        {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
2583
3953
};
2584
 
#endif
2585
3954
 
2586
3955
static PyGetSetDef pyrna_struct_getseters[] = {
2587
 
        {"id_data", (getter)pyrna_struct_get_id_data, (setter)NULL, "The :class:`ID` object this datablock is from or None, (not available for all data types)", NULL},
2588
 
        {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
2589
 
};
2590
 
 
2591
 
static PyObject *pyrna_prop_keys(BPy_PropertyRNA *self)
 
3956
        {(char *)"id_data", (getter)pyrna_struct_get_id_data, (setter)NULL, (char *)pyrna_struct_get_id_data_doc, NULL},
 
3957
        {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 
3958
};
 
3959
 
 
3960
static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *closure);
 
3961
 
 
3962
static PyGetSetDef pyrna_func_getseters[] = {
 
3963
        {(char *)"__doc__", (getter)pyrna_func_doc_get, (setter)NULL, NULL, NULL},
 
3964
        {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 
3965
};
 
3966
 
 
3967
PyDoc_STRVAR(pyrna_prop_collection_keys_doc,
 
3968
".. method:: keys()\n"
 
3969
"\n"
 
3970
"   Return the identifiers of collection members\n"
 
3971
"   (matching pythons dict.keys() functionality).\n"
 
3972
"\n"
 
3973
"   :return: the identifiers for each member of this collection.\n"
 
3974
"   :rtype: list of strings\n"
 
3975
);
 
3976
static PyObject *pyrna_prop_collection_keys(BPy_PropertyRNA *self)
2592
3977
{
2593
 
        PyObject *ret= PyList_New(0);
 
3978
        PyObject *ret = PyList_New(0);
2594
3979
        PyObject *item;
2595
3980
        char name[256], *nameptr;
 
3981
        int namelen;
2596
3982
 
2597
3983
        RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2598
 
                nameptr= RNA_struct_name_get_alloc(&itemptr, name, sizeof(name));
 
3984
                nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
2599
3985
 
2600
 
                if(nameptr) {
 
3986
                if (nameptr) {
2601
3987
                        /* add to python list */
2602
 
                        item = PyUnicode_FromString( nameptr );
 
3988
                        item = PyUnicode_FromStringAndSize(nameptr, namelen);
2603
3989
                        PyList_Append(ret, item);
2604
3990
                        Py_DECREF(item);
2605
3991
                        /* done */
2606
3992
 
2607
 
                        if(name != nameptr)
 
3993
                        if (name != nameptr) {
2608
3994
                                MEM_freeN(nameptr);
 
3995
                        }
2609
3996
                }
2610
3997
        }
2611
3998
        RNA_PROP_END;
2612
 
        
 
3999
 
2613
4000
        return ret;
2614
4001
}
2615
4002
 
2616
 
static PyObject *pyrna_prop_items(BPy_PropertyRNA *self)
 
4003
PyDoc_STRVAR(pyrna_prop_collection_items_doc,
 
4004
".. method:: items()\n"
 
4005
"\n"
 
4006
"   Return the identifiers of collection members\n"
 
4007
"   (matching pythons dict.items() functionality).\n"
 
4008
"\n"
 
4009
"   :return: (key, value) pairs for each member of this collection.\n"
 
4010
"   :rtype: list of tuples\n"
 
4011
);
 
4012
static PyObject *pyrna_prop_collection_items(BPy_PropertyRNA *self)
2617
4013
{
2618
 
        PyObject *ret= PyList_New(0);
 
4014
        PyObject *ret = PyList_New(0);
2619
4015
        PyObject *item;
2620
4016
        char name[256], *nameptr;
2621
 
        int i= 0;
 
4017
        int namelen;
 
4018
        int i = 0;
2622
4019
 
2623
4020
        RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2624
 
                if(itemptr.data) {
 
4021
                if (itemptr.data) {
2625
4022
                        /* add to python list */
2626
 
                        item= PyTuple_New(2);
2627
 
                        nameptr= RNA_struct_name_get_alloc(&itemptr, name, sizeof(name));
2628
 
                        if(nameptr) {
2629
 
                                PyTuple_SET_ITEM(item, 0, PyUnicode_FromString( nameptr ));
2630
 
                                if(name != nameptr)
 
4023
                        item = PyTuple_New(2);
 
4024
                        nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
 
4025
                        if (nameptr) {
 
4026
                                PyTuple_SET_ITEM(item, 0, PyUnicode_FromStringAndSize(nameptr, namelen));
 
4027
                                if (name != nameptr)
2631
4028
                                        MEM_freeN(nameptr);
2632
4029
                        }
2633
4030
                        else {
2634
 
                                PyTuple_SET_ITEM(item, 0, PyLong_FromSsize_t(i)); /* a bit strange but better then returning an empty list */
 
4031
                                /* a bit strange but better then returning an empty list */
 
4032
                                PyTuple_SET_ITEM(item, 0, PyLong_FromSsize_t(i));
2635
4033
                        }
2636
4034
                        PyTuple_SET_ITEM(item, 1, pyrna_struct_CreatePyObject(&itemptr));
2637
4035
 
2642
4040
                }
2643
4041
        }
2644
4042
        RNA_PROP_END;
2645
 
        
 
4043
 
2646
4044
        return ret;
2647
4045
}
2648
4046
 
2649
 
static PyObject *pyrna_prop_values(BPy_PropertyRNA *self)
 
4047
PyDoc_STRVAR(pyrna_prop_collection_values_doc,
 
4048
".. method:: values()\n"
 
4049
"\n"
 
4050
"   Return the values of collection\n"
 
4051
"   (matching pythons dict.values() functionality).\n"
 
4052
"\n"
 
4053
"   :return: the members of this collection.\n"
 
4054
"   :rtype: list\n"
 
4055
);
 
4056
static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self)
2650
4057
{
2651
 
        PyObject *ret= PyList_New(0);
2652
 
        PyObject *item;
2653
 
        
2654
 
        RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2655
 
                item = pyrna_struct_CreatePyObject(&itemptr);
2656
 
                PyList_Append(ret, item);
2657
 
                Py_DECREF(item);
2658
 
        }
2659
 
        RNA_PROP_END;
2660
 
 
2661
 
        return ret;
 
4058
        /* re-use slice*/
 
4059
        return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2662
4060
}
2663
4061
 
2664
 
static char pyrna_struct_get_doc[] =
 
4062
PyDoc_STRVAR(pyrna_struct_get_doc,
2665
4063
".. method:: get(key, default=None)\n"
2666
4064
"\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"
2668
4067
"\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"
2673
4073
"\n"
2674
 
"   .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n";
2675
 
 
 
4074
BPY_DOC_ID_PROP_TYPE_NOTE
 
4075
);
2676
4076
static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
2677
4077
{
2678
4078
        IDProperty *group, *idprop;
2679
4079
 
2680
 
        char *key;
2681
 
        PyObject* def = Py_None;
 
4080
        const char *key;
 
4081
        PyObject *def = Py_None;
 
4082
 
 
4083
        PYRNA_STRUCT_CHECK_OBJ(self);
2682
4084
 
2683
4085
        if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
2684
4086
                return NULL;
2685
4087
 
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");
2689
4091
                return NULL;
2690
4092
        }
2691
4093
 
2692
 
        group= RNA_struct_idproperties(&self->ptr, 0);
2693
 
        if(group) {
2694
 
                idprop= IDP_GetPropertyFromGroup(group, key);
 
4094
        group = RNA_struct_idprops(&self->ptr, 0);
 
4095
        if (group) {
 
4096
                idprop = IDP_GetPropertyFromGroup(group, key);
2695
4097
 
2696
 
                if(idprop)
2697
 
                        return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
 
4098
                if (idprop) {
 
4099
                        return BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
 
4100
                }
2698
4101
        }
2699
4102
 
2700
 
        Py_INCREF(def);
2701
 
        return def;
 
4103
        return Py_INCREF(def), def;
2702
4104
}
2703
4105
 
2704
 
static char pyrna_struct_as_pointer_doc[] =
 
4106
PyDoc_STRVAR(pyrna_struct_as_pointer_doc,
2705
4107
".. method:: as_pointer()\n"
2706
4108
"\n"
2707
 
"   Returns capsule which holds a pointer to blenders internal data\n"
2708
 
"\n"
2709
 
"   :return: capsule with a name set from the struct type.\n"
2710
 
"   :rtype: PyCapsule\n"
2711
 
"\n"
2712
 
"   .. note:: This is intended only for advanced script writers who need to pass blender data to their own C/Python modules.\n";
2713
 
 
 
4109
"   Returns the memory address which holds a pointer to blenders internal data\n"
 
4110
"\n"
 
4111
"   :return: int (memory address).\n"
 
4112
"   :rtype: int\n"
 
4113
"\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"
 
4116
);
2714
4117
static PyObject *pyrna_struct_as_pointer(BPy_StructRNA *self)
2715
4118
{
2716
 
        if(self->ptr.data)
2717
 
                return PyCapsule_New(self->ptr.data, RNA_struct_identifier(self->ptr.type), NULL);
2718
 
 
2719
 
        Py_RETURN_NONE;
 
4119
        return PyLong_FromVoidPtr(self->ptr.data);
2720
4120
}
2721
4121
 
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"
 
4124
"\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"
 
4127
"\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"
 
4133
);
 
4134
static PyObject *pyrna_prop_collection_get(BPy_PropertyRNA *self, PyObject *args)
2723
4135
{
2724
4136
        PointerRNA newptr;
2725
 
        
2726
 
        char *key;
2727
 
        PyObject* def = Py_None;
2728
 
 
2729
 
        if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
 
4137
 
 
4138
        PyObject *key_ob;
 
4139
        PyObject *def = Py_None;
 
4140
 
 
4141
        PYRNA_PROP_CHECK_OBJ(self);
 
4142
 
 
4143
        if (!PyArg_ParseTuple(args, "O|O:get", &key_ob, &def))
2730
4144
                return NULL;
2731
 
        
2732
 
        if(RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
2733
 
                return pyrna_struct_CreatePyObject(&newptr);
2734
 
        
2735
 
        Py_INCREF(def);
2736
 
        return def;
2737
 
}
2738
 
 
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 )
 
4145
 
 
4146
        if (PyUnicode_Check(key_ob)) {
 
4147
                const char *key = _PyUnicode_AsString(key_ob);
 
4148
 
 
4149
                if (RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
 
4150
                        return pyrna_struct_CreatePyObject(&newptr);
 
4151
        }
 
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);
 
4155
                if (ret) {
 
4156
                        return ret;
 
4157
                }
 
4158
        }
 
4159
        else {
 
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);
 
4163
        }
 
4164
 
 
4165
        return Py_INCREF(def), def;
 
4166
}
 
4167
 
 
4168
PyDoc_STRVAR(pyrna_prop_collection_find_doc,
 
4169
".. method:: find(key)\n"
 
4170
"\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"
 
4173
"\n"
 
4174
"   :arg key: The identifier for the collection member.\n"
 
4175
"   :type key: string\n"
 
4176
"   :return: index of the key.\n"
 
4177
"   :rtype: int\n"
 
4178
);
 
4179
static PyObject *pyrna_prop_collection_find(BPy_PropertyRNA *self, PyObject *key_ob)
 
4180
{
 
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 */
 
4184
 
 
4185
        char name[256], *nameptr;
 
4186
        int namelen;
 
4187
        int i = 0;
 
4188
        int index = -1;
 
4189
 
 
4190
        PYRNA_PROP_CHECK_OBJ(self);
 
4191
 
 
4192
        RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
 
4193
                nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
 
4194
 
 
4195
                if (nameptr) {
 
4196
                        if ((key_len == namelen) && memcmp(nameptr, key, key_len) == 0) {
 
4197
                                index = i;
 
4198
                                break;
 
4199
                        }
 
4200
 
 
4201
                        if (name != nameptr) {
 
4202
                                MEM_freeN(nameptr);
 
4203
                        }
 
4204
                }
 
4205
 
 
4206
                i++;
 
4207
        }
 
4208
        RNA_PROP_END;
 
4209
 
 
4210
        return PyLong_FromSsize_t(index);
 
4211
}
 
4212
 
 
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)
2742
4216
{
2743
4217
        PropertyRNA *prop;
2744
 
        *raw_type= PROP_RAW_UNSET;
2745
 
        *attr_tot= 0;
2746
 
        *attr_signed= FALSE;
 
4218
        *raw_type = PROP_RAW_UNSET;
 
4219
        *attr_tot = 0;
 
4220
        *attr_signed = FALSE;
2747
4221
 
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;
2754
4228
                break;
2755
4229
        }
2756
4230
        RNA_PROP_END;
2757
4231
}
2758
4232
 
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,
2762
4235
 
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
 
4239
                              )
2765
4240
{
2766
4241
#if 0
2767
4242
        int array_tot;
2768
4243
        int target_tot;
2769
4244
#endif
2770
4245
 
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;
2773
4248
 
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");
2776
4251
                return -1;
2777
4252
        }
2778
4253
 
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.
2780
4255
 
2781
 
        if(*tot>0) {
 
4256
        if (*tot > 0) {
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);
2784
4259
 
2785
 
#if 0   // works fine but not strictly needed, we could allow RNA_property_collection_raw_* to do the checks
2786
 
                if((*attr_tot) < 1)
2787
 
                        *attr_tot= 1;
 
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)
 
4262
                        *attr_tot = 1;
2788
4263
 
2789
4264
                if (RNA_property_type(self->prop) == PROP_COLLECTION)
2790
4265
                        array_tot = RNA_property_collection_length(&self->ptr, self->prop);
2792
4267
                        array_tot = RNA_property_array_length(&self->ptr, self->prop);
2793
4268
 
2794
4269
 
2795
 
                target_tot= array_tot * (*attr_tot);
 
4270
                target_tot = array_tot * (*attr_tot);
2796
4271
 
2797
4272
                /* rna_access.c - rna_raw_access(...) uses this same method */
2798
 
                if(target_tot != (*tot)) {
2799
 
                        PyErr_Format( PyExc_TypeError, "foreach_get(attr, sequence) sequence length mismatch given %d, needed %d", *tot, target_tot);
 
4273
                if (target_tot != (*tot)) {
 
4274
                        PyErr_Format(PyExc_TypeError,
 
4275
                                     "foreach_get(attr, sequence) sequence length mismatch given %d, needed %d",
 
4276
                                     *tot, target_tot);
2800
4277
                        return -1;
2801
4278
                }
2802
4279
#endif
2803
4280
        }
2804
4281
 
2805
 
        /* check 'attr_tot' otherwise we dont know if any values were set
2806
 
         * this isnt ideal because it means running on an empty list may fail silently when its not compatible. */
 
4282
        /* check 'attr_tot' otherwise we don't know if any values were set
 
4283
         * this isn't ideal because it means running on an empty list may fail silently when its not compatible. */
2807
4284
        if (*size == 0 && *attr_tot != 0) {
2808
 
                PyErr_SetString( PyExc_AttributeError, "attribute does not support foreach method" );
 
4285
                PyErr_SetString(PyExc_AttributeError, "attribute does not support foreach method");
2809
4286
                return -1;
2810
4287
        }
2811
4288
        return 0;
2813
4290
 
2814
4291
static int foreach_compat_buffer(RawPropertyType raw_type, int attr_signed, const char *format)
2815
4292
{
2816
 
        char f = format ? *format:'B'; /* B is assumed when not set */
 
4293
        char f = format ? *format : 'B'; /* B is assumed when not set */
2817
4294
 
2818
 
        switch(raw_type) {
2819
 
        case PROP_RAW_CHAR:
2820
 
                if (attr_signed)        return (f=='b') ? 1:0;
2821
 
                else                            return (f=='B') ? 1:0;
2822
 
        case PROP_RAW_SHORT:
2823
 
                if (attr_signed)        return (f=='h') ? 1:0;
2824
 
                else                            return (f=='H') ? 1:0;
2825
 
        case PROP_RAW_INT:
2826
 
                if (attr_signed)        return (f=='i') ? 1:0;
2827
 
                else                            return (f=='I') ? 1:0;
2828
 
        case PROP_RAW_FLOAT:
2829
 
                return (f=='f') ? 1:0;
2830
 
        case PROP_RAW_DOUBLE:
2831
 
                return (f=='d') ? 1:0;
2832
 
        case PROP_RAW_UNSET:
2833
 
                return 0;
 
4295
        switch (raw_type) {
 
4296
                case PROP_RAW_CHAR:
 
4297
                        if (attr_signed) return (f == 'b') ? 1 : 0;
 
4298
                        else             return (f == 'B') ? 1 : 0;
 
4299
                case PROP_RAW_SHORT:
 
4300
                        if (attr_signed) return (f == 'h') ? 1 : 0;
 
4301
                        else             return (f == 'H') ? 1 : 0;
 
4302
                case PROP_RAW_INT:
 
4303
                        if (attr_signed) return (f == 'i') ? 1 : 0;
 
4304
                        else             return (f == 'I') ? 1 : 0;
 
4305
                case PROP_RAW_FLOAT:
 
4306
                        return (f == 'f') ? 1 : 0;
 
4307
                case PROP_RAW_DOUBLE:
 
4308
                        return (f == 'd') ? 1 : 0;
 
4309
                case PROP_RAW_UNSET:
 
4310
                        return 0;
2834
4311
        }
2835
4312
 
2836
4313
        return 0;
2839
4316
static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
2840
4317
{
2841
4318
        PyObject *item = NULL;
2842
 
        int i=0, ok=0, buffer_is_compat;
2843
 
        void *array= NULL;
 
4319
        int i = 0, ok = 0, buffer_is_compat;
 
4320
        void *array = NULL;
2844
4321
 
2845
4322
        /* get/set both take the same args currently */
2846
 
        char *attr;
 
4323
        const char *attr;
2847
4324
        PyObject *seq;
2848
4325
        int tot, size, attr_tot, attr_signed;
2849
4326
        RawPropertyType raw_type;
2850
4327
 
2851
 
        if(foreach_parse_args(self, args,    &attr, &seq, &tot, &size, &raw_type, &attr_tot, &attr_signed) < 0)
 
4328
        if (foreach_parse_args(self, args, &attr, &seq, &tot, &size, &raw_type, &attr_tot, &attr_signed) < 0)
2852
4329
                return NULL;
2853
4330
 
2854
 
        if(tot==0)
 
4331
        if (tot == 0)
2855
4332
                Py_RETURN_NONE;
2856
4333
 
2857
4334
 
2858
4335
 
2859
 
        if(set) { /* get the array from python */
 
4336
        if (set) { /* get the array from python */
2860
4337
                buffer_is_compat = FALSE;
2861
 
                if(PyObject_CheckBuffer(seq)) {
 
4338
                if (PyObject_CheckBuffer(seq)) {
2862
4339
                        Py_buffer buf;
2863
4340
                        PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
2864
4341
 
2866
4343
 
2867
4344
                        buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
2868
4345
 
2869
 
                        if(buffer_is_compat) {
 
4346
                        if (buffer_is_compat) {
2870
4347
                                ok = RNA_property_collection_raw_set(NULL, &self->ptr, self->prop, attr, buf.buf, raw_type, tot);
2871
4348
                        }
2872
4349
 
2874
4351
                }
2875
4352
 
2876
4353
                /* could not use the buffer, fallback to sequence */
2877
 
                if(!buffer_is_compat) {
2878
 
                        array= PyMem_Malloc(size * tot);
 
4354
                if (!buffer_is_compat) {
 
4355
                        array = PyMem_Malloc(size * tot);
2879
4356
 
2880
 
                        for( ; i<tot; i++) {
2881
 
                                item= PySequence_GetItem(seq, i);
2882
 
                                switch(raw_type) {
2883
 
                                case PROP_RAW_CHAR:
2884
 
                                        ((char *)array)[i]= (char)PyLong_AsSsize_t(item);
2885
 
                                        break;
2886
 
                                case PROP_RAW_SHORT:
2887
 
                                        ((short *)array)[i]= (short)PyLong_AsSsize_t(item);
2888
 
                                        break;
2889
 
                                case PROP_RAW_INT:
2890
 
                                        ((int *)array)[i]= (int)PyLong_AsSsize_t(item);
2891
 
                                        break;
2892
 
                                case PROP_RAW_FLOAT:
2893
 
                                        ((float *)array)[i]= (float)PyFloat_AsDouble(item);
2894
 
                                        break;
2895
 
                                case PROP_RAW_DOUBLE:
2896
 
                                        ((double *)array)[i]= (double)PyFloat_AsDouble(item);
2897
 
                                        break;
2898
 
                                case PROP_RAW_UNSET:
2899
 
                                        /* should never happen */
2900
 
                                        break;
 
4357
                        for ( ; i < tot; i++) {
 
4358
                                item = PySequence_GetItem(seq, i);
 
4359
                                switch (raw_type) {
 
4360
                                        case PROP_RAW_CHAR:
 
4361
                                                ((char *)array)[i] = (char)PyLong_AsLong(item);
 
4362
                                                break;
 
4363
                                        case PROP_RAW_SHORT:
 
4364
                                                ((short *)array)[i] = (short)PyLong_AsLong(item);
 
4365
                                                break;
 
4366
                                        case PROP_RAW_INT:
 
4367
                                                ((int *)array)[i] = (int)PyLong_AsLong(item);
 
4368
                                                break;
 
4369
                                        case PROP_RAW_FLOAT:
 
4370
                                                ((float *)array)[i] = (float)PyFloat_AsDouble(item);
 
4371
                                                break;
 
4372
                                        case PROP_RAW_DOUBLE:
 
4373
                                                ((double *)array)[i] = (double)PyFloat_AsDouble(item);
 
4374
                                                break;
 
4375
                                        case PROP_RAW_UNSET:
 
4376
                                                /* should never happen */
 
4377
                                                BLI_assert(!"Invalid array type - set");
 
4378
                                                break;
2901
4379
                                }
2902
4380
 
2903
4381
                                Py_DECREF(item);
2908
4386
        }
2909
4387
        else {
2910
4388
                buffer_is_compat = FALSE;
2911
 
                if(PyObject_CheckBuffer(seq)) {
 
4389
                if (PyObject_CheckBuffer(seq)) {
2912
4390
                        Py_buffer buf;
2913
4391
                        PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
2914
4392
 
2916
4394
 
2917
4395
                        buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
2918
4396
 
2919
 
                        if(buffer_is_compat) {
 
4397
                        if (buffer_is_compat) {
2920
4398
                                ok = RNA_property_collection_raw_get(NULL, &self->ptr, self->prop, attr, buf.buf, raw_type, tot);
2921
4399
                        }
2922
4400
 
2924
4402
                }
2925
4403
 
2926
4404
                /* could not use the buffer, fallback to sequence */
2927
 
                if(!buffer_is_compat) {
2928
 
                        array= PyMem_Malloc(size * tot);
 
4405
                if (!buffer_is_compat) {
 
4406
                        array = PyMem_Malloc(size * tot);
2929
4407
 
2930
4408
                        ok = RNA_property_collection_raw_get(NULL, &self->ptr, self->prop, attr, array, raw_type, tot);
2931
4409
 
2932
 
                        if(!ok) i= tot; /* skip the loop */
2933
 
 
2934
 
                        for( ; i<tot; i++) {
2935
 
 
2936
 
                                switch(raw_type) {
2937
 
                                case PROP_RAW_CHAR:
2938
 
                                        item= PyLong_FromSsize_t(  (Py_ssize_t) ((char *)array)[i]  );
2939
 
                                        break;
2940
 
                                case PROP_RAW_SHORT:
2941
 
                                        item= PyLong_FromSsize_t(  (Py_ssize_t) ((short *)array)[i]  );
2942
 
                                        break;
2943
 
                                case PROP_RAW_INT:
2944
 
                                        item= PyLong_FromSsize_t(  (Py_ssize_t) ((int *)array)[i]  );
2945
 
                                        break;
2946
 
                                case PROP_RAW_FLOAT:
2947
 
                                        item= PyFloat_FromDouble(  (double) ((float *)array)[i]  );
2948
 
                                        break;
2949
 
                                case PROP_RAW_DOUBLE:
2950
 
                                        item= PyFloat_FromDouble(  (double) ((double *)array)[i]  );
2951
 
                                        break;
2952
 
                                case PROP_RAW_UNSET:
2953
 
                                        /* should never happen */
2954
 
                                        break;
 
4410
                        if (!ok) i = tot;  /* skip the loop */
 
4411
 
 
4412
                        for ( ; i < tot; i++) {
 
4413
 
 
4414
                                switch (raw_type) {
 
4415
                                        case PROP_RAW_CHAR:
 
4416
                                                item = PyLong_FromSsize_t((Py_ssize_t) ((char *)array)[i]);
 
4417
                                                break;
 
4418
                                        case PROP_RAW_SHORT:
 
4419
                                                item = PyLong_FromSsize_t((Py_ssize_t) ((short *)array)[i]);
 
4420
                                                break;
 
4421
                                        case PROP_RAW_INT:
 
4422
                                                item = PyLong_FromSsize_t((Py_ssize_t) ((int *)array)[i]);
 
4423
                                                break;
 
4424
                                        case PROP_RAW_FLOAT:
 
4425
                                                item = PyFloat_FromDouble((double) ((float *)array)[i]);
 
4426
                                                break;
 
4427
                                        case PROP_RAW_DOUBLE:
 
4428
                                                item = PyFloat_FromDouble((double) ((double *)array)[i]);
 
4429
                                                break;
 
4430
                                        default: /* PROP_RAW_UNSET */
 
4431
                                                /* should never happen */
 
4432
                                                BLI_assert(!"Invalid array type - get");
 
4433
                                                item = Py_None;
 
4434
                                                Py_INCREF(item);
 
4435
                                                break;
2955
4436
                                }
2956
4437
 
2957
4438
                                PySequence_SetItem(seq, i, item);
2960
4441
                }
2961
4442
        }
2962
4443
 
2963
 
        if(array)
 
4444
        if (array)
2964
4445
                PyMem_Free(array);
2965
4446
 
2966
 
        if(PyErr_Occurred()) {
 
4447
        if (PyErr_Occurred()) {
2967
4448
                /* Maybe we could make our own error */
2968
4449
                PyErr_Print();
2969
 
                PyErr_SetString(PyExc_SystemError, "could not access the py sequence");
 
4450
                PyErr_SetString(PyExc_TypeError, "couldn't access the py sequence");
2970
4451
                return NULL;
2971
4452
        }
2972
4453
        if (!ok) {
2973
 
                PyErr_SetString(PyExc_SystemError, "internal error setting the array");
 
4454
                PyErr_SetString(PyExc_RuntimeError, "internal error setting the array");
2974
4455
                return NULL;
2975
4456
        }
2976
4457
 
2977
4458
        Py_RETURN_NONE;
2978
4459
}
2979
4460
 
2980
 
static PyObject *pyrna_prop_foreach_get(BPy_PropertyRNA *self, PyObject *args)
 
4461
PyDoc_STRVAR(pyrna_prop_collection_foreach_get_doc,
 
4462
".. method:: foreach_get(attr, seq)\n"
 
4463
"\n"
 
4464
"   This is a function to give fast access to attributes within a collection.\n"
 
4465
"\n"
 
4466
"   .. code-block:: python\n"
 
4467
"\n"
 
4468
"      collection.foreach_get(someseq, attr)\n"
 
4469
"\n"
 
4470
"      # Python equivalent\n"
 
4471
"      for i in range(len(seq)): someseq[i] = getattr(collection, attr)\n"
 
4472
"\n"
 
4473
);
 
4474
static PyObject *pyrna_prop_collection_foreach_get(BPy_PropertyRNA *self, PyObject *args)
2981
4475
{
 
4476
        PYRNA_PROP_CHECK_OBJ(self);
 
4477
 
2982
4478
        return foreach_getset(self, args, 0);
2983
4479
}
2984
4480
 
2985
 
static  PyObject *pyrna_prop_foreach_set(BPy_PropertyRNA *self, PyObject *args)
 
4481
PyDoc_STRVAR(pyrna_prop_collection_foreach_set_doc,
 
4482
".. method:: foreach_set(attr, seq)\n"
 
4483
"\n"
 
4484
"   This is a function to give fast access to attributes within a collection.\n"
 
4485
"\n"
 
4486
"   .. code-block:: python\n"
 
4487
"\n"
 
4488
"      collection.foreach_set(seq, attr)\n"
 
4489
"\n"
 
4490
"      # Python equivalent\n"
 
4491
"      for i in range(len(seq)): setattr(collection[i], attr, seq[i])\n"
 
4492
"\n"
 
4493
);
 
4494
static PyObject *pyrna_prop_collection_foreach_set(BPy_PropertyRNA *self, PyObject *args)
2986
4495
{
 
4496
        PYRNA_PROP_CHECK_OBJ(self);
 
4497
 
2987
4498
        return foreach_getset(self, args, 1);
2988
4499
}
2989
4500
 
2990
4501
/* A bit of a kludge, make a list out of a collection or array,
2991
4502
 * then return the lists iter function, not especially fast but convenient for now */
2992
 
PyObject *pyrna_prop_array_iter(BPy_PropertyRNA *self)
 
4503
static PyObject *pyrna_prop_array_iter(BPy_PropertyArrayRNA *self)
2993
4504
{
2994
4505
        /* Try get values from a collection */
2995
4506
        PyObject *ret;
2996
 
        PyObject *iter= NULL;
2997
 
        int len= pyrna_prop_array_length(self);
 
4507
        PyObject *iter = NULL;
 
4508
        int len;
 
4509
 
 
4510
        PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
 
4511
 
 
4512
        len = pyrna_prop_array_length(self);
2998
4513
        ret = pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len);
2999
 
        
 
4514
 
3000
4515
        /* we know this is a list so no need to PyIter_Check
3001
4516
         * otherwise it could be NULL (unlikely) if conversion failed */
3002
 
        if(ret) {
 
4517
        if (ret) {
3003
4518
                iter = PyObject_GetIter(ret);
3004
4519
                Py_DECREF(ret);
3005
4520
        }
3007
4522
        return iter;
3008
4523
}
3009
4524
 
3010
 
PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self)
 
4525
static PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self);
 
4526
 
 
4527
#ifndef USE_PYRNA_ITER
 
4528
static PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self)
3011
4529
{
3012
4530
        /* Try get values from a collection */
3013
4531
        PyObject *ret;
3014
 
        PyObject *iter= NULL;
3015
 
        ret = pyrna_prop_values(self);
3016
 
        
 
4532
        PyObject *iter = NULL;
 
4533
        ret = pyrna_prop_collection_values(self);
 
4534
 
3017
4535
        /* we know this is a list so no need to PyIter_Check
3018
4536
         * otherwise it could be NULL (unlikely) if conversion failed */
3019
 
        if(ret) {
 
4537
        if (ret) {
3020
4538
                iter = PyObject_GetIter(ret);
3021
4539
                Py_DECREF(ret);
3022
4540
        }
3023
4541
 
3024
4542
        return iter;
3025
4543
}
 
4544
#endif /* # !USE_PYRNA_ITER */
3026
4545
 
3027
4546
static struct PyMethodDef pyrna_struct_methods[] = {
3028
4547
 
3035
4554
 
3036
4555
        {"as_pointer", (PyCFunction)pyrna_struct_as_pointer, METH_NOARGS, pyrna_struct_as_pointer_doc},
3037
4556
 
3038
 
        {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS|METH_KEYWORDS, pyrna_struct_keyframe_insert_doc},
3039
 
        {"keyframe_delete", (PyCFunction)pyrna_struct_keyframe_delete, METH_VARARGS|METH_KEYWORDS, pyrna_struct_keyframe_delete_doc},
 
4557
        /* bpy_rna_anim.c */
 
4558
        {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS | METH_KEYWORDS, pyrna_struct_keyframe_insert_doc},
 
4559
        {"keyframe_delete", (PyCFunction)pyrna_struct_keyframe_delete, METH_VARARGS | METH_KEYWORDS, pyrna_struct_keyframe_delete_doc},
3040
4560
        {"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, pyrna_struct_driver_add_doc},
3041
4561
        {"driver_remove", (PyCFunction)pyrna_struct_driver_remove, METH_VARARGS, pyrna_struct_driver_remove_doc},
 
4562
 
3042
4563
        {"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, pyrna_struct_is_property_set_doc},
3043
4564
        {"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, pyrna_struct_is_property_hidden_doc},
3044
 
        {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, pyrna_struct_path_resolve_doc},
 
4565
        {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_VARARGS, pyrna_struct_path_resolve_doc},
3045
4566
        {"path_from_id", (PyCFunction)pyrna_struct_path_from_id, METH_VARARGS, pyrna_struct_path_from_id_doc},
3046
 
        {"recast_type", (PyCFunction)pyrna_struct_recast_type, METH_NOARGS, pyrna_struct_recast_type_doc},
 
4567
        {"type_recast", (PyCFunction)pyrna_struct_type_recast, METH_NOARGS, pyrna_struct_type_recast_doc},
3047
4568
        {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
3048
4569
 
3049
 
        /* experemental */
 
4570
        /* experimental */
3050
4571
        {"callback_add", (PyCFunction)pyrna_callback_add, METH_VARARGS, NULL},
3051
4572
        {"callback_remove", (PyCFunction)pyrna_callback_remove, METH_VARARGS, NULL},
3052
 
 
3053
4573
        {NULL, NULL, 0, NULL}
3054
4574
};
3055
4575
 
3060
4580
};
3061
4581
 
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}
3066
4584
};
3067
4585
 
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},
3071
 
 
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},
3075
 
        
3076
 
        {"get", (PyCFunction)pyrna_prop_get, METH_VARARGS, NULL},
3077
 
 
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},
 
4589
 
 
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},
 
4593
 
 
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}
 
4597
};
 
4598
 
 
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}
3083
4605
};
3084
4606
 
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))
 
4610
{
 
4611
        if (PyTuple_GET_SIZE(args) == 1) {
 
4612
                BPy_StructRNA *base = (BPy_StructRNA *)PyTuple_GET_ITEM(args, 0);
 
4613
                if (Py_TYPE(base) == type) {
 
4614
                        Py_INCREF(base);
 
4615
                        return (PyObject *)base;
 
4616
                }
 
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:
 
4620
                         *
 
4621
                         *  >>> class MyObSubclass(bpy.types.Object):
 
4622
                         *  ...     def test_func(self):
 
4623
                         *  ...         print(100)
 
4624
                         *  ...
 
4625
                         *  >>> myob = MyObSubclass(bpy.context.object)
 
4626
                         *  >>> myob.test_func()
 
4627
                         *  100
 
4628
                         *
 
4629
                         * Keep this since it could be useful.
 
4630
                         */
 
4631
                        BPy_StructRNA *ret;
 
4632
                        if ((ret = (BPy_StructRNA *)type->tp_alloc(type, 0))) {
 
4633
                                ret->ptr = base->ptr;
 
4634
                        }
 
4635
                        /* pass on exception & NULL if tp_alloc fails */
 
4636
                        return (PyObject *)ret;
 
4637
                }
3088
4638
 
3089
 
        BPy_StructRNA *base = NULL;
3090
 
        
3091
 
        if (!PyArg_ParseTuple(args, "O!:bpy_struct.__new__", &pyrna_struct_Type, &base))
3092
 
                return NULL;
3093
 
        
3094
 
        if (type == &pyrna_struct_Type) {
3095
 
                return pyrna_struct_CreatePyObject(&base->ptr);
3096
 
        } else {
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",
 
4642
                             type->tp_name);
 
4643
                return NULL;
 
4644
        }
 
4645
        else {
 
4646
                PyErr_Format(PyExc_TypeError,
 
4647
                             "bpy_struct.__new__(type): expected a single argument");
 
4648
                return NULL;
3100
4649
        }
3101
4650
}
3102
4651
 
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))
 
4655
{
 
4656
        BPy_PropertyRNA *base;
3106
4657
 
3107
 
        BPy_PropertyRNA *base = NULL;
3108
 
        
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))
3110
4659
                return NULL;
3111
 
        
3112
 
        if (ELEM3(type, &pyrna_prop_Type, &pyrna_prop_array_Type, &pyrna_prop_collection_Type)) {
3113
 
                return pyrna_prop_CreatePyObject(&base->ptr, base->prop);
3114
 
        } else {
 
4660
 
 
4661
        if (type == Py_TYPE(base)) {
 
4662
                Py_INCREF(base);
 
4663
                return (PyObject *)base;
 
4664
        }
 
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;
3119
4670
        }
 
4671
        else {
 
4672
                PyErr_Format(PyExc_TypeError,
 
4673
                             "bpy_prop.__new__(type): type '%.200s' is not a subtype of bpy_prop",
 
4674
                             type->tp_name);
 
4675
                return NULL;
 
4676
        }
3120
4677
}
3121
4678
 
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)
3123
4680
{
3124
4681
        PyObject *ret;
3125
 
        int type = RNA_property_type(prop);
3126
 
        int flag = RNA_property_flag(prop);
3127
 
        int a;
 
4682
        const int type = RNA_property_type(prop);
 
4683
        const int flag = RNA_property_flag(prop);
3128
4684
 
3129
 
        if(RNA_property_array_check(ptr, prop)) {
3130
 
                int len;
 
4685
        if (RNA_property_array_check(prop)) {
 
4686
                int a, len;
3131
4687
 
3132
4688
                if (flag & PROP_DYNAMIC) {
3133
 
                        len= RNA_parameter_length_get_data(parms, prop, data);
3134
 
 
3135
 
                        data= *((void **)data);
 
4689
                        ParameterDynAlloc *data_alloc = data;
 
4690
                        len = data_alloc->array_tot;
 
4691
                        data = data_alloc->array;
3136
4692
                }
3137
4693
                else
3138
 
                        len= RNA_property_array_length(ptr, prop);
 
4694
                        len = RNA_property_array_length(ptr, prop);
3139
4695
 
3140
4696
                /* resolve the array from a new pytype */
3141
4697
 
3142
4698
                /* kazanbas: TODO make multidim sequences here */
3143
4699
 
3144
4700
                switch (type) {
3145
 
                case PROP_BOOLEAN:
3146
 
                        ret = PyTuple_New(len);
3147
 
                        for(a=0; a<len; a++)
3148
 
                                PyTuple_SET_ITEM(ret, a, PyBool_FromLong( ((int*)data)[a] ));
3149
 
                        break;
3150
 
                case PROP_INT:
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] ));
3154
 
                        break;
3155
 
                case PROP_FLOAT:
3156
 
                        switch(RNA_property_subtype(prop)) {
3157
 
                                case PROP_ALL_VECTOR_SUBTYPES:
3158
 
                                        ret= newVectorObject(data, len, Py_NEW, NULL);
3159
 
                                        break;
3160
 
                                case PROP_MATRIX:
3161
 
                                        if(len==16) {
3162
 
                                                ret= newMatrixObject(data, 4, 4, Py_NEW, NULL);
3163
 
                                                break;
3164
 
                                        }
3165
 
                                        else if (len==9) {
3166
 
                                                ret= newMatrixObject(data, 3, 3, Py_NEW, NULL);
3167
 
                                                break;
3168
 
                                        }
3169
 
                                        /* pass through */
3170
 
                                default:
3171
 
                                        ret = PyTuple_New(len);
3172
 
                                        for(a=0; a<len; a++)
3173
 
                                                PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble( ((float*)data)[a] ));
 
4701
                        case PROP_BOOLEAN:
 
4702
                                ret = PyTuple_New(len);
 
4703
                                for (a = 0; a < len; a++)
 
4704
                                        PyTuple_SET_ITEM(ret, a, PyBool_FromLong(((int *)data)[a]));
 
4705
                                break;
 
4706
                        case PROP_INT:
 
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]));
 
4710
                                break;
 
4711
                        case PROP_FLOAT:
 
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);
 
4716
                                                break;
 
4717
                                        case PROP_MATRIX:
 
4718
                                                if (len == 16) {
 
4719
                                                        ret = Matrix_CreatePyObject(data, 4, 4, Py_NEW, NULL);
 
4720
                                                        break;
 
4721
                                                }
 
4722
                                                else if (len == 9) {
 
4723
                                                        ret = Matrix_CreatePyObject(data, 3, 3, Py_NEW, NULL);
 
4724
                                                        break;
 
4725
                                                }
 
4726
                                                /* pass through */
 
4727
#endif
 
4728
                                        default:
 
4729
                                                ret = PyTuple_New(len);
 
4730
                                                for (a = 0; a < len; a++)
 
4731
                                                        PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble(((float *)data)[a]));
3174
4732
 
3175
 
                        }
3176
 
                        break;
3177
 
                default:
3178
 
                        PyErr_Format(PyExc_TypeError, "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)", type);
3179
 
                        ret = NULL;
3180
 
                        break;
 
4733
                                }
 
4734
                                break;
 
4735
                        default:
 
4736
                                PyErr_Format(PyExc_TypeError,
 
4737
                                             "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)",
 
4738
                                             type);
 
4739
                                ret = NULL;
 
4740
                                break;
3181
4741
                }
3182
4742
        }
3183
4743
        else {
3184
4744
                /* see if we can coorce into a python type - PropertyType */
3185
4745
                switch (type) {
3186
 
                case PROP_BOOLEAN:
3187
 
                        ret = PyBool_FromLong( *(int*)data );
3188
 
                        break;
3189
 
                case PROP_INT:
3190
 
                        ret = PyLong_FromSsize_t( (Py_ssize_t)*(int*)data );
3191
 
                        break;
3192
 
                case PROP_FLOAT:
3193
 
                        ret = PyFloat_FromDouble( *(float*)data );
3194
 
                        break;
3195
 
                case PROP_STRING:
3196
 
                {
3197
 
                        if(flag & PROP_THICK_WRAP)
3198
 
                                ret = PyUnicode_FromString( (char*)data );
3199
 
                        else
3200
 
                                ret = PyUnicode_FromString( *(char**)data );
3201
 
                        break;
3202
 
                }
3203
 
                case PROP_ENUM:
3204
 
                {
3205
 
                        ret= pyrna_enum_to_py(ptr, prop, *(int*)data);
3206
 
                        break;
3207
 
                }
3208
 
                case PROP_POINTER:
3209
 
                {
3210
 
                        PointerRNA newptr;
3211
 
                        StructRNA *type= RNA_property_pointer_type(ptr, prop);
3212
 
 
3213
 
                        if(flag & PROP_RNAPTR) {
3214
 
                                /* in this case we get the full ptr */
3215
 
                                newptr= *(PointerRNA*)data;
3216
 
                        }
3217
 
                        else {
3218
 
                                if(RNA_struct_is_ID(type)) {
3219
 
                                        RNA_id_pointer_create(*(void**)data, &newptr);
3220
 
                                } else {
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);
3226
 
                                }
3227
 
                        }
3228
 
 
3229
 
                        if (newptr.data) {
3230
 
                                ret = pyrna_struct_CreatePyObject(&newptr);
3231
 
                        } else {
3232
 
                                ret = Py_None;
3233
 
                                Py_INCREF(ret);
3234
 
                        }
3235
 
                        break;
3236
 
                }
3237
 
                case PROP_COLLECTION:
3238
 
                {
3239
 
                        ListBase *lb= (ListBase*)data;
3240
 
                        CollectionPointerLink *link;
3241
 
                        PyObject *linkptr;
3242
 
 
3243
 
                        ret = PyList_New(0);
3244
 
 
3245
 
                        for(link=lb->first; link; link=link->next) {
3246
 
                                linkptr= pyrna_struct_CreatePyObject(&link->ptr);
3247
 
                                PyList_Append(ret, linkptr);
3248
 
                                Py_DECREF(linkptr);
3249
 
                        }
3250
 
 
3251
 
                        break;
3252
 
                }
3253
 
                default:
3254
 
                        PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_param_to_py)", type);
3255
 
                        ret = NULL;
3256
 
                        break;
 
4746
                        case PROP_BOOLEAN:
 
4747
                                ret = PyBool_FromLong(*(int *)data);
 
4748
                                break;
 
4749
                        case PROP_INT:
 
4750
                                ret = PyLong_FromSsize_t((Py_ssize_t)*(int *)data);
 
4751
                                break;
 
4752
                        case PROP_FLOAT:
 
4753
                                ret = PyFloat_FromDouble(*(float *)data);
 
4754
                                break;
 
4755
                        case PROP_STRING:
 
4756
                        {
 
4757
                                char *data_ch;
 
4758
                                PyObject *value_coerce = NULL;
 
4759
                                const int subtype = RNA_property_subtype(prop);
 
4760
 
 
4761
                                if (flag & PROP_THICK_WRAP)
 
4762
                                        data_ch = (char *)data;
 
4763
                                else
 
4764
                                        data_ch = *(char **)data;
 
4765
 
 
4766
#ifdef USE_STRING_COERCE
 
4767
                                if (subtype == PROP_BYTESTRING) {
 
4768
                                        ret = PyBytes_FromString(data_ch);
 
4769
                                }
 
4770
                                else if (ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
 
4771
                                        ret = PyC_UnicodeFromByte(data_ch);
 
4772
                                }
 
4773
                                else {
 
4774
                                        ret = PyUnicode_FromString(data_ch);
 
4775
                                }
 
4776
#else
 
4777
                                if (subtype == PROP_BYTESTRING) {
 
4778
                                        ret = PyBytes_FromString(buf);
 
4779
                                }
 
4780
                                else {
 
4781
                                        ret = PyUnicode_FromString(data_ch);
 
4782
                                }
 
4783
#endif
 
4784
 
 
4785
#ifdef USE_STRING_COERCE
 
4786
                                Py_XDECREF(value_coerce);
 
4787
#endif
 
4788
 
 
4789
                                break;
 
4790
                        }
 
4791
                        case PROP_ENUM:
 
4792
                        {
 
4793
                                ret = pyrna_enum_to_py(ptr, prop, *(int *)data);
 
4794
                                break;
 
4795
                        }
 
4796
                        case PROP_POINTER:
 
4797
                        {
 
4798
                                PointerRNA newptr;
 
4799
                                StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
 
4800
 
 
4801
                                if (flag & PROP_RNAPTR) {
 
4802
                                        /* in this case we get the full ptr */
 
4803
                                        newptr = *(PointerRNA *)data;
 
4804
                                }
 
4805
                                else {
 
4806
                                        if (RNA_struct_is_ID(ptype)) {
 
4807
                                                RNA_id_pointer_create(*(void **)data, &newptr);
 
4808
                                        }
 
4809
                                        else {
 
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);
 
4815
                                        }
 
4816
                                }
 
4817
 
 
4818
                                if (newptr.data) {
 
4819
                                        ret = pyrna_struct_CreatePyObject(&newptr);
 
4820
                                }
 
4821
                                else {
 
4822
                                        ret = Py_None;
 
4823
                                        Py_INCREF(ret);
 
4824
                                }
 
4825
                                break;
 
4826
                        }
 
4827
                        case PROP_COLLECTION:
 
4828
                        {
 
4829
                                ListBase *lb = (ListBase *)data;
 
4830
                                CollectionPointerLink *link;
 
4831
                                PyObject *linkptr;
 
4832
 
 
4833
                                ret = PyList_New(0);
 
4834
 
 
4835
                                for (link = lb->first; link; link = link->next) {
 
4836
                                        linkptr = pyrna_struct_CreatePyObject(&link->ptr);
 
4837
                                        PyList_Append(ret, linkptr);
 
4838
                                        Py_DECREF(linkptr);
 
4839
                                }
 
4840
 
 
4841
                                break;
 
4842
                        }
 
4843
                        default:
 
4844
                                PyErr_Format(PyExc_TypeError,
 
4845
                                             "RNA Error: unknown type \"%d\" (pyrna_param_to_py)",
 
4846
                                             type);
 
4847
                                ret = NULL;
 
4848
                                break;
3257
4849
                }
3258
4850
        }
3259
4851
 
3260
4852
        return ret;
3261
4853
}
3262
4854
 
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)
 
4859
{
 
4860
        PyObject *key = NULL;
 
4861
        Py_ssize_t pos = 0;
 
4862
        PyObject *value = NULL;
 
4863
 
 
4864
        while (PyDict_Next(dict, &pos, &key, &value)) {
 
4865
                if (PyUnicode_Check(key)) {
 
4866
                        if (strcmp(key_lookup, _PyUnicode_AsString(key)) == 0) {
 
4867
                                return value;
 
4868
                        }
 
4869
                }
 
4870
        }
 
4871
 
 
4872
        return NULL;
 
4873
}
 
4874
 
 
4875
static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject *kw)
3264
4876
{
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;
3268
4880
 
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;
3276
 
 
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;
 
4887
 
 
4888
        PropertyRNA *pret_single = NULL;
 
4889
        void *retdata_single = NULL;
 
4890
 
 
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
 
4894
 
 
4895
#ifdef DEBUG_STRING_FREE
 
4896
        PyObject *string_free_ls = PyList_New(0);
 
4897
#endif
3279
4898
 
3280
4899
        /* Should never happen but it does in rare cases */
3281
 
        if(self_ptr==NULL) {
 
4900
        BLI_assert(self_ptr != NULL);
 
4901
 
 
4902
        if (self_ptr == NULL) {
3282
4903
                PyErr_SetString(PyExc_RuntimeError, "rna functions internal rna pointer is NULL, this is a bug. aborting");
3283
4904
                return NULL;
3284
4905
        }
3285
 
        
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));
 
4906
 
 
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));
3288
4911
                return NULL;
3289
4912
        }
3290
 
        
 
4913
 
 
4914
        /* for testing */
 
4915
#if 0
 
4916
        {
 
4917
                const char *fn;
 
4918
                int lineno;
 
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);
 
4922
        }
 
4923
#endif
 
4924
 
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);
3295
4929
 
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;
3298
4932
 
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);
3302
 
        ret_len= 0;
 
4935
        parms_len = RNA_parameter_list_arg_count(&parms);
 
4936
        ret_len = 0;
3303
4937
 
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);
3307
 
                err= -1;
 
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);
 
4944
                err = -1;
3308
4945
        }
3309
4946
 
3310
4947
        /* parse function parameters */
3311
 
        for (i= 0; iter.valid && err==0; RNA_parameter_list_next(&iter)) {
3312
 
                parm= iter.parm;
3313
 
                flag= RNA_property_flag(parm);
 
4948
        for (i = 0; iter.valid && err == 0; RNA_parameter_list_next(&iter)) {
 
4949
                parm = iter.parm;
 
4950
                flag = RNA_property_flag(parm);
3314
4951
 
3315
4952
                /* only useful for single argument returns, we'll need another list loop for multiple */
3316
4953
                if (flag & PROP_OUTPUT) {
3317
4954
                        ret_len++;
3318
 
                        if (pret_single==NULL) {
3319
 
                                pret_single= parm;
3320
 
                                retdata_single= iter.data;
 
4955
                        if (pret_single == NULL) {
 
4956
                                pret_single = parm;
 
4957
                                retdata_single = iter.data;
3321
4958
                        }
3322
4959
 
3323
4960
                        continue;
3324
4961
                }
3325
4962
 
3326
 
                parm_id= RNA_property_identifier(parm);
3327
 
                item= NULL;
 
4963
                item = NULL;
3328
4964
 
3329
4965
                if (i < pyargs_len) {
3330
 
                        item= PyTuple_GET_ITEM(args, i);
3331
 
                        i++;
3332
 
 
3333
 
                        kw_arg= FALSE;
 
4966
                        item = PyTuple_GET_ITEM(args, i);
 
4967
                        kw_arg = FALSE;
3334
4968
                }
3335
4969
                else if (kw != NULL) {
3336
 
                        item= PyDict_GetItemString(kw, parm_id);  /* borrow ref */
3337
 
                        if(item)
3338
 
                                kw_tot++; /* make sure invalid keywords are not given */
 
4970
#if 0
 
4971
                        item = PyDict_GetItemString(kw, RNA_property_identifier(parm)); /* borrow ref */
 
4972
#else
 
4973
                        item = small_dict_get_item_string(kw, RNA_property_identifier(parm)); /* borrow ref */
 
4974
#endif
 
4975
                        if (item)
 
4976
                                kw_tot++;  /* make sure invalid keywords are not given */
3339
4977
 
3340
 
                        kw_arg= TRUE;
 
4978
                        kw_arg = TRUE;
3341
4979
                }
3342
4980
 
3343
 
                if (item==NULL) {
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);
3346
 
                                err= -1;
 
4981
                i++; /* current argument */
 
4982
 
 
4983
                if (item == NULL) {
 
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));
 
4990
                                err = -1;
3347
4991
                                break;
3348
4992
                        }
3349
 
                        else /* PyDict_GetItemString wont raise an error */
 
4993
                        else { /* PyDict_GetItemString wont raise an error */
3350
4994
                                continue;
3351
 
                }
3352
 
 
3353
 
                err= pyrna_py_to_prop(&funcptr, parm, &parms, iter.data, item, "");
3354
 
 
3355
 
                if(err!=0) {
3356
 
                        /* the error generated isnt that useful, so generate it again with a useful prefix
 
4995
                        }
 
4996
                }
 
4997
 
 
4998
#ifdef DEBUG_STRING_FREE
 
4999
                if (item) {
 
5000
                        if (PyUnicode_Check(item)) {
 
5001
                                item = PyUnicode_FromString(_PyUnicode_AsString(item));
 
5002
                                PyList_Append(string_free_ls, item);
 
5003
                                Py_DECREF(item);
 
5004
                        }
 
5005
                }
 
5006
#endif
 
5007
                err = pyrna_py_to_prop(&funcptr, parm, iter.data, item, "");
 
5008
 
 
5009
                if (err != 0) {
 
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 */
3360
5014
 
3361
 
                        if(kw_arg==TRUE)
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);
 
5015
                        if (kw_arg == TRUE)
 
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));
3363
5021
                        else
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),
 
5026
                                             i,
 
5027
                                             RNA_property_identifier(parm));
3365
5028
 
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);
3367
5030
 
3368
5031
                        break;
3369
5032
                }
3370
5033
        }
3371
 
        
 
5034
 
3372
5035
        RNA_parameter_list_end(&iter);
3373
5036
 
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)
3378
5041
         */
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;
3382
5045
 
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();
3385
5048
 
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;
3388
5051
 
3389
5052
                while (PyDict_Next(kw, &pos, &key, &value)) {
3390
5053
 
3391
 
                        arg_name= _PyUnicode_AsString(key);
3392
 
                        found= FALSE;
 
5054
                        arg_name = _PyUnicode_AsString(key);
 
5055
                        found = FALSE;
3393
5056
 
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*/
3395
5058
                                PyErr_Clear();
3396
5059
                        }
3397
5060
                        else {
3398
5061
                                /* Search for arg_name */
3399
5062
                                RNA_parameter_list_begin(&parms, &iter);
3400
 
                                for(; iter.valid; RNA_parameter_list_next(&iter)) {
3401
 
                                        parm= iter.parm;
3402
 
                                        if (strcmp(arg_name, RNA_property_identifier(parm))==0) {
3403
 
                                                found= TRUE;
 
5063
                                for (; iter.valid; RNA_parameter_list_next(&iter)) {
 
5064
                                        parm = iter.parm;
 
5065
                                        if (strcmp(arg_name, RNA_property_identifier(parm)) == 0) {
 
5066
                                                found = TRUE;
3404
5067
                                                break;
3405
5068
                                        }
3406
5069
                                }
3407
5070
 
3408
5071
                                RNA_parameter_list_end(&iter);
3409
5072
 
3410
 
                                if(found==FALSE) {
 
5073
                                if (found == FALSE) {
3411
5074
                                        BLI_dynstr_appendf(bad_args, first ? "%s" : ", %s", arg_name);
3412
 
                                        first= FALSE;
 
5075
                                        first = FALSE;
3413
5076
                                }
3414
5077
                        }
3415
5078
                }
3416
5079
 
3417
5080
                /* list good args */
3418
 
                first= TRUE;
 
5081
                first = TRUE;
3419
5082
 
3420
5083
                RNA_parameter_list_begin(&parms, &iter);
3421
 
                for(; iter.valid; RNA_parameter_list_next(&iter)) {
3422
 
                        parm= iter.parm;
3423
 
                        if(RNA_property_flag(parm) & PROP_OUTPUT)
 
5084
                for (; iter.valid; RNA_parameter_list_next(&iter)) {
 
5085
                        parm = iter.parm;
 
5086
                        if (RNA_property_flag(parm) & PROP_OUTPUT)
3424
5087
                                continue;
3425
5088
 
3426
5089
                        BLI_dynstr_appendf(good_args, first ? "%s" : ", %s", RNA_property_identifier(parm));
3427
 
                        first= FALSE;
 
5090
                        first = FALSE;
3428
5091
                }
3429
5092
                RNA_parameter_list_end(&iter);
3430
5093
 
3431
5094
 
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);
3434
5097
 
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);
3436
5102
 
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);
3441
5107
 
3442
 
                err= -1;
 
5108
                err = -1;
3443
5109
        }
3444
5110
 
3445
 
        ret= NULL;
3446
 
        if (err==0) {
 
5111
        ret = NULL;
 
5112
        if (err == 0) {
3447
5113
                /* call function */
3448
5114
                ReportList reports;
3449
 
                bContext *C= BPy_GetContext();
 
5115
                bContext *C = BPy_GetContext();
3450
5116
 
3451
5117
                BKE_reports_init(&reports, RPT_STORE);
3452
5118
                RNA_function_call(C, &reports, self_ptr, self_func, &parms);
3453
5119
 
3454
 
                err= (BPy_reports_to_error(&reports))? -1: 0;
3455
 
                BKE_reports_clear(&reports);
 
5120
                err = (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE));
3456
5121
 
3457
5122
                /* return value */
3458
 
                if(err==0) {
 
5123
                if (err != -1) {
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 */
3463
5128
 
3464
5129
                                        RNA_parameter_list_begin(&parms, &iter);
3465
5130
 
3466
 
                                        for(; iter.valid; RNA_parameter_list_next(&iter)) {
3467
 
                                                parm= iter.parm;
3468
 
                                                flag= RNA_property_flag(parm);
 
5131
                                        for (; iter.valid; RNA_parameter_list_next(&iter)) {
 
5132
                                                parm = iter.parm;
 
5133
                                                flag = RNA_property_flag(parm);
3469
5134
 
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));
3472
5137
                                        }
3473
5138
 
3474
5139
                                        RNA_parameter_list_end(&iter);
3475
5140
                                }
3476
5141
                                else
3477
 
                                        ret= pyrna_param_to_py(&funcptr, &parms, pret_single, retdata_single);
 
5142
                                        ret = pyrna_param_to_py(&funcptr, pret_single, retdata_single);
3478
5143
 
3479
5144
                                /* possible there is an error in conversion */
3480
 
                                if(ret==NULL)
3481
 
                                        err= -1;
 
5145
                                if (ret == NULL)
 
5146
                                        err = -1;
3482
5147
                        }
3483
5148
                }
3484
5149
        }
3485
5150
 
 
5151
 
 
5152
#ifdef DEBUG_STRING_FREE
 
5153
#if 0
 
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));
 
5159
        }
 
5160
#endif
 
5161
        Py_DECREF(string_free_ls);
 
5162
#undef DEBUG_STRING_FREE
 
5163
#endif
 
5164
 
3486
5165
        /* cleanup */
3487
5166
        RNA_parameter_list_end(&iter);
3488
5167
        RNA_parameter_list_free(&parms);
3490
5169
        if (ret)
3491
5170
                return ret;
3492
5171
 
3493
 
        if (err==-1)
 
5172
        if (err == -1)
3494
5173
                return NULL;
3495
5174
 
3496
5175
        Py_RETURN_NONE;
3497
5176
}
3498
5177
 
 
5178
static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *UNUSED(closure))
 
5179
{
 
5180
        PyObject *ret;
 
5181
        char *args;
 
5182
 
 
5183
        args = RNA_function_as_string_keywords(NULL, self->func, NULL, TRUE, TRUE);
 
5184
 
 
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));
 
5189
 
 
5190
        MEM_freeN(args);
 
5191
 
 
5192
        return ret;
 
5193
}
 
5194
 
 
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 */
 
5200
 
 
5201
        /* NOTE! would be PyTypeObject, but subtypes of Type must be PyHeapTypeObject's */
 
5202
        sizeof(PyHeapTypeObject),   /* tp_basicsize */
 
5203
 
 
5204
        0,                          /* tp_itemsize */
 
5205
        /* methods */
 
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! */
 
5211
        NULL,                       /* tp_repr */
 
5212
 
 
5213
        /* Method suites for standard classes */
 
5214
        NULL,                       /* PyNumberMethods *tp_as_number; */
 
5215
        NULL,                       /* PySequenceMethods *tp_as_sequence; */
 
5216
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
5217
 
 
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; */
 
5224
 
 
5225
        /* Functions to access object as input/output buffer */
 
5226
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
5227
 
 
5228
        /*** Flags to define presence of optional/expanded features ***/
 
5229
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
 
5230
 
 
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; */
 
5235
 
 
5236
        /* delete references to contained objects */
 
5237
        NULL,                       /* inquiry tp_clear; */
 
5238
 
 
5239
        /***  Assigned meaning in release 2.1 ***/
 
5240
        /*** rich comparisons ***/
 
5241
        NULL,                       /* richcmpfunc tp_richcompare; */
 
5242
 
 
5243
        /***  weak reference enabler ***/
 
5244
        0,                          /* long tp_weaklistoffset; */
 
5245
 
 
5246
        /*** Added in release 2.2 ***/
 
5247
        /*   Iterators */
 
5248
        NULL,                       /* getiterfunc tp_iter; */
 
5249
        NULL,                       /* iternextfunc tp_iternext; */
 
5250
 
 
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 */
 
5257
#else
 
5258
        &PyType_Type,                       /* struct _typeobject *tp_base; */
 
5259
#endif
 
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; */
 
5277
        NULL
 
5278
};
 
5279
 
 
5280
 
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 */
3505
5287
        /* methods */
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 */
3512
5294
 
3513
5295
        /* Method suites for standard classes */
3514
5296
 
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; */
3518
5300
 
3519
5301
        /* More standard operations (here for binary compatibility) */
3520
5302
 
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; */
3526
5308
 
3527
5309
        /* Functions to access object as input/output buffer */
3528
5310
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
3529
5311
 
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; */
3532
5314
 
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; */
 
5320
 
 
5321
        /* delete references to contained objects */
 
5322
        (inquiry)pyrna_struct_clear, /* inquiry tp_clear; */
 
5323
#else
3536
5324
        NULL,                       /* traverseproc tp_traverse; */
3537
5325
 
3538
 
        /* delete references to contained objects */
 
5326
/* delete references to contained objects */
3539
5327
        NULL,                       /* inquiry tp_clear; */
3540
 
 
3541
 
  /***  Assigned meaning in release 2.1 ***/
3542
 
  /*** rich comparisons ***/
3543
 
        (richcmpfunc)pyrna_struct_richcmp,      /* richcmpfunc tp_richcompare; */
3544
 
 
3545
 
  /***  weak reference enabler ***/
3546
 
        0,                          /* long tp_weaklistoffset; */
3547
 
 
3548
 
  /*** Added in release 2.2 ***/
 
5328
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
 
5329
 
 
5330
        /***  Assigned meaning in release 2.1 ***/
 
5331
        /*** rich comparisons ***/
 
5332
        (richcmpfunc)pyrna_struct_richcmp, /* richcmpfunc tp_richcompare; */
 
5333
 
 
5334
        /***  weak reference enabler ***/
 
5335
#ifdef USE_WEAKREFS
 
5336
        offsetof(BPy_StructRNA, in_weakreflist), /* long tp_weaklistoffset; */
 
5337
#else
 
5338
        0,
 
5339
#endif
 
5340
        /*** Added in release 2.2 ***/
3549
5341
        /*   Iterators */
3550
5342
        NULL,                       /* getiterfunc tp_iter; */
3551
5343
        NULL,                       /* iternextfunc tp_iternext; */
3552
5344
 
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; */
3561
5353
        0,                          /* long tp_dictoffset; */
3562
5354
        NULL,                       /* initproc tp_init; */
3563
5355
        NULL,                       /* allocfunc tp_alloc; */
3564
 
        pyrna_struct_new,                       /* newfunc tp_new; */
 
5356
        pyrna_struct_new,           /* newfunc tp_new; */
3565
5357
        /*  Low-level free-memory routine */
3566
5358
        NULL,                       /* freefunc tp_free;  */
3567
5359
        /* For PyObject_IS_GC */
3578
5370
/*-----------------------BPy_PropertyRNA method def------------------------------*/
3579
5371
PyTypeObject pyrna_prop_Type = {
3580
5372
        PyVarObject_HEAD_INIT(NULL, 0)
3581
 
        "bpy_prop",             /* tp_name */
3582
 
        sizeof( BPy_PropertyRNA ),                      /* tp_basicsize */
3583
 
        0,                      /* tp_itemsize */
 
5373
        "bpy_prop",                 /* tp_name */
 
5374
        sizeof(BPy_PropertyRNA),    /* tp_basicsize */
 
5375
        0,                          /* tp_itemsize */
3584
5376
        /* methods */
3585
 
        NULL,                                           /* tp_dealloc */
3586
 
        NULL,                   /* printfunc tp_print; */
3587
 
        NULL,                                           /* getattrfunc tp_getattr; */
 
5377
        (destructor) pyrna_prop_dealloc, /* tp_dealloc */
 
5378
        NULL,                       /* printfunc tp_print; */
 
5379
        NULL,                       /* getattrfunc tp_getattr; */
3588
5380
        NULL,                       /* setattrfunc tp_setattr; */
3589
 
        NULL,                                           /* tp_compare */ /* DEPRECATED in python 3.0! */
3590
 
        ( reprfunc ) pyrna_prop_repr,   /* tp_repr */
 
5381
        NULL,                       /* tp_compare */ /* DEPRECATED in python 3.0! */
 
5382
        (reprfunc) pyrna_prop_repr, /* tp_repr */
3591
5383
 
3592
5384
        /* Method suites for standard classes */
3593
5385
 
3594
5386
        NULL,                       /* PyNumberMethods *tp_as_number; */
3595
 
        NULL,                                           /* PySequenceMethods *tp_as_sequence; */
3596
 
        NULL,                                           /* PyMappingMethods *tp_as_mapping; */
 
5387
        NULL,                       /* PySequenceMethods *tp_as_sequence; */
 
5388
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
3597
5389
 
3598
5390
        /* More standard operations (here for binary compatibility) */
3599
5391
 
3600
 
        ( hashfunc ) pyrna_prop_hash,   /* hashfunc tp_hash; */
 
5392
        (hashfunc) pyrna_prop_hash, /* hashfunc tp_hash; */
3601
5393
        NULL,                       /* ternaryfunc tp_call; */
3602
 
        NULL,                       /* reprfunc tp_str; */
 
5394
        (reprfunc) pyrna_prop_str,  /* reprfunc tp_str; */
3603
5395
 
3604
5396
        /* will only use these if this is a subtype of a py class */
3605
 
        NULL,                                           /* getattrofunc tp_getattro; */
3606
 
        NULL,                                           /* setattrofunc tp_setattro; */
 
5397
        NULL,                       /* getattrofunc tp_getattro; */
 
5398
        NULL,                       /* setattrofunc tp_setattro; */
3607
5399
 
3608
5400
        /* Functions to access object as input/output buffer */
3609
5401
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
3610
5402
 
3611
 
  /*** Flags to define presence of optional/expanded features ***/
 
5403
        /*** Flags to define presence of optional/expanded features ***/
3612
5404
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
3613
5405
 
3614
 
        NULL,                                           /*  char *tp_doc;  Documentation string */
3615
 
  /*** Assigned meaning in release 2.0 ***/
 
5406
        NULL,                       /*  char *tp_doc;  Documentation string */
 
5407
        /*** Assigned meaning in release 2.0 ***/
3616
5408
        /* call function for all accessible objects */
3617
5409
        NULL,                       /* traverseproc tp_traverse; */
3618
5410
 
3619
5411
        /* delete references to contained objects */
3620
5412
        NULL,                       /* inquiry tp_clear; */
3621
5413
 
3622
 
  /***  Assigned meaning in release 2.1 ***/
3623
 
  /*** rich comparisons ***/
3624
 
        (richcmpfunc)pyrna_prop_richcmp,        /* richcmpfunc tp_richcompare; */
3625
 
 
3626
 
  /***  weak reference enabler ***/
3627
 
        0,                          /* long tp_weaklistoffset; */
3628
 
 
3629
 
  /*** Added in release 2.2 ***/
 
5414
        /***  Assigned meaning in release 2.1 ***/
 
5415
        /*** rich comparisons ***/
 
5416
        (richcmpfunc)pyrna_prop_richcmp,    /* richcmpfunc tp_richcompare; */
 
5417
 
 
5418
        /***  weak reference enabler ***/
 
5419
#ifdef USE_WEAKREFS
 
5420
        offsetof(BPy_PropertyRNA, in_weakreflist),  /* long tp_weaklistoffset; */
 
5421
#else
 
5422
        0,
 
5423
#endif
 
5424
 
 
5425
        /*** Added in release 2.2 ***/
3630
5426
        /*   Iterators */
3631
 
        NULL,                                           /* getiterfunc tp_iter; */
 
5427
        NULL,                       /* getiterfunc tp_iter; */
3632
5428
        NULL,                       /* iternextfunc tp_iternext; */
3633
5429
 
3634
 
  /*** Attribute descriptor and subclassing stuff ***/
3635
 
        pyrna_prop_methods,                     /* struct PyMethodDef *tp_methods; */
 
5430
        /*** Attribute descriptor and subclassing stuff ***/
 
5431
        pyrna_prop_methods,         /* struct PyMethodDef *tp_methods; */
3636
5432
        NULL,                       /* struct PyMemberDef *tp_members; */
3637
 
        NULL /*pyrna_prop_getseters*/,          /* struct PyGetSetDef *tp_getset; */
 
5433
        pyrna_prop_getseters,       /* struct PyGetSetDef *tp_getset; */
3638
5434
        NULL,                       /* struct _typeobject *tp_base; */
3639
5435
        NULL,                       /* PyObject *tp_dict; */
3640
5436
        NULL,                       /* descrgetfunc tp_descr_get; */
3642
5438
        0,                          /* long tp_dictoffset; */
3643
5439
        NULL,                       /* initproc tp_init; */
3644
5440
        NULL,                       /* allocfunc tp_alloc; */
3645
 
        pyrna_prop_new,                         /* newfunc tp_new; */
 
5441
        pyrna_prop_new,             /* newfunc tp_new; */
3646
5442
        /*  Low-level free-memory routine */
3647
5443
        NULL,                       /* freefunc tp_free;  */
3648
5444
        /* For PyObject_IS_GC */
3658
5454
 
3659
5455
PyTypeObject pyrna_prop_array_Type = {
3660
5456
        PyVarObject_HEAD_INIT(NULL, 0)
3661
 
        "bpy_prop_array",               /* tp_name */
3662
 
        sizeof( BPy_PropertyRNA ),                      /* tp_basicsize */
3663
 
        0,                      /* tp_itemsize */
 
5457
        "bpy_prop_array",           /* tp_name */
 
5458
        sizeof(BPy_PropertyArrayRNA),           /* tp_basicsize */
 
5459
        0,                          /* tp_itemsize */
3664
5460
        /* methods */
3665
 
        NULL,                                           /* tp_dealloc */
 
5461
        (destructor)pyrna_prop_array_dealloc, /* tp_dealloc */
3666
5462
        NULL,                       /* printfunc tp_print; */
3667
 
        NULL,                                           /* getattrfunc tp_getattr; */
 
5463
        NULL,                       /* getattrfunc tp_getattr; */
3668
5464
        NULL,                       /* setattrfunc tp_setattr; */
3669
 
        NULL,                                           /* tp_compare */ /* DEPRECATED in python 3.0! */
3670
 
        NULL,/* subclassed */           /* tp_repr */
 
5465
        NULL,                       /* tp_compare */ /* DEPRECATED in python 3.0! */
 
5466
        NULL, /* subclassed */       /* tp_repr */
3671
5467
 
3672
5468
        /* Method suites for standard classes */
3673
5469
 
3674
 
        NULL,                       /* PyNumberMethods *tp_as_number; */
3675
 
        &pyrna_prop_array_as_sequence,  /* PySequenceMethods *tp_as_sequence; */
3676
 
        &pyrna_prop_array_as_mapping,   /* PyMappingMethods *tp_as_mapping; */
 
5470
        &pyrna_prop_array_as_number,   /* PyNumberMethods *tp_as_number; */
 
5471
        &pyrna_prop_array_as_sequence, /* PySequenceMethods *tp_as_sequence; */
 
5472
        &pyrna_prop_array_as_mapping,  /* PyMappingMethods *tp_as_mapping; */
3677
5473
 
3678
5474
        /* More standard operations (here for binary compatibility) */
3679
5475
 
3680
 
        NULL,                                           /* hashfunc tp_hash; */
 
5476
        NULL,                       /* hashfunc tp_hash; */
3681
5477
        NULL,                       /* ternaryfunc tp_call; */
3682
5478
        NULL,                       /* reprfunc tp_str; */
3683
5479
 
3684
5480
        /* will only use these if this is a subtype of a py class */
3685
 
        ( getattrofunc ) pyrna_prop_array_getattro,     /* getattrofunc tp_getattro; */
3686
 
        NULL,                                           /* setattrofunc tp_setattro; */
 
5481
        (getattrofunc) pyrna_prop_array_getattro, /* getattrofunc tp_getattro; */
 
5482
        NULL,                       /* setattrofunc tp_setattro; */
3687
5483
 
3688
5484
        /* Functions to access object as input/output buffer */
3689
5485
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
3690
5486
 
3691
 
  /*** Flags to define presence of optional/expanded features ***/
3692
 
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
 
5487
        /*** Flags to define presence of optional/expanded features ***/
 
5488
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
3693
5489
 
3694
 
        NULL,                                           /*  char *tp_doc;  Documentation string */
3695
 
  /*** Assigned meaning in release 2.0 ***/
 
5490
        NULL,                       /*  char *tp_doc;  Documentation string */
 
5491
        /*** Assigned meaning in release 2.0 ***/
3696
5492
        /* call function for all accessible objects */
3697
5493
        NULL,                       /* traverseproc tp_traverse; */
3698
5494
 
3699
5495
        /* delete references to contained objects */
3700
5496
        NULL,                       /* inquiry tp_clear; */
3701
5497
 
3702
 
  /***  Assigned meaning in release 2.1 ***/
3703
 
  /*** rich comparisons ***/
3704
 
        NULL, /* subclassed */          /* richcmpfunc tp_richcompare; */
3705
 
 
3706
 
  /***  weak reference enabler ***/
3707
 
        0,                          /* long tp_weaklistoffset; */
3708
 
 
3709
 
  /*** Added in release 2.2 ***/
 
5498
        /***  Assigned meaning in release 2.1 ***/
 
5499
        /*** rich comparisons ***/
 
5500
        NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
 
5501
 
 
5502
        /***  weak reference enabler ***/
 
5503
#ifdef USE_WEAKREFS
 
5504
        offsetof(BPy_PropertyArrayRNA, in_weakreflist), /* long tp_weaklistoffset; */
 
5505
#else
 
5506
        0,
 
5507
#endif
 
5508
        /*** Added in release 2.2 ***/
3710
5509
        /*   Iterators */
3711
 
        (getiterfunc)pyrna_prop_array_iter,     /* getiterfunc tp_iter; */
 
5510
        (getiterfunc)pyrna_prop_array_iter, /* getiterfunc tp_iter; */
3712
5511
        NULL,                       /* iternextfunc tp_iternext; */
3713
5512
 
3714
 
  /*** Attribute descriptor and subclassing stuff ***/
3715
 
        pyrna_prop_array_methods,                       /* struct PyMethodDef *tp_methods; */
 
5513
        /*** Attribute descriptor and subclassing stuff ***/
 
5514
        pyrna_prop_array_methods,   /* struct PyMethodDef *tp_methods; */
3716
5515
        NULL,                       /* struct PyMemberDef *tp_members; */
3717
 
        NULL /*pyrna_prop_getseters*/,          /* struct PyGetSetDef *tp_getset; */
3718
 
        &pyrna_prop_Type,                       /* struct _typeobject *tp_base; */
 
5516
        NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
 
5517
        &pyrna_prop_Type,           /* struct _typeobject *tp_base; */
3719
5518
        NULL,                       /* PyObject *tp_dict; */
3720
5519
        NULL,                       /* descrgetfunc tp_descr_get; */
3721
5520
        NULL,                       /* descrsetfunc tp_descr_set; */
3722
5521
        0,                          /* long tp_dictoffset; */
3723
5522
        NULL,                       /* initproc tp_init; */
3724
5523
        NULL,                       /* allocfunc tp_alloc; */
3725
 
        NULL,                                           /* newfunc tp_new; */
 
5524
        NULL,                       /* newfunc tp_new; */
3726
5525
        /*  Low-level free-memory routine */
3727
5526
        NULL,                       /* freefunc tp_free;  */
3728
5527
        /* For PyObject_IS_GC */
3729
5528
        NULL,                       /* inquiry tp_is_gc;  */
3730
 
        NULL,                   /* PyObject *tp_bases; */
 
5529
        NULL,                       /* PyObject *tp_bases; */
3731
5530
        /* method resolution order */
3732
5531
        NULL,                       /* PyObject *tp_mro;  */
3733
5532
        NULL,                       /* PyObject *tp_cache; */
3738
5537
 
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 */
3744
 
        /* methods */
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 */
3751
 
 
3752
 
        /* Method suites for standard classes */
3753
 
 
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; */
3757
 
 
3758
 
        /* More standard operations (here for binary compatibility) */
3759
 
 
3760
 
        NULL,                                           /* hashfunc tp_hash; */
3761
 
        NULL,                       /* ternaryfunc tp_call; */
3762
 
        NULL,                       /* reprfunc tp_str; */
3763
 
 
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; */
3767
 
 
3768
 
        /* Functions to access object as input/output buffer */
3769
 
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
3770
 
 
3771
 
  /*** Flags to define presence of optional/expanded features ***/
3772
 
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
3773
 
 
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; */
3778
 
 
3779
 
        /* delete references to contained objects */
3780
 
        NULL,                       /* inquiry tp_clear; */
3781
 
 
3782
 
  /***  Assigned meaning in release 2.1 ***/
3783
 
  /*** rich comparisons ***/
3784
 
        NULL, /* subclassed */          /* richcmpfunc tp_richcompare; */
3785
 
 
3786
 
  /***  weak reference enabler ***/
3787
 
        0,                          /* long tp_weaklistoffset; */
3788
 
 
3789
 
  /*** Added in release 2.2 ***/
3790
 
        /*   Iterators */
3791
 
        (getiterfunc)pyrna_prop_collection_iter,        /* getiterfunc tp_iter; */
3792
 
        NULL,                       /* iternextfunc tp_iternext; */
3793
 
 
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; */
3816
 
        NULL
3817
 
};
3818
 
 
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},
3830
 
        
3831
 
        {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc},
3832
 
 
3833
 
//      {"__get_rna", (PyCFunction)BPy_GetStructRNA, METH_NOARGS, ""},
3834
 
        {NULL, NULL, 0, NULL}
3835
 
};
 
5540
        "bpy_prop_collection",      /* tp_name */
 
5541
        sizeof(BPy_PropertyRNA),    /* tp_basicsize */
 
5542
        0,                          /* tp_itemsize */
 
5543
        /* methods */
 
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 */
 
5550
 
 
5551
        /* Method suites for standard classes */
 
5552
 
 
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; */
 
5556
 
 
5557
        /* More standard operations (here for binary compatibility) */
 
5558
 
 
5559
        NULL,                       /* hashfunc tp_hash; */
 
5560
        NULL,                       /* ternaryfunc tp_call; */
 
5561
        NULL,                       /* reprfunc tp_str; */
 
5562
 
 
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; */
 
5566
 
 
5567
        /* Functions to access object as input/output buffer */
 
5568
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
5569
 
 
5570
        /*** Flags to define presence of optional/expanded features ***/
 
5571
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
 
5572
 
 
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; */
 
5577
 
 
5578
        /* delete references to contained objects */
 
5579
        NULL,                       /* inquiry tp_clear; */
 
5580
 
 
5581
        /***  Assigned meaning in release 2.1 ***/
 
5582
        /*** rich comparisons ***/
 
5583
        NULL, /* subclassed */          /* richcmpfunc tp_richcompare; */
 
5584
 
 
5585
        /***  weak reference enabler ***/
 
5586
#ifdef USE_WEAKREFS
 
5587
        offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
 
5588
#else
 
5589
        0,
 
5590
#endif
 
5591
 
 
5592
        /*** Added in release 2.2 ***/
 
5593
        /*   Iterators */
 
5594
        (getiterfunc)pyrna_prop_collection_iter, /* getiterfunc tp_iter; */
 
5595
        NULL,                       /* iternextfunc tp_iternext; */
 
5596
 
 
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; */
 
5619
        NULL
 
5620
};
 
5621
 
 
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 */
 
5628
        /* methods */
 
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 */
 
5635
 
 
5636
        /* Method suites for standard classes */
 
5637
 
 
5638
        NULL,                       /* PyNumberMethods *tp_as_number; */
 
5639
        NULL,                       /* PySequenceMethods *tp_as_sequence; */
 
5640
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
5641
 
 
5642
        /* More standard operations (here for binary compatibility) */
 
5643
 
 
5644
        NULL,                       /* hashfunc tp_hash; */
 
5645
        NULL,                       /* ternaryfunc tp_call; */
 
5646
        NULL,                       /* reprfunc tp_str; */
 
5647
 
 
5648
        /* will only use these if this is a subtype of a py class */
 
5649
        NULL,                       /* getattrofunc tp_getattro; */
 
5650
        NULL,                       /* setattrofunc tp_setattro; */
 
5651
 
 
5652
        /* Functions to access object as input/output buffer */
 
5653
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
5654
 
 
5655
        /*** Flags to define presence of optional/expanded features ***/
 
5656
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
 
5657
 
 
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; */
 
5662
 
 
5663
        /* delete references to contained objects */
 
5664
        NULL,                       /* inquiry tp_clear; */
 
5665
 
 
5666
        /***  Assigned meaning in release 2.1 ***/
 
5667
        /*** rich comparisons ***/
 
5668
        NULL, /* subclassed */          /* richcmpfunc tp_richcompare; */
 
5669
 
 
5670
        /***  weak reference enabler ***/
 
5671
#ifdef USE_WEAKREFS
 
5672
        offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
 
5673
#else
 
5674
        0,
 
5675
#endif
 
5676
 
 
5677
        /*** Added in release 2.2 ***/
 
5678
        /*   Iterators */
 
5679
        NULL,                       /* getiterfunc tp_iter; */
 
5680
        NULL,                       /* iternextfunc tp_iternext; */
 
5681
 
 
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; */
 
5704
        NULL
 
5705
};
 
5706
 
 
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 */
 
5713
        /* methods */
 
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 */
 
5720
 
 
5721
        /* Method suites for standard classes */
 
5722
 
 
5723
        NULL,                       /* PyNumberMethods *tp_as_number; */
 
5724
        NULL,                       /* PySequenceMethods *tp_as_sequence; */
 
5725
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
5726
 
 
5727
        /* More standard operations (here for binary compatibility) */
 
5728
 
 
5729
        NULL,                       /* hashfunc tp_hash; */
 
5730
        (ternaryfunc)pyrna_func_call, /* ternaryfunc tp_call; */
 
5731
        NULL,                       /* reprfunc tp_str; */
 
5732
 
 
5733
        /* will only use these if this is a subtype of a py class */
 
5734
        NULL,                       /* getattrofunc tp_getattro; */
 
5735
        NULL,                       /* setattrofunc tp_setattro; */
 
5736
 
 
5737
        /* Functions to access object as input/output buffer */
 
5738
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
5739
 
 
5740
        /*** Flags to define presence of optional/expanded features ***/
 
5741
        Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
 
5742
 
 
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; */
 
5747
 
 
5748
        /* delete references to contained objects */
 
5749
        NULL,                       /* inquiry tp_clear; */
 
5750
 
 
5751
        /***  Assigned meaning in release 2.1 ***/
 
5752
        /*** rich comparisons ***/
 
5753
        NULL,                       /* richcmpfunc tp_richcompare; */
 
5754
 
 
5755
        /***  weak reference enabler ***/
 
5756
#ifdef USE_WEAKREFS
 
5757
        offsetof(BPy_PropertyRNA, in_weakreflist),  /* long tp_weaklistoffset; */
 
5758
#else
 
5759
        0,
 
5760
#endif
 
5761
 
 
5762
        /*** Added in release 2.2 ***/
 
5763
        /*   Iterators */
 
5764
        NULL,                       /* getiterfunc tp_iter; */
 
5765
        NULL,                       /* iternextfunc tp_iternext; */
 
5766
 
 
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; */
 
5789
        NULL
 
5790
};
 
5791
 
 
5792
#ifdef USE_PYRNA_ITER
 
5793
/* --- collection iterator: start --- */
 
5794
/* wrap rna collection iterator functions */
 
5795
/*
 
5796
 * RNA_property_collection_begin(...)
 
5797
 * RNA_property_collection_next(...)
 
5798
 * RNA_property_collection_end(...)
 
5799
 */
 
5800
 
 
5801
static void pyrna_prop_collection_iter_dealloc(BPy_PropertyCollectionIterRNA *self);
 
5802
static PyObject *pyrna_prop_collection_iter_next(BPy_PropertyCollectionIterRNA *self);
 
5803
 
 
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 */
 
5809
        /* methods */
 
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 */
 
5816
 
 
5817
        /* Method suites for standard classes */
 
5818
 
 
5819
        NULL,    /* PyNumberMethods *tp_as_number; */
 
5820
        NULL,                       /* PySequenceMethods *tp_as_sequence; */
 
5821
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
5822
 
 
5823
        /* More standard operations (here for binary compatibility) */
 
5824
 
 
5825
        NULL,                       /* hashfunc tp_hash; */
 
5826
        NULL,                       /* ternaryfunc tp_call; */
 
5827
        NULL,                       /* reprfunc tp_str; */
 
5828
 
 
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 */
 
5832
#else
 
5833
        PyObject_GenericGetAttr,    /* getattrofunc tp_getattro; */
 
5834
#endif
 
5835
        NULL,                       /* setattrofunc tp_setattro; */
 
5836
 
 
5837
        /* Functions to access object as input/output buffer */
 
5838
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
5839
 
 
5840
        /*** Flags to define presence of optional/expanded features ***/
 
5841
        Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
 
5842
 
 
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; */
 
5847
 
 
5848
        /* delete references to contained objects */
 
5849
        NULL,                       /* inquiry tp_clear; */
 
5850
 
 
5851
        /***  Assigned meaning in release 2.1 ***/
 
5852
        /*** rich comparisons ***/
 
5853
        NULL, /* subclassed */          /* richcmpfunc tp_richcompare; */
 
5854
 
 
5855
        /***  weak reference enabler ***/
 
5856
#ifdef USE_WEAKREFS
 
5857
        offsetof(BPy_PropertyCollectionIterRNA, in_weakreflist), /* long tp_weaklistoffset; */
 
5858
#else
 
5859
        0,
 
5860
#endif
 
5861
        /*** Added in release 2.2 ***/
 
5862
        /*   Iterators */
 
5863
#if defined(_MSC_VER) || defined(FREE_WINDOWS)
 
5864
        NULL, /* defer assignment */
 
5865
#else
 
5866
        PyObject_SelfIter,          /* getiterfunc tp_iter; */
 
5867
#endif
 
5868
        (iternextfunc) pyrna_prop_collection_iter_next, /* iternextfunc tp_iternext; */
 
5869
 
 
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; */
 
5892
        NULL
 
5893
};
 
5894
 
 
5895
PyObject *pyrna_prop_collection_iter_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
 
5896
{
 
5897
        BPy_PropertyCollectionIterRNA *self = PyObject_New(BPy_PropertyCollectionIterRNA, &pyrna_prop_collection_iter_Type);
 
5898
 
 
5899
#ifdef USE_WEAKREFS
 
5900
        self->in_weakreflist = NULL;
 
5901
#endif
 
5902
 
 
5903
        RNA_property_collection_begin(ptr, prop, &self->iter);
 
5904
 
 
5905
        return (PyObject *)self;
 
5906
}
 
5907
 
 
5908
static PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self)
 
5909
{
 
5910
        return pyrna_prop_collection_iter_CreatePyObject(&self->ptr, self->prop);
 
5911
}
 
5912
 
 
5913
static PyObject *pyrna_prop_collection_iter_next(BPy_PropertyCollectionIterRNA *self)
 
5914
{
 
5915
        if (self->iter.valid == FALSE) {
 
5916
                PyErr_SetString(PyExc_StopIteration, "pyrna_prop_collection_iter stop");
 
5917
                return NULL;
 
5918
        }
 
5919
        else {
 
5920
                BPy_StructRNA *pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&self->iter.ptr);
 
5921
 
 
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);
 
5929
                        }
 
5930
                }
 
5931
#endif /* !USE_PYRNA_STRUCT_REFERENCE */
 
5932
 
 
5933
                RNA_property_collection_next(&self->iter);
 
5934
 
 
5935
                return (PyObject *)pyrna;
 
5936
        }
 
5937
}
 
5938
 
 
5939
 
 
5940
static void pyrna_prop_collection_iter_dealloc(BPy_PropertyCollectionIterRNA *self)
 
5941
{
 
5942
#ifdef USE_WEAKREFS
 
5943
        if (self->in_weakreflist != NULL) {
 
5944
                PyObject_ClearWeakRefs((PyObject *)self);
 
5945
        }
 
5946
#endif
 
5947
 
 
5948
        RNA_property_collection_end(&self->iter);
 
5949
 
 
5950
        PyObject_DEL(self);
 
5951
}
 
5952
 
 
5953
/* --- collection iterator: end --- */
 
5954
#endif /* !USE_PYRNA_ITER */
 
5955
 
3836
5956
 
3837
5957
static void pyrna_subtype_set_rna(PyObject *newclass, StructRNA *srna)
3838
5958
{
3839
5959
        PointerRNA ptr;
3840
5960
        PyObject *item;
3841
 
        
 
5961
 
3842
5962
        Py_INCREF(newclass);
3843
5963
 
3844
5964
        if (RNA_struct_py_type_get(srna))
3845
 
                PyObSpit("RNA WAS SET - ", RNA_struct_py_type_get(srna));
3846
 
        
 
5965
                PyC_ObSpit("RNA WAS SET - ", RNA_struct_py_type_get(srna));
 
5966
 
3847
5967
        Py_XDECREF(((PyObject *)RNA_struct_py_type_get(srna)));
3848
 
        
 
5968
 
3849
5969
        RNA_struct_py_type_set(srna, (void *)newclass); /* Store for later use */
3850
5970
 
3851
5971
        /* Not 100% needed but useful,
3852
5972
         * having an instance within a type looks wrong however this instance IS an rna type */
3853
5973
 
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);
3857
5977
 
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);
 
5981
 
3861
5982
        /* done with rna instance */
3862
 
 
3863
 
        /* attach functions into the class
3864
 
         * so you can do... bpy.types.Scene.SomeFunction()
3865
 
         */
3866
 
        {
3867
 
                PyMethodDef *ml;
3868
 
 
3869
 
                for(ml= pyrna_struct_subtype_methods; ml->ml_name; ml++){
3870
 
                        PyObject_SetAttrString(newclass, ml->ml_name, PyCFunction_New(ml, newclass));
3871
 
                }
3872
 
        }
3873
5983
}
3874
5984
 
3875
 
static PyObject* pyrna_srna_Subtype(StructRNA *srna);
 
5985
static PyObject *pyrna_srna_Subtype(StructRNA *srna);
3876
5986
 
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)
3879
5989
{
3880
5990
        /* Assume RNA_struct_py_type_get(srna) was already checked */
3881
5991
        StructRNA *base;
3882
5992
 
3883
 
        PyObject *py_base= NULL;
 
5993
        PyObject *py_base = NULL;
3884
5994
 
3885
5995
        /* get the base type */
3886
 
        base= RNA_struct_base(srna);
 
5996
        base = RNA_struct_base(srna);
3887
5997
 
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 */
3892
6002
        }
3893
6003
 
3894
 
        if(py_base==NULL) {
3895
 
                py_base= (PyObject *)&pyrna_struct_Type;
 
6004
        if (py_base == NULL) {
 
6005
                py_base = (PyObject *)&pyrna_struct_Type;
3896
6006
        }
3897
6007
 
3898
6008
        return py_base;
3900
6010
 
3901
6011
/* check if we have a native python subclass, use it when it exists
3902
6012
 * return a borrowed reference */
3903
 
static PyObject* pyrna_srna_ExternalType(StructRNA *srna)
 
6013
static PyObject *bpy_types_dict = NULL;
 
6014
 
 
6015
static PyObject *pyrna_srna_ExternalType(StructRNA *srna)
3904
6016
{
3905
 
        PyObject *bpy_types_dict= NULL;
3906
 
        const char *idname= RNA_struct_identifier(srna);
 
6017
        const char *idname = RNA_struct_identifier(srna);
3907
6018
        PyObject *newclass;
3908
6019
 
3909
 
        if(bpy_types_dict==NULL) {
3910
 
                PyObject *bpy_types= PyImport_ImportModuleLevel("bpy_types", NULL, NULL, NULL, 0);
 
6020
        if (bpy_types_dict == NULL) {
 
6021
                PyObject *bpy_types = PyImport_ImportModuleLevel((char *)"bpy_types", NULL, NULL, NULL, 0);
3911
6022
 
3912
 
                if(bpy_types==NULL) {
 
6023
                if (bpy_types == NULL) {
3913
6024
                        PyErr_Print();
3914
6025
                        PyErr_Clear();
3915
 
                        fprintf(stderr, "pyrna_srna_ExternalType: failed to find 'bpy_types' module\n");
 
6026
                        fprintf(stderr, "%s: failed to find 'bpy_types' module\n", __func__);
3916
6027
                        return NULL;
3917
6028
                }
3918
 
 
3919
6029
                bpy_types_dict = PyModule_GetDict(bpy_types); // borrow
3920
6030
                Py_DECREF(bpy_types); // fairly safe to assume the dict is kept
3921
6031
        }
3922
6032
 
3923
 
        newclass= PyDict_GetItemString(bpy_types_dict, idname);
 
6033
        newclass = PyDict_GetItemString(bpy_types_dict, idname);
3924
6034
 
3925
6035
        /* sanity check, could skip this unless in debug mode */
3926
 
        if(newclass) {
3927
 
                PyObject *base_compare= pyrna_srna_PyBase(srna);
3928
 
                //PyObject *slots= PyObject_GetAttrString(newclass, "__slots__"); // cant do this because it gets superclasses values!
3929
 
                //PyObject *bases= PyObject_GetAttrString(newclass, "__bases__"); // can do this but faster not to.
3930
 
                PyObject *bases= ((PyTypeObject *)newclass)->tp_bases;
3931
 
                PyObject *slots = PyDict_GetItemString(((PyTypeObject *)newclass)->tp_dict, "__slots__");
 
6036
        if (newclass) {
 
6037
                PyObject *base_compare = pyrna_srna_PyBase(srna);
 
6038
                //PyObject *slots = PyObject_GetAttrString(newclass, "__slots__"); // cant do this because it gets superclasses values!
 
6039
                //PyObject *bases = PyObject_GetAttrString(newclass, "__bases__"); // can do this but faster not to.
 
6040
                PyObject *tp_bases = ((PyTypeObject *)newclass)->tp_bases;
 
6041
                PyObject *tp_slots = PyDict_GetItem(((PyTypeObject *)newclass)->tp_dict, bpy_intern_str___slots__);
3932
6042
 
3933
 
                if(slots==NULL) {
3934
 
                        fprintf(stderr, "pyrna_srna_ExternalType: expected class '%s' to have __slots__ defined\n\nSee bpy_types.py\n", idname);
3935
 
                        newclass= NULL;
 
6043
                if (tp_slots == NULL) {
 
6044
                        fprintf(stderr, "%s: expected class '%s' to have __slots__ defined\n\nSee bpy_types.py\n", __func__, idname);
 
6045
                        newclass = NULL;
3936
6046
                }
3937
 
                else if(PyTuple_GET_SIZE(bases)) {
3938
 
                        PyObject *base= PyTuple_GET_ITEM(bases, 0);
 
6047
                else if (PyTuple_GET_SIZE(tp_bases)) {
 
6048
                        PyObject *base = PyTuple_GET_ITEM(tp_bases, 0);
3939
6049
 
3940
 
                        if(base_compare != base) {
3941
 
                                fprintf(stderr, "pyrna_srna_ExternalType: incorrect subclassing of SRNA '%s'\nSee bpy_types.py\n", idname);
3942
 
                                PyObSpit("Expected! ", base_compare);
3943
 
                                newclass= NULL;
 
6050
                        if (base_compare != base) {
 
6051
                                fprintf(stderr, "%s: incorrect subclassing of SRNA '%s'\nSee bpy_types.py\n", __func__, idname);
 
6052
                                PyC_ObSpit("Expected! ", base_compare);
 
6053
                                newclass = NULL;
3944
6054
                        }
3945
6055
                        else {
3946
 
                                if(G.f & G_DEBUG)
 
6056
                                if (G.debug & G_DEBUG_PYTHON)
3947
6057
                                        fprintf(stderr, "SRNA Subclassed: '%s'\n", idname);
3948
6058
                        }
3949
6059
                }
3952
6062
        return newclass;
3953
6063
}
3954
6064
 
3955
 
static PyObject* pyrna_srna_Subtype(StructRNA *srna)
 
6065
static PyObject *pyrna_srna_Subtype(StructRNA *srna)
3956
6066
{
3957
6067
        PyObject *newclass = NULL;
3958
6068
 
 
6069
        /* stupid/simple case */
3959
6070
        if (srna == NULL) {
3960
 
                newclass= NULL; /* Nothing to do */
3961
 
        } else if ((newclass= RNA_struct_py_type_get(srna))) {
 
6071
                newclass = NULL; /* Nothing to do */
 
6072
        }   /* the class may have already been declared & allocated */
 
6073
        else if ((newclass = RNA_struct_py_type_get(srna))) {
3962
6074
                Py_INCREF(newclass);
3963
 
        } else if ((newclass= pyrna_srna_ExternalType(srna))) {
 
6075
        }   /* check if bpy_types.py module has the class defined in it */
 
6076
        else if ((newclass = pyrna_srna_ExternalType(srna))) {
3964
6077
                pyrna_subtype_set_rna(newclass, srna);
3965
6078
                Py_INCREF(newclass);
3966
 
        } else {
3967
 
                /* subclass equivelents
3968
 
                - class myClass(myBase):
3969
 
                        some='value' # or ...
3970
 
                - myClass = type(name='myClass', bases=(myBase,), dict={'__module__':'bpy.types'})
3971
 
                */
 
6079
        }   /* create a new class instance with the C api
 
6080
             * mainly for the purposing of matching the C/rna type hierarchy */
 
6081
        else {
 
6082
                /* subclass equivalents
 
6083
                 * - class myClass(myBase):
 
6084
                 *     some = 'value' # or ...
 
6085
                 * - myClass = type(name='myClass', bases=(myBase,), dict={'__module__':'bpy.types'})
 
6086
                 */
3972
6087
 
3973
6088
                /* Assume RNA_struct_py_type_get(srna) was already checked */
3974
 
                PyObject *py_base= pyrna_srna_PyBase(srna);
3975
 
 
3976
 
                const char *idname= RNA_struct_identifier(srna);
3977
 
                const char *descr= RNA_struct_ui_description(srna);
3978
 
 
3979
 
                if(!descr) descr= "(no docs)";
3980
 
                
 
6089
                PyObject *py_base = pyrna_srna_PyBase(srna);
 
6090
                PyObject *metaclass;
 
6091
                const char *idname = RNA_struct_identifier(srna);
 
6092
 
 
6093
                /* remove __doc__ for now */
 
6094
                // const char *descr = RNA_struct_ui_description(srna);
 
6095
                // if (!descr) descr = "(no docs)";
 
6096
                // "__doc__", descr
 
6097
 
 
6098
                if (RNA_struct_idprops_check(srna) &&
 
6099
                    !PyObject_IsSubclass(py_base, (PyObject *)&pyrna_struct_meta_idprop_Type))
 
6100
                {
 
6101
                        metaclass = (PyObject *)&pyrna_struct_meta_idprop_Type;
 
6102
                }
 
6103
                else {
 
6104
                        metaclass = (PyObject *)&PyType_Type;
 
6105
                }
 
6106
 
3981
6107
                /* always use O not N when calling, N causes refcount errors */
3982
 
                newclass = PyObject_CallFunction(       (PyObject*)&PyType_Type, "s(O){sssss()}", idname, py_base, "__module__","bpy.types", "__doc__",descr, "__slots__");
 
6108
                newclass = PyObject_CallFunction(metaclass, (char *)"s(O) {sss()}",
 
6109
                                                 idname, py_base, "__module__", "bpy.types", "__slots__");
 
6110
 
3983
6111
                /* newclass will now have 2 ref's, ???, probably 1 is internal since decrefing here segfaults */
3984
6112
 
3985
 
                /* PyObSpit("new class ref", newclass); */
 
6113
                /* PyC_ObSpit("new class ref", newclass); */
3986
6114
 
3987
6115
                if (newclass) {
3988
6116
                        /* srna owns one, and the other is owned by the caller */
3989
6117
                        pyrna_subtype_set_rna(newclass, srna);
3990
6118
 
3991
 
                        Py_DECREF(newclass); /* let srna own */
 
6119
                        // XXX, adding this back segfaults blender on load.
 
6120
                        // Py_DECREF(newclass); /* let srna own */
3992
6121
                }
3993
6122
                else {
3994
6123
                        /* this should not happen */
 
6124
                        printf("%s: error registering '%s'\n", __func__, idname);
3995
6125
                        PyErr_Print();
3996
6126
                        PyErr_Clear();
3997
6127
                }
3998
6128
        }
3999
 
        
 
6129
 
4000
6130
        return newclass;
4001
6131
}
4002
6132
 
4003
6133
/* use for subtyping so we know which srna is used for a PointerRNA */
4004
6134
static StructRNA *srna_from_ptr(PointerRNA *ptr)
4005
6135
{
4006
 
        if(ptr->type == &RNA_Struct) {
 
6136
        if (ptr->type == &RNA_Struct) {
4007
6137
                return ptr->data;
4008
6138
        }
4009
6139
        else {
4012
6142
}
4013
6143
 
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)
4016
6146
{
4017
6147
        return pyrna_srna_Subtype(srna_from_ptr(ptr));
4018
6148
}
4019
6149
 
4020
6150
/*-----------------------CreatePyObject---------------------------------*/
4021
 
PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
 
6151
PyObject *pyrna_struct_CreatePyObject(PointerRNA *ptr)
4022
6152
{
4023
 
        BPy_StructRNA *pyrna= NULL;
 
6153
        BPy_StructRNA *pyrna = NULL;
4024
6154
 
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;
4028
6158
        }
4029
6159
        else {
4030
6160
                PyTypeObject *tp = (PyTypeObject *)pyrna_struct_Subtype(ptr);
4031
 
                
 
6161
 
4032
6162
                if (tp) {
4033
6163
                        pyrna = (BPy_StructRNA *) tp->tp_alloc(tp, 0);
4034
6164
                        Py_DECREF(tp); /* srna owns, cant hold a ref */
4035
6165
                }
4036
6166
                else {
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);
 
6169
#ifdef USE_WEAKREFS
 
6170
                        pyrna->in_weakreflist = NULL;
 
6171
#endif
4039
6172
                }
4040
6173
        }
4041
6174
 
4042
 
        if( !pyrna ) {
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");
4044
6177
                return NULL;
4045
6178
        }
4046
 
        
4047
 
        pyrna->ptr= *ptr;
4048
 
        pyrna->freeptr= FALSE;
4049
 
        
4050
 
        // PyObSpit("NewStructRNA: ", (PyObject *)pyrna);
4051
 
        
4052
 
        return ( PyObject * ) pyrna;
 
6179
 
 
6180
        pyrna->ptr = *ptr;
 
6181
#ifdef PYRNA_FREE_SUPPORT
 
6182
        pyrna->freeptr = FALSE;
 
6183
#endif
 
6184
 
 
6185
#ifdef USE_PYRNA_STRUCT_REFERENCE
 
6186
        pyrna->reference = NULL;
 
6187
#endif
 
6188
 
 
6189
        // PyC_ObSpit("NewStructRNA: ", (PyObject *)pyrna);
 
6190
 
 
6191
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
 
6192
        if (ptr->id.data) {
 
6193
                id_weakref_pool_add(ptr->id.data, (BPy_DummyPointerRNA *)pyrna);
 
6194
        }
 
6195
#endif
 
6196
        return (PyObject *)pyrna;
4053
6197
}
4054
6198
 
4055
 
PyObject *pyrna_prop_CreatePyObject( PointerRNA *ptr, PropertyRNA *prop )
 
6199
PyObject *pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
4056
6200
{
4057
6201
        BPy_PropertyRNA *pyrna;
4058
 
        PyTypeObject *type;
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;
4062
 
 
4063
 
        pyrna = ( BPy_PropertyRNA * ) PyObject_NEW(BPy_PropertyRNA, type);
4064
 
 
4065
 
        if( !pyrna ) {
4066
 
                PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_rna object" );
 
6202
 
 
6203
        if (RNA_property_array_check(prop) == 0) {
 
6204
                PyTypeObject *type;
 
6205
 
 
6206
                if (RNA_property_type(prop) != PROP_COLLECTION) {
 
6207
                        type = &pyrna_prop_Type;
 
6208
                }
 
6209
                else {
 
6210
                        if ((RNA_property_flag(prop) & PROP_IDPROPERTY) == 0) {
 
6211
                                type = &pyrna_prop_collection_Type;
 
6212
                        }
 
6213
                        else {
 
6214
                                type = &pyrna_prop_collection_idprop_Type;
 
6215
                        }
 
6216
                }
 
6217
 
 
6218
                pyrna = (BPy_PropertyRNA *) PyObject_NEW(BPy_PropertyRNA, type);
 
6219
#ifdef USE_WEAKREFS
 
6220
                pyrna->in_weakreflist = NULL;
 
6221
#endif
 
6222
        }
 
6223
        else {
 
6224
                pyrna = (BPy_PropertyRNA *) PyObject_NEW(BPy_PropertyArrayRNA, &pyrna_prop_array_Type);
 
6225
                ((BPy_PropertyArrayRNA *)pyrna)->arraydim = 0;
 
6226
                ((BPy_PropertyArrayRNA *)pyrna)->arrayoffset = 0;
 
6227
#ifdef USE_WEAKREFS
 
6228
                ((BPy_PropertyArrayRNA *)pyrna)->in_weakreflist = NULL;
 
6229
#endif
 
6230
        }
 
6231
 
 
6232
        if (pyrna == NULL) {
 
6233
                PyErr_SetString(PyExc_MemoryError, "couldn't create BPy_rna object");
4067
6234
                return NULL;
4068
6235
        }
4069
 
        
 
6236
 
4070
6237
        pyrna->ptr = *ptr;
4071
6238
        pyrna->prop = prop;
4072
6239
 
4073
 
        pyrna->arraydim= 0;
4074
 
        pyrna->arrayoffset= 0;
4075
 
                
4076
 
        return ( PyObject * ) pyrna;
 
6240
#ifdef USE_PYRNA_INVALIDATE_WEAKREF
 
6241
        if (ptr->id.data) {
 
6242
                id_weakref_pool_add(ptr->id.data, (BPy_DummyPointerRNA *)pyrna);
 
6243
        }
 
6244
#endif
 
6245
 
 
6246
        return (PyObject *)pyrna;
 
6247
}
 
6248
 
 
6249
/* utility func to be used by external modules, *sneaky!* */
 
6250
PyObject *pyrna_id_CreatePyObject(ID *id)
 
6251
{
 
6252
        if (id) {
 
6253
                PointerRNA ptr;
 
6254
                RNA_id_pointer_create(id, &ptr);
 
6255
                return pyrna_struct_CreatePyObject(&ptr);
 
6256
        }
 
6257
        else {
 
6258
                Py_RETURN_NONE;
 
6259
        }
 
6260
}
 
6261
 
 
6262
int pyrna_id_FromPyObject(PyObject *obj, ID **id)
 
6263
{
 
6264
        if (BPy_StructRNA_Check(obj) && (RNA_struct_is_ID(((BPy_StructRNA *)obj)->ptr.type))) {
 
6265
                *id = ((BPy_StructRNA *)obj)->ptr.id.data;
 
6266
                return TRUE;
 
6267
        }
 
6268
        else {
 
6269
                *id = NULL;
 
6270
                return FALSE;
 
6271
        }
4077
6272
}
4078
6273
 
4079
6274
void BPY_rna_init(void)
4080
6275
{
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);
4084
 
#endif
4085
 
 
4086
 
        if( PyType_Ready( &pyrna_struct_Type ) < 0 )
4087
 
                return;
4088
 
 
4089
 
        if( PyType_Ready( &pyrna_prop_Type ) < 0 )
4090
 
                return;
4091
 
 
4092
 
        if( PyType_Ready( &pyrna_prop_array_Type ) < 0 )
4093
 
                return;
4094
 
 
4095
 
        if( PyType_Ready( &pyrna_prop_collection_Type ) < 0 )
4096
 
                return;
 
6277
        mathutils_rna_array_cb_index = Mathutils_RegisterCallback(&mathutils_rna_array_cb);
 
6278
        mathutils_rna_matrix_cb_index = Mathutils_RegisterCallback(&mathutils_rna_matrix_cb);
 
6279
#endif
 
6280
 
 
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;
 
6284
 
 
6285
        pyrna_prop_collection_iter_Type.tp_iter = PyObject_SelfIter;
 
6286
        pyrna_prop_collection_iter_Type.tp_getattro = PyObject_GenericGetAttr;
 
6287
#endif
 
6288
 
 
6289
        /* metaclass */
 
6290
        if (PyType_Ready(&pyrna_struct_meta_idprop_Type) < 0)
 
6291
                return;
 
6292
 
 
6293
        if (PyType_Ready(&pyrna_struct_Type) < 0)
 
6294
                return;
 
6295
 
 
6296
        if (PyType_Ready(&pyrna_prop_Type) < 0)
 
6297
                return;
 
6298
 
 
6299
        if (PyType_Ready(&pyrna_prop_array_Type) < 0)
 
6300
                return;
 
6301
 
 
6302
        if (PyType_Ready(&pyrna_prop_collection_Type) < 0)
 
6303
                return;
 
6304
 
 
6305
        if (PyType_Ready(&pyrna_prop_collection_idprop_Type) < 0)
 
6306
                return;
 
6307
 
 
6308
        if (PyType_Ready(&pyrna_func_Type) < 0)
 
6309
                return;
 
6310
 
 
6311
#ifdef USE_PYRNA_ITER
 
6312
        if (PyType_Ready(&pyrna_prop_collection_iter_Type) < 0)
 
6313
                return;
 
6314
#endif
4097
6315
}
4098
6316
 
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)
4102
6320
{
4103
6321
        BPy_StructRNA *pyrna;
4104
6322
        PointerRNA ptr;
4105
6323
 
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);
4109
 
        
4110
 
        rna_module_ptr= &pyrna->ptr;
 
6326
        pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
 
6327
 
 
6328
        rna_module_ptr = &pyrna->ptr;
4111
6329
        return (PyObject *)pyrna;
4112
6330
}
4113
6331
 
4114
6332
void BPY_update_rna_module(void)
4115
6333
{
 
6334
#if 0
4116
6335
        RNA_main_pointer_create(G.main, rna_module_ptr);
 
6336
#else
 
6337
        rna_module_ptr->data = G.main; /* just set data is enough */
 
6338
#endif
4117
6339
}
4118
6340
 
4119
6341
#if 0
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)
4123
6345
{
4124
6346
        PointerRNA ptr;
4125
 
        
4126
 
        /* for now, return the base RNA type rather then a real module */
 
6347
 
 
6348
        /* for now, return the base RNA type rather than a real module */
4127
6349
        RNA_blender_rna_pointer_create(&ptr);
4128
 
        
 
6350
 
4129
6351
        return pyrna_struct_CreatePyObject(&ptr);
4130
6352
}
4131
6353
#endif
4132
6354
 
4133
6355
 
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)
4138
6360
{
4139
6361
        PointerRNA newptr;
4140
6362
        PyObject *ret;
4141
 
        char *name= _PyUnicode_AsString(pyname);
4142
 
        
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);
 
6364
 
 
6365
        if (name == NULL) {
 
6366
                PyErr_SetString(PyExc_AttributeError, "bpy.types: __getattr__ must be a string");
 
6367
                ret = NULL;
4146
6368
        }
4147
6369
        else if (RNA_property_collection_lookup_string(&self->ptr, self->prop, name, &newptr)) {
4148
 
                ret= pyrna_struct_Subtype(&newptr);
4149
 
                if (ret==NULL) {
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);
 
6371
                if (ret == NULL) {
 
6372
                        PyErr_Format(PyExc_RuntimeError,
 
6373
                                     "bpy.types.%.200s subtype could not be generated, this is a bug!",
 
6374
                                     _PyUnicode_AsString(pyname));
4151
6375
                }
4152
6376
        }
4153
6377
        else {
4154
6378
#if 0
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));
4156
6382
                return NULL;
4157
6383
#endif
4158
6384
                /* The error raised here will be displayed */
4159
 
                ret= PyObject_GenericGetAttr((PyObject *)self, pyname);
 
6385
                ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
4160
6386
        }
4161
6387
 
4162
6388
        return ret;
4163
6389
}
4164
6390
 
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);
 
6394
 
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}
4171
6398
};
4172
6399
 
 
6400
/* used to call ..._keys() direct, but we need to filter out operator subclasses */
 
6401
#if 0
4173
6402
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
4174
6403
{
4175
 
        PyObject *list, *name;
 
6404
        PyObject *list;
 
6405
#if 0
 
6406
        PyObject *name;
4176
6407
        PyMethodDef *meth;
4177
 
        
4178
 
        list= pyrna_prop_keys(self); /* like calling structs.keys(), avoids looping here */
4179
 
 
4180
 
        for(meth=pyrna_basetype_methods; meth->ml_name; meth++) {
 
6408
#endif
 
6409
 
 
6410
        list = pyrna_prop_collection_keys(self); /* like calling structs.keys(), avoids looping here */
 
6411
 
 
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);
4184
6417
        }
4185
 
        
 
6418
#endif
4186
6419
        return list;
4187
6420
}
4188
6421
 
4189
 
PyTypeObject pyrna_basetype_Type = BLANK_PYTHON_TYPE;
 
6422
#else
 
6423
 
 
6424
static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
 
6425
{
 
6426
        PyObject *ret = PyList_New(0);
 
6427
        PyObject *item;
 
6428
 
 
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) {
 
6434
                        /* do nothing */
 
6435
                }
 
6436
                else {
 
6437
                        /* add to python list */
 
6438
                        item = PyUnicode_FromString(RNA_struct_identifier(srna));
 
6439
                        PyList_Append(ret, item);
 
6440
                        Py_DECREF(item);
 
6441
                }
 
6442
        }
 
6443
        RNA_PROP_END;
 
6444
 
 
6445
        return ret;
 
6446
}
 
6447
 
 
6448
#endif
 
6449
 
 
6450
static PyTypeObject pyrna_basetype_Type = BLANK_PYTHON_TYPE;
4190
6451
 
4191
6452
PyObject *BPY_rna_types(void)
4192
6453
{
4193
6454
        BPy_BaseTypeRNA *self;
4194
6455
 
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;
4201
 
                
4202
 
                if( PyType_Ready( &pyrna_basetype_Type ) < 0 )
 
6462
 
 
6463
                if (PyType_Ready(&pyrna_basetype_Type) < 0)
4203
6464
                        return NULL;
4204
6465
        }
4205
 
        
4206
 
        self= (BPy_BaseTypeRNA *)PyObject_NEW( BPy_BaseTypeRNA, &pyrna_basetype_Type );
4207
 
        self->arraydim = self->arrayoffset = 0; /* unused but better set */
 
6466
 
 
6467
        self = (BPy_BaseTypeRNA *)PyObject_NEW(BPy_BaseTypeRNA, &pyrna_basetype_Type);
4208
6468
 
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");
4212
 
 
 
6472
#ifdef USE_WEAKREFS
 
6473
        self->in_weakreflist = NULL;
 
6474
#endif
4213
6475
        return (PyObject *)self;
4214
6476
}
4215
6477
 
4217
6479
{
4218
6480
        BPy_StructRNA *py_srna = NULL;
4219
6481
        StructRNA *srna;
4220
 
        
 
6482
 
4221
6483
        /* ack, PyObject_GetAttrString wont look up this types tp_dict first :/ */
4222
 
        if(PyType_Check(self)) {
4223
 
                py_srna = (BPy_StructRNA *)PyDict_GetItemString(((PyTypeObject *)self)->tp_dict, "bl_rna");
 
6484
        if (PyType_Check(self)) {
 
6485
                py_srna = (BPy_StructRNA *)PyDict_GetItem(((PyTypeObject *)self)->tp_dict, bpy_intern_str_bl_rna);
4224
6486
                Py_XINCREF(py_srna);
4225
6487
        }
4226
6488
 
4227
 
        if(parent) {
 
6489
        if (parent) {
4228
6490
                /* be very careful with this since it will return a parent classes srna.
4229
6491
                 * modifying this will do confusing stuff! */
4230
 
                if(py_srna==NULL)
4231
 
                        py_srna = (BPy_StructRNA*)PyObject_GetAttrString(self, "bl_rna");
4232
 
        }
4233
 
 
4234
 
        if(py_srna==NULL) {
4235
 
                 PyErr_Format(PyExc_SystemError, "%.200s internal error, self of type '%.200s' had no bl_rna attribute, should never happen", error_prefix, Py_TYPE(self)->tp_name);
4236
 
                return NULL;
4237
 
        }
4238
 
 
4239
 
        if(!BPy_StructRNA_Check(py_srna)) {
4240
 
                 PyErr_Format(PyExc_SystemError, "%.200s internal error, bl_rna was of type '%.200s', instead of %.200s instance", error_prefix, Py_TYPE(py_srna)->tp_name, pyrna_struct_Type.tp_name);
4241
 
                 Py_DECREF(py_srna);
4242
 
                return NULL;
4243
 
        }
4244
 
 
4245
 
        if(py_srna->ptr.type != &RNA_Struct) {
4246
 
                PyErr_Format(PyExc_SystemError, "%.200s internal error, bl_rna was not a RNA_Struct type of rna struct", error_prefix);
4247
 
                 Py_DECREF(py_srna);
4248
 
                return NULL;
4249
 
        }
4250
 
 
4251
 
        srna= py_srna->ptr.data;
 
6492
                if (py_srna == NULL)
 
6493
                        py_srna = (BPy_StructRNA *)PyObject_GetAttr(self, bpy_intern_str_bl_rna);
 
6494
        }
 
6495
 
 
6496
        if (py_srna == NULL) {
 
6497
                PyErr_Format(PyExc_RuntimeError,
 
6498
                             "%.200s, missing bl_rna attribute from '%.200s' instance (may not be registered)",
 
6499
                             error_prefix, Py_TYPE(self)->tp_name);
 
6500
                return NULL;
 
6501
        }
 
6502
 
 
6503
        if (!BPy_StructRNA_Check(py_srna)) {
 
6504
                PyErr_Format(PyExc_TypeError,
 
6505
                             "%.200s, bl_rna attribute wrong type '%.200s' on '%.200s'' instance",
 
6506
                             error_prefix, Py_TYPE(py_srna)->tp_name,
 
6507
                             Py_TYPE(self)->tp_name);
 
6508
                Py_DECREF(py_srna);
 
6509
                return NULL;
 
6510
        }
 
6511
 
 
6512
        if (py_srna->ptr.type != &RNA_Struct) {
 
6513
                PyErr_Format(PyExc_TypeError,
 
6514
                             "%.200s, bl_rna attribute not a RNA_Struct, on '%.200s'' instance",
 
6515
                             error_prefix, Py_TYPE(self)->tp_name);
 
6516
                Py_DECREF(py_srna);
 
6517
                return NULL;
 
6518
        }
 
6519
 
 
6520
        srna = py_srna->ptr.data;
4252
6521
        Py_DECREF(py_srna);
4253
6522
 
4254
6523
        return srna;
4256
6525
 
4257
6526
/* Orphan functions, not sure where they should go */
4258
6527
/* get the srna for methods attached to types */
4259
 
/* */
 
6528
/*
 
6529
 * Caller needs to raise error.*/
4260
6530
StructRNA *srna_from_self(PyObject *self, const char *error_prefix)
4261
6531
{
4262
 
        /* a bit sloppy but would cause a very confusing bug if
4263
 
         * an error happened to be set here */
4264
 
        PyErr_Clear();
4265
6532
 
4266
 
        if(self==NULL) {
 
6533
        if (self == NULL) {
4267
6534
                return NULL;
4268
6535
        }
4269
6536
        else if (PyCapsule_CheckExact(self)) {
4270
6537
                return PyCapsule_GetPointer(self, NULL);
4271
6538
        }
4272
 
        else if (PyType_Check(self)==0) {
 
6539
        else if (PyType_Check(self) == 0) {
4273
6540
                return NULL;
4274
6541
        }
4275
 
        /* These cases above not errors, they just mean the type was not compatible
4276
 
         * After this any errors will be raised in the script */
4277
 
 
4278
 
        return pyrna_struct_as_srna(self, 0, error_prefix);
 
6542
        else {
 
6543
                /* These cases above not errors, they just mean the type was not compatible
 
6544
                 * After this any errors will be raised in the script */
 
6545
 
 
6546
                PyObject *error_type, *error_value, *error_traceback;
 
6547
                StructRNA *srna;
 
6548
 
 
6549
                PyErr_Fetch(&error_type, &error_value, &error_traceback);
 
6550
                PyErr_Clear();
 
6551
 
 
6552
                srna = pyrna_struct_as_srna(self, 0, error_prefix);
 
6553
 
 
6554
                if (!PyErr_Occurred()) {
 
6555
                        PyErr_Restore(error_type, error_value, error_traceback);
 
6556
                }
 
6557
 
 
6558
                return srna;
 
6559
        }
4279
6560
}
4280
6561
 
4281
 
static int deferred_register_prop(StructRNA *srna, PyObject *item, PyObject *key, PyObject *dummy_args)
 
6562
static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item)
4282
6563
{
4283
6564
        /* We only care about results from C which
4284
6565
         * are for sure types, save some time with error */
4285
 
        if(PyTuple_CheckExact(item) && PyTuple_GET_SIZE(item)==2) {
4286
 
 
4287
 
                PyObject *py_func_ptr, *py_kw, *py_srna_cobject, *py_ret;
4288
 
                PyObject *(*pyfunc)(PyObject *, PyObject *, PyObject *);
4289
 
 
4290
 
                if(PyArg_ParseTuple(item, "O!O!", &PyCapsule_Type, &py_func_ptr, &PyDict_Type, &py_kw)) {
4291
 
 
4292
 
                        if(*_PyUnicode_AsString(key)=='_') {
4293
 
                                PyErr_Format(PyExc_ValueError, "bpy_struct \"%.200s\" registration error: %.200s could not register because the property starts with an '_'\n", RNA_struct_identifier(srna), _PyUnicode_AsString(key));
4294
 
                                Py_DECREF(dummy_args);
 
6566
        if (pyrna_is_deferred_prop(item)) {
 
6567
 
 
6568
                PyObject *py_func, *py_kw, *py_srna_cobject, *py_ret;
 
6569
 
 
6570
                if (PyArg_ParseTuple(item, "OO!", &py_func, &PyDict_Type, &py_kw)) {
 
6571
                        PyObject *args_fake;
 
6572
 
 
6573
                        if (*_PyUnicode_AsString(key) == '_') {
 
6574
                                PyErr_Format(PyExc_ValueError,
 
6575
                                             "bpy_struct \"%.200s\" registration error: "
 
6576
                                             "%.200s could not register because the property starts with an '_'\n",
 
6577
                                             RNA_struct_identifier(srna), _PyUnicode_AsString(key));
4295
6578
                                return -1;
4296
6579
                        }
4297
 
                        pyfunc = PyCapsule_GetPointer(py_func_ptr, NULL);
4298
6580
                        py_srna_cobject = PyCapsule_New(srna, NULL, NULL);
4299
6581
 
4300
6582
                        /* not 100% nice :/, modifies the dict passed, should be ok */
4301
 
                        PyDict_SetItemString(py_kw, "attr", key);
4302
 
 
4303
 
                        py_ret = pyfunc(py_srna_cobject, dummy_args, py_kw);
4304
 
                        Py_DECREF(py_srna_cobject);
4305
 
 
4306
 
                        if(py_ret) {
 
6583
                        PyDict_SetItem(py_kw, bpy_intern_str_attr, key);
 
6584
 
 
6585
                        args_fake = PyTuple_New(1);
 
6586
                        PyTuple_SET_ITEM(args_fake, 0, py_srna_cobject);
 
6587
 
 
6588
                        py_ret = PyObject_Call(py_func, args_fake, py_kw);
 
6589
 
 
6590
                        if (py_ret) {
4307
6591
                                Py_DECREF(py_ret);
 
6592
                                Py_DECREF(args_fake); /* free's py_srna_cobject too */
4308
6593
                        }
4309
6594
                        else {
 
6595
                                /* _must_ print before decreffing args_fake */
4310
6596
                                PyErr_Print();
4311
6597
                                PyErr_Clear();
4312
6598
 
4313
 
                                // PyLineSpit();
4314
 
                                PyErr_Format(PyExc_ValueError, "bpy_struct \"%.200s\" registration error: %.200s could not register\n", RNA_struct_identifier(srna), _PyUnicode_AsString(key));
 
6599
                                Py_DECREF(args_fake); /* free's py_srna_cobject too */
4315
6600
 
4316
 
                                Py_DECREF(dummy_args);
 
6601
                                // PyC_LineSpit();
 
6602
                                PyErr_Format(PyExc_ValueError,
 
6603
                                             "bpy_struct \"%.200s\" registration error: "
 
6604
                                             "%.200s could not register\n",
 
6605
                                             RNA_struct_identifier(srna), _PyUnicode_AsString(key));
4317
6606
                                return -1;
4318
6607
                        }
4319
6608
                }
4321
6610
                        /* Since this is a class dict, ignore args that can't be passed */
4322
6611
 
4323
6612
                        /* for testing only */
4324
 
                        /* PyObSpit("Why doesn't this work??", item);
4325
 
                        PyErr_Print(); */
 
6613
#if 0
 
6614
                        PyC_ObSpit("Why doesn't this work??", item);
 
6615
                        PyErr_Print();
 
6616
#endif
4326
6617
                        PyErr_Clear();
4327
6618
                }
4328
6619
        }
4330
6621
        return 0;
4331
6622
}
4332
6623
 
4333
 
int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
 
6624
static int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
4334
6625
{
4335
6626
        PyObject *item, *key;
4336
6627
        PyObject *order;
4337
 
        PyObject *dummy_args;
4338
6628
        Py_ssize_t pos = 0;
4339
 
        int ret;
4340
 
 
4341
 
        dummy_args = PyTuple_New(0);
4342
 
 
4343
 
        order= PyDict_GetItemString(class_dict, "order");
4344
 
 
4345
 
        if(order==NULL)
4346
 
                PyErr_Clear();
4347
 
 
4348
 
        if(order && PyList_Check(order)) {
4349
 
                for(pos= 0; pos<PyList_GET_SIZE(order); pos++) {
4350
 
                        key= PyList_GET_ITEM(order, pos);
4351
 
                        item= PyDict_GetItem(class_dict, key);
4352
 
                        ret= deferred_register_prop(srna, item, key, dummy_args);
4353
 
                        if(ret==-1)
4354
 
                                break;
 
6629
        int ret = 0;
 
6630
 
 
6631
        /* in both cases PyDict_CheckExact(class_dict) will be true even
 
6632
         * though Operators have a metaclass dict namespace */
 
6633
 
 
6634
        if ((order = PyDict_GetItem(class_dict, bpy_intern_str_order)) && PyList_CheckExact(order)) {
 
6635
                for (pos = 0; pos < PyList_GET_SIZE(order); pos++) {
 
6636
                        key = PyList_GET_ITEM(order, pos);
 
6637
                        /* however unlikely its possible
 
6638
                         * fails in py 3.3 beta with __qualname__ */
 
6639
                        if ((item = PyDict_GetItem(class_dict, key))) {
 
6640
                                ret = deferred_register_prop(srna, key, item);
 
6641
                                if (ret != 0) {
 
6642
                                        break;
 
6643
                                }
 
6644
                        }
4355
6645
                }
4356
6646
        }
4357
6647
        else {
4358
6648
                while (PyDict_Next(class_dict, &pos, &key, &item)) {
4359
 
                        ret= deferred_register_prop(srna, item, key, dummy_args);
 
6649
                        ret = deferred_register_prop(srna, key, item);
4360
6650
 
4361
 
                        if(ret==-1)
 
6651
                        if (ret != 0)
4362
6652
                                break;
4363
6653
                }
4364
6654
        }
4365
6655
 
4366
 
        Py_DECREF(dummy_args);
4367
 
 
4368
 
        return 0;
 
6656
        return ret;
 
6657
}
 
6658
 
 
6659
static int pyrna_deferred_register_class_recursive(StructRNA *srna, PyTypeObject *py_class)
 
6660
{
 
6661
        const int len = PyTuple_GET_SIZE(py_class->tp_bases);
 
6662
        int i, ret;
 
6663
 
 
6664
        /* first scan base classes for registerable properties */
 
6665
        for (i = 0; i < len; i++) {
 
6666
                PyTypeObject *py_superclass = (PyTypeObject *)PyTuple_GET_ITEM(py_class->tp_bases, i);
 
6667
 
 
6668
                /* the rules for using these base classes are not clear,
 
6669
                 * 'object' is of course not worth looking into and
 
6670
                 * existing subclasses of RNA would cause a lot more dictionary
 
6671
                 * looping then is needed (SomeOperator would scan Operator.__dict__)
 
6672
                 * which is harmless but not at all useful.
 
6673
                 *
 
6674
                 * So only scan base classes which are not subclasses if blender types.
 
6675
                 * This best fits having 'mix-in' classes for operators and render engines.
 
6676
                 */
 
6677
                if (py_superclass != &PyBaseObject_Type &&
 
6678
                    !PyObject_IsSubclass((PyObject *)py_superclass, (PyObject *)&pyrna_struct_Type))
 
6679
                {
 
6680
                        ret = pyrna_deferred_register_class_recursive(srna, py_superclass);
 
6681
 
 
6682
                        if (ret != 0) {
 
6683
                                return ret;
 
6684
                        }
 
6685
                }
 
6686
        }
 
6687
 
 
6688
        /* not register out own properties */
 
6689
        return pyrna_deferred_register_props(srna, py_class->tp_dict); /* getattr(..., "__dict__") returns a proxy */
 
6690
}
 
6691
 
 
6692
int pyrna_deferred_register_class(StructRNA *srna, PyObject *py_class)
 
6693
{
 
6694
        /* Panels and Menus don't need this
 
6695
         * save some time and skip the checks here */
 
6696
        if (!RNA_struct_idprops_register_check(srna))
 
6697
                return 0;
 
6698
 
 
6699
        return pyrna_deferred_register_class_recursive(srna, (PyTypeObject *)py_class);
4369
6700
}
4370
6701
 
4371
6702
/*-------------------- Type Registration ------------------------*/
4372
6703
 
4373
6704
static int rna_function_arg_count(FunctionRNA *func)
4374
6705
{
4375
 
        const ListBase *lb= RNA_function_defined_parameters(func);
 
6706
        const ListBase *lb = RNA_function_defined_parameters(func);
4376
6707
        PropertyRNA *parm;
4377
6708
        Link *link;
4378
 
        int count= 1;
 
6709
        int count = (RNA_function_flag(func) & FUNC_NO_SELF) ? 0 : 1;
4379
6710
 
4380
 
        for(link=lb->first; link; link=link->next) {
4381
 
                parm= (PropertyRNA*)link;
4382
 
                if(!(RNA_property_flag(parm) & PROP_OUTPUT))
 
6711
        for (link = lb->first; link; link = link->next) {
 
6712
                parm = (PropertyRNA *)link;
 
6713
                if (!(RNA_property_flag(parm) & PROP_OUTPUT))
4383
6714
                        count++;
4384
6715
        }
4385
 
        
 
6716
 
4386
6717
        return count;
4387
6718
}
4388
6719
 
4392
6723
        Link *link;
4393
6724
        FunctionRNA *func;
4394
6725
        PropertyRNA *prop;
4395
 
        StructRNA *srna= dummyptr->type;
4396
 
        const char *class_type= RNA_struct_identifier(srna);
4397
 
        PyObject *py_class= (PyObject*)py_data;
4398
 
        PyObject *base_class= RNA_struct_py_type_get(srna);
4399
 
        PyObject *item, *fitem;
4400
 
        PyObject *py_arg_count;
 
6726
        StructRNA *srna = dummyptr->type;
 
6727
        const char *class_type = RNA_struct_identifier(srna);
 
6728
        PyObject *py_class = (PyObject *)py_data;
 
6729
        PyObject *base_class = RNA_struct_py_type_get(srna);
 
6730
        PyObject *item;
4401
6731
        int i, flag, arg_count, func_arg_count;
4402
6732
        const char *py_class_name = ((PyTypeObject *)py_class)->tp_name; // __name__
4403
6733
 
4404
6734
 
4405
6735
        if (base_class) {
4406
6736
                if (!PyObject_IsSubclass(py_class, base_class)) {
4407
 
                        PyErr_Format( PyExc_TypeError, "expected %.200s subclass of class \"%.200s\"", class_type, py_class_name);
 
6737
                        PyErr_Format(PyExc_TypeError,
 
6738
                                     "expected %.200s subclass of class \"%.200s\"",
 
6739
                                     class_type, py_class_name);
4408
6740
                        return -1;
4409
6741
                }
4410
6742
        }
4411
6743
 
4412
6744
        /* verify callback functions */
4413
 
        lb= RNA_struct_defined_functions(srna);
4414
 
        i= 0;
4415
 
        for(link=lb->first; link; link=link->next) {
4416
 
                func= (FunctionRNA*)link;
4417
 
                flag= RNA_function_flag(func);
 
6745
        lb = RNA_struct_type_functions(srna);
 
6746
        i = 0;
 
6747
        for (link = lb->first; link; link = link->next) {
 
6748
                func = (FunctionRNA *)link;
 
6749
                flag = RNA_function_flag(func);
4418
6750
 
4419
 
                if(!(flag & FUNC_REGISTER))
 
6751
                if (!(flag & FUNC_REGISTER))
4420
6752
                        continue;
4421
6753
 
4422
6754
                item = PyObject_GetAttrString(py_class, RNA_function_identifier(func));
4423
6755
 
4424
 
                have_function[i]= (item != NULL);
 
6756
                have_function[i] = (item != NULL);
4425
6757
                i++;
4426
6758
 
4427
 
                if (item==NULL) {
4428
 
                        if ((flag & FUNC_REGISTER_OPTIONAL)==0) {
4429
 
                                PyErr_Format( PyExc_AttributeError, "expected %.200s, %.200s class to have an \"%.200s\" attribute", class_type, py_class_name, RNA_function_identifier(func));
 
6759
                if (item == NULL) {
 
6760
                        if ((flag & FUNC_REGISTER_OPTIONAL) == 0) {
 
6761
                                PyErr_Format(PyExc_AttributeError,
 
6762
                                             "expected %.200s, %.200s class to have an \"%.200s\" attribute",
 
6763
                                             class_type, py_class_name,
 
6764
                                             RNA_function_identifier(func));
4430
6765
                                return -1;
4431
6766
                        }
4432
6767
 
4433
6768
                        PyErr_Clear();
4434
6769
                }
4435
6770
                else {
4436
 
                        Py_DECREF(item); /* no need to keep a ref, the class owns it */
4437
 
 
4438
 
                        if (PyMethod_Check(item))
4439
 
                                fitem= PyMethod_Function(item); /* py 2.x */
4440
 
                        else
4441
 
                                fitem= item; /* py 3.x */
4442
 
 
4443
 
                        if (PyFunction_Check(fitem)==0) {
4444
 
                                PyErr_Format( PyExc_TypeError, "expected %.200s, %.200s class \"%.200s\" attribute to be a function", class_type, py_class_name, RNA_function_identifier(func));
4445
 
                                return -1;
4446
 
                        }
4447
 
 
4448
 
                        func_arg_count= rna_function_arg_count(func);
4449
 
 
4450
 
                        if (func_arg_count >= 0) { /* -1 if we dont care*/
4451
 
                                py_arg_count = PyObject_GetAttrString(PyFunction_GET_CODE(fitem), "co_argcount");
4452
 
                                arg_count = PyLong_AsSsize_t(py_arg_count);
4453
 
                                Py_DECREF(py_arg_count);
 
6771
                        Py_DECREF(item); /* no need to keep a ref, the class owns it (technically we should keep a ref but...) */
 
6772
                        if (flag & FUNC_NO_SELF) {
 
6773
                                if (PyMethod_Check(item) == 0) {
 
6774
                                        PyErr_Format(PyExc_TypeError,
 
6775
                                                     "expected %.200s, %.200s class \"%.200s\" attribute to be a method, not a %.200s",
 
6776
                                                     class_type, py_class_name, RNA_function_identifier(func), Py_TYPE(item)->tp_name);
 
6777
                                        return -1;
 
6778
                                }
 
6779
                                item = ((PyMethodObject *)item)->im_func;
 
6780
                        }
 
6781
                        else {
 
6782
                                if (PyFunction_Check(item) == 0) {
 
6783
                                        PyErr_Format(PyExc_TypeError,
 
6784
                                                     "expected %.200s, %.200s class \"%.200s\" attribute to be a function, not a %.200s",
 
6785
                                                     class_type, py_class_name, RNA_function_identifier(func), Py_TYPE(item)->tp_name);
 
6786
                                        return -1;
 
6787
                                }
 
6788
                        }
 
6789
 
 
6790
                        func_arg_count = rna_function_arg_count(func);
 
6791
 
 
6792
                        if (func_arg_count >= 0) { /* -1 if we don't care*/
 
6793
                                arg_count = ((PyCodeObject *)PyFunction_GET_CODE(item))->co_argcount;
 
6794
 
 
6795
                                /* note, the number of args we check for and the number of args we give to
 
6796
                                 * @classmethods are different (quirk of python),
 
6797
                                 * this is why rna_function_arg_count() doesn't return the value -1*/
 
6798
                                if (flag & FUNC_NO_SELF)
 
6799
                                        func_arg_count++;
4454
6800
 
4455
6801
                                if (arg_count != func_arg_count) {
4456
 
                                        PyErr_Format( PyExc_AttributeError, "expected %.200s, %.200s class \"%.200s\" function to have %d args, found %d", class_type, py_class_name, RNA_function_identifier(func), func_arg_count, arg_count);
 
6802
                                        PyErr_Format(PyExc_ValueError,
 
6803
                                                     "expected %.200s, %.200s class \"%.200s\" function to have %d args, found %d",
 
6804
                                                     class_type, py_class_name, RNA_function_identifier(func),
 
6805
                                                     func_arg_count, arg_count);
4457
6806
                                        return -1;
4458
6807
                                }
4459
6808
                        }
4461
6810
        }
4462
6811
 
4463
6812
        /* verify properties */
4464
 
        lb= RNA_struct_defined_properties(srna);
4465
 
        for(link=lb->first; link; link=link->next) {
 
6813
        lb = RNA_struct_type_properties(srna);
 
6814
        for (link = lb->first; link; link = link->next) {
4466
6815
                const char *identifier;
4467
 
                prop= (PropertyRNA*)link;
4468
 
                flag= RNA_property_flag(prop);
 
6816
                prop = (PropertyRNA *)link;
 
6817
                flag = RNA_property_flag(prop);
4469
6818
 
4470
 
                if(!(flag & PROP_REGISTER))
 
6819
                if (!(flag & PROP_REGISTER))
4471
6820
                        continue;
4472
6821
 
4473
 
                identifier= RNA_property_identifier(prop);
 
6822
                identifier = RNA_property_identifier(prop);
4474
6823
                item = PyObject_GetAttrString(py_class, identifier);
4475
6824
 
4476
 
                if (item==NULL) {
 
6825
                if (item == NULL) {
4477
6826
                        /* Sneaky workaround to use the class name as the bl_idname */
4478
6827
 
4479
 
#define         BPY_REPLACEMENT_STRING(rna_attr, py_attr) \
4480
 
                        if(strcmp(identifier, rna_attr) == 0) { \
4481
 
                                item= PyObject_GetAttrString(py_class, py_attr); \
4482
 
                                if(item && item != Py_None) { \
4483
 
                                        if(pyrna_py_to_prop(dummyptr, prop, NULL, NULL, item, "validating class error:") != 0) { \
4484
 
                                                Py_DECREF(item); \
4485
 
                                                return -1; \
4486
 
                                        } \
4487
 
                                } \
4488
 
                                Py_XDECREF(item); \
4489
 
                        } \
 
6828
#define     BPY_REPLACEMENT_STRING(rna_attr, py_attr)                         \
 
6829
        if (strcmp(identifier, rna_attr) == 0) {                                  \
 
6830
                item = PyObject_GetAttrString(py_class, py_attr);                     \
 
6831
                if (item && item != Py_None) {                                        \
 
6832
                        if (pyrna_py_to_prop(dummyptr, prop, NULL,                        \
 
6833
                                             item, "validating class:") != 0)             \
 
6834
                        {                                                                 \
 
6835
                                Py_DECREF(item);                                              \
 
6836
                                return -1;                                                    \
 
6837
                        }                                                                 \
 
6838
                }                                                                     \
 
6839
                Py_XDECREF(item);                                                     \
 
6840
        }                                                                         \
4490
6841
 
4491
6842
 
4492
6843
                        BPY_REPLACEMENT_STRING("bl_idname", "__name__");
4493
6844
                        BPY_REPLACEMENT_STRING("bl_description", "__doc__");
4494
6845
 
4495
 
#undef          BPY_REPLACEMENT_STRING
 
6846
#undef      BPY_REPLACEMENT_STRING
4496
6847
 
4497
6848
                        if (item == NULL && (((flag & PROP_REGISTER_OPTIONAL) != PROP_REGISTER_OPTIONAL))) {
4498
 
                                PyErr_Format( PyExc_AttributeError, "expected %.200s, %.200s class to have an \"%.200s\" attribute", class_type, py_class_name, identifier);
 
6849
                                PyErr_Format(PyExc_AttributeError,
 
6850
                                             "expected %.200s, %.200s class to have an \"%.200s\" attribute",
 
6851
                                             class_type, py_class_name, identifier);
4499
6852
                                return -1;
4500
6853
                        }
4501
6854
 
4504
6857
                else {
4505
6858
                        Py_DECREF(item); /* no need to keep a ref, the class owns it */
4506
6859
 
4507
 
                        if(pyrna_py_to_prop(dummyptr, prop, NULL, NULL, item, "validating class error:") != 0)
 
6860
                        if (pyrna_py_to_prop(dummyptr, prop, NULL, item, "validating class:") != 0)
4508
6861
                                return -1;
4509
6862
                }
4510
6863
        }
4512
6865
        return 0;
4513
6866
}
4514
6867
 
4515
 
extern void BPY_update_modules( void ); //XXX temp solution
4516
 
 
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)
4519
6870
{
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;
4527
 
 
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;
 
6880
 
 
6881
        /* annoying!, need to check if the screen gets set to NULL which is a
 
6882
         * hint that the file was actually re-loaded. */
 
6883
        char is_valid_wm;
 
6884
 
 
6885
        PropertyRNA *pret_single = NULL;
 
6886
        void *retdata_single = NULL;
4530
6887
 
4531
6888
        PyGILState_STATE gilstate;
4532
6889
 
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) ||*/
 
6896
                                   !is_operator);
 
6897
#endif
 
6898
 
 
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));
 
6904
                return -1;
 
6905
        }
 
6906
 
 
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! */
 
6909
        if (C == NULL)
 
6910
                C = BPy_GetContext();
 
6911
 
 
6912
        is_valid_wm = (CTX_wm_manager(C) != NULL);
 
6913
 
4534
6914
        bpy_context_set(C, &gilstate);
4535
6915
 
4536
 
        py_class= RNA_struct_py_type_get(ptr->type);
4537
 
        
4538
 
        /* exception, operators store their PyObjects for re-use */
4539
 
        if(ptr->data) {
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;
 
6916
        if (!is_static) {
 
6917
                /* some datatypes (operator, render engine) can store PyObjects for re-use */
 
6918
                if (ptr->data) {
 
6919
                        void **instance = RNA_struct_instance(ptr);
 
6920
 
 
6921
                        if (instance) {
 
6922
                                if (*instance) {
 
6923
                                        py_class_instance = *instance;
 
6924
                                        Py_INCREF(py_class_instance);
 
6925
                                }
 
6926
                                else {
 
6927
                                        /* store the instance here once its created */
 
6928
                                        py_class_instance_store = instance;
 
6929
                                }
 
6930
                        }
 
6931
                }
 
6932
                /* end exception */
 
6933
 
 
6934
                if (py_class_instance == NULL)
 
6935
                        py_srna = pyrna_struct_CreatePyObject(ptr);
 
6936
 
 
6937
                if (py_class_instance) {
 
6938
                        /* special case, instance is cached */
 
6939
                }
 
6940
                else if (py_srna == NULL) {
 
6941
                        py_class_instance = NULL;
 
6942
                }
 
6943
                else if (py_srna == Py_None) { /* probably wont ever happen but possible */
 
6944
                        Py_DECREF(py_srna);
 
6945
                        py_class_instance = NULL;
 
6946
                }
 
6947
                else {
 
6948
#if 1
 
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().
 
6952
                         */
 
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__ */
 
6957
#endif
 
6958
 
 
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) {
 
6962
                                        Py_DECREF(py_srna);
 
6963
                                        py_srna = NULL;
 
6964
                                        /* err set below */
 
6965
                                }
 
6966
                                Py_DECREF(args);
 
6967
#ifdef USE_PEDANTIC_WRITE
 
6968
                                rna_disallow_writes = prev_write;
 
6969
#endif
 
6970
                        }
 
6971
                        py_class_instance = py_srna;
 
6972
 
 
6973
#else
 
6974
                        const int prev_write = rna_disallow_writes;
 
6975
                        rna_disallow_writes = TRUE;
 
6976
 
 
6977
                        /* 'almost' all the time calling the class isn't needed.
 
6978
                         * We could just do... */
 
6979
#if 0
 
6980
                        py_class_instance = py_srna;
 
6981
                        Py_INCREF(py_class_instance);
 
6982
#endif
 
6983
                        /*
 
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.
 
6987
                         */
 
6988
                        args = PyTuple_New(1);
 
6989
                        PyTuple_SET_ITEM(args, 0, py_srna);
 
6990
                        py_class_instance = PyObject_Call(py_class, args, NULL);
 
6991
                        Py_DECREF(args);
 
6992
 
 
6993
                        rna_disallow_writes = prev_write;
 
6994
 
 
6995
#endif
 
6996
 
 
6997
                        if (py_class_instance == NULL) {
 
6998
                                err = -1; /* so the error is not overridden below */
 
6999
                        }
 
7000
                        else if (py_class_instance_store) {
 
7001
                                *py_class_instance_store = py_class_instance;
4544
7002
                                Py_INCREF(py_class_instance);
4545
7003
                        }
4546
 
                        else {
4547
 
                                /* store the instance here once its created */
4548
 
                                py_class_instance_store= &op->py_instance;
4549
 
                        }
4550
 
                }
4551
 
        }
4552
 
        /* end exception */
4553
 
 
4554
 
        if(py_class_instance==NULL)
4555
 
                py_srna= pyrna_struct_CreatePyObject(ptr);
4556
 
 
4557
 
        if(py_class_instance) {
4558
 
                /* special case, instance is cached */
4559
 
        }
4560
 
        else if(py_srna == NULL) {
4561
 
                py_class_instance = NULL;
4562
 
        }
4563
 
        else if(py_srna == Py_None) { /* probably wont ever happen but possible */
4564
 
                Py_DECREF(py_srna);
4565
 
                py_class_instance = NULL;
4566
 
        }
4567
 
        else {
4568
 
                args = PyTuple_New(1);
4569
 
                PyTuple_SET_ITEM(args, 0, py_srna);
4570
 
                py_class_instance = PyObject_Call(py_class, args, NULL);
4571
 
                Py_DECREF(args);
4572
 
 
4573
 
                if(py_class_instance_store) {
4574
 
                        *py_class_instance_store = py_class_instance;
4575
 
                        Py_INCREF(py_class_instance);
4576
 
                }
4577
 
        }
4578
 
 
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);
4582
 
 
4583
 
                if(item) {
 
7004
                }
 
7005
        }
 
7006
 
 
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);
 
7010
 
 
7011
                if (item) {
4584
7012
                        RNA_pointer_create(NULL, &RNA_Function, func, &funcptr);
4585
7013
 
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);
 
7015
 
 
7016
                        if (is_static) {
 
7017
                                i = 0;
 
7018
                        }
 
7019
                        else {
 
7020
                                PyTuple_SET_ITEM(args, 0, py_class_instance);
 
7021
                                i = 1;
 
7022
                        }
4588
7023
 
4589
7024
                        RNA_parameter_list_begin(parms, &iter);
4590
7025
 
4591
7026
                        /* parse function parameters */
4592
 
                        for (i= 1; iter.valid; RNA_parameter_list_next(&iter)) {
4593
 
                                parm= iter.parm;
4594
 
                                flag= RNA_property_flag(parm);
 
7027
                        for (; iter.valid; RNA_parameter_list_next(&iter)) {
 
7028
                                parm = iter.parm;
 
7029
                                flag = RNA_property_flag(parm);
4595
7030
 
4596
7031
                                /* only useful for single argument returns, we'll need another list loop for multiple */
4597
7032
                                if (flag & PROP_OUTPUT) {
4598
7033
                                        ret_len++;
4599
 
                                        if (pret_single==NULL) {
4600
 
                                                pret_single= parm;
4601
 
                                                retdata_single= iter.data;
 
7034
                                        if (pret_single == NULL) {
 
7035
                                                pret_single = parm;
 
7036
                                                retdata_single = iter.data;
4602
7037
                                        }
4603
7038
 
4604
7039
                                        continue;
4605
7040
                                }
4606
7041
 
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);
4609
7044
                                i++;
4610
7045
                        }
4611
7046
 
 
7047
#ifdef USE_PEDANTIC_WRITE
 
7048
                        rna_disallow_writes = is_readonly ? TRUE : FALSE;
 
7049
#endif
 
7050
                        /* *** Main Caller *** */
 
7051
 
4612
7052
                        ret = PyObject_Call(item, args, NULL);
4613
7053
 
 
7054
                        /* *** Done Calling *** */
 
7055
 
 
7056
#ifdef USE_PEDANTIC_WRITE
 
7057
                        rna_disallow_writes = FALSE;
 
7058
#endif
 
7059
 
4614
7060
                        RNA_parameter_list_end(&iter);
4615
7061
                        Py_DECREF(item);
4616
7062
                        Py_DECREF(args);
4618
7064
                else {
4619
7065
                        PyErr_Print();
4620
7066
                        PyErr_Clear();
4621
 
                        PyErr_Format(PyExc_TypeError, "could not find function %.200s in %.200s to execute callback.", RNA_function_identifier(func), RNA_struct_identifier(ptr->type));
4622
 
                        err= -1;
 
7067
                        PyErr_Format(PyExc_TypeError,
 
7068
                                     "could not find function %.200s in %.200s to execute callback",
 
7069
                                     RNA_function_identifier(func), RNA_struct_identifier(ptr->type));
 
7070
                        err = -1;
4623
7071
                }
4624
7072
        }
4625
7073
        else {
4626
 
                PyErr_Format(PyExc_RuntimeError, "could not create instance of %.200s to call callback function %.200s.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func));
4627
 
                err= -1;
 
7074
                /* the error may be already set if the class instance couldn't be created */
 
7075
                if (err != -1) {
 
7076
                        PyErr_Format(PyExc_RuntimeError,
 
7077
                                     "could not create instance of %.200s to call callback function %.200s",
 
7078
                                     RNA_struct_identifier(ptr->type), RNA_function_identifier(func));
 
7079
                        err = -1;
 
7080
                }
4628
7081
        }
4629
7082
 
4630
7083
        if (ret == NULL) { /* covers py_class_instance failing too */
4631
 
                err= -1;
 
7084
                err = -1;
4632
7085
        }
4633
7086
        else {
4634
 
                if(ret_len==1) {
4635
 
                        err= pyrna_py_to_prop(&funcptr, pret_single, parms, retdata_single, ret, "calling class function:");
 
7087
                if (ret_len == 0 && ret != Py_None) {
 
7088
                        PyErr_Format(PyExc_RuntimeError,
 
7089
                                     "expected class %.200s, function %.200s to return None, not %.200s",
 
7090
                                     RNA_struct_identifier(ptr->type), RNA_function_identifier(func),
 
7091
                                     Py_TYPE(ret)->tp_name);
 
7092
                        err = -1;
 
7093
                }
 
7094
                else if (ret_len == 1) {
 
7095
                        err = pyrna_py_to_prop(&funcptr, pret_single, retdata_single, ret, "");
 
7096
 
 
7097
                        /* when calling operator funcs only gives Function.result with
 
7098
                         * no line number since the func has finished calling on error,
 
7099
                         * re-raise the exception with more info since it would be slow to
 
7100
                         * create prefix on every call (when there are no errors) */
 
7101
                        if (err == -1) {
 
7102
                                PyC_Err_Format_Prefix(PyExc_RuntimeError,
 
7103
                                                      "class %.200s, function %.200s: incompatible return value ",
 
7104
                                                      RNA_struct_identifier(ptr->type), RNA_function_identifier(func));
 
7105
                        }
4636
7106
                }
4637
7107
                else if (ret_len > 1) {
4638
7108
 
4639
 
                        if(PyTuple_Check(ret)==0) {
4640
 
                                PyErr_Format(PyExc_RuntimeError, "expected class %.200s, function %.200s to return a tuple of size %d.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func), ret_len);
4641
 
                                err= -1;
 
7109
                        if (PyTuple_Check(ret) == 0) {
 
7110
                                PyErr_Format(PyExc_RuntimeError,
 
7111
                                             "expected class %.200s, function %.200s to return a tuple of size %d, not %.200s",
 
7112
                                             RNA_struct_identifier(ptr->type), RNA_function_identifier(func),
 
7113
                                             ret_len, Py_TYPE(ret)->tp_name);
 
7114
                                err = -1;
4642
7115
                        }
4643
7116
                        else if (PyTuple_GET_SIZE(ret) != ret_len) {
4644
 
                                PyErr_Format(PyExc_RuntimeError, "class %.200s, function %.200s to returned %d items, expected %d.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func), PyTuple_GET_SIZE(ret), ret_len);
4645
 
                                err= -1;
 
7117
                                PyErr_Format(PyExc_RuntimeError,
 
7118
                                             "class %.200s, function %.200s to returned %d items, expected %d",
 
7119
                                             RNA_struct_identifier(ptr->type), RNA_function_identifier(func),
 
7120
                                             PyTuple_GET_SIZE(ret), ret_len);
 
7121
                                err = -1;
4646
7122
                        }
4647
7123
                        else {
4648
7124
 
4649
7125
                                RNA_parameter_list_begin(parms, &iter);
4650
7126
 
4651
7127
                                /* parse function parameters */
4652
 
                                for (i= 0; iter.valid; RNA_parameter_list_next(&iter)) {
4653
 
                                        parm= iter.parm;
4654
 
                                        flag= RNA_property_flag(parm);
 
7128
                                for (i = 0; iter.valid; RNA_parameter_list_next(&iter)) {
 
7129
                                        parm = iter.parm;
 
7130
                                        flag = RNA_property_flag(parm);
4655
7131
 
4656
7132
                                        /* only useful for single argument returns, we'll need another list loop for multiple */
4657
7133
                                        if (flag & PROP_OUTPUT) {
4658
 
                                                err= pyrna_py_to_prop(&funcptr, parm, parms, iter.data, PyTuple_GET_ITEM(ret, i++), "calling class function:");
4659
 
                                                if(err)
 
7134
                                                err = pyrna_py_to_prop(&funcptr, parm, iter.data,
 
7135
                                                                       PyTuple_GET_ITEM(ret, i++),
 
7136
                                                                       "calling class function:");
 
7137
                                                if (err) {
4660
7138
                                                        break;
 
7139
                                                }
4661
7140
                                        }
4662
7141
                                }
4663
7142
 
4667
7146
                Py_DECREF(ret);
4668
7147
        }
4669
7148
 
4670
 
        if(err != 0) {
 
7149
        if (err != 0) {
 
7150
                ReportList *reports;
 
7151
                /* alert the user, else they wont know unless they see the console. */
 
7152
                if ((!is_static) &&
 
7153
                    (ptr->data) &&
 
7154
                    (RNA_struct_is_a(ptr->type, &RNA_Operator)) &&
 
7155
                    (is_valid_wm == (CTX_wm_manager(C) != NULL)))
 
7156
                {
 
7157
                        wmOperator *op = ptr->data;
 
7158
                        reports = op->reports;
 
7159
                }
 
7160
                else {
 
7161
                        /* wont alert users but they can view in 'info' space */
 
7162
                        reports = CTX_wm_reports(C);
 
7163
                }
 
7164
 
 
7165
                BPy_errors_to_report(reports);
 
7166
 
 
7167
                /* also print in the console for py */
4671
7168
                PyErr_Print();
4672
7169
                PyErr_Clear();
4673
7170
        }
4674
7171
 
4675
7172
        bpy_context_clear(C, &gilstate);
4676
 
        
 
7173
 
4677
7174
        return err;
4678
7175
}
4679
7176
 
4680
7177
static void bpy_class_free(void *pyob_ptr)
4681
7178
{
4682
 
        PyObject *self= (PyObject *)pyob_ptr;
 
7179
        PyObject *self = (PyObject *)pyob_ptr;
4683
7180
        PyGILState_STATE gilstate;
4684
7181
 
4685
7182
        gilstate = PyGILState_Ensure();
4686
7183
 
4687
7184
        // breaks re-registering classes
4688
 
        // PyDict_Clear(((PyTypeObject*)self)->tp_dict);
 
7185
        // PyDict_Clear(((PyTypeObject *)self)->tp_dict);
4689
7186
        //
4690
7187
        // remove the rna attribute instead.
4691
 
        PyDict_DelItemString(((PyTypeObject *)self)->tp_dict, "bl_rna");
4692
 
        if(PyErr_Occurred())
 
7188
        PyDict_DelItem(((PyTypeObject *)self)->tp_dict, bpy_intern_str_bl_rna);
 
7189
        if (PyErr_Occurred())
4693
7190
                PyErr_Clear();
4694
7191
 
4695
 
        if(G.f&G_DEBUG) {
4696
 
                if(self->ob_refcnt > 1) {
4697
 
                        PyObSpit("zombie class - ref should be 1", self);
 
7192
#if 0 /* needs further investigation, too annoying so quiet for now */
 
7193
        if (G.debug & G_DEBUG_PYTHON) {
 
7194
                if (self->ob_refcnt > 1) {
 
7195
                        PyC_ObSpit("zombie class - ref should be 1", self);
4698
7196
                }
4699
7197
        }
4700
 
 
 
7198
#endif
4701
7199
        Py_DECREF((PyObject *)pyob_ptr);
4702
7200
 
4703
7201
        PyGILState_Release(gilstate);
4709
7207
 
4710
7208
        PointerRNA ptr;
4711
7209
        PropertyRNA *prop;
4712
 
        
 
7210
 
4713
7211
        gilstate = PyGILState_Ensure();
4714
7212
 
4715
7213
        /* avoid doing this lookup for every getattr */
4717
7215
        prop = RNA_struct_find_property(&ptr, "structs");
4718
7216
 
4719
7217
        RNA_PROP_BEGIN(&ptr, itemptr, prop) {
4720
 
                Py_DECREF(pyrna_struct_Subtype(&itemptr));
 
7218
                PyObject *item = pyrna_struct_Subtype(&itemptr);
 
7219
                if (item == NULL) {
 
7220
                        if (PyErr_Occurred()) {
 
7221
                                PyErr_Print();
 
7222
                                PyErr_Clear();
 
7223
                        }
 
7224
                }
 
7225
                else {
 
7226
                        Py_DECREF(item);
 
7227
                }
4721
7228
        }
4722
7229
        RNA_PROP_END;
4723
7230
 
4736
7243
 
4737
7244
 
4738
7245
        RNA_PROP_BEGIN(&ptr, itemptr, prop) {
4739
 
                StructRNA *srna= srna_from_ptr(&itemptr);
4740
 
                void *py_ptr= RNA_struct_py_type_get(srna);
 
7246
                StructRNA *srna = srna_from_ptr(&itemptr);
 
7247
                void *py_ptr = RNA_struct_py_type_get(srna);
4741
7248
 
4742
 
                if(py_ptr) {
4743
 
#if 0   // XXX - should be able to do this but makes python crash on exit
 
7249
                if (py_ptr) {
 
7250
#if 0   // XXX - should be able to do this but makes python crash on exit
4744
7251
                        bpy_class_free(py_ptr);
4745
7252
#endif
4746
7253
                        RNA_struct_py_type_set(srna, NULL);
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
4761
7268
 * */
4762
 
 
4763
 
PyObject *pyrna_basetype_register(PyObject *self, PyObject *py_class)
 
7269
PyDoc_STRVAR(pyrna_register_class_doc,
 
7270
".. method:: register_class(cls)\n"
 
7271
"\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"
 
7275
"\n"
 
7276
"   If the class has a *register* class method it will be called\n"
 
7277
"   before registration.\n"
 
7278
"\n"
 
7279
"   .. note::\n"
 
7280
"\n"
 
7281
"      :exc:`ValueError` exception is raised if the class is not a\n"
 
7282
"      subclass of a registerable blender class.\n"
 
7283
"\n"
 
7284
);
 
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)
4764
7287
{
4765
 
        bContext *C= NULL;
 
7288
        bContext *C = NULL;
4766
7289
        ReportList reports;
4767
7290
        StructRegisterFunc reg;
4768
7291
        StructRNA *srna;
4769
7292
        StructRNA *srna_new;
4770
 
        PyObject *item;
4771
 
        const char *identifier= "";
4772
 
 
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;
 
7295
 
 
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);
 
7300
                return NULL;
 
7301
        }
 
7302
 
 
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");
 
7307
                return NULL;
 
7308
        }
 
7309
 
 
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);
4775
7315
                return NULL;
4776
7316
        }
4777
7317
 
4778
7318
        /* warning: gets parent classes srna, only for the register function */
4779
 
        srna= pyrna_struct_as_srna(py_class, 1, "bpy.types.register(...):");
4780
 
        if(srna==NULL)
4781
 
                return NULL;
4782
 
        
 
7319
        srna = pyrna_struct_as_srna(py_class, 1, "register_class(...):");
 
7320
        if (srna == NULL)
 
7321
                return NULL;
 
7322
 
 
7323
        /* fails in cases, cant use this check but would like to :| */
 
7324
#if 0
 
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));
 
7329
                return NULL;
 
7330
        }
 
7331
#endif
 
7332
 
4783
7333
        /* check that we have a register callback for this type */
4784
 
        reg= RNA_struct_register(srna);
 
7334
        reg = RNA_struct_register(srna);
4785
7335
 
4786
 
        if(!reg) {
4787
 
                PyErr_SetString(PyExc_ValueError, "bpy.types.register(...): expected a Type subclassed from a registerable rna type (no register supported).");
 
7336
        if (!reg) {
 
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));
4788
7341
                return NULL;
4789
7342
        }
4790
 
        
 
7343
 
4791
7344
        /* get the context, so register callback can do necessary refreshes */
4792
 
        C= BPy_GetContext();
 
7345
        C = BPy_GetContext();
4793
7346
 
4794
7347
        /* call the register callback with reports & identifier */
4795
7348
        BKE_reports_init(&reports, RPT_STORE);
4796
7349
 
4797
 
        item= PyObject_GetAttrString(py_class, "__name__");
4798
 
 
4799
 
        if(item) {
4800
 
                identifier= _PyUnicode_AsString(item);
4801
 
                Py_DECREF(item); /* no need to keep a ref, the class owns it */
4802
 
        }
4803
 
 
4804
 
        srna_new= reg(C, &reports, py_class, identifier, bpy_class_validate, bpy_class_call, bpy_class_free);
4805
 
 
4806
 
        if(!srna_new) {
4807
 
                BPy_reports_to_error(&reports);
4808
 
                BKE_reports_clear(&reports);
4809
 
                return NULL;
4810
 
        }
4811
 
 
4812
 
        BKE_reports_clear(&reports);
 
7350
        identifier = ((PyTypeObject *)py_class)->tp_name;
 
7351
 
 
7352
        srna_new = reg(CTX_data_main(C), &reports, py_class, identifier,
 
7353
                       bpy_class_validate, bpy_class_call, bpy_class_free);
 
7354
 
 
7355
        if (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE) == -1)
 
7356
                return NULL;
 
7357
 
 
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)
 
7361
                return NULL;
4813
7362
 
4814
7363
        pyrna_subtype_set_rna(py_class, srna_new); /* takes a ref to py_class */
4815
7364
 
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.
4820
7369
        }
4821
7370
 
4822
7371
        /* Can't use this because it returns a dict proxy
4823
7372
         *
4824
 
         * item= PyObject_GetAttrString(py_class, "__dict__");
 
7373
         * item = PyObject_GetAttrString(py_class, "__dict__");
4825
7374
         */
4826
 
        item= ((PyTypeObject*)py_class)->tp_dict;
4827
 
        if(item) {
4828
 
                if(pyrna_deferred_register_props(srna_new, item)!=0) {
 
7375
        if (pyrna_deferred_register_class(srna_new, py_class) != 0)
 
7376
                return NULL;
 
7377
 
 
7378
        /* call classed register method () */
 
7379
        py_cls_meth = PyObject_GetAttr(py_class, bpy_intern_str_register);
 
7380
        if (py_cls_meth == NULL) {
 
7381
                PyErr_Clear();
 
7382
        }
 
7383
        else {
 
7384
                PyObject *ret = PyObject_CallObject(py_cls_meth, NULL);
 
7385
                if (ret) {
 
7386
                        Py_DECREF(ret);
 
7387
                }
 
7388
                else {
4829
7389
                        return NULL;
4830
7390
                }
4831
7391
        }
4832
 
        else {
4833
 
                PyErr_Clear();
4834
 
        }
4835
7392
 
4836
7393
        Py_RETURN_NONE;
4837
7394
}
4838
7395
 
4839
 
PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *py_class)
4840
 
{
4841
 
        bContext *C= NULL;
 
7396
 
 
7397
static int pyrna_srna_contains_pointer_prop_srna(StructRNA *srna_props, StructRNA *srna, const char **prop_identifier)
 
7398
{
 
7399
        PropertyRNA *prop;
 
7400
        LinkData *link;
 
7401
 
 
7402
        /* verify properties */
 
7403
        const ListBase *lb = RNA_struct_type_properties(srna);
 
7404
 
 
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)) {
 
7408
                        PointerRNA tptr;
 
7409
                        RNA_pointer_create(NULL, &RNA_Struct, srna_props, &tptr);
 
7410
 
 
7411
                        if (RNA_property_pointer_type(&tptr, prop) == srna) {
 
7412
                                *prop_identifier = RNA_property_identifier(prop);
 
7413
                                return 1;
 
7414
                        }
 
7415
                }
 
7416
        }
 
7417
 
 
7418
        return 0;
 
7419
}
 
7420
 
 
7421
PyDoc_STRVAR(pyrna_unregister_class_doc,
 
7422
".. method:: unregister_class(cls)\n"
 
7423
"\n"
 
7424
"   Unload the python class from blender.\n"
 
7425
"\n"
 
7426
"   If the class has an *unregister* class method it will be called\n"
 
7427
"   before unregistering.\n"
 
7428
);
 
7429
PyMethodDef meth_bpy_unregister_class = {
 
7430
        "unregister_class", pyrna_unregister_class, METH_O, pyrna_unregister_class_doc
 
7431
};
 
7432
static PyObject *pyrna_unregister_class(PyObject *UNUSED(self), PyObject *py_class)
 
7433
{
 
7434
        bContext *C = NULL;
4842
7435
        StructUnregisterFunc unreg;
4843
7436
        StructRNA *srna;
4844
 
 
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.");
4847
 
                return NULL;
4848
 
        }*/
4849
 
 
4850
 
        srna= pyrna_struct_as_srna(py_class, 0, "bpy.types.unregister(...):");
4851
 
        if(srna==NULL)
4852
 
                return NULL;
4853
 
        
 
7437
        PyObject *py_cls_meth;
 
7438
 
 
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);
 
7443
                return NULL;
 
7444
        }
 
7445
 
 
7446
#if 0
 
7447
        if (PyDict_GetItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna) == NULL) {
 
7448
                PWM_cursor_wait(0);
 
7449
                PyErr_SetString(PyExc_ValueError, "unregister_class(): not a registered as a subclass");
 
7450
                return NULL;
 
7451
        }
 
7452
#endif
 
7453
 
 
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);
 
7459
                return NULL;
 
7460
        }
 
7461
 
 
7462
        srna = pyrna_struct_as_srna(py_class, 0, "unregister_class(...):");
 
7463
        if (srna == NULL)
 
7464
                return NULL;
 
7465
 
4854
7466
        /* check that we have a unregister callback for this type */
4855
 
        unreg= RNA_struct_unregister(srna);
 
7467
        unreg = RNA_struct_unregister(srna);
4856
7468
 
4857
 
        if(!unreg) {
4858
 
                PyErr_SetString(PyExc_ValueError, "bpy.types.unregister(...): expected a Type subclassed from a registerable rna type (no unregister supported).");
 
7469
        if (!unreg) {
 
7470
                PyErr_SetString(PyExc_ValueError,
 
7471
                                "unregister_class(...): "
 
7472
                                "expected a Type subclassed from a registerable rna type (no unregister supported)");
4859
7473
                return NULL;
4860
7474
        }
4861
 
        
 
7475
 
 
7476
        /* call classed unregister method */
 
7477
        py_cls_meth = PyObject_GetAttr(py_class, bpy_intern_str_unregister);
 
7478
        if (py_cls_meth == NULL) {
 
7479
                PyErr_Clear();
 
7480
        }
 
7481
        else {
 
7482
                PyObject *ret = PyObject_CallObject(py_cls_meth, NULL);
 
7483
                if (ret) {
 
7484
                        Py_DECREF(ret);
 
7485
                }
 
7486
                else {
 
7487
                        return NULL;
 
7488
                }
 
7489
        }
 
7490
 
 
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;
 
7495
                PointerRNA ptr_rna;
 
7496
                PropertyRNA *prop_rna;
 
7497
                const char *prop_identifier = NULL;
 
7498
 
 
7499
                RNA_blender_rna_pointer_create(&ptr_rna);
 
7500
                prop_rna = RNA_struct_find_property(&ptr_rna, "structs");
 
7501
 
 
7502
 
 
7503
 
 
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)) {
 
7508
                                break;
 
7509
                        }
 
7510
                }
 
7511
                RNA_PROP_END;
 
7512
 
 
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);
 
7517
                        return NULL;
 
7518
                }
 
7519
        }
 
7520
 
4862
7521
        /* get the context, so register callback can do necessary refreshes */
4863
 
        C= BPy_GetContext();
 
7522
        C = BPy_GetContext();
4864
7523
 
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 */
4867
7526
 
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;
4871
7530
 
4872
7531
        Py_RETURN_NONE;
4873
7532
}