Blender  V2.93
bpy_rna.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
27 #include <Python.h>
28 
29 #include <float.h> /* FLT_MIN/MAX */
30 #include <stddef.h>
31 
32 #include "RNA_types.h"
33 
34 #include "BLI_bitmap.h"
35 #include "BLI_dynstr.h"
36 #include "BLI_listbase.h"
37 #include "BLI_math_rotation.h"
38 #include "BLI_string.h"
39 #include "BLI_utildefines.h"
40 
41 #include "BPY_extern.h"
42 #include "BPY_extern_clog.h"
43 
44 #include "bpy_capi_utils.h"
45 #include "bpy_intern_string.h"
46 #include "bpy_props.h"
47 #include "bpy_rna.h"
48 #include "bpy_rna_anim.h"
49 #include "bpy_rna_callback.h"
50 
51 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
52 # include "BLI_ghash.h"
53 #endif
54 
55 #include "RNA_access.h"
56 #include "RNA_define.h" /* RNA_def_property_free_identifier */
57 #include "RNA_enum_types.h"
58 
59 #include "CLG_log.h"
60 
61 #include "MEM_guardedalloc.h"
62 
63 #include "BKE_context.h"
64 #include "BKE_global.h" /* evil G.* */
65 #include "BKE_idprop.h"
66 #include "BKE_idtype.h"
67 #include "BKE_main.h"
68 #include "BKE_report.h"
69 
70 /* Only for types. */
71 #include "BKE_node.h"
72 
73 #include "DEG_depsgraph_query.h"
74 
75 #include "../generic/idprop_py_api.h" /* For IDprop lookups. */
76 #include "../generic/py_capi_utils.h"
77 #include "../generic/python_utildefines.h"
78 
79 #define USE_PEDANTIC_WRITE
80 #define USE_MATHUTILS
81 #define USE_STRING_COERCE
82 
90 #define USE_POSTPONED_ANNOTATIONS
91 
92 /* Unfortunately Python needs to hold a global reference to the context.
93  * If we remove this is means `bpy.context` won't be usable from some parts of the code:
94  * `bpy.app.handler` callbacks for example.
95  * Even though this is arguably "correct", it's going to cause problems for existing scripts,
96  * so accept having this for the time being. */
97 BPy_StructRNA *bpy_context_module = NULL; /* for fast access */
98 
99 static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);
100 static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self);
101 
102 static PyObject *pyrna_register_class(PyObject *self, PyObject *py_class);
103 static PyObject *pyrna_unregister_class(PyObject *self, PyObject *py_class);
104 
105 #define BPY_DOC_ID_PROP_TYPE_NOTE \
106  " .. note::\n" \
107  "\n" \
108  " Only the :class:`bpy.types.ID`, :class:`bpy.types.Bone` and\n" \
109  " :class:`bpy.types.PoseBone` classes support custom properties.\n"
110 
112 {
113  if (pysrna->ptr.type) {
114  return 0;
115  }
116  PyErr_Format(
117  PyExc_ReferenceError, "StructRNA of type %.200s has been removed", Py_TYPE(pysrna)->tp_name);
118  return -1;
119 }
120 
122 {
123  if (self->ptr.type) {
124  return 0;
125  }
126  PyErr_Format(PyExc_ReferenceError,
127  "PropertyRNA of type %.200s.%.200s has been removed",
128  Py_TYPE(self)->tp_name,
130  return -1;
131 }
132 
134 {
136 }
137 
138 #ifdef USE_PYRNA_INVALIDATE_GC
139 # define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g) + 1))
140 
141 /* Only for sizeof(). */
142 struct gc_generation {
143  PyGC_Head head;
144  int threshold;
145  int count;
146 } gc_generation;
147 
148 static void id_release_gc(struct ID *id)
149 {
150  uint j;
151  // uint i = 0;
152  for (j = 0; j < 3; j++) {
153  /* Hack below to get the 2 other lists from _PyGC_generation0 that are normally not exposed. */
154  PyGC_Head *gen = (PyGC_Head *)(((char *)_PyGC_generation0) + (sizeof(gc_generation) * j));
155  PyGC_Head *g = gen->gc.gc_next;
156  while ((g = g->gc.gc_next) != gen) {
157  PyObject *ob = FROM_GC(g);
158  if (PyType_IsSubtype(Py_TYPE(ob), &pyrna_struct_Type) ||
159  PyType_IsSubtype(Py_TYPE(ob), &pyrna_prop_Type)) {
161  if (ob_ptr->ptr.owner_id == id) {
162  pyrna_invalidate(ob_ptr);
163  // printf("freeing: %p %s, %.200s\n", (void *)ob, id->name, Py_TYPE(ob)->tp_name);
164  // i++;
165  }
166  }
167  }
168  }
169  // printf("id_release_gc freed '%s': %d\n", id->name, i);
170 }
171 #endif
172 
173 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
174 //#define DEBUG_RNA_WEAKREF
175 
176 struct GHash *id_weakref_pool = NULL;
177 static PyObject *id_free_weakref_cb(PyObject *weakinfo_pair, PyObject *weakref);
178 static PyMethodDef id_free_weakref_cb_def = {
179  "id_free_weakref_cb", (PyCFunction)id_free_weakref_cb, METH_O, NULL};
180 
181 /* Adds a reference to the list, remember to decref. */
182 static GHash *id_weakref_pool_get(ID *id)
183 {
184  GHash *weakinfo_hash = NULL;
185 
186  if (id_weakref_pool) {
187  weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
188  }
189  else {
190  /* First time, allocate pool. */
191  id_weakref_pool = BLI_ghash_ptr_new("rna_global_pool");
192  weakinfo_hash = NULL;
193  }
194 
195  if (weakinfo_hash == NULL) {
196  /* We use a ghash as a set, we could use libHX's HXMAP_SINGULAR, but would be an extra dep. */
197  weakinfo_hash = BLI_ghash_ptr_new("rna_id");
198  BLI_ghash_insert(id_weakref_pool, id, weakinfo_hash);
199  }
200 
201  return weakinfo_hash;
202 }
203 
204 /* Called from pyrna_struct_CreatePyObject() and pyrna_prop_CreatePyObject(). */
205 static void id_weakref_pool_add(ID *id, BPy_DummyPointerRNA *pyrna)
206 {
207  PyObject *weakref;
208  PyObject *weakref_capsule;
209  PyObject *weakref_cb_py;
210 
211  /* Create a new function instance and insert the list as 'self'
212  * so we can remove ourself from it. */
213  GHash *weakinfo_hash = id_weakref_pool_get(id); /* New or existing. */
214 
215  weakref_capsule = PyCapsule_New(weakinfo_hash, NULL, NULL);
216  weakref_cb_py = PyCFunction_New(&id_free_weakref_cb_def, weakref_capsule);
217  Py_DECREF(weakref_capsule);
218 
219  /* Add weakref to weakinfo_hash list. */
220  weakref = PyWeakref_NewRef((PyObject *)pyrna, weakref_cb_py);
221 
222  Py_DECREF(weakref_cb_py); /* Function owned by the weakref now. */
223 
224  /* Important to add at the end of the hash, since first removal looks at the end. */
225 
226  /* Using a hash table as a set, all 'id's are the same. */
227  BLI_ghash_insert(weakinfo_hash, weakref, id);
228  /* weakinfo_hash owns the weakref */
229 }
230 
231 /* Workaround to get the last id without a lookup. */
232 static ID *_id_tmp_ptr;
233 static void value_id_set(void *id)
234 {
235  _id_tmp_ptr = (ID *)id;
236 }
237 
238 static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash);
239 static PyObject *id_free_weakref_cb(PyObject *weakinfo_pair, PyObject *weakref)
240 {
241  /* Important to search backwards. */
242  GHash *weakinfo_hash = PyCapsule_GetPointer(weakinfo_pair, NULL);
243 
244  if (BLI_ghash_len(weakinfo_hash) > 1) {
245  BLI_ghash_remove(weakinfo_hash, weakref, NULL, NULL);
246  }
247  else { /* Get the last id and free it. */
248  BLI_ghash_remove(weakinfo_hash, weakref, NULL, value_id_set);
249  id_release_weakref_list(_id_tmp_ptr, weakinfo_hash);
250  }
251 
252  Py_DECREF(weakref);
253 
254  Py_RETURN_NONE;
255 }
256 
257 static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash)
258 {
259  GHashIterator weakinfo_hash_iter;
260 
261  BLI_ghashIterator_init(&weakinfo_hash_iter, weakinfo_hash);
262 
263 # ifdef DEBUG_RNA_WEAKREF
264  fprintf(stdout, "id_release_weakref: '%s', %d items\n", id->name, BLI_ghash_len(weakinfo_hash));
265 # endif
266 
267  while (!BLI_ghashIterator_done(&weakinfo_hash_iter)) {
268  PyObject *weakref = (PyObject *)BLI_ghashIterator_getKey(&weakinfo_hash_iter);
269  PyObject *item = PyWeakref_GET_OBJECT(weakref);
270  if (item != Py_None) {
271 
272 # ifdef DEBUG_RNA_WEAKREF
273  PyC_ObSpit("id_release_weakref item ", item);
274 # endif
275 
277  }
278 
279  Py_DECREF(weakref);
280 
281  BLI_ghashIterator_step(&weakinfo_hash_iter);
282  }
283 
284  BLI_ghash_remove(id_weakref_pool, (void *)id, NULL, NULL);
285  BLI_ghash_free(weakinfo_hash, NULL, NULL);
286 
287  if (BLI_ghash_len(id_weakref_pool) == 0) {
288  BLI_ghash_free(id_weakref_pool, NULL, NULL);
289  id_weakref_pool = NULL;
290 # ifdef DEBUG_RNA_WEAKREF
291  printf("id_release_weakref freeing pool\n");
292 # endif
293  }
294 }
295 
296 static void id_release_weakref(struct ID *id)
297 {
298  GHash *weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
299  if (weakinfo_hash) {
300  id_release_weakref_list(id, weakinfo_hash);
301  }
302 }
303 
304 #endif /* USE_PYRNA_INVALIDATE_WEAKREF */
305 
306 void BPY_id_release(struct ID *id)
307 {
308 #ifdef USE_PYRNA_INVALIDATE_GC
309  id_release_gc(id);
310 #endif
311 
312 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
313  if (id_weakref_pool) {
314  PyGILState_STATE gilstate = PyGILState_Ensure();
315 
316  id_release_weakref(id);
317 
318  PyGILState_Release(gilstate);
319  }
320 #endif /* USE_PYRNA_INVALIDATE_WEAKREF */
321 
322  (void)id;
323 }
324 
325 #ifdef USE_PEDANTIC_WRITE
326 static bool rna_disallow_writes = false;
327 
328 static bool rna_id_write_error(PointerRNA *ptr, PyObject *key)
329 {
330  ID *id = ptr->owner_id;
331  if (id) {
332  const short idcode = GS(id->name);
333  /* May need more ID types added here. */
334  if (!ELEM(idcode, ID_WM, ID_SCR, ID_WS)) {
335  const char *idtype = BKE_idtype_idcode_to_name(idcode);
336  const char *pyname;
337  if (key && PyUnicode_Check(key)) {
338  pyname = PyUnicode_AsUTF8(key);
339  }
340  else {
341  pyname = "<UNKNOWN>";
342  }
343 
344  /* Make a nice string error. */
345  BLI_assert(idtype != NULL);
346  PyErr_Format(PyExc_AttributeError,
347  "Writing to ID classes in this context is not allowed: "
348  "%.200s, %.200s datablock, error setting %.200s.%.200s",
349  id->name + 2,
350  idtype,
352  pyname);
353 
354  return true;
355  }
356  }
357  return false;
358 }
359 #endif /* USE_PEDANTIC_WRITE */
360 
361 #ifdef USE_PEDANTIC_WRITE
363 {
364  return !rna_disallow_writes;
365 }
366 
367 void pyrna_write_set(bool val)
368 {
369  rna_disallow_writes = !val;
370 }
371 #else /* USE_PEDANTIC_WRITE */
372 bool pyrna_write_check(void)
373 {
374  return true;
375 }
376 void pyrna_write_set(bool UNUSED(val))
377 {
378  /* pass */
379 }
380 #endif /* USE_PEDANTIC_WRITE */
381 
382 static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self);
383 static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self);
384 static int pyrna_py_to_prop(
385  PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix);
386 static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item);
387 
388 #ifdef USE_MATHUTILS
389 # include "../mathutils/mathutils.h" /* So we can have mathutils callbacks. */
390 
392  PointerRNA *ptr,
393  PropertyRNA *prop,
394  Py_ssize_t start,
395  Py_ssize_t stop,
396  Py_ssize_t length);
398  const short order_fallback,
399  PropertyRNA **r_prop_eul_order);
400 
401 /* bpyrna vector/euler/quat callbacks. */
402 static uchar mathutils_rna_array_cb_index = -1; /* Index for our callbacks. */
403 
404 /* Subtype not used much yet. */
405 # define MATHUTILS_CB_SUBTYPE_EUL 0
406 # define MATHUTILS_CB_SUBTYPE_VEC 1
407 # define MATHUTILS_CB_SUBTYPE_QUAT 2
408 # define MATHUTILS_CB_SUBTYPE_COLOR 3
409 
411 {
412  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
413 
414  PYRNA_PROP_CHECK_INT(self);
415 
416  return self->prop ? 0 : -1;
417 }
418 
419 static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
420 {
421  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
422 
423  PYRNA_PROP_CHECK_INT(self);
424 
425  if (self->prop == NULL) {
426  return -1;
427  }
428 
429  RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
430 
431  /* Euler order exception. */
432  if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
433  EulerObject *eul = (EulerObject *)bmo;
434  PropertyRNA *prop_eul_order = NULL;
435  eul->order = pyrna_rotation_euler_order_get(&self->ptr, eul->order, &prop_eul_order);
436  }
437 
438  return 0;
439 }
440 
441 static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
442 {
443  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
444  float min, max;
445 
446  PYRNA_PROP_CHECK_INT(self);
447 
448  if (self->prop == NULL) {
449  return -1;
450  }
451 
452 # ifdef USE_PEDANTIC_WRITE
454  return -1;
455  }
456 # endif /* USE_PEDANTIC_WRITE */
457 
458  if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
459  PyErr_Format(PyExc_AttributeError,
460  "bpy_prop \"%.200s.%.200s\" is read-only",
461  RNA_struct_identifier(self->ptr.type),
463  return -1;
464  }
465 
466  RNA_property_float_range(&self->ptr, self->prop, &min, &max);
467 
468  if (min != -FLT_MAX || max != FLT_MAX) {
469  int i, len = RNA_property_array_length(&self->ptr, self->prop);
470  for (i = 0; i < len; i++) {
471  CLAMP(bmo->data[i], min, max);
472  }
473  }
474 
475  RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
476  if (RNA_property_update_check(self->prop)) {
477  RNA_property_update(BPY_context_get(), &self->ptr, self->prop);
478  }
479 
480  /* Euler order exception. */
481  if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
482  EulerObject *eul = (EulerObject *)bmo;
483  PropertyRNA *prop_eul_order = NULL;
484  const short order = pyrna_rotation_euler_order_get(&self->ptr, eul->order, &prop_eul_order);
485  if (order != eul->order) {
486  RNA_property_enum_set(&self->ptr, prop_eul_order, eul->order);
487  if (RNA_property_update_check(prop_eul_order)) {
488  RNA_property_update(BPY_context_get(), &self->ptr, prop_eul_order);
489  }
490  }
491  }
492  return 0;
493 }
494 
495 static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
496 {
497  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
498 
499  PYRNA_PROP_CHECK_INT(self);
500 
501  if (self->prop == NULL) {
502  return -1;
503  }
504 
505  bmo->data[index] = RNA_property_float_get_index(&self->ptr, self->prop, index);
506  return 0;
507 }
508 
509 static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
510 {
511  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
512 
513  PYRNA_PROP_CHECK_INT(self);
514 
515  if (self->prop == NULL) {
516  return -1;
517  }
518 
519 # ifdef USE_PEDANTIC_WRITE
521  return -1;
522  }
523 # endif /* USE_PEDANTIC_WRITE */
524 
525  if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
526  PyErr_Format(PyExc_AttributeError,
527  "bpy_prop \"%.200s.%.200s\" is read-only",
528  RNA_struct_identifier(self->ptr.type),
530  return -1;
531  }
532 
533  RNA_property_float_clamp(&self->ptr, self->prop, &bmo->data[index]);
534  RNA_property_float_set_index(&self->ptr, self->prop, index, bmo->data[index]);
535 
536  if (RNA_property_update_check(self->prop)) {
537  RNA_property_update(BPY_context_get(), &self->ptr, self->prop);
538  }
539 
540  return 0;
541 }
542 
549 };
550 
551 /* bpyrna matrix callbacks */
552 static uchar mathutils_rna_matrix_cb_index = -1; /* Index for our callbacks. */
553 
554 static int mathutils_rna_matrix_get(BaseMathObject *bmo, int UNUSED(subtype))
555 {
556  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
557 
558  PYRNA_PROP_CHECK_INT(self);
559 
560  if (self->prop == NULL) {
561  return -1;
562  }
563 
564  RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
565  return 0;
566 }
567 
568 static int mathutils_rna_matrix_set(BaseMathObject *bmo, int UNUSED(subtype))
569 {
570  BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
571 
572  PYRNA_PROP_CHECK_INT(self);
573 
574  if (self->prop == NULL) {
575  return -1;
576  }
577 
578 # ifdef USE_PEDANTIC_WRITE
580  return -1;
581  }
582 # endif /* USE_PEDANTIC_WRITE */
583 
584  if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
585  PyErr_Format(PyExc_AttributeError,
586  "bpy_prop \"%.200s.%.200s\" is read-only",
587  RNA_struct_identifier(self->ptr.type),
589  return -1;
590  }
591 
592  /* Can ignore clamping here. */
593  RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
594 
595  if (RNA_property_update_check(self->prop)) {
596  RNA_property_update(BPY_context_get(), &self->ptr, self->prop);
597  }
598  return 0;
599 }
600 
605  NULL,
606  NULL,
607 };
608 
610  const short order_fallback,
611  PropertyRNA **r_prop_eul_order)
612 {
613  /* Attempt to get order. */
614  if (*r_prop_eul_order == NULL) {
615  *r_prop_eul_order = RNA_struct_find_property(ptr, "rotation_mode");
616  }
617 
618  if (*r_prop_eul_order) {
619  const short order = RNA_property_enum_get(ptr, *r_prop_eul_order);
620  /* Could be quaternion or axis-angle. */
622  return order;
623  }
624  }
625 
626  return order_fallback;
627 }
628 
629 #endif /* USE_MATHUTILS */
630 
635 #define PROP_ALL_VECTOR_SUBTYPES \
636  PROP_COORDS: \
637  case PROP_TRANSLATION: \
638  case PROP_DIRECTION: \
639  case PROP_VELOCITY: \
640  case PROP_ACCELERATION: \
641  case PROP_XYZ: \
642  case PROP_XYZ_LENGTH
643 
645 {
646  PyObject *ret = NULL;
647 
648 #ifdef USE_MATHUTILS
649  int subtype, totdim;
650  int len;
651  const int flag = RNA_property_flag(prop);
652  const int type = RNA_property_type(prop);
653  const bool is_thick = (flag & PROP_THICK_WRAP) != 0;
654 
655  /* disallow dynamic sized arrays to be wrapped since the size could change
656  * to a size mathutils does not support */
657  if (flag & PROP_DYNAMIC) {
658  return NULL;
659  }
660 
662  if (type == PROP_FLOAT) {
663  /* pass */
664  }
665  else if (type == PROP_INT) {
666  if (is_thick) {
667  goto thick_wrap_slice;
668  }
669  else {
670  return NULL;
671  }
672  }
673  else {
674  return NULL;
675  }
676 
677  subtype = RNA_property_subtype(prop);
678  totdim = RNA_property_array_dimension(ptr, prop, NULL);
679 
680  if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) {
681  if (!is_thick) {
682  /* Owned by the mathutils PyObject. */
684  }
685 
686  switch (subtype) {
688  if (len >= 2 && len <= 4) {
689  if (is_thick) {
692  }
693  else {
694  PyObject *vec_cb = Vector_CreatePyObject_cb(
696  Py_DECREF(ret); /* The vector owns 'ret' now. */
697  ret = vec_cb; /* Return the vector instead. */
698  }
699  }
700  break;
701  case PROP_MATRIX:
702  if (len == 16) {
703  if (is_thick) {
705  RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
706  }
707  else {
708  PyObject *mat_cb = Matrix_CreatePyObject_cb(
710  Py_DECREF(ret); /* The matrix owns 'ret' now. */
711  ret = mat_cb; /* Return the matrix instead. */
712  }
713  }
714  else if (len == 9) {
715  if (is_thick) {
717  RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
718  }
719  else {
720  PyObject *mat_cb = Matrix_CreatePyObject_cb(
722  Py_DECREF(ret); /* The matrix owns 'ret' now. */
723  ret = mat_cb; /* Return the matrix instead. */
724  }
725  }
726  break;
727  case PROP_EULER:
728  case PROP_QUATERNION:
729  if (len == 3) { /* Euler. */
730  if (is_thick) {
731  /* Attempt to get order,
732  * only needed for thick types since wrapped with update via callbacks. */
733  PropertyRNA *prop_eul_order = NULL;
735  ptr, EULER_ORDER_XYZ, &prop_eul_order);
736 
737  ret = Euler_CreatePyObject(NULL, order, NULL); /* TODO, get order from RNA. */
739  }
740  else {
741  /* Order will be updated from callback on use. */
742  /* TODO, get order from RNA. */
743  PyObject *eul_cb = Euler_CreatePyObject_cb(
745  Py_DECREF(ret); /* The euler owns 'ret' now. */
746  ret = eul_cb; /* Return the euler instead. */
747  }
748  }
749  else if (len == 4) {
750  if (is_thick) {
753  }
754  else {
755  PyObject *quat_cb = Quaternion_CreatePyObject_cb(
757  Py_DECREF(ret); /* The quat owns 'ret' now. */
758  ret = quat_cb; /* Return the quat instead. */
759  }
760  }
761  break;
762  case PROP_COLOR:
763  case PROP_COLOR_GAMMA:
764  if (len == 3) { /* Color. */
765  if (is_thick) {
768  }
769  else {
770  PyObject *col_cb = Color_CreatePyObject_cb(
772  Py_DECREF(ret); /* The color owns 'ret' now. */
773  ret = col_cb; /* Return the color instead. */
774  }
775  }
776  break;
777  default:
778  break;
779  }
780  }
781 
782  if (ret == NULL) {
783  if (is_thick) {
784  /* This is an array we can't reference (since it is not thin wrappable)
785  * and cannot be coerced into a mathutils type, so return as a list. */
786  thick_wrap_slice:
788  }
789  else {
790  ret = pyrna_prop_CreatePyObject(ptr, prop); /* Owned by the mathutils PyObject. */
791  }
792  }
793 #else /* USE_MATHUTILS */
794  (void)ptr;
795  (void)prop;
796 #endif /* USE_MATHUTILS */
797 
798  return ret;
799 }
800 
805  const char *identifier,
806  int *r_value,
807  const char *error_prefix)
808 {
809  if (RNA_enum_value_from_id(item, identifier, r_value) == 0) {
810  const char *enum_str = BPy_enum_as_string(item);
811  PyErr_Format(
812  PyExc_ValueError, "%s: '%.200s' not found in (%s)", error_prefix, identifier, enum_str);
813  MEM_freeN((void *)enum_str);
814  return -1;
815  }
816 
817  return 0;
818 }
819 
820 /* note on __cmp__:
821  * checking the 'ptr->data' matches works in almost all cases,
822  * however there are a few RNA properties that are fake sub-structs and
823  * share the pointer with the parent, in those cases this happens 'a.b == a'
824  * see: r43352 for example.
825  *
826  * So compare the 'ptr->type' as well to avoid this problem.
827  * It's highly unlikely this would happen that 'ptr->data' and 'ptr->prop' would match,
828  * but _not_ 'ptr->type' but include this check for completeness.
829  * - campbell */
830 
832 {
833  return (((a->ptr.data == b->ptr.data) && (a->ptr.type == b->ptr.type)) ? 0 : -1);
834 }
835 
837 {
838  return (((a->prop == b->prop) && (a->ptr.data == b->ptr.data) && (a->ptr.type == b->ptr.type)) ?
839  0 :
840  -1);
841 }
842 
843 static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
844 {
845  PyObject *res;
846  int ok = -1; /* Zero is true. */
847 
850  }
851 
852  switch (op) {
853  case Py_NE:
854  ok = !ok;
856  case Py_EQ:
857  res = ok ? Py_False : Py_True;
858  break;
859 
860  case Py_LT:
861  case Py_LE:
862  case Py_GT:
863  case Py_GE:
864  res = Py_NotImplemented;
865  break;
866  default:
867  PyErr_BadArgument();
868  return NULL;
869  }
870 
871  return Py_INCREF_RET(res);
872 }
873 
874 static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
875 {
876  PyObject *res;
877  int ok = -1; /* Zero is true. */
878 
881  }
882 
883  switch (op) {
884  case Py_NE:
885  ok = !ok;
887  case Py_EQ:
888  res = ok ? Py_False : Py_True;
889  break;
890 
891  case Py_LT:
892  case Py_LE:
893  case Py_GT:
894  case Py_GE:
895  res = Py_NotImplemented;
896  break;
897  default:
898  PyErr_BadArgument();
899  return NULL;
900  }
901 
902  return Py_INCREF_RET(res);
903 }
904 
905 /*----------------------repr--------------------------------------------*/
906 static PyObject *pyrna_struct_str(BPy_StructRNA *self)
907 {
908  PyObject *ret;
909  const char *name;
910  const char *extra_info = "";
911 
912  if (!PYRNA_STRUCT_IS_VALID(self)) {
913  return PyUnicode_FromFormat("<bpy_struct, %.200s invalid>", Py_TYPE(self)->tp_name);
914  }
915 
916  ID *id = self->ptr.owner_id;
917  if (id && id != DEG_get_original_id(id)) {
918  extra_info = ", evaluated";
919  }
920 
921  /* Print name if available.
922  *
923  * Always include the pointer address since it can help identify unique data,
924  * or when data is re-allocated internally. */
925  name = RNA_struct_name_get_alloc(&self->ptr, NULL, 0, NULL);
926  if (name) {
927  ret = PyUnicode_FromFormat("<bpy_struct, %.200s(\"%.200s\") at %p%s>",
928  RNA_struct_identifier(self->ptr.type),
929  name,
930  self->ptr.data,
931  extra_info);
932  MEM_freeN((void *)name);
933  return ret;
934  }
935 
936  return PyUnicode_FromFormat("<bpy_struct, %.200s at %p%s>",
937  RNA_struct_identifier(self->ptr.type),
938  self->ptr.data,
939  extra_info);
940 }
941 
942 static PyObject *pyrna_struct_repr(BPy_StructRNA *self)
943 {
944  ID *id = self->ptr.owner_id;
945  PyObject *tmp_str;
946  PyObject *ret;
947 
948  if (id == NULL || !PYRNA_STRUCT_IS_VALID(self) || (DEG_get_original_id(id) != id)) {
949  /* fallback */
950  return pyrna_struct_str(self);
951  }
952 
953  tmp_str = PyUnicode_FromString(id->name + 2);
954 
955  if (RNA_struct_is_ID(self->ptr.type) && (id->flag & LIB_EMBEDDED_DATA) == 0) {
956  ret = PyUnicode_FromFormat(
957  "bpy.data.%s[%R]", BKE_idtype_idcode_to_name_plural(GS(id->name)), tmp_str);
958  }
959  else {
960  const char *path;
961  ID *real_id = NULL;
962  path = RNA_path_from_real_ID_to_struct(G_MAIN, &self->ptr, &real_id);
963  if (path != NULL) {
964  /* 'real_id' may be NULL in some cases, although the only valid one is evaluated data,
965  * which should have already been caught above.
966  * So assert, but handle it without crashing for release builds. */
967  BLI_assert(real_id != NULL);
968 
969  if (real_id != NULL) {
970  Py_DECREF(tmp_str);
971  tmp_str = PyUnicode_FromString(real_id->name + 2);
972  ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
974  tmp_str,
975  path);
976  }
977  else {
978  /* Can't find the path, print something useful as a fallback. */
979  ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
981  tmp_str,
982  RNA_struct_identifier(self->ptr.type));
983  }
984  MEM_freeN((void *)path);
985  }
986  else {
987  /* Can't find the path, print something useful as a fallback. */
988  ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
990  tmp_str,
991  RNA_struct_identifier(self->ptr.type));
992  }
993  }
994 
995  Py_DECREF(tmp_str);
996 
997  return ret;
998 }
999 
1000 static PyObject *pyrna_prop_str(BPy_PropertyRNA *self)
1001 {
1002  PyObject *ret;
1003  PointerRNA ptr;
1004  const char *name;
1005  const char *type_id = NULL;
1006  char type_fmt[64] = "";
1007  int type;
1008 
1009  PYRNA_PROP_CHECK_OBJ(self);
1010 
1011  type = RNA_property_type(self->prop);
1012 
1014  /* Should never happen. */
1015  PyErr_SetString(PyExc_RuntimeError, "could not use property type, internal error");
1016  return NULL;
1017  }
1018 
1019  /* This should never fail. */
1020  int len = -1;
1021  char *c = type_fmt;
1022 
1023  while ((*c++ = tolower(*type_id++))) {
1024  }
1025 
1026  if (type == PROP_COLLECTION) {
1028  }
1029  else if (RNA_property_array_check(self->prop)) {
1031  }
1032 
1033  if (len != -1) {
1034  sprintf(--c, "[%d]", len);
1035  }
1036 
1037  /* If a pointer, try to print name of pointer target too. */
1038  if (type == PROP_POINTER) {
1039  ptr = RNA_property_pointer_get(&self->ptr, self->prop);
1040  name = RNA_struct_name_get_alloc(&ptr, NULL, 0, NULL);
1041 
1042  if (name) {
1043  ret = PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s(\"%.200s\")>",
1044  type_fmt,
1045  RNA_struct_identifier(self->ptr.type),
1047  name);
1048  MEM_freeN((void *)name);
1049  return ret;
1050  }
1051  }
1052  if (type == PROP_COLLECTION) {
1053  PointerRNA r_ptr;
1054  if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
1055  return PyUnicode_FromFormat(
1056  "<bpy_%.200s, %.200s>", type_fmt, RNA_struct_identifier(r_ptr.type));
1057  }
1058  }
1059 
1060  return PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s>",
1061  type_fmt,
1062  RNA_struct_identifier(self->ptr.type),
1063  RNA_property_identifier(self->prop));
1064 }
1065 
1066 static PyObject *pyrna_prop_repr_ex(BPy_PropertyRNA *self, const int index_dim, const int index)
1067 {
1068  ID *id = self->ptr.owner_id;
1069  PyObject *tmp_str;
1070  PyObject *ret;
1071  const char *path;
1072 
1073  PYRNA_PROP_CHECK_OBJ(self);
1074 
1075  if (id == NULL) {
1076  /* Fallback. */
1077  return pyrna_prop_str(self);
1078  }
1079 
1080  tmp_str = PyUnicode_FromString(id->name + 2);
1081 
1082  /* Note that using G_MAIN is absolutely not ideal, but we have no access to actual Main DB from
1083  * here. */
1084  ID *real_id = NULL;
1086  G_MAIN, &self->ptr, self->prop, index_dim, index, &real_id);
1087 
1088  if (path) {
1089  if (real_id != id) {
1090  Py_DECREF(tmp_str);
1091  tmp_str = PyUnicode_FromString(real_id->name + 2);
1092  }
1093  const char *data_delim = (path[0] == '[') ? "" : ".";
1094  ret = PyUnicode_FromFormat("bpy.data.%s[%R]%s%s",
1096  tmp_str,
1097  data_delim,
1098  path);
1099 
1100  MEM_freeN((void *)path);
1101  }
1102  else {
1103  /* Can't find the path, print something useful as a fallback. */
1104  ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
1106  tmp_str,
1107  RNA_property_identifier(self->prop));
1108  }
1109 
1110  Py_DECREF(tmp_str);
1111 
1112  return ret;
1113 }
1114 
1115 static PyObject *pyrna_prop_repr(BPy_PropertyRNA *self)
1116 {
1117  return pyrna_prop_repr_ex(self, 0, -1);
1118 }
1119 
1121 {
1122  return pyrna_prop_repr_ex((BPy_PropertyRNA *)self, self->arraydim, self->arrayoffset);
1123 }
1124 
1125 static PyObject *pyrna_func_repr(BPy_FunctionRNA *self)
1126 {
1127  return PyUnicode_FromFormat("<%.200s %.200s.%.200s()>",
1128  Py_TYPE(self)->tp_name,
1129  RNA_struct_identifier(self->ptr.type),
1130  RNA_function_identifier(self->func));
1131 }
1132 
1133 static Py_hash_t pyrna_struct_hash(BPy_StructRNA *self)
1134 {
1135  return _Py_HashPointer(self->ptr.data);
1136 }
1137 
1138 /* From Python's meth_hash v3.1.2. */
1140 {
1141  long x, y;
1142  if (self->ptr.data == NULL) {
1143  x = 0;
1144  }
1145  else {
1146  x = _Py_HashPointer(self->ptr.data);
1147  if (x == -1) {
1148  return -1;
1149  }
1150  }
1151  y = _Py_HashPointer((void *)(self->prop));
1152  if (y == -1) {
1153  return -1;
1154  }
1155  x ^= y;
1156  if (x == -1) {
1157  x = -2;
1158  }
1159  return x;
1160 }
1161 
1162 #ifdef USE_PYRNA_STRUCT_REFERENCE
1163 static int pyrna_struct_traverse(BPy_StructRNA *self, visitproc visit, void *arg)
1164 {
1165  Py_VISIT(self->reference);
1166  return 0;
1167 }
1168 
1169 static int pyrna_struct_clear(BPy_StructRNA *self)
1170 {
1171  Py_CLEAR(self->reference);
1172  return 0;
1173 }
1174 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1175 
1176 /* Use our own dealloc so we can free a property if we use one. */
1178 {
1179 #ifdef PYRNA_FREE_SUPPORT
1180  if (self->freeptr && self->ptr.data) {
1181  IDP_FreeProperty(self->ptr.data);
1182  self->ptr.data = NULL;
1183  }
1184 #endif /* PYRNA_FREE_SUPPORT */
1185 
1186 #ifdef USE_WEAKREFS
1187  if (self->in_weakreflist != NULL) {
1188  PyObject_ClearWeakRefs((PyObject *)self);
1189  }
1190 #endif
1191 
1192 #ifdef USE_PYRNA_STRUCT_REFERENCE
1193  if (self->reference) {
1194  PyObject_GC_UnTrack(self);
1195  pyrna_struct_clear(self);
1196  }
1197 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1198 
1199  /* Note, for subclassed PyObjects calling PyObject_DEL() directly crashes. */
1200  Py_TYPE(self)->tp_free(self);
1201 }
1202 
1203 #ifdef USE_PYRNA_STRUCT_REFERENCE
1204 static void pyrna_struct_reference_set(BPy_StructRNA *self, PyObject *reference)
1205 {
1206  if (self->reference) {
1207  PyObject_GC_UnTrack(self);
1208  Py_CLEAR(self->reference);
1209  }
1210  /* Reference is now NULL. */
1211 
1212  if (reference) {
1213  self->reference = reference;
1214  Py_INCREF(reference);
1215  PyObject_GC_Track(self);
1216  }
1217 }
1218 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1219 
1220 /* Use our own dealloc so we can free a property if we use one. */
1222 {
1223 #ifdef USE_WEAKREFS
1224  if (self->in_weakreflist != NULL) {
1225  PyObject_ClearWeakRefs((PyObject *)self);
1226  }
1227 #endif
1228  /* Note, for subclassed PyObjects calling PyObject_DEL() directly crashes. */
1229  Py_TYPE(self)->tp_free(self);
1230 }
1231 
1233 {
1234 #ifdef USE_WEAKREFS
1235  if (self->in_weakreflist != NULL) {
1236  PyObject_ClearWeakRefs((PyObject *)self);
1237  }
1238 #endif
1239  /* Note, for subclassed PyObjects calling PyObject_DEL() directly crashes. */
1240  Py_TYPE(self)->tp_free(self);
1241 }
1242 
1243 static const char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
1244 {
1245  const EnumPropertyItem *item;
1246  const char *result;
1247  bool free = false;
1248 
1249  RNA_property_enum_items(BPY_context_get(), ptr, prop, &item, NULL, &free);
1250  if (item) {
1251  result = BPy_enum_as_string(item);
1252  }
1253  else {
1254  result = "";
1255  }
1256 
1257  if (free) {
1258  MEM_freeN((void *)item);
1259  }
1260 
1261  return result;
1262 }
1263 
1265  PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *r_value, const char *error_prefix)
1266 {
1267  const char *param = PyUnicode_AsUTF8(item);
1268 
1269  if (param == NULL) {
1270  PyErr_Format(PyExc_TypeError,
1271  "%.200s expected a string enum, not %.200s",
1272  error_prefix,
1273  Py_TYPE(item)->tp_name);
1274  return -1;
1275  }
1276 
1277  if (!RNA_property_enum_value(BPY_context_get(), ptr, prop, param, r_value)) {
1278  const char *enum_str = pyrna_enum_as_string(ptr, prop);
1279  PyErr_Format(PyExc_TypeError,
1280  "%.200s enum \"%.200s\" not found in (%s)",
1281  error_prefix,
1282  param,
1283  enum_str);
1284  MEM_freeN((void *)enum_str);
1285  return -1;
1286  }
1287 
1288  return 0;
1289 }
1290 
1300  PyObject *value,
1301  int type_size,
1302  bool type_convert_sign,
1303  int bitmap_size,
1304  const char *error_prefix)
1305 {
1306  /* Set looping. */
1307  Py_ssize_t pos = 0;
1308  Py_ssize_t hash = 0;
1309  PyObject *key;
1310 
1311  BLI_bitmap *bitmap = BLI_BITMAP_NEW(bitmap_size, __func__);
1312 
1313  while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1314  const char *param = PyUnicode_AsUTF8(key);
1315  if (param == NULL) {
1316  PyErr_Format(PyExc_TypeError,
1317  "%.200s expected a string, not %.200s",
1318  error_prefix,
1319  Py_TYPE(key)->tp_name);
1320  goto error;
1321  }
1322 
1323  int ret;
1324  if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
1325  goto error;
1326  }
1327 
1328  int index = ret;
1329 
1330  if (type_convert_sign) {
1331  if (type_size == 2) {
1332  union {
1333  signed short as_signed;
1334  ushort as_unsigned;
1335  } ret_convert;
1336  ret_convert.as_signed = (signed short)ret;
1337  index = (int)ret_convert.as_unsigned;
1338  }
1339  else if (type_size == 1) {
1340  union {
1341  signed char as_signed;
1342  uchar as_unsigned;
1343  } ret_convert;
1344  ret_convert.as_signed = (signed char)ret;
1345  index = (int)ret_convert.as_unsigned;
1346  }
1347  else {
1349  }
1350  }
1351  BLI_assert(index < bitmap_size);
1352  BLI_BITMAP_ENABLE(bitmap, index);
1353  }
1354 
1355  return bitmap;
1356 
1357 error:
1358  MEM_freeN(bitmap);
1359  return NULL;
1360 }
1361 
1362 /* 'value' _must_ be a set type, error check before calling. */
1364  PyObject *value,
1365  int *r_value,
1366  const char *error_prefix)
1367 {
1368  /* Set of enum items, concatenate all values with OR. */
1369  int ret, flag = 0;
1370 
1371  /* Set looping. */
1372  Py_ssize_t pos = 0;
1373  Py_ssize_t hash = 0;
1374  PyObject *key;
1375 
1376  *r_value = 0;
1377 
1378  while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1379  const char *param = PyUnicode_AsUTF8(key);
1380 
1381  if (param == NULL) {
1382  PyErr_Format(PyExc_TypeError,
1383  "%.200s expected a string, not %.200s",
1384  error_prefix,
1385  Py_TYPE(key)->tp_name);
1386  return -1;
1387  }
1388 
1389  if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
1390  return -1;
1391  }
1392 
1393  flag |= ret;
1394  }
1395 
1396  *r_value = flag;
1397  return 0;
1398 }
1399 
1401  PointerRNA *ptr, PropertyRNA *prop, PyObject *value, int *r_value, const char *error_prefix)
1402 {
1403  const EnumPropertyItem *item;
1404  int ret;
1405  bool free = false;
1406 
1407  *r_value = 0;
1408 
1409  if (!PyAnySet_Check(value)) {
1410  PyErr_Format(PyExc_TypeError,
1411  "%.200s, %.200s.%.200s expected a set, not a %.200s",
1412  error_prefix,
1415  Py_TYPE(value)->tp_name);
1416  return -1;
1417  }
1418 
1419  RNA_property_enum_items(BPY_context_get(), ptr, prop, &item, NULL, &free);
1420 
1421  if (item) {
1422  ret = pyrna_set_to_enum_bitfield(item, value, r_value, error_prefix);
1423  }
1424  else {
1425  if (PySet_GET_SIZE(value)) {
1426  PyErr_Format(PyExc_TypeError,
1427  "%.200s: empty enum \"%.200s\" could not have any values assigned",
1428  error_prefix,
1429  RNA_property_identifier(prop));
1430  ret = -1;
1431  }
1432  else {
1433  ret = 0;
1434  }
1435  }
1436 
1437  if (free) {
1438  MEM_freeN((void *)item);
1439  }
1440 
1441  return ret;
1442 }
1443 
1444 PyObject *pyrna_enum_bitfield_to_py(const EnumPropertyItem *items, int value)
1445 {
1446  PyObject *ret = PySet_New(NULL);
1447  const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
1448 
1449  if (RNA_enum_bitflag_identifiers(items, value, identifier)) {
1450  PyObject *item;
1451  int index;
1452  for (index = 0; identifier[index]; index++) {
1453  item = PyUnicode_FromString(identifier[index]);
1454  PySet_Add(ret, item);
1455  Py_DECREF(item);
1456  }
1457  }
1458 
1459  return ret;
1460 }
1461 
1462 static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val)
1463 {
1464  PyObject *item, *ret = NULL;
1465 
1466  if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1467  const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
1468 
1469  ret = PySet_New(NULL);
1470 
1471  if (RNA_property_enum_bitflag_identifiers(BPY_context_get(), ptr, prop, val, identifier)) {
1472  int index;
1473 
1474  for (index = 0; identifier[index]; index++) {
1475  item = PyUnicode_FromString(identifier[index]);
1476  PySet_Add(ret, item);
1477  Py_DECREF(item);
1478  }
1479  }
1480  }
1481  else {
1482  const char *identifier;
1483  if (RNA_property_enum_identifier(BPY_context_get(), ptr, prop, val, &identifier)) {
1484  ret = PyUnicode_FromString(identifier);
1485  }
1486  else {
1487  /* Static, no need to free. */
1488  const EnumPropertyItem *enum_item;
1489  bool free_dummy;
1490  RNA_property_enum_items_ex(NULL, ptr, prop, true, &enum_item, NULL, &free_dummy);
1491  BLI_assert(!free_dummy);
1492 
1493  /* Do not print warning in case of DummyRNA_NULL_items,
1494  * this one will never match any value... */
1495  if (enum_item != DummyRNA_NULL_items) {
1496  const char *ptr_name = RNA_struct_name_get_alloc(ptr, NULL, 0, NULL);
1497 
1498  /* Prefer not to fail silently in case of API errors, maybe disable it later. */
1500  "current value '%d' "
1501  "matches no enum in '%s', '%s', '%s'",
1502  val,
1504  ptr_name,
1505  RNA_property_identifier(prop));
1506 
1507 #if 0 /* Gives Python decoding errors while generating docs :( */
1508  char error_str[256];
1509  BLI_snprintf(error_str,
1510  sizeof(error_str),
1511  "RNA Warning: Current value \"%d\" "
1512  "matches no enum in '%s', '%s', '%s'",
1513  val,
1515  ptr_name,
1516  RNA_property_identifier(prop));
1517 
1518  PyErr_Warn(PyExc_RuntimeWarning, error_str);
1519 #endif
1520 
1521  if (ptr_name) {
1522  MEM_freeN((void *)ptr_name);
1523  }
1524  }
1525 
1526  ret = PyUnicode_FromString("");
1527 #if 0
1528  PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
1529  ret = NULL;
1530 #endif
1531  }
1532  }
1533 
1534  return ret;
1535 }
1536 
1538 {
1539  PyObject *ret;
1540  const int type = RNA_property_type(prop);
1541 
1542  if (RNA_property_array_check(prop)) {
1543  return pyrna_py_from_array(ptr, prop);
1544  }
1545 
1546  /* See if we can coerce into a Python type - 'PropertyType'. */
1547  switch (type) {
1548  case PROP_BOOLEAN:
1549  ret = PyBool_FromLong(RNA_property_boolean_get(ptr, prop));
1550  break;
1551  case PROP_INT:
1552  ret = PyLong_FromLong(RNA_property_int_get(ptr, prop));
1553  break;
1554  case PROP_FLOAT:
1555  ret = PyFloat_FromDouble(RNA_property_float_get(ptr, prop));
1556  break;
1557  case PROP_STRING: {
1558  const int subtype = RNA_property_subtype(prop);
1559  const char *buf;
1560  int buf_len;
1561  char buf_fixed[32];
1562 
1563  buf = RNA_property_string_get_alloc(ptr, prop, buf_fixed, sizeof(buf_fixed), &buf_len);
1564 #ifdef USE_STRING_COERCE
1565  /* Only file paths get special treatment, they may contain non utf-8 chars. */
1566  if (subtype == PROP_BYTESTRING) {
1567  ret = PyBytes_FromStringAndSize(buf, buf_len);
1568  }
1569  else if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1570  ret = PyC_UnicodeFromByteAndSize(buf, buf_len);
1571  }
1572  else {
1573  ret = PyUnicode_FromStringAndSize(buf, buf_len);
1574  }
1575 #else /* USE_STRING_COERCE */
1576  if (subtype == PROP_BYTESTRING) {
1577  ret = PyBytes_FromStringAndSize(buf, buf_len);
1578  }
1579  else {
1580  ret = PyUnicode_FromStringAndSize(buf, buf_len);
1581  }
1582 #endif /* USE_STRING_COERCE */
1583  if (buf_fixed != buf) {
1584  MEM_freeN((void *)buf);
1585  }
1586  break;
1587  }
1588  case PROP_ENUM: {
1590  break;
1591  }
1592  case PROP_POINTER: {
1593  PointerRNA newptr;
1594  newptr = RNA_property_pointer_get(ptr, prop);
1595  if (newptr.data) {
1596  ret = pyrna_struct_CreatePyObject(&newptr);
1597  }
1598  else {
1599  ret = Py_None;
1600  Py_INCREF(ret);
1601  }
1602  break;
1603  }
1604  case PROP_COLLECTION:
1606  break;
1607  default:
1608  PyErr_Format(PyExc_TypeError,
1609  "bpy_struct internal error: unknown type '%d' (pyrna_prop_to_py)",
1610  type);
1611  ret = NULL;
1612  break;
1613  }
1614 
1615  return ret;
1616 }
1617 
1623  PyObject *kw,
1624  const bool all_args,
1625  const char *error_prefix)
1626 {
1627  int error_val = 0;
1628  int totkw;
1629  const char *arg_name = NULL;
1630  PyObject *item;
1631 
1632  totkw = kw ? PyDict_Size(kw) : 0;
1633 
1634  RNA_STRUCT_BEGIN (ptr, prop) {
1635  arg_name = RNA_property_identifier(prop);
1636 
1637  if (STREQ(arg_name, "rna_type")) {
1638  continue;
1639  }
1640 
1641  if (kw == NULL) {
1642  PyErr_Format(PyExc_TypeError,
1643  "%.200s: no keywords, expected \"%.200s\"",
1644  error_prefix,
1645  arg_name ? arg_name : "<UNKNOWN>");
1646  error_val = -1;
1647  break;
1648  }
1649 
1650  item = PyDict_GetItemString(kw, arg_name); /* Wont set an error. */
1651 
1652  if (item == NULL) {
1653  if (all_args) {
1654  PyErr_Format(PyExc_TypeError,
1655  "%.200s: keyword \"%.200s\" missing",
1656  error_prefix,
1657  arg_name ? arg_name : "<UNKNOWN>");
1658  error_val = -1; /* pyrna_py_to_prop sets the error. */
1659  break;
1660  }
1661  }
1662  else {
1663  if (pyrna_py_to_prop(ptr, prop, NULL, item, error_prefix)) {
1664  error_val = -1;
1665  break;
1666  }
1667  totkw--;
1668  }
1669  }
1671 
1672  if (error_val == 0 && totkw > 0) { /* Some keywords were given that were not used :/. */
1673  PyObject *key, *value;
1674  Py_ssize_t pos = 0;
1675 
1676  while (PyDict_Next(kw, &pos, &key, &value)) {
1677  arg_name = PyUnicode_AsUTF8(key);
1678  if (RNA_struct_find_property(ptr, arg_name) == NULL) {
1679  break;
1680  }
1681  arg_name = NULL;
1682  }
1683 
1684  PyErr_Format(PyExc_TypeError,
1685  "%.200s: keyword \"%.200s\" unrecognized",
1686  error_prefix,
1687  arg_name ? arg_name : "<UNKNOWN>");
1688  error_val = -1;
1689  }
1690 
1691  return error_val;
1692 }
1693 
1694 static PyObject *pyrna_func_to_py(const PointerRNA *ptr, FunctionRNA *func)
1695 {
1696  BPy_FunctionRNA *pyfunc = (BPy_FunctionRNA *)PyObject_NEW(BPy_FunctionRNA, &pyrna_func_Type);
1697  pyfunc->ptr = *ptr;
1698  pyfunc->func = func;
1699  return (PyObject *)pyfunc;
1700 }
1701 
1702 static int pyrna_py_to_prop(
1703  PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix)
1704 {
1705  /* XXX hard limits should be checked here. */
1706  const int type = RNA_property_type(prop);
1707 
1708  if (RNA_property_array_check(prop)) {
1709  /* Done getting the length. */
1710  if (pyrna_py_to_array(ptr, prop, data, value, error_prefix) == -1) {
1711  return -1;
1712  }
1713  }
1714  else {
1715  /* Normal Property (not an array). */
1716 
1717  /* See if we can coerce into a Python type - 'PropertyType'. */
1718  switch (type) {
1719  case PROP_BOOLEAN: {
1720  int param;
1721  /* Prefer not to have an exception here
1722  * however so many poll functions return None or a valid Object.
1723  * It's a hassle to convert these into a bool before returning. */
1724  if (RNA_parameter_flag(prop) & PARM_OUTPUT) {
1725  param = PyObject_IsTrue(value);
1726  }
1727  else {
1728  param = PyC_Long_AsI32(value);
1729 
1730  if (UNLIKELY(param & ~1)) { /* Only accept 0/1. */
1731  param = -1; /* Error out below. */
1732  }
1733  }
1734 
1735  if (param == -1) {
1736  PyErr_Format(PyExc_TypeError,
1737  "%.200s %.200s.%.200s expected True/False or 0/1, not %.200s",
1738  error_prefix,
1741  Py_TYPE(value)->tp_name);
1742  return -1;
1743  }
1744 
1745  if (data) {
1746  *((bool *)data) = param;
1747  }
1748  else {
1749  RNA_property_boolean_set(ptr, prop, param);
1750  }
1751 
1752  break;
1753  }
1754  case PROP_INT: {
1755  int overflow;
1756  const long param = PyLong_AsLongAndOverflow(value, &overflow);
1757  if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
1758  PyErr_Format(PyExc_ValueError,
1759  "%.200s %.200s.%.200s value not in 'int' range "
1760  "(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
1761  error_prefix,
1763  RNA_property_identifier(prop));
1764  return -1;
1765  }
1766  if (param == -1 && PyErr_Occurred()) {
1767  PyErr_Format(PyExc_TypeError,
1768  "%.200s %.200s.%.200s expected an int type, not %.200s",
1769  error_prefix,
1772  Py_TYPE(value)->tp_name);
1773  return -1;
1774  }
1775 
1776  int param_i = (int)param;
1777  if (data) {
1778  RNA_property_int_clamp(ptr, prop, &param_i);
1779  *((int *)data) = param_i;
1780  }
1781  else {
1782  RNA_property_int_set(ptr, prop, param_i);
1783  }
1784 
1785  break;
1786  }
1787  case PROP_FLOAT: {
1788  const float param = PyFloat_AsDouble(value);
1789  if (PyErr_Occurred()) {
1790  PyErr_Format(PyExc_TypeError,
1791  "%.200s %.200s.%.200s expected a float type, not %.200s",
1792  error_prefix,
1795  Py_TYPE(value)->tp_name);
1796  return -1;
1797  }
1798 
1799  if (data) {
1800  RNA_property_float_clamp(ptr, prop, (float *)&param);
1801  *((float *)data) = param;
1802  }
1803  else {
1804  RNA_property_float_set(ptr, prop, param);
1805  }
1806 
1807  break;
1808  }
1809  case PROP_STRING: {
1810  const int subtype = RNA_property_subtype(prop);
1811  const char *param;
1812 
1813  if (value == Py_None) {
1814  if ((RNA_property_flag(prop) & PROP_NEVER_NULL) == 0) {
1815  if (data) {
1816  if (RNA_property_flag(prop) & PROP_THICK_WRAP) {
1817  *(char *)data = 0;
1818  }
1819  else {
1820  *((char **)data) = (char *)NULL;
1821  }
1822  }
1823  else {
1825  }
1826  }
1827  else {
1828  PyC_Err_Format_Prefix(PyExc_TypeError,
1829  "%.200s %.200s.%.200s doesn't support None from string types",
1830  error_prefix,
1832  RNA_property_identifier(prop));
1833  return -1;
1834  }
1835  }
1836  else if (subtype == PROP_BYTESTRING) {
1837 
1838  /* Byte String. */
1839 
1840  param = PyBytes_AsString(value);
1841 
1842  if (param == NULL) {
1843  if (PyBytes_Check(value)) {
1844  /* there was an error assigning a string type,
1845  * rather than setting a new error, prefix the existing one
1846  */
1847  PyC_Err_Format_Prefix(PyExc_TypeError,
1848  "%.200s %.200s.%.200s error assigning bytes",
1849  error_prefix,
1851  RNA_property_identifier(prop));
1852  }
1853  else {
1854  PyErr_Format(PyExc_TypeError,
1855  "%.200s %.200s.%.200s expected a bytes type, not %.200s",
1856  error_prefix,
1859  Py_TYPE(value)->tp_name);
1860  }
1861 
1862  return -1;
1863  }
1864 
1865  if (data) {
1866  if (RNA_property_flag(prop) & PROP_THICK_WRAP) {
1867  BLI_strncpy((char *)data, (char *)param, RNA_property_string_maxlength(prop));
1868  }
1869  else {
1870  *((char **)data) = (char *)param;
1871  }
1872  }
1873  else {
1874  RNA_property_string_set_bytes(ptr, prop, param, PyBytes_Size(value));
1875  }
1876  }
1877  else {
1878  /* Unicode String. */
1879 #ifdef USE_STRING_COERCE
1880  PyObject *value_coerce = NULL;
1881  if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1882  /* TODO, get size. */
1883  param = PyC_UnicodeAsByte(value, &value_coerce);
1884  }
1885  else {
1886  param = PyUnicode_AsUTF8(value);
1887  }
1888 #else /* USE_STRING_COERCE */
1889  param = PyUnicode_AsUTF8(value);
1890 #endif /* USE_STRING_COERCE */
1891 
1892  if (param == NULL) {
1893  if (PyUnicode_Check(value)) {
1894  /* there was an error assigning a string type,
1895  * rather than setting a new error, prefix the existing one
1896  */
1897  PyC_Err_Format_Prefix(PyExc_TypeError,
1898  "%.200s %.200s.%.200s error assigning string",
1899  error_prefix,
1901  RNA_property_identifier(prop));
1902  }
1903  else {
1904  PyErr_Format(PyExc_TypeError,
1905  "%.200s %.200s.%.200s expected a string type, not %.200s",
1906  error_prefix,
1909  Py_TYPE(value)->tp_name);
1910  }
1911 
1912  return -1;
1913  }
1914 
1915  /* Same as bytes. */
1916  /* XXX, this is suspect, but needed for function calls,
1917  * need to see if there's a better way. */
1918  if (data) {
1919  if (RNA_property_flag(prop) & PROP_THICK_WRAP) {
1920  BLI_strncpy((char *)data, (char *)param, RNA_property_string_maxlength(prop));
1921  }
1922  else {
1923  *((char **)data) = (char *)param;
1924  }
1925  }
1926  else {
1927  RNA_property_string_set(ptr, prop, param);
1928  }
1929 
1930 #ifdef USE_STRING_COERCE
1931  Py_XDECREF(value_coerce);
1932 #endif /* USE_STRING_COERCE */
1933  }
1934  break;
1935  }
1936  case PROP_ENUM: {
1937  int val = 0;
1938 
1939  /* Type checking is done by each function. */
1940  if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1941  /* Set of enum items, concatenate all values with OR. */
1942  if (pyrna_prop_to_enum_bitfield(ptr, prop, value, &val, error_prefix) == -1) {
1943  return -1;
1944  }
1945  }
1946  else {
1947  /* Simple enum string. */
1948  if (pyrna_string_to_enum(value, ptr, prop, &val, error_prefix) == -1) {
1949  return -1;
1950  }
1951  }
1952 
1953  if (data) {
1954  *((int *)data) = val;
1955  }
1956  else {
1957  RNA_property_enum_set(ptr, prop, val);
1958  }
1959 
1960  break;
1961  }
1962  case PROP_POINTER: {
1963  PyObject *value_new = NULL;
1964 
1965  StructRNA *ptr_type = RNA_property_pointer_type(ptr, prop);
1966  const int flag = RNA_property_flag(prop);
1967  const int flag_parameter = RNA_parameter_flag(prop);
1968 
1969  /* This is really nasty! Done so we can fake the operator having direct properties, eg:
1970  * layout.prop(self, "filepath")
1971  * ... which in fact should be:
1972  * layout.prop(self.properties, "filepath")
1973  *
1974  * we need to do this trick.
1975  * if the prop is not an operator type and the PyObject is an operator,
1976  * use its properties in place of itself.
1977  *
1978  * This is so bad that it is almost a good reason to do away with fake
1979  * 'self.properties -> self'
1980  * class mixing. If this causes problems in the future it should be removed.
1981  */
1982  if ((ptr_type == &RNA_AnyType) && (BPy_StructRNA_Check(value))) {
1983  const StructRNA *base_type = RNA_struct_base_child_of(
1984  ((const BPy_StructRNA *)value)->ptr.type, NULL);
1985  if (ELEM(base_type, &RNA_Operator, &RNA_Gizmo)) {
1986  value = PyObject_GetAttr(value, bpy_intern_str_properties);
1987  value_new = value;
1988  }
1989  }
1990 
1991  /* if property is an OperatorProperties/GizmoProperties pointer and value is a map,
1992  * forward back to pyrna_pydict_to_props */
1993  if (PyDict_Check(value)) {
1994  const StructRNA *base_type = RNA_struct_base_child_of(ptr_type, NULL);
1995  if (base_type == &RNA_OperatorProperties) {
1996  PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
1997  return pyrna_pydict_to_props(&opptr, value, false, error_prefix);
1998  }
1999  if (base_type == &RNA_GizmoProperties) {
2000  PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
2001  return pyrna_pydict_to_props(&opptr, value, false, error_prefix);
2002  }
2003  }
2004 
2005  /* Another exception, allow passing a collection as an RNA property. */
2006  if (Py_TYPE(value) == &pyrna_prop_collection_Type) { /* Ok to ignore idprop collections. */
2007  PointerRNA c_ptr;
2008  BPy_PropertyRNA *value_prop = (BPy_PropertyRNA *)value;
2009  if (RNA_property_collection_type_get(&value_prop->ptr, value_prop->prop, &c_ptr)) {
2010  value = pyrna_struct_CreatePyObject(&c_ptr);
2011  value_new = value;
2012  }
2013  else {
2014  PyErr_Format(PyExc_TypeError,
2015  "%.200s %.200s.%.200s collection has no type, "
2016  "can't be used as a %.200s type",
2017  error_prefix,
2020  RNA_struct_identifier(ptr_type));
2021  return -1;
2022  }
2023  }
2024 
2025  BPy_StructRNA *param;
2026  if (value == Py_None) {
2027  if (flag & PROP_NEVER_NULL) {
2028  PyErr_Format(PyExc_TypeError,
2029  "%.200s %.200s.%.200s does not support a 'None' assignment %.200s type",
2030  error_prefix,
2033  RNA_struct_identifier(ptr_type));
2034  Py_XDECREF(value_new);
2035  return -1;
2036  }
2037  param = NULL;
2038  }
2039  else {
2040  if (!BPy_StructRNA_Check(value)) {
2041  PyErr_Format(PyExc_TypeError,
2042  "%.200s %.200s.%.200s expected a %.200s type, not %.200s",
2043  error_prefix,
2046  RNA_struct_identifier(ptr_type),
2047  Py_TYPE(value)->tp_name);
2048  Py_XDECREF(value_new);
2049  return -1;
2050  }
2051  param = (BPy_StructRNA *)value;
2052 
2053  const ID *value_owner_id = ((BPy_StructRNA *)value)->ptr.owner_id;
2054  if (value_owner_id != NULL) {
2055  if ((flag & PROP_ID_SELF_CHECK) && (ptr->owner_id == value_owner_id)) {
2056  PyErr_Format(PyExc_TypeError,
2057  "%.200s %.200s.%.200s ID type does not support assignment to itself",
2058  error_prefix,
2060  RNA_property_identifier(prop));
2061  Py_XDECREF(value_new);
2062  return -1;
2063  }
2064 
2065  if (value_owner_id->tag & LIB_TAG_TEMP_MAIN) {
2066  /* Allow passing temporary ID's to functions, but not attribute assignment. */
2067  if (ptr->type != &RNA_Function) {
2068  PyErr_Format(PyExc_TypeError,
2069  "%.200s %.200s.%.200s ID type assignment is temporary, can't assign",
2070  error_prefix,
2072  RNA_property_identifier(prop));
2073  Py_XDECREF(value_new);
2074  return -1;
2075  }
2076  }
2077  }
2078  }
2079 
2080  bool raise_error = false;
2081  if (data) {
2082 
2083  if (flag_parameter & PARM_RNAPTR) {
2084  if (flag & PROP_THICK_WRAP) {
2085  if (param == NULL) {
2086  memset(data, 0, sizeof(PointerRNA));
2087  }
2088  else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
2089  *((PointerRNA *)data) = param->ptr;
2090  }
2091  else {
2092  raise_error = true;
2093  }
2094  }
2095  else {
2096  /* For function calls, we sometimes want to pass the 'ptr' directly,
2097  * but watch out that it remains valid!
2098  * We could possibly support this later if needed. */
2099  BLI_assert(value_new == NULL);
2100  if (param == NULL) {
2101  *((void **)data) = NULL;
2102  }
2103  else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
2104  *((PointerRNA **)data) = &param->ptr;
2105  }
2106  else {
2107  raise_error = true;
2108  }
2109  }
2110  }
2111  else if (param == NULL) {
2112  *((void **)data) = NULL;
2113  }
2114  else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
2115  *((void **)data) = param->ptr.data;
2116  }
2117  else {
2118  raise_error = true;
2119  }
2120  }
2121  else {
2122  /* Data == NULL, assign to RNA. */
2123  if ((param == NULL) || RNA_struct_is_a(param->ptr.type, ptr_type)) {
2124  ReportList reports;
2125  BKE_reports_init(&reports, RPT_STORE);
2127  ptr, prop, (param == NULL) ? PointerRNA_NULL : param->ptr, &reports);
2128  const int err = (BPy_reports_to_error(&reports, PyExc_RuntimeError, true));
2129  if (err == -1) {
2130  Py_XDECREF(value_new);
2131  return -1;
2132  }
2133  }
2134  else {
2135  raise_error = true;
2136  }
2137  }
2138 
2139  if (raise_error) {
2140  if (pyrna_struct_validity_check(param) == -1) {
2141  /* Error set. */
2142  }
2143  else {
2144  PointerRNA tmp;
2145  RNA_pointer_create(NULL, ptr_type, NULL, &tmp);
2146  PyErr_Format(PyExc_TypeError,
2147  "%.200s %.200s.%.200s expected a %.200s type, not %.200s",
2148  error_prefix,
2152  RNA_struct_identifier(param->ptr.type));
2153  }
2154  Py_XDECREF(value_new);
2155  return -1;
2156  }
2157 
2158  Py_XDECREF(value_new);
2159 
2160  break;
2161  }
2162  case PROP_COLLECTION: {
2163  Py_ssize_t seq_len, i;
2164  PyObject *item;
2165  PointerRNA itemptr;
2166  ListBase *lb;
2167  CollectionPointerLink *link;
2168 
2169  lb = (data) ? (ListBase *)data : NULL;
2170 
2171  /* Convert a sequence of dict's into a collection. */
2172  if (!PySequence_Check(value)) {
2173  PyErr_Format(
2174  PyExc_TypeError,
2175  "%.200s %.200s.%.200s expected a sequence for an RNA collection, not %.200s",
2176  error_prefix,
2179  Py_TYPE(value)->tp_name);
2180  return -1;
2181  }
2182 
2183  seq_len = PySequence_Size(value);
2184  for (i = 0; i < seq_len; i++) {
2185  item = PySequence_GetItem(value, i);
2186 
2187  if (item == NULL) {
2188  PyErr_Format(
2189  PyExc_TypeError,
2190  "%.200s %.200s.%.200s failed to get sequence index '%d' for an RNA collection",
2191  error_prefix,
2194  i);
2195  Py_XDECREF(item);
2196  return -1;
2197  }
2198 
2199  if (PyDict_Check(item) == 0) {
2200  PyErr_Format(PyExc_TypeError,
2201  "%.200s %.200s.%.200s expected a each sequence "
2202  "member to be a dict for an RNA collection, not %.200s",
2203  error_prefix,
2206  Py_TYPE(item)->tp_name);
2207  Py_XDECREF(item);
2208  return -1;
2209  }
2210 
2211  if (lb) {
2212  link = MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
2213  link->ptr = itemptr;
2214  BLI_addtail(lb, link);
2215  }
2216  else {
2217  RNA_property_collection_add(ptr, prop, &itemptr);
2218  }
2219 
2221  &itemptr, item, true, "Converting a Python list to an RNA collection") == -1) {
2222  PyObject *msg = PyC_ExceptionBuffer();
2223  const char *msg_char = PyUnicode_AsUTF8(msg);
2224 
2225  PyErr_Format(PyExc_TypeError,
2226  "%.200s %.200s.%.200s error converting a member of a collection "
2227  "from a dicts into an RNA collection, failed with: %s",
2228  error_prefix,
2231  msg_char);
2232 
2233  Py_DECREF(item);
2234  Py_DECREF(msg);
2235  return -1;
2236  }
2237  Py_DECREF(item);
2238  }
2239 
2240  break;
2241  }
2242  default:
2243  PyErr_Format(PyExc_AttributeError,
2244  "%.200s %.200s.%.200s unknown property type (pyrna_py_to_prop)",
2245  error_prefix,
2247  RNA_property_identifier(prop));
2248  return -1;
2249  }
2250  }
2251 
2252  /* Run RNA property functions. */
2253  if (RNA_property_update_check(prop)) {
2255  }
2256 
2257  return 0;
2258 }
2259 
2260 static PyObject *pyrna_prop_array_to_py_index(BPy_PropertyArrayRNA *self, int index)
2261 {
2263  return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
2264 }
2265 
2266 static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, PyObject *value)
2267 {
2268  int ret = 0;
2269  PointerRNA *ptr = &self->ptr;
2270  PropertyRNA *prop = self->prop;
2271 
2272  const int totdim = RNA_property_array_dimension(ptr, prop, NULL);
2273 
2274  if (totdim > 1) {
2275  // char error_str[512];
2277  &self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "") == -1) {
2278  /* Error is set. */
2279  ret = -1;
2280  }
2281  }
2282  else {
2283  /* See if we can coerce into a Python type - 'PropertyType'. */
2284  switch (RNA_property_type(prop)) {
2285  case PROP_BOOLEAN: {
2286  const int param = PyC_Long_AsBool(value);
2287 
2288  if (param == -1) {
2289  /* Error is set. */
2290  ret = -1;
2291  }
2292  else {
2293  RNA_property_boolean_set_index(ptr, prop, index, param);
2294  }
2295  break;
2296  }
2297  case PROP_INT: {
2298  int param = PyC_Long_AsI32(value);
2299  if (param == -1 && PyErr_Occurred()) {
2300  PyErr_SetString(PyExc_TypeError, "expected an int type");
2301  ret = -1;
2302  }
2303  else {
2304  RNA_property_int_clamp(ptr, prop, &param);
2305  RNA_property_int_set_index(ptr, prop, index, param);
2306  }
2307  break;
2308  }
2309  case PROP_FLOAT: {
2310  float param = PyFloat_AsDouble(value);
2311  if (PyErr_Occurred()) {
2312  PyErr_SetString(PyExc_TypeError, "expected a float type");
2313  ret = -1;
2314  }
2315  else {
2316  RNA_property_float_clamp(ptr, prop, &param);
2317  RNA_property_float_set_index(ptr, prop, index, param);
2318  }
2319  break;
2320  }
2321  default:
2322  PyErr_SetString(PyExc_AttributeError, "not an array type");
2323  ret = -1;
2324  break;
2325  }
2326  }
2327 
2328  /* Run RNA property functions. */
2329  if (RNA_property_update_check(prop)) {
2331  }
2332 
2333  return ret;
2334 }
2335 
2336 /* ---------------sequence------------------------------------------- */
2338 {
2340 
2341  if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1) {
2342  return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
2343  }
2344 
2345  return RNA_property_array_length(&self->ptr, self->prop);
2346 }
2347 
2349 {
2350  PYRNA_PROP_CHECK_INT(self);
2351 
2352  return RNA_property_collection_length(&self->ptr, self->prop);
2353 }
2354 
2355 /* bool functions are for speed, so we can avoid getting the length
2356  * of 1000's of items in a linked list for eg. */
2358 {
2359  PYRNA_PROP_CHECK_INT(self);
2360 
2361  return RNA_property_array_length(&self->ptr, self->prop) ? 1 : 0;
2362 }
2363 
2365 {
2366  /* No callback defined, just iterate and find the nth item. */
2368  int test;
2369 
2370  PYRNA_PROP_CHECK_INT(self);
2371 
2372  RNA_property_collection_begin(&self->ptr, self->prop, &iter);
2373  test = iter.valid;
2375  return test;
2376 }
2377 
2378 /* notice getting the length of the collection is avoided unless negative
2379  * index is used or to detect internal error with a valid index.
2380  * This is done for faster lookups. */
2381 #define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err) \
2382  if (keynum < 0) { \
2383  keynum_abs += RNA_property_collection_length(&self->ptr, self->prop); \
2384  if (keynum_abs < 0) { \
2385  PyErr_Format(PyExc_IndexError, "bpy_prop_collection[%d]: out of range.", keynum); \
2386  return ret_err; \
2387  } \
2388  } \
2389  (void)0
2390 
2391 /* Internal use only. */
2392 static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
2393 {
2394  PointerRNA newptr;
2395  Py_ssize_t keynum_abs = keynum;
2396 
2397  PYRNA_PROP_CHECK_OBJ(self);
2398 
2400 
2401  if (RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum_abs, &newptr)) {
2402  return pyrna_struct_CreatePyObject(&newptr);
2403  }
2404 
2405  const int len = RNA_property_collection_length(&self->ptr, self->prop);
2406  if (keynum_abs >= len) {
2407  PyErr_Format(PyExc_IndexError,
2408  "bpy_prop_collection[index]: "
2409  "index %d out of range, size %d",
2410  keynum,
2411  len);
2412  }
2413  else {
2414  PyErr_Format(PyExc_RuntimeError,
2415  "bpy_prop_collection[index]: internal error, "
2416  "valid index %d given in %d sized collection, but value not found",
2417  keynum_abs,
2418  len);
2419  }
2420 
2421  return NULL;
2422 }
2423 
2424 /* Values type must have been already checked. */
2426  Py_ssize_t keynum,
2427  PyObject *value)
2428 {
2429  Py_ssize_t keynum_abs = keynum;
2430  const PointerRNA *ptr = (value == Py_None) ? (&PointerRNA_NULL) : &((BPy_StructRNA *)value)->ptr;
2431 
2432  PYRNA_PROP_CHECK_INT(self);
2433 
2435 
2436  if (RNA_property_collection_assign_int(&self->ptr, self->prop, keynum_abs, ptr) == 0) {
2437  const int len = RNA_property_collection_length(&self->ptr, self->prop);
2438  if (keynum_abs >= len) {
2439  PyErr_Format(PyExc_IndexError,
2440  "bpy_prop_collection[index] = value: "
2441  "index %d out of range, size %d",
2442  keynum,
2443  len);
2444  }
2445  else {
2446 
2447  PyErr_Format(PyExc_IndexError,
2448  "bpy_prop_collection[index] = value: "
2449  "failed assignment (unknown reason)",
2450  keynum);
2451  }
2452  return -1;
2453  }
2454 
2455  return 0;
2456 }
2457 
2458 static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
2459 {
2460  int len;
2461 
2463 
2464  len = pyrna_prop_array_length(self);
2465 
2466  if (keynum < 0) {
2467  keynum += len;
2468  }
2469 
2470  if (keynum >= 0 && keynum < len) {
2471  return pyrna_prop_array_to_py_index(self, keynum);
2472  }
2473 
2474  PyErr_Format(PyExc_IndexError, "bpy_prop_array[index]: index %d out of range", keynum);
2475  return NULL;
2476 }
2477 
2478 static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, const char *keyname)
2479 {
2480  PointerRNA newptr;
2481 
2482  PYRNA_PROP_CHECK_OBJ(self);
2483 
2484  if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr)) {
2485  return pyrna_struct_CreatePyObject(&newptr);
2486  }
2487 
2488  PyErr_Format(PyExc_KeyError, "bpy_prop_collection[key]: key \"%.200s\" not found", keyname);
2489  return NULL;
2490 }
2491 // static PyObject *pyrna_prop_array_subscript_str(BPy_PropertyRNA *self, char *keyname)
2492 
2505  PyObject *key,
2506  const char *err_prefix,
2507  const short err_not_found,
2508  PointerRNA *r_ptr)
2509 {
2510  const char *keyname;
2511 
2512  /* First validate the args, all we know is that they are a tuple. */
2513  if (PyTuple_GET_SIZE(key) != 2) {
2514  PyErr_Format(PyExc_KeyError,
2515  "%s: tuple key must be a pair, not size %d",
2516  err_prefix,
2517  PyTuple_GET_SIZE(key));
2518  return -1;
2519  }
2520  if (self->ptr.type != &RNA_BlendData) {
2521  PyErr_Format(PyExc_KeyError,
2522  "%s: is only valid for bpy.data collections, not %.200s",
2523  err_prefix,
2524  RNA_struct_identifier(self->ptr.type));
2525  return -1;
2526  }
2527  if ((keyname = PyUnicode_AsUTF8(PyTuple_GET_ITEM(key, 0))) == NULL) {
2528  PyErr_Format(PyExc_KeyError,
2529  "%s: id must be a string, not %.200s",
2530  err_prefix,
2531  Py_TYPE(PyTuple_GET_ITEM(key, 0))->tp_name);
2532  return -1;
2533  }
2534 
2535  PyObject *keylib = PyTuple_GET_ITEM(key, 1);
2536  Library *lib;
2537  bool found = false;
2538 
2539  if (keylib == Py_None) {
2540  lib = NULL;
2541  }
2542  else if (PyUnicode_Check(keylib)) {
2543  Main *bmain = self->ptr.data;
2544  const char *keylib_str = PyUnicode_AsUTF8(keylib);
2545  lib = BLI_findstring(&bmain->libraries, keylib_str, offsetof(Library, filepath));
2546  if (lib == NULL) {
2547  if (err_not_found) {
2548  PyErr_Format(PyExc_KeyError,
2549  "%s: lib filepath '%.1024s' "
2550  "does not reference a valid library",
2551  err_prefix,
2552  keylib_str);
2553  return -1;
2554  }
2555 
2556  return 0;
2557  }
2558  }
2559  else {
2560  PyErr_Format(PyExc_KeyError,
2561  "%s: lib must be a string or None, not %.200s",
2562  err_prefix,
2563  Py_TYPE(keylib)->tp_name);
2564  return -1;
2565  }
2566 
2567  /* lib is either a valid pointer or NULL,
2568  * either way can do direct comparison with id.lib */
2569 
2570  RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop) {
2571  ID *id = itemptr.data; /* Always an ID. */
2572  if (id->lib == lib && (STREQLEN(keyname, id->name + 2, sizeof(id->name) - 2))) {
2573  found = true;
2574  if (r_ptr) {
2575  *r_ptr = itemptr;
2576  }
2577  break;
2578  }
2579  }
2580  RNA_PROP_END;
2581 
2582  /* We may want to fail silently as with collection.get(). */
2583  if ((found == false) && err_not_found) {
2584  /* Only runs for getitem access so use fixed string. */
2585  PyErr_SetString(PyExc_KeyError, "bpy_prop_collection[key, lib]: not found");
2586  return -1;
2587  }
2588 
2589  return found; /* 1 / 0, no exception. */
2590 }
2591 
2593  PyObject *key,
2594  const char *err_prefix,
2595  const bool err_not_found)
2596 {
2597  PointerRNA ptr;
2599  self, key, err_prefix, err_not_found, &ptr);
2600 
2601  if (contains == 1) {
2603  }
2604 
2605  return NULL;
2606 }
2607 
2609  Py_ssize_t start,
2610  Py_ssize_t stop)
2611 {
2612  CollectionPropertyIterator rna_macro_iter;
2613  int count;
2614 
2615  PyObject *list;
2616  PyObject *item;
2617 
2618  PYRNA_PROP_CHECK_OBJ(self);
2619 
2620  list = PyList_New(0);
2621 
2622  /* Skip to start. */
2623  RNA_property_collection_begin(&self->ptr, self->prop, &rna_macro_iter);
2624  RNA_property_collection_skip(&rna_macro_iter, start);
2625 
2626  /* Add items until stop. */
2627  for (count = start; rna_macro_iter.valid; RNA_property_collection_next(&rna_macro_iter)) {
2628  item = pyrna_struct_CreatePyObject(&rna_macro_iter.ptr);
2629  PyList_APPEND(list, item);
2630 
2631  count++;
2632  if (count == stop) {
2633  break;
2634  }
2635  }
2636 
2637  RNA_property_collection_end(&rna_macro_iter);
2638 
2639  return list;
2640 }
2641 
2648  PointerRNA *ptr,
2649  PropertyRNA *prop,
2650  Py_ssize_t start,
2651  Py_ssize_t stop,
2652  Py_ssize_t length)
2653 {
2654  int count, totdim;
2655  PyObject *tuple;
2656 
2657  /* Isn't needed, internal use only. */
2658  // PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2659 
2660  tuple = PyTuple_New(stop - start);
2661 
2662  totdim = RNA_property_array_dimension(ptr, prop, NULL);
2663 
2664  if (totdim > 1) {
2665  for (count = start; count < stop; count++) {
2666  PyTuple_SET_ITEM(tuple, count - start, pyrna_prop_array_to_py_index(self, count));
2667  }
2668  }
2669  else {
2670  switch (RNA_property_type(prop)) {
2671  case PROP_FLOAT: {
2672  float values_stack[PYRNA_STACK_ARRAY];
2673  float *values;
2674  if (length > PYRNA_STACK_ARRAY) {
2675  values = PyMem_MALLOC(sizeof(float) * length);
2676  }
2677  else {
2678  values = values_stack;
2679  }
2680  RNA_property_float_get_array(ptr, prop, values);
2681 
2682  for (count = start; count < stop; count++) {
2683  PyTuple_SET_ITEM(tuple, count - start, PyFloat_FromDouble(values[count]));
2684  }
2685 
2686  if (values != values_stack) {
2687  PyMem_FREE(values);
2688  }
2689  break;
2690  }
2691  case PROP_BOOLEAN: {
2692  bool values_stack[PYRNA_STACK_ARRAY];
2693  bool *values;
2694  if (length > PYRNA_STACK_ARRAY) {
2695  values = PyMem_MALLOC(sizeof(bool) * length);
2696  }
2697  else {
2698  values = values_stack;
2699  }
2700 
2701  RNA_property_boolean_get_array(ptr, prop, values);
2702  for (count = start; count < stop; count++) {
2703  PyTuple_SET_ITEM(tuple, count - start, PyBool_FromLong(values[count]));
2704  }
2705 
2706  if (values != values_stack) {
2707  PyMem_FREE(values);
2708  }
2709  break;
2710  }
2711  case PROP_INT: {
2712  int values_stack[PYRNA_STACK_ARRAY];
2713  int *values;
2714  if (length > PYRNA_STACK_ARRAY) {
2715  values = PyMem_MALLOC(sizeof(int) * length);
2716  }
2717  else {
2718  values = values_stack;
2719  }
2720 
2721  RNA_property_int_get_array(ptr, prop, values);
2722  for (count = start; count < stop; count++) {
2723  PyTuple_SET_ITEM(tuple, count - start, PyLong_FromLong(values[count]));
2724  }
2725 
2726  if (values != values_stack) {
2727  PyMem_FREE(values);
2728  }
2729  break;
2730  }
2731  default:
2732  BLI_assert(!"Invalid array type");
2733 
2734  PyErr_SetString(PyExc_TypeError, "not an array type");
2735  Py_DECREF(tuple);
2736  tuple = NULL;
2737  break;
2738  }
2739  }
2740  return tuple;
2741 }
2742 
2743 static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
2744 {
2745  PYRNA_PROP_CHECK_OBJ(self);
2746 
2747  if (PyUnicode_Check(key)) {
2748  return pyrna_prop_collection_subscript_str(self, PyUnicode_AsUTF8(key));
2749  }
2750  if (PyIndex_Check(key)) {
2751  const Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2752  if (i == -1 && PyErr_Occurred()) {
2753  return NULL;
2754  }
2755 
2756  return pyrna_prop_collection_subscript_int(self, i);
2757  }
2758  if (PySlice_Check(key)) {
2759  PySliceObject *key_slice = (PySliceObject *)key;
2760  Py_ssize_t step = 1;
2761 
2762  if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2763  return NULL;
2764  }
2765  if (step != 1) {
2766  PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2767  return NULL;
2768  }
2769  if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2770  return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2771  }
2772 
2773  Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2774 
2775  /* Avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2776  if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) {
2777  return NULL;
2778  }
2779  if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) {
2780  return NULL;
2781  }
2782 
2783  if (start < 0 || stop < 0) {
2784  /* Only get the length for negative values. */
2785  const Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2786  if (start < 0) {
2787  start += len;
2788  CLAMP_MIN(start, 0);
2789  }
2790  if (stop < 0) {
2791  stop += len;
2792  CLAMP_MIN(stop, 0);
2793  }
2794  }
2795 
2796  if (stop - start <= 0) {
2797  return PyList_New(0);
2798  }
2799 
2800  return pyrna_prop_collection_subscript_slice(self, start, stop);
2801  }
2802  if (PyTuple_Check(key)) {
2803  /* Special case, for ID datablocks we. */
2805  self, key, "bpy_prop_collection[id, lib]", true);
2806  }
2807 
2808  PyErr_Format(PyExc_TypeError,
2809  "bpy_prop_collection[key]: invalid key, "
2810  "must be a string or an int, not %.200s",
2811  Py_TYPE(key)->tp_name);
2812  return NULL;
2813 }
2814 
2815 /* generic check to see if a PyObject is compatible with a collection
2816  * -1 on failure, 0 on success, sets the error */
2817 static int pyrna_prop_collection_type_check(BPy_PropertyRNA *self, PyObject *value)
2818 {
2819  StructRNA *prop_srna;
2820 
2821  if (value == Py_None) {
2822  if (RNA_property_flag(self->prop) & PROP_NEVER_NULL) {
2823  PyErr_Format(PyExc_TypeError,
2824  "bpy_prop_collection[key] = value: invalid, "
2825  "this collection doesn't support None assignment");
2826  return -1;
2827  }
2828 
2829  return 0; /* None is OK. */
2830  }
2831  if (BPy_StructRNA_Check(value) == 0) {
2832  PyErr_Format(PyExc_TypeError,
2833  "bpy_prop_collection[key] = value: invalid, "
2834  "expected a StructRNA type or None, not a %.200s",
2835  Py_TYPE(value)->tp_name);
2836  return -1;
2837  }
2838  if ((prop_srna = RNA_property_pointer_type(&self->ptr, self->prop))) {
2839  StructRNA *value_srna = ((BPy_StructRNA *)value)->ptr.type;
2840  if (RNA_struct_is_a(value_srna, prop_srna) == 0) {
2841  PyErr_Format(PyExc_TypeError,
2842  "bpy_prop_collection[key] = value: invalid, "
2843  "expected a '%.200s' type or None, not a '%.200s'",
2844  RNA_struct_identifier(prop_srna),
2845  RNA_struct_identifier(value_srna));
2846  return -1;
2847  }
2848 
2849  return 0; /* OK, this is the correct type! */
2850  }
2851 
2852  PyErr_Format(PyExc_TypeError,
2853  "bpy_prop_collection[key] = value: internal error, "
2854  "failed to get the collection type");
2855  return -1;
2856 }
2857 
2858 /* note: currently this is a copy of 'pyrna_prop_collection_subscript' with
2859  * large blocks commented, we may support slice/key indices later */
2861  PyObject *key,
2862  PyObject *value)
2863 {
2864  PYRNA_PROP_CHECK_INT(self);
2865 
2866  /* Validate the assigned value. */
2867  if (value == NULL) {
2868  PyErr_SetString(PyExc_TypeError, "del bpy_prop_collection[key]: not supported");
2869  return -1;
2870  }
2871  if (pyrna_prop_collection_type_check(self, value) == -1) {
2872  return -1; /* Exception is set. */
2873  }
2874 
2875 #if 0
2876  if (PyUnicode_Check(key)) {
2877  return pyrna_prop_collection_subscript_str(self, PyUnicode_AsUTF8(key));
2878  }
2879  else
2880 #endif
2881  if (PyIndex_Check(key)) {
2882  const Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2883  if (i == -1 && PyErr_Occurred()) {
2884  return -1;
2885  }
2886 
2887  return pyrna_prop_collection_ass_subscript_int(self, i, value);
2888  }
2889 #if 0 /* TODO, fake slice assignment. */
2890  else if (PySlice_Check(key)) {
2891  PySliceObject *key_slice = (PySliceObject *)key;
2892  Py_ssize_t step = 1;
2893 
2894  if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2895  return NULL;
2896  }
2897  else if (step != 1) {
2898  PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2899  return NULL;
2900  }
2901  else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2902  return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2903  }
2904  else {
2905  Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2906 
2907  /* Avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2908  if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) {
2909  return NULL;
2910  }
2911  if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) {
2912  return NULL;
2913  }
2914 
2915  if (start < 0 || stop < 0) {
2916  /* Only get the length for negative values. */
2917  Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2918  if (start < 0) {
2919  start += len;
2920  CLAMP_MIN(start, 0);
2921  }
2922  if (stop < 0) {
2923  stop += len;
2924  CLAMP_MIN(stop, 0);
2925  }
2926  }
2927 
2928  if (stop - start <= 0) {
2929  return PyList_New(0);
2930  }
2931  else {
2932  return pyrna_prop_collection_subscript_slice(self, start, stop);
2933  }
2934  }
2935  }
2936 #endif
2937 
2938  PyErr_Format(PyExc_TypeError,
2939  "bpy_prop_collection[key]: invalid key, "
2940  "must be a string or an int, not %.200s",
2941  Py_TYPE(key)->tp_name);
2942  return -1;
2943 }
2944 
2945 static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
2946 {
2948 
2949 #if 0
2950  if (PyUnicode_Check(key)) {
2951  return pyrna_prop_array_subscript_str(self, PyUnicode_AsUTF8(key));
2952  }
2953  else
2954 #endif
2955  if (PyIndex_Check(key)) {
2956  const Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2957  if (i == -1 && PyErr_Occurred()) {
2958  return NULL;
2959  }
2960  return pyrna_prop_array_subscript_int(self, i);
2961  }
2962  if (PySlice_Check(key)) {
2963  Py_ssize_t step = 1;
2964  PySliceObject *key_slice = (PySliceObject *)key;
2965 
2966  if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2967  return NULL;
2968  }
2969  if (step != 1) {
2970  PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported");
2971  return NULL;
2972  }
2973  if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2974  /* Note: no significant advantage with optimizing [:] slice as with collections,
2975  * but include here for consistency with collection slice func */
2976  const Py_ssize_t len = (Py_ssize_t)pyrna_prop_array_length(self);
2977  return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len);
2978  }
2979 
2980  const int len = pyrna_prop_array_length(self);
2981  Py_ssize_t start, stop, slicelength;
2982 
2983  if (PySlice_GetIndicesEx(key, len, &start, &stop, &step, &slicelength) < 0) {
2984  return NULL;
2985  }
2986 
2987  if (slicelength <= 0) {
2988  return PyTuple_New(0);
2989  }
2990 
2991  return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, start, stop, len);
2992  }
2993 
2994  PyErr_SetString(PyExc_AttributeError, "bpy_prop_array[key]: invalid key, key must be an int");
2995  return NULL;
2996 }
2997 
3002 static PyObject *prop_subscript_ass_array_slice__as_seq_fast(PyObject *value, int length)
3003 {
3004  PyObject *value_fast;
3005  if (!(value_fast = PySequence_Fast(value,
3006  "bpy_prop_array[slice] = value: "
3007  "element in assignment is not a sequence type"))) {
3008  return NULL;
3009  }
3010  if (PySequence_Fast_GET_SIZE(value_fast) != length) {
3011  Py_DECREF(value_fast);
3012  PyErr_SetString(PyExc_ValueError,
3013  "bpy_prop_array[slice] = value: "
3014  "re-sizing bpy_struct element in arrays isn't supported");
3015 
3016  return NULL;
3017  }
3018 
3019  return value_fast;
3020 }
3021 
3023  PyObject **value_items, float *value, int totdim, const int dimsize[], const float range[2])
3024 {
3025  const int length = dimsize[0];
3026  if (totdim > 1) {
3027  int index = 0;
3028  int i;
3029  for (i = 0; i != length; i++) {
3030  PyObject *subvalue = prop_subscript_ass_array_slice__as_seq_fast(value_items[i], dimsize[1]);
3031  if (UNLIKELY(subvalue == NULL)) {
3032  return 0;
3033  }
3034 
3036  PySequence_Fast_ITEMS(subvalue), &value[index], totdim - 1, &dimsize[1], range);
3037 
3038  Py_DECREF(subvalue);
3039  }
3040  return index;
3041  }
3042 
3043  BLI_assert(totdim == 1);
3044  const float min = range[0], max = range[1];
3045  int i;
3046  for (i = 0; i != length; i++) {
3047  float v = PyFloat_AsDouble(value_items[i]);
3048  CLAMP(v, min, max);
3049  value[i] = v;
3050  }
3051  return i;
3052 }
3053 
3055  PyObject **value_items, int *value, int totdim, const int dimsize[], const int range[2])
3056 {
3057  const int length = dimsize[0];
3058  if (totdim > 1) {
3059  int index = 0;
3060  int i;
3061  for (i = 0; i != length; i++) {
3062  PyObject *subvalue = prop_subscript_ass_array_slice__as_seq_fast(value_items[i], dimsize[1]);
3063  if (UNLIKELY(subvalue == NULL)) {
3064  return 0;
3065  }
3066 
3068  PySequence_Fast_ITEMS(subvalue), &value[index], totdim - 1, &dimsize[1], range);
3069 
3070  Py_DECREF(subvalue);
3071  }
3072  return index;
3073  }
3074 
3075  BLI_assert(totdim == 1);
3076  const int min = range[0], max = range[1];
3077  int i;
3078  for (i = 0; i != length; i++) {
3079  int v = PyLong_AsLong(value_items[i]);
3080  CLAMP(v, min, max);
3081  value[i] = v;
3082  }
3083  return i;
3084 }
3085 
3086 static int prop_subscript_ass_array_slice__bool_recursive(PyObject **value_items,
3087  bool *value,
3088  int totdim,
3089  const int dimsize[])
3090 {
3091  const int length = dimsize[0];
3092  if (totdim > 1) {
3093  int index = 0;
3094  int i;
3095  for (i = 0; i != length; i++) {
3096  PyObject *subvalue = prop_subscript_ass_array_slice__as_seq_fast(value_items[i], dimsize[1]);
3097  if (UNLIKELY(subvalue == NULL)) {
3098  return 0;
3099  }
3100 
3102  PySequence_Fast_ITEMS(subvalue), &value[index], totdim - 1, &dimsize[1]);
3103 
3104  Py_DECREF(subvalue);
3105  }
3106  return index;
3107  }
3108 
3109  BLI_assert(totdim == 1);
3110  int i;
3111  for (i = 0; i != length; i++) {
3112  const int v = PyLong_AsLong(value_items[i]);
3113  value[i] = v;
3114  }
3115  return i;
3116 }
3117 
3118 /* Could call `pyrna_py_to_prop_array_index(self, i, value)` in a loop, but it is slow. */
3120  PropertyRNA *prop,
3121  int arraydim,
3122  int arrayoffset,
3123  int start,
3124  int stop,
3125  int length,
3126  PyObject *value_orig)
3127 {
3128  const int length_flat = RNA_property_array_length(ptr, prop);
3129  PyObject *value;
3130  PyObject **value_items;
3131  void *values_alloc = NULL;
3132  int ret = 0;
3133 
3134  if (value_orig == NULL) {
3135  PyErr_SetString(
3136  PyExc_TypeError,
3137  "bpy_prop_array[slice] = value: deleting with list types is not supported by bpy_struct");
3138  return -1;
3139  }
3140 
3141  if (!(value = PySequence_Fast(
3142  value_orig, "bpy_prop_array[slice] = value: assignment is not a sequence type"))) {
3143  return -1;
3144  }
3145 
3146  if (PySequence_Fast_GET_SIZE(value) != stop - start) {
3147  Py_DECREF(value);
3148  PyErr_SetString(PyExc_TypeError,
3149  "bpy_prop_array[slice] = value: re-sizing bpy_struct arrays isn't supported");
3150  return -1;
3151  }
3152 
3153  int dimsize[3];
3154  const int totdim = RNA_property_array_dimension(ptr, prop, dimsize);
3155  if (totdim > 1) {
3156  BLI_assert(dimsize[arraydim] == length);
3157  }
3158 
3159  int span = 1;
3160  if (totdim > 1) {
3161  for (int i = arraydim + 1; i < totdim; i++) {
3162  span *= dimsize[i];
3163  }
3164  }
3165 
3166  value_items = PySequence_Fast_ITEMS(value);
3167  switch (RNA_property_type(prop)) {
3168  case PROP_FLOAT: {
3169  float values_stack[PYRNA_STACK_ARRAY];
3170  float *values = (length_flat > PYRNA_STACK_ARRAY) ?
3171  (values_alloc = PyMem_MALLOC(sizeof(*values) * length_flat)) :
3172  values_stack;
3173  if (start != 0 || stop != length) {
3174  /* Partial assignment? - need to get the array. */
3175  RNA_property_float_get_array(ptr, prop, values);
3176  }
3177 
3178  float range[2];
3179  RNA_property_float_range(ptr, prop, &range[0], &range[1]);
3180 
3181  dimsize[arraydim] = stop - start;
3183  &values[arrayoffset + (start * span)],
3184  totdim - arraydim,
3185  &dimsize[arraydim],
3186  range);
3187 
3188  if (PyErr_Occurred()) {
3189  ret = -1;
3190  }
3191  else {
3192  RNA_property_float_set_array(ptr, prop, values);
3193  }
3194  break;
3195  }
3196  case PROP_INT: {
3197  int values_stack[PYRNA_STACK_ARRAY];
3198  int *values = (length_flat > PYRNA_STACK_ARRAY) ?
3199  (values_alloc = PyMem_MALLOC(sizeof(*values) * length_flat)) :
3200  values_stack;
3201  if (start != 0 || stop != length) {
3202  /* Partial assignment? - need to get the array. */
3203  RNA_property_int_get_array(ptr, prop, values);
3204  }
3205 
3206  int range[2];
3207  RNA_property_int_range(ptr, prop, &range[0], &range[1]);
3208 
3209  dimsize[arraydim] = stop - start;
3211  &values[arrayoffset + (start * span)],
3212  totdim - arraydim,
3213  &dimsize[arraydim],
3214  range);
3215 
3216  if (PyErr_Occurred()) {
3217  ret = -1;
3218  }
3219  else {
3220  RNA_property_int_set_array(ptr, prop, values);
3221  }
3222  break;
3223  }
3224  case PROP_BOOLEAN: {
3225  bool values_stack[PYRNA_STACK_ARRAY];
3226  bool *values = (length_flat > PYRNA_STACK_ARRAY) ?
3227  (values_alloc = PyMem_MALLOC(sizeof(bool) * length_flat)) :
3228  values_stack;
3229 
3230  if (start != 0 || stop != length) {
3231  /* Partial assignment? - need to get the array. */
3232  RNA_property_boolean_get_array(ptr, prop, values);
3233  }
3234 
3235  dimsize[arraydim] = stop - start;
3237  &values[arrayoffset + (start * span)],
3238  totdim - arraydim,
3239  &dimsize[arraydim]);
3240 
3241  if (PyErr_Occurred()) {
3242  ret = -1;
3243  }
3244  else {
3245  RNA_property_boolean_set_array(ptr, prop, values);
3246  }
3247  break;
3248  }
3249  default:
3250  PyErr_SetString(PyExc_TypeError, "not an array type");
3251  ret = -1;
3252  break;
3253  }
3254 
3255  Py_DECREF(value);
3256 
3257  if (values_alloc) {
3258  PyMem_FREE(values_alloc);
3259  }
3260 
3261  return ret;
3262 }
3263 
3265  Py_ssize_t keynum,
3266  PyObject *value)
3267 {
3268  int len;
3269 
3271 
3272  len = pyrna_prop_array_length(self);
3273 
3274  if (keynum < 0) {
3275  keynum += len;
3276  }
3277 
3278  if (keynum >= 0 && keynum < len) {
3279  return pyrna_py_to_prop_array_index(self, keynum, value);
3280  }
3281 
3282  PyErr_SetString(PyExc_IndexError, "bpy_prop_array[index] = value: index out of range");
3283  return -1;
3284 }
3285 
3287  PyObject *key,
3288  PyObject *value)
3289 {
3290  // char *keyname = NULL; /* Not supported yet. */
3291  int ret = -1;
3292 
3294 
3295  if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
3296  PyErr_Format(PyExc_AttributeError,
3297  "bpy_prop_collection: attribute \"%.200s\" from \"%.200s\" is read-only",
3299  RNA_struct_identifier(self->ptr.type));
3300  ret = -1;
3301  }
3302 
3303  else if (PyIndex_Check(key)) {
3304  const Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
3305  if (i == -1 && PyErr_Occurred()) {
3306  ret = -1;
3307  }
3308  else {
3309  ret = prop_subscript_ass_array_int(self, i, value);
3310  }
3311  }
3312  else if (PySlice_Check(key)) {
3313  const Py_ssize_t len = pyrna_prop_array_length(self);
3314  Py_ssize_t start, stop, step, slicelength;
3315 
3316  if (PySlice_GetIndicesEx(key, len, &start, &stop, &step, &slicelength) < 0) {
3317  ret = -1;
3318  }
3319  else if (slicelength <= 0) {
3320  ret = 0; /* Do nothing. */
3321  }
3322  else if (step == 1) {
3324  &self->ptr, self->prop, self->arraydim, self->arrayoffset, start, stop, len, value);
3325  }
3326  else {
3327  PyErr_SetString(PyExc_TypeError, "slice steps not supported with RNA");
3328  ret = -1;
3329  }
3330  }
3331  else {
3332  PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
3333  ret = -1;
3334  }
3335 
3336  if (ret != -1) {
3337  if (RNA_property_update_check(self->prop)) {
3338  RNA_property_update(BPY_context_get(), &self->ptr, self->prop);
3339  }
3340  }
3341 
3342  return ret;
3343 }
3344 
3345 /* For slice only. */
3346 static PyMappingMethods pyrna_prop_array_as_mapping = {
3347  (lenfunc)pyrna_prop_array_length, /* mp_length */
3348  (binaryfunc)pyrna_prop_array_subscript, /* mp_subscript */
3349  (objobjargproc)pyrna_prop_array_ass_subscript, /* mp_ass_subscript */
3350 };
3351 
3352 static PyMappingMethods pyrna_prop_collection_as_mapping = {
3353  (lenfunc)pyrna_prop_collection_length, /* mp_length */
3354  (binaryfunc)pyrna_prop_collection_subscript, /* mp_subscript */
3355  (objobjargproc)pyrna_prop_collection_ass_subscript, /* mp_ass_subscript */
3356 };
3357 
3358 /* Only for fast bool's, large structs, assign nb_bool on init. */
3359 static PyNumberMethods pyrna_prop_array_as_number = {
3360  NULL, /* nb_add */
3361  NULL, /* nb_subtract */
3362  NULL, /* nb_multiply */
3363  NULL, /* nb_remainder */
3364  NULL, /* nb_divmod */
3365  NULL, /* nb_power */
3366  NULL, /* nb_negative */
3367  NULL, /* nb_positive */
3368  NULL, /* nb_absolute */
3369  (inquiry)pyrna_prop_array_bool, /* nb_bool */
3370 };
3371 static PyNumberMethods pyrna_prop_collection_as_number = {
3372  NULL, /* nb_add */
3373  NULL, /* nb_subtract */
3374  NULL, /* nb_multiply */
3375  NULL, /* nb_remainder */
3376  NULL, /* nb_divmod */
3377  NULL, /* nb_power */
3378  NULL, /* nb_negative */
3379  NULL, /* nb_positive */
3380  NULL, /* nb_absolute */
3381  (inquiry)pyrna_prop_collection_bool, /* nb_bool */
3382 };
3383 
3384 static int pyrna_prop_array_contains(BPy_PropertyRNA *self, PyObject *value)
3385 {
3386  return pyrna_array_contains_py(&self->ptr, self->prop, value);
3387 }
3388 
3389 static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *key)
3390 {
3391  PointerRNA newptr; /* Not used, just so RNA_property_collection_lookup_string runs. */
3392 
3393  if (PyTuple_Check(key)) {
3394  /* Special case, for ID data-blocks. */
3396  self, key, "(id, lib) in bpy_prop_collection", false, NULL);
3397  }
3398 
3399  /* Key in dict style check. */
3400  const char *keyname = PyUnicode_AsUTF8(key);
3401 
3402  if (keyname == NULL) {
3403  PyErr_SetString(PyExc_TypeError,
3404  "bpy_prop_collection.__contains__: expected a string or a tuple of strings");
3405  return -1;
3406  }
3407 
3408  if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr)) {
3409  return 1;
3410  }
3411 
3412  return 0;
3413 }
3414 
3415 static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
3416 {
3417  IDProperty *group;
3418  const char *name = PyUnicode_AsUTF8(value);
3419 
3420  PYRNA_STRUCT_CHECK_INT(self);
3421 
3422  if (!name) {
3423  PyErr_SetString(PyExc_TypeError, "bpy_struct.__contains__: expected a string");
3424  return -1;
3425  }
3426 
3427  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3428  PyErr_SetString(PyExc_TypeError, "bpy_struct: this type doesn't support IDProperties");
3429  return -1;
3430  }
3431 
3432  group = RNA_struct_idprops(&self->ptr, 0);
3433 
3434  if (!group) {
3435  return 0;
3436  }
3437 
3438  return IDP_GetPropertyFromGroup(group, name) ? 1 : 0;
3439 }
3440 
3441 static PySequenceMethods pyrna_prop_array_as_sequence = {
3442  (lenfunc)pyrna_prop_array_length,
3443  NULL, /* sq_concat */
3444  NULL, /* sq_repeat */
3445  (ssizeargfunc)pyrna_prop_array_subscript_int,
3446  /* sq_item */ /* Only set this so PySequence_Check() returns True */
3447  NULL, /* sq_slice */
3448  (ssizeobjargproc)prop_subscript_ass_array_int, /* sq_ass_item */
3449  NULL, /* *was* sq_ass_slice */
3450  (objobjproc)pyrna_prop_array_contains, /* sq_contains */
3451  (binaryfunc)NULL, /* sq_inplace_concat */
3452  (ssizeargfunc)NULL, /* sq_inplace_repeat */
3453 };
3454 
3455 static PySequenceMethods pyrna_prop_collection_as_sequence = {
3457  NULL, /* sq_concat */
3458  NULL, /* sq_repeat */
3460  /* sq_item */ /* Only set this so PySequence_Check() returns True */
3461  NULL, /* *was* sq_slice */
3462  (ssizeobjargproc) /* pyrna_prop_collection_ass_subscript_int */
3463  NULL /* let mapping take this one */, /* sq_ass_item */
3464  NULL, /* *was* sq_ass_slice */
3465  (objobjproc)pyrna_prop_collection_contains, /* sq_contains */
3466  (binaryfunc)NULL, /* sq_inplace_concat */
3467  (ssizeargfunc)NULL, /* sq_inplace_repeat */
3468 };
3469 
3470 static PySequenceMethods pyrna_struct_as_sequence = {
3471  NULL, /* Can't set the len otherwise it can evaluate as false */
3472  NULL, /* sq_concat */
3473  NULL, /* sq_repeat */
3474  NULL,
3475  /* sq_item */ /* Only set this so PySequence_Check() returns True */
3476  NULL, /* *was* sq_slice */
3477  NULL, /* sq_ass_item */
3478  NULL, /* *was* sq_ass_slice */
3479  (objobjproc)pyrna_struct_contains, /* sq_contains */
3480  (binaryfunc)NULL, /* sq_inplace_concat */
3481  (ssizeargfunc)NULL, /* sq_inplace_repeat */
3482 };
3483 
3484 static PyObject *pyrna_struct_subscript(BPy_StructRNA *self, PyObject *key)
3485 {
3486  /* Mostly copied from BPy_IDGroup_Map_GetItem. */
3487  IDProperty *group, *idprop;
3488  const char *name = PyUnicode_AsUTF8(key);
3489 
3490  PYRNA_STRUCT_CHECK_OBJ(self);
3491 
3492  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3493  PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
3494  return NULL;
3495  }
3496 
3497  if (name == NULL) {
3498  PyErr_SetString(PyExc_TypeError,
3499  "bpy_struct[key]: only strings are allowed as keys of ID properties");
3500  return NULL;
3501  }
3502 
3503  group = RNA_struct_idprops(&self->ptr, 0);
3504 
3505  if (group == NULL) {
3506  PyErr_Format(PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
3507  return NULL;
3508  }
3509 
3510  idprop = IDP_GetPropertyFromGroup(group, name);
3511 
3512  if (idprop == NULL) {
3513  PyErr_Format(PyExc_KeyError, "bpy_struct[key]: key \"%s\" not found", name);
3514  return NULL;
3515  }
3516 
3517  return BPy_IDGroup_WrapData(self->ptr.owner_id, idprop, group);
3518 }
3519 
3520 static int pyrna_struct_ass_subscript(BPy_StructRNA *self, PyObject *key, PyObject *value)
3521 {
3522  IDProperty *group;
3523 
3524  PYRNA_STRUCT_CHECK_INT(self);
3525 
3526  group = RNA_struct_idprops(&self->ptr, 1);
3527 
3528 #ifdef USE_PEDANTIC_WRITE
3529  if (rna_disallow_writes && rna_id_write_error(&self->ptr, key)) {
3530  return -1;
3531  }
3532 #endif /* USE_PEDANTIC_WRITE */
3533 
3534  if (group == NULL) {
3535  PyErr_SetString(PyExc_TypeError,
3536  "bpy_struct[key] = val: id properties not supported for this type");
3537  return -1;
3538  }
3539 
3540  if (value && BPy_StructRNA_Check(value)) {
3541  BPy_StructRNA *val = (BPy_StructRNA *)value;
3542  if (val && self->ptr.type && val->ptr.type) {
3543  if (!RNA_struct_idprops_datablock_allowed(self->ptr.type) &&
3545  PyErr_SetString(
3546  PyExc_TypeError,
3547  "bpy_struct[key] = val: datablock id properties not supported for this type");
3548  return -1;
3549  }
3550  }
3551  }
3552 
3553  return BPy_Wrap_SetMapItem(group, key, value);
3554 }
3555 
3556 static PyMappingMethods pyrna_struct_as_mapping = {
3557  (lenfunc)NULL, /* mp_length */
3558  (binaryfunc)pyrna_struct_subscript, /* mp_subscript */
3559  (objobjargproc)pyrna_struct_ass_subscript, /* mp_ass_subscript */
3560 };
3561 
3562 PyDoc_STRVAR(pyrna_struct_keys_doc,
3563  ".. method:: keys()\n"
3564  "\n"
3565  " Returns the keys of this objects custom properties (matches Python's\n"
3566  " dictionary function of the same name).\n"
3567  "\n"
3568  " :return: custom property keys.\n"
3569  " :rtype: list of strings\n"
3571 static PyObject *pyrna_struct_keys(BPy_PropertyRNA *self)
3572 {
3573  IDProperty *group;
3574 
3575  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3576  PyErr_SetString(PyExc_TypeError, "bpy_struct.keys(): this type doesn't support IDProperties");
3577  return NULL;
3578  }
3579 
3580  group = RNA_struct_idprops(&self->ptr, 0);
3581 
3582  if (group == NULL) {
3583  return PyList_New(0);
3584  }
3585 
3586  return BPy_Wrap_GetKeys(group);
3587 }
3588 
3589 PyDoc_STRVAR(pyrna_struct_items_doc,
3590  ".. method:: items()\n"
3591  "\n"
3592  " Returns the items of this objects custom properties (matches Python's\n"
3593  " dictionary function of the same name).\n"
3594  "\n"
3595  " :return: custom property key, value pairs.\n"
3596  " :rtype: list of key, value tuples\n"
3598 static PyObject *pyrna_struct_items(BPy_PropertyRNA *self)
3599 {
3600  IDProperty *group;
3601 
3602  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3603  PyErr_SetString(PyExc_TypeError, "bpy_struct.items(): this type doesn't support IDProperties");
3604  return NULL;
3605  }
3606 
3607  group = RNA_struct_idprops(&self->ptr, 0);
3608 
3609  if (group == NULL) {
3610  return PyList_New(0);
3611  }
3612 
3613  return BPy_Wrap_GetItems(self->ptr.owner_id, group);
3614 }
3615 
3616 PyDoc_STRVAR(pyrna_struct_values_doc,
3617  ".. method:: values()\n"
3618  "\n"
3619  " Returns the values of this objects custom properties (matches Python's\n"
3620  " dictionary function of the same name).\n"
3621  "\n"
3622  " :return: custom property values.\n"
3623  " :rtype: list\n"
3625 static PyObject *pyrna_struct_values(BPy_PropertyRNA *self)
3626 {
3627  IDProperty *group;
3628 
3629  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
3630  PyErr_SetString(PyExc_TypeError,
3631  "bpy_struct.values(): this type doesn't support IDProperties");
3632  return NULL;
3633  }
3634 
3635  group = RNA_struct_idprops(&self->ptr, 0);
3636 
3637  if (group == NULL) {
3638  return PyList_New(0);
3639  }
3640 
3641  return BPy_Wrap_GetValues(self->ptr.owner_id, group);
3642 }
3643 
3644 PyDoc_STRVAR(pyrna_struct_is_property_set_doc,
3645  ".. method:: is_property_set(property, ghost=True)\n"
3646  "\n"
3647  " Check if a property is set, use for testing operator properties.\n"
3648  "\n"
3649  " :arg ghost: Used for operators that re-run with previous settings.\n"
3650  " In this case the property is not marked as set,\n"
3651  " yet the value from the previous execution is used.\n"
3652  "\n"
3653  " In rare cases you may want to set this option to false.\n"
3654  "\n"
3655  " :type ghost: boolean\n"
3656  " :return: True when the property has been set.\n"
3657  " :rtype: boolean\n");
3658 static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args, PyObject *kw)
3659 {
3660  PropertyRNA *prop;
3661  const char *name;
3662  bool use_ghost = true;
3663 
3664  PYRNA_STRUCT_CHECK_OBJ(self);
3665 
3666  static const char *_keywords[] = {"", "ghost", NULL};
3667  static _PyArg_Parser _parser = {"s|$O&:is_property_set", _keywords, 0};
3668  if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &name, PyC_ParseBool, &use_ghost)) {
3669  return NULL;
3670  }
3671 
3672  if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3673  PyErr_Format(PyExc_TypeError,
3674  "%.200s.is_property_set(\"%.200s\") not found",
3675  RNA_struct_identifier(self->ptr.type),
3676  name);
3677  return NULL;
3678  }
3679 
3680  return PyBool_FromLong(RNA_property_is_set_ex(&self->ptr, prop, use_ghost));
3681 }
3682 
3683 PyDoc_STRVAR(pyrna_struct_property_unset_doc,
3684  ".. method:: property_unset(property)\n"
3685  "\n"
3686  " Unset a property, will use default value afterward.\n");
3687 static PyObject *pyrna_struct_property_unset(BPy_StructRNA *self, PyObject *args)
3688 {
3689  PropertyRNA *prop;
3690  const char *name;
3691 
3692  PYRNA_STRUCT_CHECK_OBJ(self);
3693 
3694  if (!PyArg_ParseTuple(args, "s:property_unset", &name)) {
3695  return NULL;
3696  }
3697 
3698  if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3699  PyErr_Format(PyExc_TypeError,
3700  "%.200s.property_unset(\"%.200s\") not found",
3701  RNA_struct_identifier(self->ptr.type),
3702  name);
3703  return NULL;
3704  }
3705 
3706  RNA_property_unset(&self->ptr, prop);
3707 
3708  Py_RETURN_NONE;
3709 }
3710 
3711 PyDoc_STRVAR(pyrna_struct_is_property_hidden_doc,
3712  ".. method:: is_property_hidden(property)\n"
3713  "\n"
3714  " Check if a property is hidden.\n"
3715  "\n"
3716  " :return: True when the property is hidden.\n"
3717  " :rtype: boolean\n");
3718 static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *args)
3719 {
3720  PropertyRNA *prop;
3721  const char *name;
3722 
3723  PYRNA_STRUCT_CHECK_OBJ(self);
3724 
3725  if (!PyArg_ParseTuple(args, "s:is_property_hidden", &name)) {
3726  return NULL;
3727  }
3728 
3729  if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3730  PyErr_Format(PyExc_TypeError,
3731  "%.200s.is_property_hidden(\"%.200s\") not found",
3732  RNA_struct_identifier(self->ptr.type),
3733  name);
3734  return NULL;
3735  }
3736 
3737  return PyBool_FromLong(RNA_property_flag(prop) & PROP_HIDDEN);
3738 }
3739 
3740 PyDoc_STRVAR(pyrna_struct_is_property_readonly_doc,
3741  ".. method:: is_property_readonly(property)\n"
3742  "\n"
3743  " Check if a property is readonly.\n"
3744  "\n"
3745  " :return: True when the property is readonly (not writable).\n"
3746  " :rtype: boolean\n");
3747 static PyObject *pyrna_struct_is_property_readonly(BPy_StructRNA *self, PyObject *args)
3748 {
3749  PropertyRNA *prop;
3750  const char *name;
3751 
3752  PYRNA_STRUCT_CHECK_OBJ(self);
3753 
3754  if (!PyArg_ParseTuple(args, "s:is_property_readonly", &name)) {
3755  return NULL;
3756  }
3757 
3758  if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3759  PyErr_Format(PyExc_TypeError,
3760  "%.200s.is_property_readonly(\"%.200s\") not found",
3761  RNA_struct_identifier(self->ptr.type),
3762  name);
3763  return NULL;
3764  }
3765 
3766  return PyBool_FromLong(!RNA_property_editable(&self->ptr, prop));
3767 }
3768 
3769 PyDoc_STRVAR(pyrna_struct_is_property_overridable_library_doc,
3770  ".. method:: is_property_overridable_library(property)\n"
3771  "\n"
3772  " Check if a property is overridable.\n"
3773  "\n"
3774  " :return: True when the property is overridable.\n"
3775  " :rtype: boolean\n");
3776 static PyObject *pyrna_struct_is_property_overridable_library(BPy_StructRNA *self, PyObject *args)
3777 {
3778  PropertyRNA *prop;
3779  const char *name;
3780 
3781  PYRNA_STRUCT_CHECK_OBJ(self);
3782 
3783  if (!PyArg_ParseTuple(args, "s:is_property_overridable_library", &name)) {
3784  return NULL;
3785  }
3786 
3787  if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3788  PyErr_Format(PyExc_TypeError,
3789  "%.200s.is_property_overridable_library(\"%.200s\") not found",
3790  RNA_struct_identifier(self->ptr.type),
3791  name);
3792  return NULL;
3793  }
3794 
3795  return PyBool_FromLong((long)RNA_property_overridable_get(&self->ptr, prop));
3796 }
3797 
3798 PyDoc_STRVAR(pyrna_struct_property_overridable_library_set_doc,
3799  ".. method:: property_overridable_library_set(property, overridable)\n"
3800  "\n"
3801  " Define a property as overridable or not (only for custom properties!).\n"
3802  "\n"
3803  " :return: True when the overridable status of the property was successfully set.\n"
3804  " :rtype: boolean\n");
3805 static PyObject *pyrna_struct_property_overridable_library_set(BPy_StructRNA *self, PyObject *args)
3806 {
3807  PropertyRNA *prop;
3808  const char *name;
3809  int is_overridable;
3810 
3811  PYRNA_STRUCT_CHECK_OBJ(self);
3812 
3813  if (!PyArg_ParseTuple(args, "sp:property_overridable_library_set", &name, &is_overridable)) {
3814  return NULL;
3815  }
3816 
3817  if ((prop = RNA_struct_find_property(&self->ptr, name)) == NULL) {
3818  PyErr_Format(PyExc_TypeError,
3819  "%.200s.property_overridable_library_set(\"%.200s\") not found",
3820  RNA_struct_identifier(self->ptr.type),
3821  name);
3822  return NULL;
3823  }
3824 
3825  return PyBool_FromLong(
3826  (long)RNA_property_overridable_library_set(&self->ptr, prop, (bool)is_overridable));
3827 }
3828 
3829 PyDoc_STRVAR(pyrna_struct_path_resolve_doc,
3830  ".. method:: path_resolve(path, coerce=True)\n"
3831  "\n"
3832  " Returns the property from the path, raise an exception when not found.\n"
3833  "\n"
3834  " :arg path: path which this property resolves.\n"
3835  " :type path: string\n"
3836  " :arg coerce: optional argument, when True, the property will be converted\n"
3837  " into its Python representation.\n"
3838  " :type coerce: boolean\n");
3839 static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *args)
3840 {
3841  const char *path;
3842  PyObject *coerce = Py_True;
3843  PointerRNA r_ptr;
3844  PropertyRNA *r_prop;
3845  int index = -1;
3846 
3847  PYRNA_STRUCT_CHECK_OBJ(self);
3848 
3849  if (!PyArg_ParseTuple(args, "s|O!:path_resolve", &path, &PyBool_Type, &coerce)) {
3850  return NULL;
3851  }
3852 
3853  if (RNA_path_resolve_full(&self->ptr, path, &r_ptr, &r_prop, &index)) {
3854  if (r_prop) {
3855  if (index != -1) {
3856  if (index >= RNA_property_array_length(&r_ptr, r_prop) || index < 0) {
3857  PyErr_Format(PyExc_IndexError,
3858  "%.200s.path_resolve(\"%.200s\") index out of range",
3859  RNA_struct_identifier(self->ptr.type),
3860  path);
3861  return NULL;
3862  }
3863 
3864  return pyrna_array_index(&r_ptr, r_prop, index);
3865  }
3866 
3867  if (coerce == Py_False) {
3868  return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
3869  }
3870 
3871  return pyrna_prop_to_py(&r_ptr, r_prop);
3872  }
3873 
3874  return pyrna_struct_CreatePyObject(&r_ptr);
3875  }
3876 
3877  PyErr_Format(PyExc_ValueError,
3878  "%.200s.path_resolve(\"%.200s\") could not be resolved",
3879  RNA_struct_identifier(self->ptr.type),
3880  path);
3881  return NULL;
3882 }
3883 
3884 PyDoc_STRVAR(pyrna_struct_path_from_id_doc,
3885  ".. method:: path_from_id(property=\"\")\n"
3886  "\n"
3887  " Returns the data path from the ID to this object (string).\n"
3888  "\n"
3889  " :arg property: Optional property name which can be used if the path is\n"
3890  " to a property of this object.\n"
3891  " :type property: string\n"
3892  " :return: The path from :class:`bpy.types.bpy_struct.id_data`\n"
3893  " to this struct and property (when given).\n"
3894  " :rtype: str\n");
3895 static PyObject *pyrna_struct_path_from_id(BPy_StructRNA *self, PyObject *args)
3896 {
3897  const char *name = NULL;
3898  const char *path;
3899  PropertyRNA *prop;
3900  PyObject *ret;
3901 
3902  PYRNA_STRUCT_CHECK_OBJ(self);
3903 
3904  if (!PyArg_ParseTuple(args, "|s:path_from_id", &name)) {
3905  return NULL;
3906  }
3907 
3908  if (name) {
3909  prop = RNA_struct_find_property(&self->ptr, name);
3910  if (prop == NULL) {
3911  PyErr_Format(PyExc_AttributeError,
3912  "%.200s.path_from_id(\"%.200s\") not found",
3913  RNA_struct_identifier(self->ptr.type),
3914  name);
3915  return NULL;
3916  }
3917 
3918  path = RNA_path_from_ID_to_property(&self->ptr, prop);
3919  }
3920  else {
3921  path = RNA_path_from_ID_to_struct(&self->ptr);
3922  }
3923 
3924  if (path == NULL) {
3925  if (name) {
3926  PyErr_Format(PyExc_ValueError,
3927  "%.200s.path_from_id(\"%s\") found, but does not support path creation",
3928  RNA_struct_identifier(self->ptr.type),
3929  name);
3930  }
3931  else {
3932  PyErr_Format(PyExc_ValueError,
3933  "%.200s.path_from_id() does not support path creation for this type",
3934  RNA_struct_identifier(self->ptr.type));
3935  }
3936  return NULL;
3937  }
3938 
3939  ret = PyUnicode_FromString(path);
3940  MEM_freeN((void *)path);
3941 
3942  return ret;
3943 }
3944 
3945 PyDoc_STRVAR(pyrna_prop_path_from_id_doc,
3946  ".. method:: path_from_id()\n"
3947  "\n"
3948  " Returns the data path from the ID to this property (string).\n"
3949  "\n"
3950  " :return: The path from :class:`bpy.types.bpy_struct.id_data` to this property.\n"
3951  " :rtype: str\n");
3953 {
3954  const char *path;
3955  PropertyRNA *prop = self->prop;
3956  PyObject *ret;
3957 
3958  path = RNA_path_from_ID_to_property(&self->ptr, self->prop);
3959 
3960  if (path == NULL) {
3961  PyErr_Format(PyExc_ValueError,
3962  "%.200s.%.200s.path_from_id() does not support path creation for this type",
3963  RNA_struct_identifier(self->ptr.type),
3964  RNA_property_identifier(prop));
3965  return NULL;
3966  }
3967 
3968  ret = PyUnicode_FromString(path);
3969  MEM_freeN((void *)path);
3970 
3971  return ret;
3972 }
3973 
3974 PyDoc_STRVAR(pyrna_prop_as_bytes_doc,
3975  ".. method:: as_bytes()\n"
3976  "\n"
3977  " Returns this string property as a byte rather than a Python string.\n"
3978  "\n"
3979  " :return: The string as bytes.\n"
3980  " :rtype: bytes\n");
3981 static PyObject *pyrna_prop_as_bytes(BPy_PropertyRNA *self)
3982 {
3983 
3984  if (RNA_property_type(self->prop) != PROP_STRING) {
3985  PyErr_Format(PyExc_TypeError,
3986  "%.200s.%.200s.as_bytes() must be a string",
3987  RNA_struct_identifier(self->ptr.type),
3988  RNA_property_identifier(self->prop));
3989  return NULL;
3990  }
3991 
3992  PyObject *ret;
3993  char buf_fixed[256], *buf;
3994  int buf_len;
3995 
3997  &self->ptr, self->prop, buf_fixed, sizeof(buf_fixed), &buf_len);
3998 
3999  ret = PyBytes_FromStringAndSize(buf, buf_len);
4000 
4001  if (buf_fixed != buf) {
4002  MEM_freeN(buf);
4003  }
4004 
4005  return ret;
4006 }
4007 
4008 PyDoc_STRVAR(pyrna_prop_update_doc,
4009  ".. method:: update()\n"
4010  "\n"
4011  " Execute the properties update callback.\n"
4012  "\n"
4013  " .. note::\n"
4014  " This is called when assigning a property,\n"
4015  " however in rare cases it's useful to call explicitly.\n");
4016 static PyObject *pyrna_prop_update(BPy_PropertyRNA *self)
4017 {
4018  RNA_property_update(BPY_context_get(), &self->ptr, self->prop);
4019  Py_RETURN_NONE;
4020 }
4021 
4022 PyDoc_STRVAR(pyrna_struct_type_recast_doc,
4023  ".. method:: type_recast()\n"
4024  "\n"
4025  " Return a new instance, this is needed because types\n"
4026  " such as textures can be changed at runtime.\n"
4027  "\n"
4028  " :return: a new instance of this object with the type initialized again.\n"
4029  " :rtype: subclass of :class:`bpy.types.bpy_struct`\n");
4031 {
4032  PointerRNA r_ptr;
4033 
4034  PYRNA_STRUCT_CHECK_OBJ(self);
4035 
4036  RNA_pointer_recast(&self->ptr, &r_ptr);
4037  return pyrna_struct_CreatePyObject(&r_ptr);
4038 }
4039 
4043 static PyObject *pyrna_struct_bl_rna_find_subclass_recursive(PyObject *cls, const char *id)
4044 {
4045  PyObject *ret_test = NULL;
4046  PyObject *subclasses = ((PyTypeObject *)cls)->tp_subclasses;
4047  if (subclasses) {
4048  /* Unfortunately we can't use the dict key because Python class names
4049  * don't match the bl_idname used internally. */
4050  BLI_assert(PyDict_CheckExact(subclasses));
4051  PyObject *key = NULL;
4052  Py_ssize_t pos = 0;
4053  PyObject *value = NULL;
4054  while (PyDict_Next(subclasses, &pos, &key, &value)) {
4055  BLI_assert(PyWeakref_CheckRef(value));
4056  PyObject *subcls = PyWeakref_GET_OBJECT(value);
4057  if (subcls != Py_None) {
4058  BPy_StructRNA *py_srna = (BPy_StructRNA *)PyDict_GetItem(((PyTypeObject *)subcls)->tp_dict,
4060  if (py_srna) {
4061  StructRNA *srna = py_srna->ptr.data;
4062  if (STREQ(id, RNA_struct_identifier(srna))) {
4063  ret_test = subcls;
4064  break;
4065  }
4066  }
4067  ret_test = pyrna_struct_bl_rna_find_subclass_recursive(subcls, id);
4068  if (ret_test) {
4069  break;
4070  }
4071  }
4072  }
4073  }
4074  return ret_test;
4075 }
4076 
4077 PyDoc_STRVAR(pyrna_struct_bl_rna_get_subclass_py_doc,
4078  ".. classmethod:: bl_rna_get_subclass_py(id, default=None)\n"
4079  "\n"
4080  " :arg id: The RNA type identifier.\n"
4081  " :type id: string\n"
4082  " :return: The class or default when not found.\n"
4083  " :rtype: type\n");
4084 static PyObject *pyrna_struct_bl_rna_get_subclass_py(PyObject *cls, PyObject *args)
4085 {
4086  char *id;
4087  PyObject *ret_default = Py_None;
4088 
4089  if (!PyArg_ParseTuple(args, "s|O:bl_rna_get_subclass_py", &id, &ret_default)) {
4090  return NULL;
4091  }
4092  PyObject *ret = pyrna_struct_bl_rna_find_subclass_recursive(cls, id);
4093  if (ret == NULL) {
4094  ret = ret_default;
4095  }
4096  return Py_INCREF_RET(ret);
4097 }
4098 
4099 PyDoc_STRVAR(pyrna_struct_bl_rna_get_subclass_doc,
4100  ".. classmethod:: bl_rna_get_subclass(id, default=None)\n"
4101  "\n"
4102  " :arg id: The RNA type identifier.\n"
4103  " :type id: string\n"
4104  " :return: The RNA type or default when not found.\n"
4105  " :rtype: :class:`bpy.types.Struct` subclass\n");
4106 static PyObject *pyrna_struct_bl_rna_get_subclass(PyObject *cls, PyObject *args)
4107 {
4108  char *id;
4109  PyObject *ret_default = Py_None;
4110 
4111  if (!PyArg_ParseTuple(args, "s|O:bl_rna_get_subclass", &id, &ret_default)) {
4112  return NULL;
4113  }
4114 
4115  const BPy_StructRNA *py_srna = (BPy_StructRNA *)PyDict_GetItem(((PyTypeObject *)cls)->tp_dict,
4117  if (py_srna == NULL) {
4118  PyErr_SetString(PyExc_ValueError, "Not a registered class");
4119  return NULL;
4120  }
4121  const StructRNA *srna_base = py_srna->ptr.data;
4122 
4123  PointerRNA ptr;
4124  if (srna_base == &RNA_Node) {
4125  bNodeType *nt = nodeTypeFind(id);
4126  if (nt) {
4129  }
4130  }
4131  else {
4132  /* TODO, panels, menus etc. */
4133  PyErr_Format(
4134  PyExc_ValueError, "Class type \"%.200s\" not supported", RNA_struct_identifier(srna_base));
4135  return NULL;
4136  }
4137 
4138  return Py_INCREF_RET(ret_default);
4139 }
4140 
4141 static void pyrna_dir_members_py__add_keys(PyObject *list, PyObject *dict)
4142 {
4143  PyObject *list_tmp;
4144 
4145  list_tmp = PyDict_Keys(dict);
4146  PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
4147  Py_DECREF(list_tmp);
4148 }
4149 
4150 static void pyrna_dir_members_py(PyObject *list, PyObject *self)
4151 {
4152  PyObject *dict;
4153  PyObject **dict_ptr;
4154 
4155  dict_ptr = _PyObject_GetDictPtr((PyObject *)self);
4156 
4157  if (dict_ptr && (dict = *dict_ptr)) {
4158  pyrna_dir_members_py__add_keys(list, dict);
4159  }
4160 
4161  dict = ((PyTypeObject *)Py_TYPE(self))->tp_dict;
4162  if (dict) {
4163  pyrna_dir_members_py__add_keys(list, dict);
4164  }
4165 
4166  /* Since this is least common case, handle it last. */
4167  if (BPy_PropertyRNA_Check(self)) {
4168  BPy_PropertyRNA *self_prop = (BPy_PropertyRNA *)self;
4169  if (RNA_property_type(self_prop->prop) == PROP_COLLECTION) {
4170  PointerRNA r_ptr;
4171 
4172  if (RNA_property_collection_type_get(&self_prop->ptr, self_prop->prop, &r_ptr)) {
4173  PyObject *cls = pyrna_struct_Subtype(&r_ptr); /* borrows */
4174  dict = ((PyTypeObject *)cls)->tp_dict;
4175  pyrna_dir_members_py__add_keys(list, dict);
4176  Py_DECREF(cls);
4177  }
4178  }
4179  }
4180 }
4181 
4182 static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
4183 {
4184  const char *idname;
4185 
4186  /* For looping over attrs and funcs. */
4187  PointerRNA tptr;
4188  PropertyRNA *iterprop;
4189 
4190  {
4192  iterprop = RNA_struct_find_property(&tptr, "functions");
4193 
4194  RNA_PROP_BEGIN (&tptr, itemptr, iterprop) {
4195  FunctionRNA *func = itemptr.data;
4196  if (RNA_function_defined(func)) {
4197  idname = RNA_function_identifier(itemptr.data);
4198  PyList_APPEND(list, PyUnicode_FromString(idname));
4199  }
4200  }
4201  RNA_PROP_END;
4202  }
4203 
4204  {
4205  /*
4206  * Collect RNA attributes
4207  */
4208  char name[256], *nameptr;
4209  int namelen;
4210 
4211  iterprop = RNA_struct_iterator_property(ptr->type);
4212 
4213  RNA_PROP_BEGIN (ptr, itemptr, iterprop) {
4214  /* Custom-properties are exposed using `__getitem__`, exclude from `__dir__`. */
4215  if (RNA_property_is_idprop(itemptr.data)) {
4216  continue;
4217  }
4218  nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
4219 
4220  if (nameptr) {
4221  PyList_APPEND(list, PyUnicode_FromStringAndSize(nameptr, namelen));
4222 
4223  if (name != nameptr) {
4224  MEM_freeN(nameptr);
4225  }
4226  }
4227  }
4228  RNA_PROP_END;
4229  }
4230 }
4231 
4232 static PyObject *pyrna_struct_dir(BPy_StructRNA *self)
4233 {
4234  PyObject *ret;
4235 
4236  PYRNA_STRUCT_CHECK_OBJ(self);
4237 
4238  /* Include this in case this instance is a subtype of a Python class
4239  * In these instances we may want to return a function or variable provided by the subtype. */
4240  ret = PyList_New(0);
4241 
4242  if (!BPy_StructRNA_CheckExact(self)) {
4243  pyrna_dir_members_py(ret, (PyObject *)self);
4244  }
4245 
4246  pyrna_dir_members_rna(ret, &self->ptr);
4247 
4248  if (self->ptr.type == &RNA_Context) {
4249  ListBase lb = CTX_data_dir_get(self->ptr.data);
4250  LinkData *link;
4251 
4252  for (link = lb.first; link; link = link->next) {
4253  PyList_APPEND(ret, PyUnicode_FromString(link->data));
4254  }
4255 
4256  BLI_freelistN(&lb);
4257  }
4258 
4259  {
4260  /* set(), this is needed to remove-doubles because the deferred
4261  * register-props will be in both the Python __dict__ and accessed as RNA */
4262 
4263  PyObject *set = PySet_New(ret);
4264 
4265  Py_DECREF(ret);
4266  ret = PySequence_List(set);
4267  Py_DECREF(set);
4268  }
4269 
4270  return ret;
4271 }
4272 
4273 /* ---------------getattr-------------------------------------------- */
4274 static PyObject *pyrna_struct_getattro(BPy_StructRNA *self, PyObject *pyname)
4275 {
4276  const char *name = PyUnicode_AsUTF8(pyname);
4277  PyObject *ret;
4278  PropertyRNA *prop;
4279  FunctionRNA *func;
4280 
4281  PYRNA_STRUCT_CHECK_OBJ(self);
4282 
4283  if (name == NULL) {
4284  PyErr_SetString(PyExc_AttributeError, "bpy_struct: __getattr__ must be a string");
4285  ret = NULL;
4286  }
4287  else if (
4288  /* RNA can't start with a "_", so for __dict__ and similar we can skip using RNA lookups. */
4289  name[0] == '_') {
4290  /* Annoying exception, maybe we need to have different types for this... */
4291  if (STR_ELEM(name, "__getitem__", "__setitem__") &&
4292  !RNA_struct_idprops_check(self->ptr.type)) {
4293  PyErr_SetString(PyExc_AttributeError, "bpy_struct: no __getitem__ support for this type");
4294  ret = NULL;
4295  }
4296  else {
4297  ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
4298  }
4299  }
4300  else if ((prop = RNA_struct_find_property(&self->ptr, name))) {
4301  ret = pyrna_prop_to_py(&self->ptr, prop);
4302  }
4303  /* RNA function only if callback is declared (no optional functions). */
4304  else if ((func = RNA_struct_find_function(self->ptr.type, name)) && RNA_function_defined(func)) {
4305  ret = pyrna_func_to_py(&self->ptr, func);
4306  }
4307  else if (self->ptr.type == &RNA_Context) {
4308  bContext *C = self->ptr.data;
4309  if (C == NULL) {
4310  PyErr_Format(PyExc_AttributeError,
4311  "bpy_struct: Context is 'NULL', can't get \"%.200s\" from context",
4312  name);
4313  ret = NULL;
4314  }
4315  else {
4316  PointerRNA newptr;
4317  ListBase newlb;
4318  short newtype;
4319 
4320  const eContextResult done = CTX_data_get(C, name, &newptr, &newlb, &newtype);
4321 
4322  if (done == CTX_RESULT_OK) {
4323  switch (newtype) {
4324  case CTX_DATA_TYPE_POINTER:
4325  if (newptr.data == NULL) {
4326  ret = Py_None;
4327  Py_INCREF(ret);
4328  }
4329  else {
4330  ret = pyrna_struct_CreatePyObject(&newptr);
4331  }
4332  break;
4333  case CTX_DATA_TYPE_COLLECTION: {
4334  CollectionPointerLink *link;
4335 
4336  ret = PyList_New(0);
4337 
4338  for (link = newlb.first; link; link = link->next) {
4339  PyList_APPEND(ret, pyrna_struct_CreatePyObject(&link->ptr));
4340  }
4341  break;
4342  }
4343  default:
4344  /* Should never happen. */
4345  BLI_assert(!"Invalid context type");
4346 
4347  PyErr_Format(PyExc_AttributeError,
4348  "bpy_struct: Context type invalid %d, can't get \"%.200s\" from context",
4349  newtype,
4350  name);
4351  ret = NULL;
4352  break;
4353  }
4354  }
4355  else if (done == CTX_RESULT_NO_DATA) {
4356  ret = Py_None;
4357  Py_INCREF(ret);
4358  }
4359  else { /* Not found in the context. */
4360  /* Lookup the subclass. raise an error if it's not found. */
4361  ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
4362  }
4363 
4364  BLI_freelistN(&newlb);
4365  }
4366  }
4367  else {
4368 #if 0
4369  PyErr_Format(PyExc_AttributeError, "bpy_struct: attribute \"%.200s\" not found", name);
4370  ret = NULL;
4371 #endif
4372  /* Include this in case this instance is a subtype of a Python class
4373  * In these instances we may want to return a function or variable provided by the subtype
4374  *
4375  * Also needed to return methods when it's not a subtype.
4376  */
4377 
4378  /* The error raised here will be displayed */
4379  ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
4380  }
4381 
4382  return ret;
4383 }
4384 
4385 #if 0
4386 static int pyrna_struct_pydict_contains(PyObject *self, PyObject *pyname)
4387 {
4388  PyObject *dict = *(_PyObject_GetDictPtr((PyObject *)self));
4389  if (UNLIKELY(dict == NULL)) {
4390  return 0;
4391  }
4392 
4393  return PyDict_Contains(dict, pyname);
4394 }
4395 #endif
4396 
4397 /* --------------- setattr------------------------------------------- */
4398 
4399 #if 0
4400 static PyObject *pyrna_struct_meta_idprop_getattro(PyObject *cls, PyObject *attr)
4401 {
4402  PyObject *ret = PyType_Type.tp_getattro(cls, attr);
4403 
4404  /* Allows:
4405  * >>> bpy.types.Scene.foo = BoolProperty()
4406  * >>> bpy.types.Scene.foo
4407  * <bpy_struct, BoolProperty("foo")>
4408  * ...rather than returning the deferred class register tuple
4409  * as checked by BPy_PropDeferred_CheckTypeExact()
4410  *
4411  * Disable for now,
4412  * this is faking internal behavior in a way that's too tricky to maintain well. */
4413 # if 0
4414  if ((ret == NULL) /* || BPy_PropDeferred_CheckTypeExact(ret) */ ) {
4415  StructRNA *srna = srna_from_self(cls, "StructRNA.__getattr__");
4416  if (srna) {
4417  PropertyRNA *prop = RNA_struct_type_find_property(srna, PyUnicode_AsUTF8(attr));
4418  if (prop) {
4419  PointerRNA tptr;
4420  PyErr_Clear(); /* Clear error from tp_getattro. */
4421  RNA_pointer_create(NULL, &RNA_Property, prop, &tptr);
4423  }
4424  }
4425  }
4426 # endif
4427 
4428  return ret;
4429 }
4430 #endif
4431 
4432 static int pyrna_struct_meta_idprop_setattro(PyObject *cls, PyObject *attr, PyObject *value)
4433 {
4434  StructRNA *srna = srna_from_self(cls, "StructRNA.__setattr__");
4435  const bool is_deferred_prop = (value && BPy_PropDeferred_CheckTypeExact(value));
4436  const char *attr_str = PyUnicode_AsUTF8(attr);
4437 
4438  if (srna && !pyrna_write_check() &&
4439  (is_deferred_prop || RNA_struct_type_find_property(srna, attr_str))) {
4440  PyErr_Format(PyExc_AttributeError,
4441  "pyrna_struct_meta_idprop_setattro() "
4442  "can't set in readonly state '%.200s.%S'",
4443  ((PyTypeObject *)cls)->tp_name,
4444  attr);
4445  return -1;
4446  }
4447 
4448  if (srna == NULL) {
4449  /* Allow setting on unregistered classes which can be registered later on. */
4450 #if 0
4451  if (value && is_deferred_prop) {
4452  PyErr_Format(PyExc_AttributeError,
4453  "pyrna_struct_meta_idprop_setattro() unable to get srna from class '%.200s'",
4454  ((PyTypeObject *)cls)->tp_name);
4455  return -1;
4456  }
4457 #endif
4458  /* srna_from_self may set an error. */
4459  PyErr_Clear();
4460  return PyType_Type.tp_setattro(cls, attr, value);
4461  }
4462 
4463  if (value) {
4464  /* Check if the value is a property. */
4465  if (is_deferred_prop) {
4466  const int ret = deferred_register_prop(srna, attr, value);
4467  if (ret == -1) {
4468  /* Error set. */
4469  return ret;
4470  }
4471 
4472  /* pass through and assign to the classes __dict__ as well
4473  * so when the value isn't assigned it still creates the RNA property,
4474  * but gets confusing from script writers POV if the assigned value can't be read back. */
4475  }
4476  else {
4477  /* Remove existing property if it's set or we also end up with confusion. */
4478  RNA_def_property_free_identifier(srna, attr_str); /* Ignore on failure. */
4479  }
4480  }
4481  else { /* __delattr__ */
4482  /* First find if this is a registered property. */
4483  const int ret = RNA_def_property_free_identifier(srna, attr_str);
4484  if (ret == -1) {
4485  PyErr_Format(
4486  PyExc_TypeError, "struct_meta_idprop.detattr(): '%s' not a dynamic property", attr_str);
4487  return -1;
4488  }
4489  }
4490 
4491  /* Fallback to standard py, delattr/setattr. */
4492  return PyType_Type.tp_setattro(cls, attr, value);
4493 }
4494 
4495 static int pyrna_struct_setattro(BPy_StructRNA *self, PyObject *pyname, PyObject *value)
4496 {
4497  const char *name = PyUnicode_AsUTF8(pyname);
4498  PropertyRNA *prop = NULL;
4499 
4500  PYRNA_STRUCT_CHECK_INT(self);
4501 
4502 #ifdef USE_PEDANTIC_WRITE
4503  if (rna_disallow_writes && rna_id_write_error(&self->ptr, pyname)) {
4504  return -1;
4505  }
4506 #endif /* USE_PEDANTIC_WRITE */
4507 
4508  if (name == NULL) {
4509  PyErr_SetString(PyExc_AttributeError, "bpy_struct: __setattr__ must be a string");
4510  return -1;
4511  }
4512  if (name[0] != '_' && (prop = RNA_struct_find_property(&self->ptr, name))) {
4513  if (!RNA_property_editable_flag(&self->ptr, prop)) {
4514  PyErr_Format(PyExc_AttributeError,
4515  "bpy_struct: attribute \"%.200s\" from \"%.200s\" is read-only",
4517  RNA_struct_identifier(self->ptr.type));
4518  return -1;
4519  }
4520  }
4521  else if (self->ptr.type == &RNA_Context) {
4522  /* Code just raises correct error, context prop's can't be set,
4523  * unless it's a part of the py class. */
4524  bContext *C = self->ptr.data;
4525  if (C == NULL) {
4526  PyErr_Format(PyExc_AttributeError,
4527  "bpy_struct: Context is 'NULL', can't set \"%.200s\" from context",
4528  name);
4529  return -1;
4530  }
4531 
4532  PointerRNA newptr;
4533  ListBase newlb;
4534  short newtype;
4535 
4536  const eContextResult done = CTX_data_get(C, name, &newptr, &newlb, &newtype);
4537 
4538  if (done == CTX_RESULT_OK) {
4539  PyErr_Format(
4540  PyExc_AttributeError, "bpy_struct: Context property \"%.200s\" is read-only", name);
4541  BLI_freelistN(&newlb);
4542  return -1;
4543  }
4544 
4545  BLI_freelistN(&newlb);
4546  }
4547 
4548  /* pyrna_py_to_prop sets its own exceptions */
4549  if (prop) {
4550  if (value == NULL) {
4551  PyErr_SetString(PyExc_AttributeError, "bpy_struct: del not supported");
4552  return -1;
4553  }
4554  return pyrna_py_to_prop(&self->ptr, prop, NULL, value, "bpy_struct: item.attr = val:");
4555  }
4556 
4557  return PyObject_GenericSetAttr((PyObject *)self, pyname, value);
4558 }
4559 
4560 static PyObject *pyrna_prop_dir(BPy_PropertyRNA *self)
4561 {
4562  PyObject *ret;
4563  PointerRNA r_ptr;
4564 
4565  /* Include this in case this instance is a subtype of a Python class
4566  * In these instances we may want to return a function or variable provided by the subtype. */
4567  ret = PyList_New(0);
4568 
4569  if (!BPy_PropertyRNA_CheckExact(self)) {
4570  pyrna_dir_members_py(ret, (PyObject *)self);
4571  }
4572 
4573  if (RNA_property_type(self->prop) == PROP_COLLECTION) {
4574  if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
4575  pyrna_dir_members_rna(ret, &r_ptr);
4576  }
4577  }
4578 
4579  return ret;
4580 }
4581 
4582 static PyObject *pyrna_prop_array_getattro(BPy_PropertyRNA *self, PyObject *pyname)
4583 {
4584  return PyObject_GenericGetAttr((PyObject *)self, pyname);
4585 }
4586 
4587 static PyObject *pyrna_prop_collection_getattro(BPy_PropertyRNA *self, PyObject *pyname)
4588 {
4589  const char *name = PyUnicode_AsUTF8(pyname);
4590 
4591  if (name == NULL) {
4592  PyErr_SetString(PyExc_AttributeError, "bpy_prop_collection: __getattr__ must be a string");
4593  return NULL;
4594  }
4595  if (name[0] != '_') {
4596  PyObject *ret;
4597  PropertyRNA *prop;
4598  FunctionRNA *func;
4599 
4600  PointerRNA r_ptr;
4601  if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
4602  if ((prop = RNA_struct_find_property(&r_ptr, name))) {
4603  ret = pyrna_prop_to_py(&r_ptr, prop);
4604 
4605  return ret;
4606  }
4607  if ((func = RNA_struct_find_function(r_ptr.type, name))) {
4608  PyObject *self_collection = pyrna_struct_CreatePyObject(&r_ptr);
4609  ret = pyrna_func_to_py(&((BPy_DummyPointerRNA *)self_collection)->ptr, func);
4610  Py_DECREF(self_collection);
4611 
4612  return ret;
4613  }
4614  }
4615  }
4616 
4617 #if 0
4618  return PyObject_GenericGetAttr((PyObject *)self, pyname);
4619 #else
4620  {
4621  /* Could just do this except for 1 awkward case.
4622  * `PyObject_GenericGetAttr((PyObject *)self, pyname);`
4623  * so as to support `bpy.data.library.load()` */
4624 
4625  PyObject *ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
4626 
4627  if (ret == NULL && name[0] != '_') { /* Avoid inheriting `__call__` and similar. */
4628  /* Since this is least common case, handle it last. */
4629  PointerRNA r_ptr;
4630  if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
4631  PyObject *cls;
4632 
4633  PyObject *error_type, *error_value, *error_traceback;
4634  PyErr_Fetch(&error_type, &error_value, &error_traceback);
4635  PyErr_Clear();
4636 
4637  cls = pyrna_struct_Subtype(&r_ptr);
4638  ret = PyObject_GenericGetAttr(cls, pyname);
4639  Py_DECREF(cls);
4640 
4641  /* Restore the original error. */
4642  if (ret == NULL) {
4643  PyErr_Restore(error_type, error_value, error_traceback);
4644  }
4645  else {
4646  if (Py_TYPE(ret) == &PyMethodDescr_Type) {
4647  PyMethodDef *m = ((PyMethodDescrObject *)ret)->d_method;
4648  /* TODO: #METH_CLASS */
4649  if (m->ml_flags & METH_STATIC) {
4650  /* Keep 'ret' as-is. */
4651  }
4652  else {
4653  Py_DECREF(ret);
4654  ret = PyCMethod_New(m, (PyObject *)self, NULL, NULL);
4655  }
4656  }
4657  }
4658  }
4659  }
4660 
4661  return ret;
4662  }
4663 #endif
4664 }
4665 
4666 /* --------------- setattr------------------------------------------- */
4667 static int pyrna_prop_collection_setattro(BPy_PropertyRNA *self, PyObject *pyname, PyObject *value)
4668 {
4669  const char *name = PyUnicode_AsUTF8(pyname);
4670  PropertyRNA *prop;
4671  PointerRNA r_ptr;
4672 
4673 #ifdef USE_PEDANTIC_WRITE
4674  if (rna_disallow_writes && rna_id_write_error(&self->ptr, pyname)) {
4675  return -1;
4676  }
4677 #endif /* USE_PEDANTIC_WRITE */
4678 
4679  if (name == NULL) {
4680  PyErr_SetString(PyExc_AttributeError, "bpy_prop: __setattr__ must be a string");
4681  return -1;
4682  }
4683  if (value == NULL) {
4684  PyErr_SetString(PyExc_AttributeError, "bpy_prop: del not supported");
4685  return -1;
4686  }
4687  if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
4688  if ((prop = RNA_struct_find_property(&r_ptr, name))) {
4689  /* pyrna_py_to_prop sets its own exceptions. */
4690  return pyrna_py_to_prop(&r_ptr, prop, NULL, value, "BPy_PropertyRNA - Attribute (setattr):");
4691  }
4692  }
4693 
4694  PyErr_Format(PyExc_AttributeError, "bpy_prop_collection: attribute \"%.200s\" not found", name);
4695  return -1;
4696 }
4697 
4702 {
4703  PointerRNA r_ptr;
4704 
4705 #ifdef USE_PEDANTIC_WRITE
4707  return NULL;
4708  }
4709 #endif /* USE_PEDANTIC_WRITE */
4710 
4711  RNA_property_collection_add(&self->ptr, self->prop, &r_ptr);
4712  if (!r_ptr.data) {
4713  PyErr_SetString(PyExc_TypeError,
4714  "bpy_prop_collection.add(): not supported for this collection");
4715  return NULL;
4716  }
4717 
4718  return pyrna_struct_CreatePyObject(&r_ptr);
4719 }
4720 
4721 static PyObject *pyrna_prop_collection_idprop_remove(BPy_PropertyRNA *self, PyObject *value)
4722 {
4723  const int key = PyLong_AsLong(value);
4724 
4725 #ifdef USE_PEDANTIC_WRITE
4727  return NULL;
4728  }
4729 #endif /* USE_PEDANTIC_WRITE */
4730 
4731  if (key == -1 && PyErr_Occurred()) {
4732  PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.remove(): expected one int argument");
4733  return NULL;
4734  }
4735 
4736  if (!RNA_property_collection_remove(&self->ptr, self->prop, key)) {
4737  PyErr_SetString(PyExc_TypeError,
4738  "bpy_prop_collection.remove() not supported for this collection");
4739  return NULL;
4740  }
4741 
4742  Py_RETURN_NONE;
4743 }
4744 
4746 {
4747 #ifdef USE_PEDANTIC_WRITE
4749  return NULL;
4750  }
4751 #endif /* USE_PEDANTIC_WRITE */
4752 
4753  RNA_property_collection_clear(&self->ptr, self->prop);
4754 
4755  Py_RETURN_NONE;
4756 }
4757 
4758 static PyObject *pyrna_prop_collection_idprop_move(BPy_PropertyRNA *self, PyObject *args)
4759 {
4760  int key = 0, pos = 0;
4761 
4762 #ifdef USE_PEDANTIC_WRITE
4764  return NULL;
4765  }
4766 #endif /* USE_PEDANTIC_WRITE */
4767 
4768  if (!PyArg_ParseTuple(args, "ii", &key, &pos)) {
4769  PyErr_SetString(PyExc_TypeError, "bpy_prop_collection.move(): expected two ints as arguments");
4770  return NULL;
4771  }
4772 
4773  if (!RNA_property_collection_move(&self->ptr, self->prop, key, pos)) {
4774  PyErr_SetString(PyExc_TypeError,
4775  "bpy_prop_collection.move() not supported for this collection");
4776  return NULL;
4777  }
4778 
4779  Py_RETURN_NONE;
4780 }
4781 
4782 PyDoc_STRVAR(pyrna_struct_get_id_data_doc,
4783  "The :class:`bpy.types.ID` object this datablock is from or None, (not available for "
4784  "all data types)");
4786 {
4787  /* Used for struct and pointer since both have a ptr. */
4788  if (self->ptr.owner_id) {
4789  PointerRNA id_ptr;
4790  RNA_id_pointer_create((ID *)self->ptr.owner_id, &id_ptr);
4791  return pyrna_struct_CreatePyObject(&id_ptr);
4792  }
4793 
4794  Py_RETURN_NONE;
4795 }
4796 
4797 PyDoc_STRVAR(pyrna_struct_get_data_doc,
4798  "The data this property is using, *type* :class:`bpy.types.bpy_struct`");
4800 {
4801  return pyrna_struct_CreatePyObject(&self->ptr);
4802 }
4803 
4804 PyDoc_STRVAR(pyrna_struct_get_rna_type_doc, "The property type for introspection");
4806 {
4807  PointerRNA tptr;
4808  RNA_pointer_create(NULL, &RNA_Property, self->prop, &tptr);
4809  return pyrna_struct_Subtype(&tptr);
4810 }
4811 
4812 /*****************************************************************************/
4813 /* Python attributes get/set structure: */
4814 /*****************************************************************************/
4815 
4816 static PyGetSetDef pyrna_prop_getseters[] = {
4817  {"id_data",
4818  (getter)pyrna_struct_get_id_data,
4819  (setter)NULL,
4820  pyrna_struct_get_id_data_doc,
4821  NULL},
4822  {"data", (getter)pyrna_struct_get_data, (setter)NULL, pyrna_struct_get_data_doc, NULL},
4823  {"rna_type",
4824  (getter)pyrna_struct_get_rna_type,
4825  (setter)NULL,
4826  pyrna_struct_get_rna_type_doc,
4827  NULL},
4828  {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
4829 };
4830 
4831 static PyGetSetDef pyrna_struct_getseters[] = {
4832  {"id_data",
4833  (getter)pyrna_struct_get_id_data,
4834  (setter)NULL,
4835  pyrna_struct_get_id_data_doc,
4836  NULL},
4837  {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
4838 };
4839 
4840 static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *closure);
4841 
4842 static PyGetSetDef pyrna_func_getseters[] = {
4843  {"__doc__", (getter)pyrna_func_doc_get, (setter)NULL, NULL, NULL},
4844  {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
4845 };
4846 
4847 PyDoc_STRVAR(pyrna_prop_collection_keys_doc,
4848  ".. method:: keys()\n"
4849  "\n"
4850  " Return the identifiers of collection members\n"
4851  " (matching Python's dict.keys() functionality).\n"
4852  "\n"
4853  " :return: the identifiers for each member of this collection.\n"
4854  " :rtype: list of strings\n");
4856 {
4857  PyObject *ret = PyList_New(0);
4858  char name[256], *nameptr;
4859  int namelen;
4860 
4861  RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop) {
4862  nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
4863 
4864  if (nameptr) {
4865  PyList_APPEND(ret, PyUnicode_FromStringAndSize(nameptr, namelen));
4866 
4867  if (name != nameptr) {
4868  MEM_freeN(nameptr);
4869  }
4870  }
4871  }
4872  RNA_PROP_END;
4873 
4874  return ret;
4875 }
4876 
4877 PyDoc_STRVAR(pyrna_prop_collection_items_doc,
4878  ".. method:: items()\n"
4879  "\n"
4880  " Return the identifiers of collection members\n"
4881  " (matching Python's dict.items() functionality).\n"
4882  "\n"
4883  " :return: (key, value) pairs for each member of this collection.\n"
4884  " :rtype: list of tuples\n");
4886 {
4887  PyObject *ret = PyList_New(0);
4888  PyObject *item;
4889  char name[256], *nameptr;
4890  int namelen;
4891  int i = 0;
4892 
4893  RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop) {
4894  if (itemptr.data) {
4895  /* Add to Python list. */
4896  item = PyTuple_New(2);
4897  nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
4898  if (nameptr) {
4899  PyTuple_SET_ITEM(item, 0, PyUnicode_FromStringAndSize(nameptr, namelen));
4900  if (name != nameptr) {
4901  MEM_freeN(nameptr);
4902  }
4903  }
4904  else {
4905  /* A bit strange, but better than returning an empty list. */
4906  PyTuple_SET_ITEM(item, 0, PyLong_FromLong(i));
4907  }
4908  PyTuple_SET_ITEM(item, 1, pyrna_struct_CreatePyObject(&itemptr));
4909 
4910  PyList_APPEND(ret, item);
4911 
4912  i++;
4913  }
4914  }
4915  RNA_PROP_END;
4916 
4917  return ret;
4918 }
4919 
4920 PyDoc_STRVAR(pyrna_prop_collection_values_doc,
4921  ".. method:: values()\n"
4922  "\n"
4923  " Return the values of collection\n"
4924  " (matching Python's dict.values() functionality).\n"
4925  "\n"
4926  " :return: the members of this collection.\n"
4927  " :rtype: list\n");
4929 {
4930  /* Re-use slice. */
4931  return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
4932 }
4933 
4934 PyDoc_STRVAR(pyrna_struct_get_doc,
4935  ".. method:: get(key, default=None)\n"
4936  "\n"
4937  " Returns the value of the custom property assigned to key or default\n"
4938  " when not found (matches Python's dictionary function of the same name).\n"
4939  "\n"
4940  " :arg key: The key associated with the custom property.\n"
4941  " :type key: string\n"
4942  " :arg default: Optional argument for the value to return if\n"
4943  " *key* is not found.\n"
4944  " :type default: Undefined\n"
4946 static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
4947 {
4948  IDProperty *group, *idprop;
4949 
4950  const char *key;
4951  PyObject *def = Py_None;
4952 
4953  PYRNA_STRUCT_CHECK_OBJ(self);
4954 
4955  if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) {
4956  return NULL;
4957  }
4958 
4959  /* Mostly copied from BPy_IDGroup_Map_GetItem. */
4960  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
4961  PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
4962  return NULL;
4963  }
4964 
4965  group = RNA_struct_idprops(&self->ptr, 0);
4966  if (group) {
4967  idprop = IDP_GetPropertyFromGroup(group, key);
4968 
4969  if (idprop) {
4970  return BPy_IDGroup_WrapData(self->ptr.owner_id, idprop, group);
4971  }
4972  }
4973 
4974  return Py_INCREF_RET(def);
4975 }
4976 
4977 PyDoc_STRVAR(pyrna_struct_pop_doc,
4978  ".. method:: pop(key, default=None)\n"
4979  "\n"
4980  " Remove and return the value of the custom property assigned to key or default\n"
4981  " when not found (matches Python's dictionary function of the same name).\n"
4982  "\n"
4983  " :arg key: The key associated with the custom property.\n"
4984  " :type key: string\n"
4985  " :arg default: Optional argument for the value to return if\n"
4986  " *key* is not found.\n"
4987  " :type default: Undefined\n"
4989 static PyObject *pyrna_struct_pop(BPy_StructRNA *self, PyObject *args)
4990 {
4991  IDProperty *group, *idprop;
4992 
4993  const char *key;
4994  PyObject *def = NULL;
4995 
4996  PYRNA_STRUCT_CHECK_OBJ(self);
4997 
4998  if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) {
4999  return NULL;
5000  }
5001 
5002  /* Mostly copied from BPy_IDGroup_Map_GetItem. */
5003  if (RNA_struct_idprops_check(self->ptr.type) == 0) {
5004  PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
5005  return NULL;
5006  }
5007 
5008  group = RNA_struct_idprops(&self->ptr, 0);
5009  if (group) {
5010  idprop = IDP_GetPropertyFromGroup(group, key);
5011 
5012  if (idprop) {
5013  /* Don't use #BPy_IDGroup_WrapData as the id-property is being removed from the ID. */
5014  PyObject *ret = BPy_IDGroup_MapDataToPy(idprop);
5015  /* Internal error. */
5016  if (UNLIKELY(ret == NULL)) {
5017  return NULL;
5018  }
5019  IDP_FreeFromGroup(group, idprop);
5020  return ret;
5021  }
5022  }
5023 
5024  if (def == NULL) {
5025  PyErr_SetString(PyExc_KeyError, "key not found");
5026  return NULL;
5027  }
5028  return Py_INCREF_RET(def);
5029 }
5030 
5031 PyDoc_STRVAR(pyrna_struct_as_pointer_doc,
5032  ".. method:: as_pointer()\n"
5033  "\n"
5034  " Returns the memory address which holds a pointer to Blender's internal data\n"
5035  "\n"
5036  " :return: int (memory address).\n"
5037  " :rtype: int\n"
5038  "\n"
5039  " .. note:: This is intended only for advanced script writers who need to\n"
5040  " pass blender data to their own C/Python modules.\n");
5042 {
5043  return PyLong_FromVoidPtr(self->ptr.data);
5044 }
5045 
5046 PyDoc_STRVAR(pyrna_prop_collection_get_doc,
5047  ".. method:: get(key, default=None)\n"
5048  "\n"
5049  " Returns the value of the item assigned to key or default when not found\n"
5050  " (matches Python's dictionary function of the same name).\n"
5051  "\n"
5052  " :arg key: The identifier for the collection member.\n"
5053  " :type key: string\n"
5054  " :arg default: Optional argument for the value to return if\n"
5055  " *key* is not found.\n"
5056  " :type default: Undefined\n");
5057 static PyObject *pyrna_prop_collection_get(BPy_PropertyRNA *self, PyObject *args)
5058 {
5059  PointerRNA newptr;
5060 
5061  PyObject *key_ob;
5062  PyObject *def = Py_None;
5063 
5064  PYRNA_PROP_CHECK_OBJ(self);
5065 
5066  if (!PyArg_ParseTuple(args, "O|O:get", &key_ob, &def)) {
5067  return NULL;
5068  }
5069 
5070  if (PyUnicode_Check(key_ob)) {
5071  const char *key = PyUnicode_AsUTF8(key_ob);
5072 
5073  if (RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr)) {
5074  return pyrna_struct_CreatePyObject(&newptr);
5075  }
5076  }
5077  else if (PyTuple_Check(key_ob)) {
5079  self, key_ob, "bpy_prop_collection.get((id, lib))", false);
5080  if (ret) {
5081  return ret;
5082  }
5083  }
5084  else {
5085  PyErr_Format(PyExc_KeyError,
5086  "bpy_prop_collection.get(key, ...): key must be a string or tuple, not %.200s",
5087  Py_TYPE(key_ob)->tp_name);
5088  }
5089 
5090  return Py_INCREF_RET(def);
5091 }
5092 
5093 PyDoc_STRVAR(pyrna_prop_collection_find_doc,
5094  ".. method:: find(key)\n"
5095  "\n"
5096  " Returns the index of a key in a collection or -1 when not found\n"
5097  " (matches Python's string find function of the same name).\n"
5098  "\n"
5099  " :arg key: The identifier for the collection member.\n"
5100  " :type key: string\n"
5101  " :return: index of the key.\n"
5102  " :rtype: int\n");
5103 static PyObject *pyrna_prop_collection_find(BPy_PropertyRNA *self, PyObject *key_ob)
5104 {
5105  Py_ssize_t key_len_ssize_t;
5106  const char *key = PyUnicode_AsUTF8AndSize(key_ob, &key_len_ssize_t);
5107  const int key_len = (int)key_len_ssize_t; /* Compare with same type. */
5108 
5109  char name[256], *nameptr;
5110  int namelen;
5111  int i = 0;
5112  int index = -1;
5113 
5114  PYRNA_PROP_CHECK_OBJ(self);
5115 
5116  RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop) {
5117  nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
5118 
5119  if (nameptr) {
5120  if ((key_len == namelen) && memcmp(nameptr, key, key_len) == 0) {
5121  index = i;
5122  break;
5123  }
5124 
5125  if (name != nameptr) {
5126  MEM_freeN(nameptr);
5127  }
5128  }
5129 
5130  i++;
5131  }
5132  RNA_PROP_END;
5133 
5134  return PyLong_FromLong(index);
5135 }
5136 
5138  const char *attr,
5139  /* Values to assign. */
5140  RawPropertyType *r_raw_type,
5141  int *r_attr_tot,
5142  bool *r_attr_signed)
5143 {
5144  PropertyRNA *prop;
5145  bool attr_ok = true;
5146  *r_raw_type = PROP_RAW_UNSET;
5147  *r_attr_tot = 0;
5148  *r_attr_signed = false;
5149 
5150  /* NOTE: this is fail with zero length lists, so don't let this get called in that case. */
5151  RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop) {
5152  prop = RNA_struct_find_property(&itemptr, attr);
5153  if (prop) {
5154  *r_raw_type = RNA_property_raw_type(prop);
5155  *r_attr_tot = RNA_property_array_length(&itemptr, prop);
5156  *r_attr_signed = (RNA_property_subtype(prop) != PROP_UNSIGNED);
5157  }
5158  else {
5159  attr_ok = false;
5160  }
5161  break;
5162  }
5163  RNA_PROP_END;
5164 
5165  return attr_ok;
5166 }
5167 
5168 /* pyrna_prop_collection_foreach_get/set both use this. */
5170  PyObject *args,
5171 
5172  /* Values to assign. */
5173  const char **r_attr,
5174  PyObject **r_seq,
5175  int *r_tot,
5176  int *r_size,
5177  RawPropertyType *r_raw_type,
5178  int *r_attr_tot,
5179  bool *r_attr_signed)
5180 {
5181 #if 0
5182  int array_tot;
5183  int target_tot;
5184 #endif
5185 
5186  *r_size = *r_attr_tot = 0;
5187  *r_attr_signed = false;
5188  *r_raw_type = PROP_RAW_UNSET;
5189 
5190  if (!PyArg_ParseTuple(args, "sO:foreach_get/set", r_attr, r_seq)) {
5191  return -1;
5192  }
5193 
5194  if (!PySequence_Check(*r_seq) && PyObject_CheckBuffer(*r_seq)) {
5195  PyErr_Format(
5196  PyExc_TypeError,
5197  "foreach_get/set expected second argument to be a sequence or buffer, not a %.200s",
5198  Py_TYPE(*r_seq)->tp_name);
5199  return -1;
5200  }
5201 
5202  /* TODO - buffer may not be a sequence! array.array() is though. */
5203  *r_tot = PySequence_Size(*r_seq);
5204 
5205  if (*r_tot > 0) {
5206  if (!foreach_attr_type(self, *r_attr, r_raw_type, r_attr_tot, r_attr_signed)) {
5207  PyErr_Format(PyExc_AttributeError,
5208  "foreach_get/set '%.200s.%200s[...]' elements have no attribute '%.200s'",
5209  RNA_struct_identifier(self->ptr.type),
5211  *r_attr);
5212  return -1;
5213  }
5214  *r_size = RNA_raw_type_sizeof(*r_raw_type);
5215 
5216 #if 0 /* Works fine, but not strictly needed. \
5217  * we could allow RNA_property_collection_raw_* to do the checks */
5218  if ((*r_attr_tot) < 1) {
5219  *r_attr_tot = 1;
5220  }
5221 
5222  if (RNA_property_type(self->prop) == PROP_COLLECTION) {
5223  array_tot = RNA_property_collection_length(&self->ptr, self->prop);
5224  }
5225  else {
5226  array_tot = RNA_property_array_length(&self->ptr, self->prop);
5227  }
5228 
5229  target_tot = array_tot * (*r_attr_tot);
5230 
5231  /* rna_access.c - rna_raw_access(...) uses this same method. */
5232  if (target_tot != (*r_tot)) {
5233  PyErr_Format(PyExc_TypeError,
5234  "foreach_get(attr, sequence) sequence length mismatch given %d, needed %d",
5235  *r_tot,
5236  target_tot);
5237  return -1;
5238  }
5239 #endif
5240  }
5241 
5242  /* Check 'r_attr_tot' otherwise we don't know if any values were set.
5243  * This isn't ideal because it means running on an empty list may
5244  * fail silently when it's not compatible. */
5245  if (*r_size == 0 && *r_attr_tot != 0) {
5246  PyErr_SetString(PyExc_AttributeError, "attribute does not support foreach method");
5247  return -1;
5248  }
5249  return 0;
5250 }
5251 
5252 static bool foreach_compat_buffer(RawPropertyType raw_type, int attr_signed, const char *format)
5253 {
5254  const char f = format ? *format : 'B'; /* B is assumed when not set */
5255 
5256  switch (raw_type) {
5257  case PROP_RAW_CHAR:
5258  if (attr_signed) {
5259  return (f == 'b') ? 1 : 0;
5260  }
5261  else {
5262  return (f == 'B') ? 1 : 0;
5263  }
5264  case PROP_RAW_SHORT:
5265  if (attr_signed) {
5266  return (f == 'h') ? 1 : 0;
5267  }
5268  else {
5269  return (f == 'H') ? 1 : 0;
5270  }
5271  case PROP_RAW_INT:
5272  if (attr_signed) {
5273  return (f == 'i') ? 1 : 0;
5274  }
5275  else {
5276  return (f == 'I') ? 1 : 0;
5277  }
5278  case PROP_RAW_BOOLEAN:
5279  return (f == '?') ? 1 : 0;
5280  case PROP_RAW_FLOAT:
5281  return (f == 'f') ? 1 : 0;
5282  case PROP_RAW_DOUBLE:
5283  return (f == 'd') ? 1 : 0;
5284  case PROP_RAW_UNSET:
5285  return 0;
5286  }
5287 
5288  return 0;
5289 }
5290 
5291 static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
5292 {
5293  PyObject *item = NULL;
5294  int i = 0, ok = 0;
5295  bool buffer_is_compat;
5296  void *array = NULL;
5297 
5298  /* Get/set both take the same args currently. */
5299  const char *attr;
5300  PyObject *seq;
5301  int tot, size, attr_tot;
5302  bool attr_signed;
5303  RawPropertyType raw_type;
5304 
5305  if (foreach_parse_args(
5306  self, args, &attr, &seq, &tot, &size, &raw_type, &attr_tot, &attr_signed) == -1) {
5307  return NULL;
5308  }
5309 
5310  if (tot == 0) {
5311  Py_RETURN_NONE;
5312  }
5313 
5314  if (set) { /* Get the array from python. */
5315  buffer_is_compat = false;
5316  if (PyObject_CheckBuffer(seq)) {
5317  Py_buffer buf;
5318  PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
5319 
5320  /* Check if the buffer matches. */
5321 
5322  buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
5323 
5324  if (buffer_is_compat) {
5326  NULL, &self->ptr, self->prop, attr, buf.buf, raw_type, tot);
5327  }
5328 
5329  PyBuffer_Release(&buf);
5330  }
5331 
5332  /* Could not use the buffer, fallback to sequence. */
5333  if (!buffer_is_compat) {
5334  array = PyMem_Malloc(size * tot);
5335 
5336  for (; i < tot; i++) {
5337  item = PySequence_GetItem(seq, i);
5338  switch (raw_type) {
5339  case PROP_RAW_CHAR:
5340  ((char *)array)[i] = (char)PyLong_AsLong(item);
5341  break;
5342  case PROP_RAW_SHORT:
5343  ((short *)array)[i] = (short)PyLong_AsLong(item);
5344  break;
5345  case PROP_RAW_INT:
5346  ((int *)array)[i] = (int)PyLong_AsLong(item);
5347  break;
5348  case PROP_RAW_BOOLEAN:
5349  ((bool *)array)[i] = (int)PyLong_AsLong(item) != 0;
5350  break;
5351  case PROP_RAW_FLOAT:
5352  ((float *)array)[i] = (float)PyFloat_AsDouble(item);
5353  break;
5354  case PROP_RAW_DOUBLE:
5355  ((double *)array)[i] = (double)PyFloat_AsDouble(item);
5356  break;
5357  case PROP_RAW_UNSET:
5358  /* Should never happen. */
5359  BLI_assert(!"Invalid array type - set");
5360  break;
5361  }
5362 
5363  Py_DECREF(item);
5364  }
5365 
5367  NULL, &self->ptr, self->prop, attr, array, raw_type, tot);
5368  }
5369  }
5370  else {
5371  buffer_is_compat = false;
5372  if (PyObject_CheckBuffer(seq)) {
5373  Py_buffer buf;
5374  PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
5375 
5376  /* Check if the buffer matches, TODO - signed/unsigned types. */
5377 
5378  buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
5379 
5380  if (buffer_is_compat) {
5382  NULL, &self->ptr, self->prop, attr, buf.buf, raw_type, tot);
5383  }
5384 
5385  PyBuffer_Release(&buf);
5386  }
5387 
5388  /* Could not use the buffer, fallback to sequence. */
5389  if (!buffer_is_compat) {
5390  array = PyMem_Malloc(size * tot);
5391 
5393  NULL, &self->ptr, self->prop, attr, array, raw_type, tot);
5394 
5395  if (!ok) {
5396  /* Skip the loop. */
5397  i = tot;
5398  }
5399 
5400  for (; i < tot; i++) {
5401 
5402  switch (raw_type) {
5403  case PROP_RAW_CHAR:
5404  item = PyLong_FromLong((long)((char *)array)[i]);
5405  break;
5406  case PROP_RAW_SHORT:
5407  item = PyLong_FromLong((long)((short *)array)[i]);
5408  break;
5409  case PROP_RAW_INT:
5410  item = PyLong_FromLong((long)((int *)array)[i]);
5411  break;
5412  case PROP_RAW_FLOAT:
5413  item = PyFloat_FromDouble((double)((float *)array)[i]);
5414  break;
5415  case PROP_RAW_DOUBLE:
5416  item = PyFloat_FromDouble((double)((double *)array)[i]);
5417  break;
5418  case PROP_RAW_BOOLEAN:
5419  item = PyBool_FromLong((long)((bool *)array)[i]);
5420  break;
5421  default: /* PROP_RAW_UNSET */
5422  /* Should never happen. */
5423  BLI_assert(!"Invalid array type - get");
5424  item = Py_None;
5425  Py_INCREF(item);
5426  break;
5427  }
5428 
5429  PySequence_SetItem(seq, i, item);
5430  Py_DECREF(item);
5431  }
5432  }
5433  }
5434 
5435  if (array) {
5436  PyMem_Free(array);
5437  }
5438 
5439  if (PyErr_Occurred()) {
5440  /* Maybe we could make our own error. */
5441  PyErr_Print();
5442  PyErr_SetString(PyExc_TypeError, "couldn't access the py sequence");
5443  return NULL;
5444  }
5445  if (!ok) {
5446  PyErr_SetString(PyExc_RuntimeError, "internal error setting the array");
5447  return NULL;
5448  }
5449 
5450  Py_RETURN_NONE;
5451 }
5452 
5453 PyDoc_STRVAR(pyrna_prop_collection_foreach_get_doc,
5454  ".. method:: foreach_get(attr, seq)\n"
5455  "\n"
5456  " This is a function to give fast access to attributes within a collection.\n");
5457 static PyObject *pyrna_prop_collection_foreach_get(BPy_PropertyRNA *self, PyObject *args)
5458 {
5459  PYRNA_PROP_CHECK_OBJ(self);
5460 
5461  return foreach_getset(self, args, 0);
5462 }
5463 
5464 PyDoc_STRVAR(pyrna_prop_collection_foreach_set_doc,
5465  ".. method:: foreach_set(attr, seq)\n"
5466  "\n"
5467  " This is a function to give fast access to attributes within a collection.\n");
5468 static PyObject *pyrna_prop_collection_foreach_set(BPy_PropertyRNA *self, PyObject *args)
5469 {
5470  PYRNA_PROP_CHECK_OBJ(self);
5471 
5472  return foreach_getset(self, args, 1);
5473 }
5474 
5476  PyObject *args,
5477  const bool do_set)
5478 {
5479  PyObject *item = NULL;
5480  Py_ssize_t i, seq_size, size;
5481  void *array = NULL;
5482  const PropertyType prop_type = RNA_property_type(self->prop);
5483 
5484  /* Get/set both take the same args currently. */
5485  PyObject *seq;
5486 
5487  if (!ELEM(prop_type, PROP_INT, PROP_FLOAT)) {
5488  PyErr_Format(PyExc_TypeError, "foreach_get/set available only for int and float");
5489  return NULL;
5490  }
5491 
5492  if (!PyArg_ParseTuple(args, "O:foreach_get/set", &seq)) {
5493  return NULL;
5494  }
5495 
5496  if (!PySequence_Check(seq) && PyObject_CheckBuffer(seq)) {
5497  PyErr_Format(
5498  PyExc_TypeError,
5499  "foreach_get/set expected second argument to be a sequence or buffer, not a %.200s",
5500  Py_TYPE(seq)->tp_name);
5501  return NULL;
5502  }
5503 
5504  size = pyrna_prop_array_length(self);
5505  seq_size = PySequence_Size(seq);
5506 
5507  if (size != seq_size) {
5508  PyErr_Format(PyExc_TypeError, "expected sequence size %d, got %d", size, seq_size);
5509  return NULL;
5510  }
5511 
5512  Py_buffer buf;
5513  if (PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT) == -1) {
5514  PyErr_Clear();
5515 
5516  switch (prop_type) {
5517  case PROP_INT:
5518  array = PyMem_Malloc(sizeof(int) * size);
5519  if (do_set) {
5520  for (i = 0; i < size; i++) {
5521  item = PySequence_GetItem(seq, i);
5522  ((int *)array)[i] = (int)PyLong_AsLong(item);
5523  Py_DECREF(item);
5524  }
5525 
5526  RNA_property_int_set_array(&self->ptr, self->prop, array);
5527  }
5528  else {
5529  RNA_property_int_get_array(&self->ptr, self->prop, array);
5530 
5531  for (i = 0; i < size; i++) {
5532  item = PyLong_FromLong((long)((int *)array)[i]);
5533  PySequence_SetItem(seq, i, item);
5534  Py_DECREF(item);
5535  }
5536  }
5537 
5538  break;
5539  case PROP_FLOAT:
5540  array = PyMem_Malloc(sizeof(float) * size);
5541  if (do_set) {
5542  for (i = 0; i < size; i++) {
5543  item = PySequence_GetItem(seq, i);
5544  ((float *)array)[i] = (float)PyFloat_AsDouble(item);
5545  Py_DECREF(item);
5546  }
5547 
5549  }
5550  else {
5552 
5553  for (i = 0; i < size; i++) {
5554  item = PyFloat_FromDouble((double)((float *)array)[i]);
5555  PySequence_SetItem(seq, i, item);
5556  Py_DECREF(item);
5557  }
5558  }
5559  break;
5560  case PROP_BOOLEAN:
5561  case PROP_STRING:
5562  case PROP_ENUM:
5563  case PROP_POINTER:
5564  case PROP_COLLECTION:
5565  /* Should never happen. */
5567  break;
5568  }
5569 
5570  PyMem_Free(array);
5571 
5572  if (PyErr_Occurred()) {
5573  /* Maybe we could make our own error. */
5574  PyErr_Print();
5575  PyErr_SetString(PyExc_TypeError, "couldn't access the py sequence");
5576  return NULL;
5577  }
5578  }
5579  else {
5580  const char f = buf.format ? buf.format[0] : 0;
5581  if ((prop_type == PROP_INT && (buf.itemsize != sizeof(int) || (f != 'l' && f != 'i'))) ||
5582  (prop_type == PROP_FLOAT && (buf.itemsize != sizeof(float) || f != 'f'))) {
5583  PyBuffer_Release(&buf);
5584  PyErr_Format(PyExc_TypeError, "incorrect sequence item type: %s", buf.format);
5585  return NULL;
5586  }
5587 
5588  switch (prop_type) {
5589  case PROP_INT:
5590  if (do_set) {
5591  RNA_property_int_set_array(&self->ptr, self->prop, buf.buf);
5592  }
5593  else {
5594  RNA_property_int_get_array(&self->ptr, self->prop, buf.buf);
5595  }
5596  break;
5597  case PROP_FLOAT:
5598  if (do_set) {
5599  RNA_property_float_set_array(&self->ptr, self->prop, buf.buf);
5600  }
5601  else {
5602  RNA_property_float_get_array(&self->ptr, self->prop, buf.buf);
5603  }
5604  break;
5605  case PROP_BOOLEAN:
5606  case PROP_STRING:
5607  case PROP_ENUM:
5608  case PROP_POINTER:
5609  case PROP_COLLECTION:
5610  /* Should never happen. */
5612  break;
5613  }
5614 
5615  PyBuffer_Release(&buf);
5616  }
5617 
5618  Py_RETURN_NONE;
5619 }
5620 
5621 PyDoc_STRVAR(pyrna_prop_array_foreach_get_doc,
5622  ".. method:: foreach_get(seq)\n"
5623  "\n"
5624  " This is a function to give fast access to array data.\n");
5625 static PyObject *pyrna_prop_array_foreach_get(BPy_PropertyArrayRNA *self, PyObject *args)
5626 {
5628 
5629  return pyprop_array_foreach_getset(self, args, false);
5630 }
5631 
5632 PyDoc_STRVAR(pyrna_prop_array_foreach_set_doc,
5633  ".. method:: foreach_set(seq)\n"
5634  "\n"
5635  " This is a function to give fast access to array data.\n");
5636 static PyObject *pyrna_prop_array_foreach_set(BPy_PropertyArrayRNA *self, PyObject *args)
5637 {
5639 
5640  return pyprop_array_foreach_getset(self, args, true);
5641 }
5642 
5643 /* A bit of a kludge, make a list out of a collection or array,
5644  * then return the list's iter function, not especially fast, but convenient for now. */
5646 {
5647  /* Try get values from a collection. */
5648  PyObject *ret;
5649  PyObject *iter = NULL;
5650  int len;
5651 
5653 
5654  len = pyrna_prop_array_length(self);
5655  ret = pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len);
5656 
5657  /* we know this is a list so no need to PyIter_Check
5658  * otherwise it could be NULL (unlikely) if conversion failed */
5659  if (ret) {
5660  iter = PyObject_GetIter(ret);
5661  Py_DECREF(ret);
5662  }
5663 
5664  return iter;
5665 }
5666 
5668 
5669 #ifndef USE_PYRNA_ITER
5670 static PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self)
5671 {
5672  /* Try get values from a collection. */
5673  PyObject *ret;
5674  PyObject *iter = NULL;
5676 
5677  /* we know this is a list so no need to PyIter_Check
5678  * otherwise it could be NULL (unlikely) if conversion failed */
5679  if (ret) {
5680  iter = PyObject_GetIter(ret);
5681  Py_DECREF(ret);
5682  }
5683 
5684  return iter;
5685 }
5686 #endif /* # !USE_PYRNA_ITER */
5687 
5688 static struct PyMethodDef pyrna_struct_methods[] = {
5689 
5690  /* Only for PointerRNA's with ID'props. */
5691  {"keys", (PyCFunction)pyrna_struct_keys, METH_NOARGS, pyrna_struct_keys_doc},
5692  {"values", (PyCFunction)pyrna_struct_values, METH_NOARGS, pyrna_struct_values_doc},
5693  {"items", (PyCFunction)pyrna_struct_items, METH_NOARGS, pyrna_struct_items_doc},
5694 
5695  {"get", (PyCFunction)pyrna_struct_get, METH_VARARGS, pyrna_struct_get_doc},
5696  {"pop", (PyCFunction)pyrna_struct_pop, METH_VARARGS, pyrna_struct_pop_doc},
5697 
5698  {"as_pointer", (PyCFunction)pyrna_struct_as_pointer, METH_NOARGS, pyrna_struct_as_pointer_doc},
5699 
5700  /* bpy_rna_anim.c */
5701  {"keyframe_insert",
5702  (PyCFunction)pyrna_struct_keyframe_insert,
5703  METH_VARARGS | METH_KEYWORDS,
5705  {"keyframe_delete",
5706  (PyCFunction)pyrna_struct_keyframe_delete,
5707  METH_VARARGS | METH_KEYWORDS,
5709  {"driver_add",
5710  (PyCFunction)pyrna_struct_driver_add,
5711  METH_VARARGS,
5713  {"driver_remove",
5714  (PyCFunction)pyrna_struct_driver_remove,
5715  METH_VARARGS,
5717 
5718  {"is_property_set",
5719  (PyCFunction)pyrna_struct_is_property_set,
5720  METH_VARARGS | METH_KEYWORDS,
5721  pyrna_struct_is_property_set_doc},
5722  {"property_unset",
5723  (PyCFunction)pyrna_struct_property_unset,
5724  METH_VARARGS,
5725  pyrna_struct_property_unset_doc},
5726  {"is_property_hidden",
5727  (PyCFunction)pyrna_struct_is_property_hidden,
5728  METH_VARARGS,
5729  pyrna_struct_is_property_hidden_doc},
5730  {"is_property_readonly",
5731  (PyCFunction)pyrna_struct_is_property_readonly,
5732  METH_VARARGS,
5733  pyrna_struct_is_property_readonly_doc},
5734  {"is_property_overridable_library",
5736  METH_VARARGS,
5737  pyrna_struct_is_property_overridable_library_doc},
5738  {"property_overridable_library_set",
5740  METH_VARARGS,
5741  pyrna_struct_property_overridable_library_set_doc},
5742  {"path_resolve",
5743  (PyCFunction)pyrna_struct_path_resolve,
5744  METH_VARARGS,
5745  pyrna_struct_path_resolve_doc},
5746  {"path_from_id",
5747  (PyCFunction)pyrna_struct_path_from_id,
5748  METH_VARARGS,
5749  pyrna_struct_path_from_id_doc},
5750  {"type_recast",
5751  (PyCFunction)pyrna_struct_type_recast,
5752  METH_NOARGS,
5753  pyrna_struct_type_recast_doc},
5754  {"bl_rna_get_subclass_py",
5756  METH_VARARGS | METH_CLASS,
5757  pyrna_struct_bl_rna_get_subclass_py_doc},
5758  {"bl_rna_get_subclass",
5759  (PyCFunction)pyrna_struct_bl_rna_get_subclass,
5760  METH_VARARGS | METH_CLASS,
5761  pyrna_struct_bl_rna_get_subclass_doc},
5762  {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
5763 
5764 /* experimental */
5765 /* unused for now */
5766 #if 0
5767  {"callback_add", (PyCFunction)pyrna_callback_add, METH_VARARGS, NULL},
5768  {"callback_remove", (PyCFunction)pyrna_callback_remove, METH_VARARGS, NULL},
5769 
5770  {"callback_add", (PyCFunction)pyrna_callback_classmethod_add, METH_VARARGS | METH_CLASS, NULL},
5771  {"callback_remove",
5772  (PyCFunction)pyrna_callback_classmethod_remove,
5773  METH_VARARGS | METH_CLASS,
5774  NULL},
5775 #endif
5776  {NULL, NULL, 0, NULL},
5777 };
5778 
5779 static struct PyMethodDef pyrna_prop_methods[] = {
5780  {"path_from_id",
5781  (PyCFunction)pyrna_prop_path_from_id,
5782  METH_NOARGS,
5783  pyrna_prop_path_from_id_doc},
5784  {"as_bytes", (PyCFunction)pyrna_prop_as_bytes, METH_NOARGS, pyrna_prop_as_bytes_doc},
5785  {"update", (PyCFunction)pyrna_prop_update, METH_NOARGS, pyrna_prop_update_doc},
5786  {"__dir__", (PyCFunction)pyrna_prop_dir, METH_NOARGS, NULL},
5787  {NULL, NULL, 0, NULL},
5788 };
5789 
5790 static struct PyMethodDef pyrna_prop_array_methods[] = {
5791  {"foreach_get",
5792  (PyCFunction)pyrna_prop_array_foreach_get,
5793  METH_VARARGS,
5794  pyrna_prop_array_foreach_get_doc},
5795  {"foreach_set",
5796  (PyCFunction)pyrna_prop_array_foreach_set,
5797  METH_VARARGS,
5798  pyrna_prop_array_foreach_set_doc},
5799 
5800  {NULL, NULL, 0, NULL},
5801 };
5802 
5803 static struct PyMethodDef pyrna_prop_collection_methods[] = {
5804  {"foreach_get",
5805  (PyCFunction)pyrna_prop_collection_foreach_get,
5806  METH_VARARGS,
5807  pyrna_prop_collection_foreach_get_doc},
5808  {"foreach_set",
5809  (PyCFunction)pyrna_prop_collection_foreach_set,
5810  METH_VARARGS,
5811  pyrna_prop_collection_foreach_set_doc},
5812 
5813  {"keys", (PyCFunction)pyrna_prop_collection_keys, METH_NOARGS, pyrna_prop_collection_keys_doc},
5814  {"items",
5815  (PyCFunction)pyrna_prop_collection_items,
5816  METH_NOARGS,
5817  pyrna_prop_collection_items_doc},
5818  {"values",
5819  (PyCFunction)pyrna_prop_collection_values,
5820  METH_NOARGS,
5821  pyrna_prop_collection_values_doc},
5822 
5823  {"get", (PyCFunction)pyrna_prop_collection_get, METH_VARARGS, pyrna_prop_collection_get_doc},
5824  {"find", (PyCFunction)pyrna_prop_collection_find, METH_O, pyrna_prop_collection_find_doc},
5825  {NULL, NULL, 0, NULL},
5826 };
5827 
5828 static struct PyMethodDef pyrna_prop_collection_idprop_methods[] = {
5829  {"add", (PyCFunction)pyrna_prop_collection_idprop_add, METH_NOARGS, NULL},
5830  {"remove", (PyCFunction)pyrna_prop_collection_idprop_remove, METH_O, NULL},
5831  {"clear", (PyCFunction)pyrna_prop_collection_idprop_clear, METH_NOARGS, NULL},
5832  {"move", (PyCFunction)pyrna_prop_collection_idprop_move, METH_VARARGS, NULL},
5833  {NULL, NULL, 0, NULL},
5834 };
5835 
5836 /* only needed for subtyping, so a new class gets a valid BPy_StructRNA
5837  * todo - also accept useful args */
5838 static PyObject *pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *UNUSED(kwds))
5839 {
5840  if (PyTuple_GET_SIZE(args) == 1) {
5841  BPy_StructRNA *base = (BPy_StructRNA *)PyTuple_GET_ITEM(args, 0);
5842  if (Py_TYPE(base) == type) {
5843  Py_INCREF(base);
5844  return (PyObject *)base;
5845  }
5846  if (PyType_IsSubtype(Py_TYPE(base), &pyrna_struct_Type)) {
5847  /* this almost never runs, only when using user defined subclasses of built-in object.
5848  * this isn't common since it's NOT related to registerable subclasses. eg:
5849  *
5850  * >>> class MyObSubclass(bpy.types.Object):
5851  * ... def test_func(self):
5852  * ... print(100)
5853  * ...
5854  * >>> myob = MyObSubclass(bpy.context.object)
5855  * >>> myob.test_func()
5856  * 100
5857  *
5858  * Keep this since it could be useful.
5859  */
5860  BPy_StructRNA *ret;
5861  if ((ret = (BPy_StructRNA *)type->tp_alloc(type, 0))) {
5862  ret->ptr = base->ptr;
5863 #ifdef USE_PYRNA_STRUCT_REFERENCE
5864  /* #PyType_GenericAlloc will have set tracking.
5865  * We only want tracking when `StructRNA.reference` has been set. */
5866  PyObject_GC_UnTrack(ret);
5867 #endif
5868  }
5869  /* Pass on exception & NULL if tp_alloc fails. */
5870  return (PyObject *)ret;
5871  }
5872 
5873  /* Error, invalid type given. */
5874  PyErr_Format(PyExc_TypeError,
5875  "bpy_struct.__new__(type): type '%.200s' is not a subtype of bpy_struct",
5876  type->tp_name);
5877  return NULL;
5878  }
5879 
5880  PyErr_Format(PyExc_TypeError, "bpy_struct.__new__(type): expected a single argument");
5881  return NULL;
5882 }
5883 
5884 /* only needed for subtyping, so a new class gets a valid BPy_StructRNA
5885  * todo - also accept useful args */
5886 static PyObject *pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *UNUSED(kwds))
5887 {
5888  BPy_PropertyRNA *base;
5889 
5890  if (!PyArg_ParseTuple(args, "O!:bpy_prop.__new__", &pyrna_prop_Type, &base)) {
5891  return NULL;
5892  }
5893 
5894  if (type == Py_TYPE(base)) {
5895  return Py_INCREF_RET((PyObject *)base);
5896  }
5897  if (PyType_IsSubtype(type, &pyrna_prop_Type)) {
5898  BPy_PropertyRNA *ret = (BPy_PropertyRNA *)type->tp_alloc(type, 0);
5899  ret->ptr = base->ptr;
5900  ret->prop = base->prop;
5901  return (PyObject *)ret;
5902  }
5903 
5904  PyErr_Format(PyExc_TypeError,
5905  "bpy_prop.__new__(type): type '%.200s' is not a subtype of bpy_prop",
5906  type->tp_name);
5907  return NULL;
5908 }
5909 
5910 static PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
5911 {
5912  PyObject *ret;
5913  const int type = RNA_property_type(prop);
5914  const int flag = RNA_property_flag(prop);
5915  const int flag_parameter = RNA_parameter_flag(prop);
5916 
5917  if (RNA_property_array_check(prop)) {
5918  int a, len;
5919 
5920  if (flag & PROP_DYNAMIC) {
5921  ParameterDynAlloc *data_alloc = data;
5922  len = data_alloc->array_tot;
5923  data = data_alloc->array;
5924  }
5925  else {
5927  }
5928 
5929  /* Resolve the array from a new pytype. */
5930 
5931  /* TODO(Kazanbas): make multi-dimensional sequences here. */
5932 
5933  switch (type) {
5934  case PROP_BOOLEAN:
5935  ret = PyTuple_New(len);
5936  for (a = 0; a < len; a++) {
5937  PyTuple_SET_ITEM(ret, a, PyBool_FromLong(((bool *)data)[a]));
5938  }
5939  break;
5940  case PROP_INT:
5941  ret = PyTuple_New(len);
5942  for (a = 0; a < len; a++) {
5943  PyTuple_SET_ITEM(ret, a, PyLong_FromLong(((int *)data)[a]));
5944  }
5945  break;
5946  case PROP_FLOAT:
5947  switch (RNA_property_subtype(prop)) {
5948 #ifdef USE_MATHUTILS
5951  break;
5952  case PROP_MATRIX:
5953  if (len == 16) {
5954  ret = Matrix_CreatePyObject(data, 4, 4, NULL);
5955  break;
5956  }
5957  else if (len == 9) {
5958  ret = Matrix_CreatePyObject(data, 3, 3, NULL);
5959  break;
5960  }
5962 #endif
5963  default:
5964  ret = PyTuple_New(len);
5965  for (a = 0; a < len; a++) {
5966  PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble(((float *)data)[a]));
5967  }
5968  break;
5969  }
5970  break;
5971  default:
5972  PyErr_Format(
5973  PyExc_TypeError, "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)", type);
5974  ret = NULL;
5975  break;
5976  }
5977  }
5978  else {
5979  /* See if we can coerce into a python type - PropertyType. */
5980  switch (type) {
5981  case PROP_BOOLEAN:
5982  ret = PyBool_FromLong(*(bool *)data);
5983  break;
5984  case PROP_INT:
5985  ret = PyLong_FromLong(*(int *)data);
5986  break;
5987  case PROP_FLOAT:
5988  ret = PyFloat_FromDouble(*(float *)data);
5989  break;
5990  case PROP_STRING: {
5991  const char *data_ch;
5992  PyObject *value_coerce = NULL;
5993  const int subtype = RNA_property_subtype(prop);
5994 
5995  if (flag & PROP_THICK_WRAP) {
5996  data_ch = (char *)data;
5997  }
5998  else {
5999  data_ch = *(char **)data;
6000  }
6001 
6002 #ifdef USE_STRING_COERCE
6003  if (subtype == PROP_BYTESTRING) {
6004  ret = PyBytes_FromString(data_ch);
6005  }
6006  else if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
6007  ret = PyC_UnicodeFromByte(data_ch);
6008  }
6009  else {
6010  ret = PyUnicode_FromString(data_ch);
6011  }
6012 #else
6013  if (subtype == PROP_BYTESTRING) {
6014  ret = PyBytes_FromString(buf);
6015  }
6016  else {
6017  ret = PyUnicode_FromString(data_ch);
6018  }
6019 #endif
6020 
6021 #ifdef USE_STRING_COERCE
6022  Py_XDECREF(value_coerce);
6023 #endif
6024 
6025  break;
6026  }
6027  case PROP_ENUM: {
6028  ret = pyrna_enum_to_py(ptr, prop, *(int *)data);
6029  break;
6030  }
6031  case PROP_POINTER: {
6032  PointerRNA newptr;
6033  StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
6034 
6035  if (flag_parameter & PARM_RNAPTR) {
6036  /* In this case we get the full ptr. */
6037  newptr = *(PointerRNA *)data;
6038  }
6039  else {
6040  if (RNA_struct_is_ID(ptype)) {
6041  RNA_id_pointer_create(*(void **)data, &newptr);
6042  }
6043  else {
6044  /* note: this is taken from the function's ID pointer
6045  * and will break if a function returns a pointer from
6046  * another ID block, watch this! - it should at least be
6047  * easy to debug since they are all ID's */
6048  RNA_pointer_create(ptr->owner_id, ptype, *(void **)data, &newptr);
6049  }
6050  }
6051 
6052  if (newptr.data) {
6053  ret = pyrna_struct_CreatePyObject(&newptr);
6054  }
6055  else {
6056  ret = Py_None;
6057  Py_INCREF(ret);
6058  }
6059  break;
6060  }
6061  case PROP_COLLECTION: {
6063  CollectionPointerLink *link;
6064 
6065  ret = PyList_New(0);
6066 
6067  for (link = lb->first; link; link = link->next) {
6068  PyList_APPEND(ret, pyrna_struct_CreatePyObject(&link->ptr));
6069  }
6070 
6071  break;
6072  }
6073  default:
6074  PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_param_to_py)", type);
6075  ret = NULL;
6076  break;
6077  }
6078  }
6079 
6080  return ret;
6081 }
6082 
6088 static PyObject *small_dict_get_item_string(PyObject *dict, const char *key_lookup)
6089 {
6090  PyObject *key = NULL;
6091  Py_ssize_t pos = 0;
6092  PyObject *value = NULL;
6093 
6094  while (PyDict_Next(dict, &pos, &key, &value)) {
6095  if (PyUnicode_Check(key)) {
6096  if (STREQ(key_lookup, PyUnicode_AsUTF8(key))) {
6097  return value;
6098  }
6099  }
6100  }
6101 
6102  return NULL;
6103 }
6104 
6105 static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject *kw)
6106 {
6107  /* Note, both BPy_StructRNA and BPy_PropertyRNA can be used here. */
6108  PointerRNA *self_ptr = &self->ptr;
6109  FunctionRNA *self_func = self->func;
6110 
6111  PointerRNA funcptr;
6112  ParameterList parms;
6113  ParameterIterator iter;
6114  PropertyRNA *parm;
6115  PyObject *ret, *item;
6116  int i, pyargs_len, pykw_len, parms_len, ret_len, flag_parameter, err = 0, kw_tot = 0;
6117  bool kw_arg;
6118 
6119  PropertyRNA *pret_single = NULL;
6120  void *retdata_single = NULL;
6121 
6122  /* enable this so all strings are copied and freed after calling.
6123  * this exposes bugs where the pointer to the string is held and re-used */
6124  /* #define DEBUG_STRING_FREE */
6125 
6126 #ifdef DEBUG_STRING_FREE
6127  PyObject *string_free_ls = PyList_New(0);
6128 #endif
6129 
6130  /* Should never happen, but it does in rare cases. */
6131  BLI_assert(self_ptr != NULL);
6132 
6133  if (self_ptr == NULL) {
6134  PyErr_SetString(PyExc_RuntimeError,
6135  "RNA functions internal RNA pointer is NULL, this is a bug. aborting");
6136  return NULL;
6137  }
6138 
6139  if (self_func == NULL) {
6140  PyErr_Format(
6141  PyExc_RuntimeError,
6142  "%.200s.<unknown>(): RNA function internal function is NULL, this is a bug. aborting",
6143  RNA_struct_identifier(self_ptr->type));
6144  return NULL;
6145  }
6146 
6147  /* For testing. */
6148 #if 0
6149  {
6150  const char *fn;
6151  int lineno;
6152  PyC_FileAndNum(&fn, &lineno);
6153  printf("pyrna_func_call > %.200s.%.200s : %.200s:%d\n",
6154  RNA_struct_identifier(self_ptr->type),
6155  RNA_function_identifier(self_func),
6156  fn,
6157  lineno);
6158  }
6159 #endif
6160 
6161  /* include the ID pointer for pyrna_param_to_py() so we can include the
6162  * ID pointer on return values, this only works when returned values have
6163  * the same ID as the functions. */
6164  RNA_pointer_create(self_ptr->owner_id, &RNA_Function, self_func, &funcptr);
6165 
6166  pyargs_len = PyTuple_GET_SIZE(args);
6167  pykw_len = kw ? PyDict_Size(kw) : 0;
6168 
6169  RNA_parameter_list_create(&parms, self_ptr, self_func);
6170  RNA_parameter_list_begin(&parms, &iter);
6171  parms_len = RNA_parameter_list_arg_count(&parms);
6172  ret_len = 0;
6173 
6174  if (pyargs_len + pykw_len > parms_len) {
6175  RNA_parameter_list_end(&iter);
6176  PyErr_Format(PyExc_TypeError,
6177  "%.200s.%.200s(): takes at most %d arguments, got %d",
6178  RNA_struct_identifier(self_ptr->type),
6179  RNA_function_identifier(self_func),
6180  parms_len,
6181  pyargs_len + pykw_len);
6182  err = -1;
6183  }
6184 
6185  /* Parse function parameters. */
6186  for (i = 0; iter.valid && err == 0; RNA_parameter_list_next(&iter)) {
6187  parm = iter.parm;
6188  flag_parameter = RNA_parameter_flag(parm);
6189 
6190  /* Only useful for single argument returns, we'll need another list loop for multiple. */
6191  if (flag_parameter & PARM_OUTPUT) {
6192  ret_len++;
6193  if (pret_single == NULL) {
6194  pret_single = parm;
6195  retdata_single = iter.data;
6196  }
6197 
6198  continue;
6199  }
6200 
6201  item = NULL;
6202 
6203  if (i < pyargs_len) {
6204  /* New in 2.8x, optional arguments must be keywords. */
6205  if (UNLIKELY((flag_parameter & PARM_REQUIRED) == 0)) {
6206  PyErr_Format(PyExc_TypeError,
6207  "%.200s.%.200s(): required parameter \"%.200s\" to be a keyword argument!",
6208  RNA_struct_identifier(self_ptr->type),
6209  RNA_function_identifier(self_func),
6210  RNA_property_identifier(parm));
6211  err = -1;
6212  break;
6213  }
6214 
6215  item = PyTuple_GET_ITEM(args, i);
6216  kw_arg = false;
6217  }
6218  else if (kw != NULL) {
6219 #if 0
6220  item = PyDict_GetItemString(kw, RNA_property_identifier(parm)); /* Borrow reference. */
6221 #else
6222  item = small_dict_get_item_string(kw, RNA_property_identifier(parm)); /* Borrow reference. */
6223 #endif
6224  if (item) {
6225  kw_tot++; /* Make sure invalid keywords are not given. */
6226  }
6227 
6228  kw_arg = true;
6229  }
6230 
6231  i++; /* Current argument. */
6232 
6233  if (item == NULL) {
6234  if (flag_parameter & PARM_REQUIRED) {
6235  PyErr_Format(PyExc_TypeError,
6236  "%.200s.%.200s(): required parameter \"%.200s\" not specified",
6237  RNA_struct_identifier(self_ptr->type),
6238  RNA_function_identifier(self_func),
6239  RNA_property_identifier(parm));
6240  err = -1;
6241  break;
6242  }
6243  /* PyDict_GetItemString wont raise an error. */
6244  continue;
6245  }
6246 
6247 #ifdef DEBUG_STRING_FREE
6248  if (item) {
6249  if (PyUnicode_Check(item)) {
6250  PyList_APPEND(string_free_ls, PyUnicode_FromString(PyUnicode_AsUTF8(item)));
6251  }
6252  }
6253 #endif
6254  err = pyrna_py_to_prop(&funcptr, parm, iter.data, item, "");
6255 
6256  if (err != 0) {
6257  /* the error generated isn't that useful, so generate it again with a useful prefix
6258  * could also write a function to prepend to error messages */
6259  char error_prefix[512];
6260  PyErr_Clear(); /* Re-raise. */
6261 
6262  if (kw_arg == true) {
6263  BLI_snprintf(error_prefix,
6264  sizeof(error_prefix),
6265  "%.200s.%.200s(): error with keyword argument \"%.200s\" - ",
6266  RNA_struct_identifier(self_ptr->type),
6267  RNA_function_identifier(self_func),
6268  RNA_property_identifier(parm));
6269  }
6270  else {
6271  BLI_snprintf(error_prefix,
6272  sizeof(error_prefix),
6273  "%.200s.%.200s(): error with argument %d, \"%.200s\" - ",
6274  RNA_struct_identifier(self_ptr->type),
6275  RNA_function_identifier(self_func),
6276  i,
6277  RNA_property_identifier(parm));
6278  }
6279 
6280  pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix);
6281 
6282  break;
6283  }
6284  }
6285 
6286  RNA_parameter_list_end(&iter);
6287 
6288  /* Check if we gave args that don't exist in the function
6289  * Printing the error is slow, but it should only happen when developing.
6290  * The "if" below is quick check to make sure less keyword args were passed than we gave.
6291  * (Don't overwrite the error if we have one,
6292  * otherwise can skip important messages and confuse with args).
6293  */
6294  if (err == 0 && kw && (pykw_len > kw_tot)) {
6295  PyObject *key, *value;
6296  Py_ssize_t pos = 0;
6297 
6298  DynStr *bad_args = BLI_dynstr_new();
6299  DynStr *good_args = BLI_dynstr_new();
6300 
6301  const char *arg_name, *bad_args_str, *good_args_str;
6302  bool found = false, first = true;
6303 
6304  while (PyDict_Next(kw, &pos, &key, &value)) {
6305 
6306  arg_name = PyUnicode_AsUTF8(key);
6307  found = false;
6308 
6309  if (arg_name == NULL) { /* Unlikely the argname is not a string, but ignore if it is. */
6310  PyErr_Clear();
6311  }
6312  else {
6313  /* Search for arg_name. */
6314  RNA_parameter_list_begin(&parms, &iter);
6315  for (; iter.valid; RNA_parameter_list_next(&iter)) {
6316  parm = iter.parm;
6317  if (STREQ(arg_name, RNA_property_identifier(parm))) {
6318  found = true;
6319  break;
6320  }
6321  }
6322 
6323  RNA_parameter_list_end(&iter);
6324 
6325  if (found == false) {
6326  BLI_dynstr_appendf(bad_args, first ? "%s" : ", %s", arg_name);
6327  first = false;
6328  }
6329  }
6330  }
6331 
6332  /* List good args. */
6333  first = true;
6334 
6335  RNA_parameter_list_begin(&parms, &iter);
6336  for (; iter.valid; RNA_parameter_list_next(&iter)) {
6337  parm = iter.parm;
6338  if (RNA_parameter_flag(parm) & PARM_OUTPUT) {
6339  continue;
6340  }
6341 
6342  BLI_dynstr_appendf(good_args, first ? "%s" : ", %s", RNA_property_identifier(parm));
6343  first = false;
6344  }
6345  RNA_parameter_list_end(&iter);
6346 
6347  bad_args_str = BLI_dynstr_get_cstring(bad_args);
6348  good_args_str = BLI_dynstr_get_cstring(good_args);
6349 
6350  PyErr_Format(
6351  PyExc_TypeError,
6352  "%.200s.%.200s(): was called with invalid keyword argument(s) (%s), expected (%s)",
6353  RNA_struct_identifier(self_ptr->type),
6354  RNA_function_identifier(self_func),
6355  bad_args_str,
6356  good_args_str);
6357 
6358  BLI_dynstr_free(bad_args);
6359  BLI_dynstr_free(good_args);
6360  MEM_freeN((void *)bad_args_str);
6361  MEM_freeN((void *)good_args_str);
6362 
6363  err = -1;
6364  }
6365 
6366  ret = NULL;
6367  if (err == 0) {
6368  /* Call function. */
6369  ReportList reports;
6370  bContext *C = BPY_context_get();
6371 
6372  BKE_reports_init(&reports, RPT_STORE);
6373  RNA_function_call(C, &reports, self_ptr, self_func, &parms);
6374 
6375  err = (BPy_reports_to_error(&reports, PyExc_RuntimeError, true));
6376 
6377  /* Return value. */
6378  if (err != -1) {
6379  if (ret_len > 0) {
6380  if (ret_len > 1) {
6381  ret = PyTuple_New(ret_len);
6382  i = 0; /* Arg index. */
6383 
6384  RNA_parameter_list_begin(&parms, &iter);
6385 
6386  for (; iter.valid; RNA_parameter_list_next(&iter)) {
6387  parm = iter.parm;
6388 
6389  if (RNA_parameter_flag(parm) & PARM_OUTPUT) {
6390  PyTuple_SET_ITEM(ret, i++, pyrna_param_to_py(&funcptr, parm, iter.data));
6391  }
6392  }
6393 
6394  RNA_parameter_list_end(&iter);
6395  }
6396  else {
6397  ret = pyrna_param_to_py(&funcptr, pret_single, retdata_single);
6398  }
6399 
6400  /* Possible there is an error in conversion. */
6401  if (ret == NULL) {
6402  err = -1;
6403  }
6404  }
6405  }
6406  }
6407 
6408 #ifdef DEBUG_STRING_FREE
6409 # if 0
6410  if (PyList_GET_SIZE(string_free_ls)) {
6411  printf("%.200s.%.200s(): has %d strings\n",
6412  RNA_struct_identifier(self_ptr->type),
6413  RNA_function_identifier(self_func),
6414  (int)PyList_GET_SIZE(string_free_ls));
6415  }
6416 # endif
6417  Py_DECREF(string_free_ls);
6418 # undef DEBUG_STRING_FREE
6419 #endif
6420 
6421  /* Cleanup. */
6422  RNA_parameter_list_end(&iter);
6423  RNA_parameter_list_free(&parms);
6424 
6425  if (ret) {
6426  return ret;
6427  }
6428 
6429  if (err == -1) {
6430  return NULL;
6431  }
6432 
6433  Py_RETURN_NONE;
6434 }
6435 
6436 static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *UNUSED(closure))
6437 {
6438  PyObject *ret;
6439  char *args;
6440 
6441  args = RNA_function_as_string_keywords(NULL, self->func, true, true, INT_MAX);
6442 
6443  ret = PyUnicode_FromFormat("%.200s.%.200s(%.200s)\n%s",
6444  RNA_struct_identifier(self->ptr.type),
6446  args,
6448 
6449  MEM_freeN(args);
6450 
6451  return ret;
6452 }
6453 
6454 /* Subclasses of pyrna_struct_Type which support idprop definitions use this as a metaclass. */
6455 /* note: tp_base member is set to &PyType_Type on init */
6457  PyVarObject_HEAD_INIT(NULL, 0) "bpy_struct_meta_idprop", /* tp_name */
6458 
6459  /* NOTE! would be PyTypeObject, but subtypes of Type must be PyHeapTypeObject's */
6460  sizeof(PyHeapTypeObject), /* tp_basicsize */
6461 
6462  0, /* tp_itemsize */
6463  /* methods */
6464  NULL, /* tp_dealloc */
6465  0, /* tp_vectorcall_offset */
6466  NULL, /* getattrfunc tp_getattr; */
6467  NULL, /* setattrfunc tp_setattr; */
6468  NULL,
6469  /* tp_compare */ /* deprecated in Python 3.0! */
6470  NULL, /* tp_repr */
6471 
6472  /* Method suites for standard classes */
6473  NULL, /* PyNumberMethods *tp_as_number; */
6474  NULL, /* PySequenceMethods *tp_as_sequence; */
6475  NULL, /* PyMappingMethods *tp_as_mapping; */
6476 
6477  /* More standard operations (here for binary compatibility) */
6478  NULL, /* hashfunc tp_hash; */
6479  NULL, /* ternaryfunc tp_call; */
6480  NULL, /* reprfunc tp_str; */
6481  NULL /*(getattrofunc) pyrna_struct_meta_idprop_getattro*/, /* getattrofunc tp_getattro; */
6482  (setattrofunc)pyrna_struct_meta_idprop_setattro, /* setattrofunc tp_setattro; */
6483 
6484  /* Functions to access object as input/output buffer */
6485  NULL, /* PyBufferProcs *tp_as_buffer; */
6486 
6487  /*** Flags to define presence of optional/expanded features ***/
6488  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
6489 
6490  NULL, /* char *tp_doc; Documentation string */
6491  /*** Assigned meaning in release 2.0 ***/
6492  /* call function for all accessible objects */
6493  NULL, /* traverseproc tp_traverse; */
6494 
6495  /* delete references to contained objects */
6496  NULL, /* inquiry tp_clear; */
6497 
6498  /*** Assigned meaning in release 2.1 ***/
6499  /*** rich comparisons ***/
6500  NULL, /* richcmpfunc tp_richcompare; */
6501 
6502  /*** weak reference enabler ***/
6503  0, /* long tp_weaklistoffset; */
6504 
6505  /*** Added in release 2.2 ***/
6506  /* Iterators */
6507  NULL, /* getiterfunc tp_iter; */
6508  NULL, /* iternextfunc tp_iternext; */
6509 
6510  /*** Attribute descriptor and subclassing stuff ***/
6511  NULL, /* struct PyMethodDef *tp_methods; */
6512  NULL, /* struct PyMemberDef *tp_members; */
6513  NULL, /* struct PyGetSetDef *tp_getset; */
6514 #if defined(_MSC_VER)
6515  NULL, /* defer assignment */
6516 #else
6517  &PyType_Type, /* struct _typeobject *tp_base; */
6518 #endif
6519  NULL, /* PyObject *tp_dict; */
6520  NULL, /* descrgetfunc tp_descr_get; */
6521  NULL, /* descrsetfunc tp_descr_set; */
6522  0, /* long tp_dictoffset; */
6523  NULL, /* initproc tp_init; */
6524  NULL, /* allocfunc tp_alloc; */
6525  NULL, /* newfunc tp_new; */
6526  /* Low-level free-memory routine */
6527  NULL, /* freefunc tp_free; */
6528  /* For PyObject_IS_GC */
6529  NULL, /* inquiry tp_is_gc; */
6530  NULL, /* PyObject *tp_bases; */
6531  /* method resolution order */
6532  NULL, /* PyObject *tp_mro; */
6533  NULL, /* PyObject *tp_cache; */
6534  NULL, /* PyObject *tp_subclasses; */
6535  NULL, /* PyObject *tp_weaklist; */
6536  NULL,
6537 };
6538 
6539 /*-----------------------BPy_StructRNA method def------------------------------*/
6540 PyTypeObject pyrna_struct_Type = {
6541  PyVarObject_HEAD_INIT(NULL, 0) "bpy_struct", /* tp_name */
6542  sizeof(BPy_StructRNA), /* tp_basicsize */
6543  0, /* tp_itemsize */
6544  /* methods */
6545  (destructor)pyrna_struct_dealloc, /* tp_dealloc */
6546  0, /* tp_vectorcall_offset */
6547  NULL, /* getattrfunc tp_getattr; */
6548  NULL, /* setattrfunc tp_setattr; */
6549  NULL,
6550  /* tp_compare */ /* DEPRECATED in Python 3.0! */
6551  (reprfunc)pyrna_struct_repr, /* tp_repr */
6552 
6553  /* Method suites for standard classes */
6554 
6555  NULL, /* PyNumberMethods *tp_as_number; */
6556  &pyrna_struct_as_sequence, /* PySequenceMethods *tp_as_sequence; */
6557  &pyrna_struct_as_mapping, /* PyMappingMethods *tp_as_mapping; */
6558 
6559  /* More standard operations (here for binary compatibility) */
6560 
6561  (hashfunc)pyrna_struct_hash, /* hashfunc tp_hash; */
6562  NULL, /* ternaryfunc tp_call; */
6563  (reprfunc)pyrna_struct_str, /* reprfunc tp_str; */
6564  (getattrofunc)pyrna_struct_getattro, /* getattrofunc tp_getattro; */
6565  (setattrofunc)pyrna_struct_setattro, /* setattrofunc tp_setattro; */
6566 
6567  /* Functions to access object as input/output buffer */
6568  NULL, /* PyBufferProcs *tp_as_buffer; */
6569 
6570  /*** Flags to define presence of optional/expanded features ***/
6571  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
6572 #ifdef USE_PYRNA_STRUCT_REFERENCE
6573  | Py_TPFLAGS_HAVE_GC
6574 #endif
6575  , /* long tp_flags; */
6576 
6577  NULL, /* char *tp_doc; Documentation string */
6578 /*** Assigned meaning in release 2.0 ***/
6579 /* call function for all accessible objects */
6580 #ifdef USE_PYRNA_STRUCT_REFERENCE
6581  (traverseproc)pyrna_struct_traverse, /* traverseproc tp_traverse; */
6582 
6583  /* delete references to contained objects */
6584  (inquiry)pyrna_struct_clear, /* inquiry tp_clear; */
6585 #else
6586  NULL, /* traverseproc tp_traverse; */
6587 
6588  /* delete references to contained objects */
6589  NULL, /* inquiry tp_clear; */
6590 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
6591 
6592  /*** Assigned meaning in release 2.1 ***/
6593  /*** rich comparisons ***/
6594  (richcmpfunc)pyrna_struct_richcmp, /* richcmpfunc tp_richcompare; */
6595 
6596 /*** weak reference enabler ***/
6597 #ifdef USE_WEAKREFS
6598  offsetof(BPy_StructRNA, in_weakreflist), /* long tp_weaklistoffset; */
6599 #else
6600  0,
6601 #endif
6602  /*** Added in release 2.2 ***/
6603  /* Iterators */
6604  NULL, /* getiterfunc tp_iter; */
6605  NULL, /* iternextfunc tp_iternext; */
6606 
6607  /*** Attribute descriptor and subclassing stuff ***/
6608  pyrna_struct_methods, /* struct PyMethodDef *tp_methods; */
6609  NULL, /* struct PyMemberDef *tp_members; */
6610  pyrna_struct_getseters, /* struct PyGetSetDef *tp_getset; */
6611  NULL, /* struct _typeobject *tp_base; */
6612  NULL, /* PyObject *tp_dict; */
6613  NULL, /* descrgetfunc tp_descr_get; */
6614  NULL, /* descrsetfunc tp_descr_set; */
6615  0, /* long tp_dictoffset; */
6616  NULL, /* initproc tp_init; */
6617  NULL, /* allocfunc tp_alloc; */
6618  pyrna_struct_new, /* newfunc tp_new; */
6619  /* Low-level free-memory routine */
6620  NULL, /* freefunc tp_free; */
6621  /* For PyObject_IS_GC */
6622  NULL, /* inquiry tp_is_gc; */
6623  NULL, /* PyObject *tp_bases; */
6624  /* method resolution order */
6625  NULL, /* PyObject *tp_mro; */
6626  NULL, /* PyObject *tp_cache; */
6627  NULL, /* PyObject *tp_subclasses; */
6628  NULL, /* PyObject *tp_weaklist; */
6629  NULL,
6630 };
6631 
6632 /*-----------------------BPy_PropertyRNA method def------------------------------*/
6633 PyTypeObject pyrna_prop_Type = {
6634  PyVarObject_HEAD_INIT(NULL, 0) "bpy_prop", /* tp_name */
6635  sizeof(BPy_PropertyRNA), /* tp_basicsize */
6636  0, /* tp_itemsize */
6637  /* methods */
6638  (destructor)pyrna_prop_dealloc, /* tp_dealloc */
6639  0, /* tp_vectorcall_offset */
6640  NULL, /* getattrfunc tp_getattr; */
6641  NULL, /* setattrfunc tp_setattr; */
6642  NULL,
6643  /* tp_compare */ /* DEPRECATED in Python 3.0! */
6644  (reprfunc)pyrna_prop_repr, /* tp_repr */
6645 
6646  /* Method suites for standard classes */
6647 
6648  NULL, /* PyNumberMethods *tp_as_number; */
6649  NULL, /* PySequenceMethods *tp_as_sequence; */
6650  NULL, /* PyMappingMethods *tp_as_mapping; */
6651 
6652  /* More standard operations (here for binary compatibility) */
6653 
6654  (hashfunc)pyrna_prop_hash, /* hashfunc tp_hash; */
6655  NULL, /* ternaryfunc tp_call; */
6656  (reprfunc)pyrna_prop_str, /* reprfunc tp_str; */
6657 
6658  /* will only use these if this is a subtype of a py class */
6659  NULL, /* getattrofunc tp_getattro; */
6660  NULL, /* setattrofunc tp_setattro; */
6661 
6662  /* Functions to access object as input/output buffer */
6663  NULL, /* PyBufferProcs *tp_as_buffer; */
6664 
6665  /*** Flags to define presence of optional/expanded features ***/
6666  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
6667 
6668  NULL, /* char *tp_doc; Documentation string */
6669  /*** Assigned meaning in release 2.0 ***/
6670  /* call function for all accessible objects */
6671  NULL, /* traverseproc tp_traverse; */
6672 
6673  /* delete references to contained objects */
6674  NULL, /* inquiry tp_clear; */
6675 
6676  /*** Assigned meaning in release 2.1 ***/
6677  /*** rich comparisons ***/
6678  (richcmpfunc)pyrna_prop_richcmp, /* richcmpfunc tp_richcompare; */
6679 
6680 /*** weak reference enabler ***/
6681 #ifdef USE_WEAKREFS
6682  offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
6683 #else
6684  0,
6685 #endif
6686 
6687  /*** Added in release 2.2 ***/
6688  /* Iterators */
6689  NULL, /* getiterfunc tp_iter; */
6690  NULL, /* iternextfunc tp_iternext; */
6691 
6692  /*** Attribute descriptor and subclassing stuff ***/
6693  pyrna_prop_methods, /* struct PyMethodDef *tp_methods; */
6694  NULL, /* struct PyMemberDef *tp_members; */
6695  pyrna_prop_getseters, /* struct PyGetSetDef *tp_getset; */
6696  NULL, /* struct _typeobject *tp_base; */
6697  NULL, /* PyObject *tp_dict; */
6698  NULL, /* descrgetfunc tp_descr_get; */
6699  NULL, /* descrsetfunc tp_descr_set; */
6700  0, /* long tp_dictoffset; */
6701  NULL, /* initproc tp_init; */
6702  NULL, /* allocfunc tp_alloc; */
6703  pyrna_prop_new, /* newfunc tp_new; */
6704  /* Low-level free-memory routine */
6705  NULL, /* freefunc tp_free; */
6706  /* For PyObject_IS_GC */
6707  NULL, /* inquiry tp_is_gc; */
6708  NULL, /* PyObject *tp_bases; */
6709  /* method resolution order */
6710  NULL, /* PyObject *tp_mro; */
6711  NULL, /* PyObject *tp_cache; */
6712  NULL, /* PyObject *tp_subclasses; */
6713  NULL, /* PyObject *tp_weaklist; */
6714  NULL,
6715 };
6716 
6717 PyTypeObject pyrna_prop_array_Type = {
6718  PyVarObject_HEAD_INIT(NULL, 0) "bpy_prop_array", /* tp_name */
6719  sizeof(BPy_PropertyArrayRNA), /* tp_basicsize */
6720  0, /* tp_itemsize */
6721  /* methods */
6722  (destructor)pyrna_prop_array_dealloc, /* tp_dealloc */
6723  0, /* tp_vectorcall_offset */
6724  NULL, /* getattrfunc tp_getattr; */
6725  NULL, /* setattrfunc tp_setattr; */
6726  NULL,
6727  /* tp_compare */ /* DEPRECATED in Python 3.0! */
6728  (reprfunc)pyrna_prop_array_repr, /* tp_repr */
6729 
6730  /* Method suites for standard classes */
6731 
6732  &pyrna_prop_array_as_number, /* PyNumberMethods *tp_as_number; */
6733  &pyrna_prop_array_as_sequence, /* PySequenceMethods *tp_as_sequence; */
6734  &pyrna_prop_array_as_mapping, /* PyMappingMethods *tp_as_mapping; */
6735 
6736  /* More standard operations (here for binary compatibility) */
6737 
6738  NULL, /* hashfunc tp_hash; */
6739  NULL, /* ternaryfunc tp_call; */
6740  NULL, /* reprfunc tp_str; */
6741 
6742  /* will only use these if this is a subtype of a py class */
6743  (getattrofunc)pyrna_prop_array_getattro, /* getattrofunc tp_getattro; */
6744  NULL, /* setattrofunc tp_setattro; */
6745 
6746  /* Functions to access object as input/output buffer */
6747  NULL, /* PyBufferProcs *tp_as_buffer; */
6748 
6749  /*** Flags to define presence of optional/expanded features ***/
6750  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
6751 
6752  NULL, /* char *tp_doc; Documentation string */
6753  /*** Assigned meaning in release 2.0 ***/
6754  /* call function for all accessible objects */
6755  NULL, /* traverseproc tp_traverse; */
6756 
6757  /* delete references to contained objects */
6758  NULL, /* inquiry tp_clear; */
6759 
6760  /*** Assigned meaning in release 2.1 ***/
6761  /*** rich comparisons (subclassed) ***/
6762  NULL, /* richcmpfunc tp_richcompare; */
6763 
6764 /*** weak reference enabler ***/
6765 #ifdef USE_WEAKREFS
6766  offsetof(BPy_PropertyArrayRNA, in_weakreflist), /* long tp_weaklistoffset; */
6767 #else
6768  0,
6769 #endif
6770  /*** Added in release 2.2 ***/
6771  /* Iterators */
6772  (getiterfunc)pyrna_prop_array_iter, /* getiterfunc tp_iter; */
6773  NULL, /* iternextfunc tp_iternext; */
6774 
6775  /*** Attribute descriptor and subclassing stuff ***/
6776  pyrna_prop_array_methods, /* struct PyMethodDef *tp_methods; */
6777  NULL, /* struct PyMemberDef *tp_members; */
6778  NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
6779  &pyrna_prop_Type, /* struct _typeobject *tp_base; */
6780  NULL, /* PyObject *tp_dict; */
6781  NULL, /* descrgetfunc tp_descr_get; */
6782  NULL, /* descrsetfunc tp_descr_set; */
6783  0, /* long tp_dictoffset; */
6784  NULL, /* initproc tp_init; */
6785  NULL, /* allocfunc tp_alloc; */
6786  NULL, /* newfunc tp_new; */
6787  /* Low-level free-memory routine */
6788  NULL, /* freefunc tp_free; */
6789  /* For PyObject_IS_GC */
6790  NULL, /* inquiry tp_is_gc; */
6791  NULL, /* PyObject *tp_bases; */
6792  /* method resolution order */
6793  NULL, /* PyObject *tp_mro; */
6794  NULL, /* PyObject *tp_cache; */
6795  NULL, /* PyObject *tp_subclasses; */
6796  NULL, /* PyObject *tp_weaklist; */
6797  NULL,
6798 };
6799 
6801  PyVarObject_HEAD_INIT(NULL, 0) "bpy_prop_collection", /* tp_name */
6802  sizeof(BPy_PropertyRNA), /* tp_basicsize */
6803  0, /* tp_itemsize */
6804  /* methods */
6805  (destructor)pyrna_prop_dealloc, /* tp_dealloc */
6806  0, /* tp_vectorcall_offset */
6807  NULL, /* getattrfunc tp_getattr; */
6808  NULL, /* setattrfunc tp_setattr; */
6809  NULL,
6810  /* tp_compare */ /* DEPRECATED in Python 3.0! */
6811  NULL,
6812  /* subclassed */ /* tp_repr */
6813 
6814  /* Method suites for standard classes */
6815 
6816  &pyrna_prop_collection_as_number, /* PyNumberMethods *tp_as_number; */
6817  &pyrna_prop_collection_as_sequence, /* PySequenceMethods *tp_as_sequence; */
6818  &pyrna_prop_collection_as_mapping, /* PyMappingMethods *tp_as_mapping; */
6819 
6820  /* More standard operations (here for binary compatibility) */
6821 
6822  NULL, /* hashfunc tp_hash; */
6823  NULL, /* ternaryfunc tp_call; */
6824  NULL, /* reprfunc tp_str; */
6825 
6826  /* will only use these if this is a subtype of a py class */
6827  (getattrofunc)pyrna_prop_collection_getattro, /* getattrofunc tp_getattro; */
6828  (setattrofunc)pyrna_prop_collection_setattro, /* setattrofunc tp_setattro; */
6829 
6830  /* Functions to access object as input/output buffer */
6831  NULL, /* PyBufferProcs *tp_as_buffer; */
6832 
6833  /*** Flags to define presence of optional/expanded features ***/
6834  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
6835 
6836  NULL, /* char *tp_doc; Documentation string */
6837  /*** Assigned meaning in release 2.0 ***/
6838  /* call function for all accessible objects */
6839  NULL, /* traverseproc tp_traverse; */
6840 
6841  /* delete references to contained objects */
6842  NULL, /* inquiry tp_clear; */
6843 
6844  /*** Assigned meaning in release 2.1 ***/
6845  /*** rich comparisons (subclassed) ***/
6846  NULL, /* richcmpfunc tp_richcompare; */
6847 
6848 /*** weak reference enabler ***/
6849 #ifdef USE_WEAKREFS
6850  offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
6851 #else
6852  0,
6853 #endif
6854 
6855  /*** Added in release 2.2 ***/
6856  /* Iterators */
6857  (getiterfunc)pyrna_prop_collection_iter, /* getiterfunc tp_iter; */
6858  NULL, /* iternextfunc tp_iternext; */
6859 
6860  /*** Attribute descriptor and subclassing stuff ***/
6861  pyrna_prop_collection_methods, /* struct PyMethodDef *tp_methods; */
6862  NULL, /* struct PyMemberDef *tp_members; */
6863  NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
6864  &pyrna_prop_Type, /* struct _typeobject *tp_base; */
6865  NULL, /* PyObject *tp_dict; */
6866  NULL, /* descrgetfunc tp_descr_get; */
6867  NULL, /* descrsetfunc tp_descr_set; */
6868  0, /* long tp_dictoffset; */
6869  NULL, /* initproc tp_init; */
6870  NULL, /* allocfunc tp_alloc; */
6871  NULL, /* newfunc tp_new; */
6872  /* Low-level free-memory routine */
6873  NULL, /* freefunc tp_free; */
6874  /* For PyObject_IS_GC */
6875  NULL, /* inquiry tp_is_gc; */
6876  NULL, /* PyObject *tp_bases; */
6877  /* method resolution order */
6878  NULL, /* PyObject *tp_mro; */
6879  NULL, /* PyObject *tp_cache; */
6880  NULL, /* PyObject *tp_subclasses; */
6881  NULL, /* PyObject *tp_weaklist; */
6882  NULL,
6883 };
6884 
6885 /* only for add/remove/move methods */
6886 static PyTypeObject pyrna_prop_collection_idprop_Type = {
6887  PyVarObject_HEAD_INIT(NULL, 0) "bpy_prop_collection_idprop", /* tp_name */
6888  sizeof(BPy_PropertyRNA), /* tp_basicsize */
6889  0, /* tp_itemsize */
6890  /* methods */
6891  (destructor)pyrna_prop_dealloc, /* tp_dealloc */
6892  0, /* tp_vectorcall_offset */
6893  NULL, /* getattrfunc tp_getattr; */
6894  NULL, /* setattrfunc tp_setattr; */
6895  NULL,
6896  /* tp_compare */ /* DEPRECATED in Python 3.0! */
6897  NULL,
6898  /* subclassed */ /* tp_repr */
6899 
6900  /* Method suites for standard classes */
6901 
6902  NULL, /* PyNumberMethods *tp_as_number; */
6903  NULL, /* PySequenceMethods *tp_as_sequence; */
6904  NULL, /* PyMappingMethods *tp_as_mapping; */
6905 
6906  /* More standard operations (here for binary compatibility) */
6907 
6908  NULL, /* hashfunc tp_hash; */
6909  NULL, /* ternaryfunc tp_call; */
6910  NULL, /* reprfunc tp_str; */
6911 
6912  /* will only use these if this is a subtype of a py class */
6913  NULL, /* getattrofunc tp_getattro; */
6914  NULL, /* setattrofunc tp_setattro; */
6915 
6916  /* Functions to access object as input/output buffer */
6917  NULL, /* PyBufferProcs *tp_as_buffer; */
6918 
6919  /*** Flags to define presence of optional/expanded features ***/
6920  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* long tp_flags; */
6921 
6922  NULL, /* char *tp_doc; Documentation string */
6923  /*** Assigned meaning in release 2.0 ***/
6924  /* call function for all accessible objects */
6925  NULL, /* traverseproc tp_traverse; */
6926 
6927  /* delete references to contained objects */
6928  NULL, /* inquiry tp_clear; */
6929 
6930  /*** Assigned meaning in release 2.1 ***/
6931  /*** rich comparisons (subclassed) ***/
6932  NULL, /* richcmpfunc tp_richcompare; */
6933 
6934 /*** weak reference enabler ***/
6935 #ifdef USE_WEAKREFS
6936  offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
6937 #else
6938  0,
6939 #endif
6940 
6941  /*** Added in release 2.2 ***/
6942  /* Iterators */
6943  NULL, /* getiterfunc tp_iter; */
6944  NULL, /* iternextfunc tp_iternext; */
6945 
6946  /*** Attribute descriptor and subclassing stuff ***/
6947  pyrna_prop_collection_idprop_methods, /* struct PyMethodDef *tp_methods; */
6948  NULL, /* struct PyMemberDef *tp_members; */
6949  NULL /*pyrna_prop_getseters*/, /* struct PyGetSetDef *tp_getset; */
6950  &pyrna_prop_collection_Type, /* struct _typeobject *tp_base; */
6951  NULL, /* PyObject *tp_dict; */
6952  NULL, /* descrgetfunc tp_descr_get; */
6953  NULL, /* descrsetfunc tp_descr_set; */
6954  0, /* long tp_dictoffset; */
6955  NULL, /* initproc tp_init; */
6956  NULL, /* allocfunc tp_alloc; */
6957  NULL, /* newfunc tp_new; */
6958  /* Low-level free-memory routine */
6959  NULL, /* freefunc tp_free; */
6960  /* For PyObject_IS_GC */
6961  NULL, /* inquiry tp_is_gc; */
6962  NULL, /* PyObject *tp_bases; */
6963  /* method resolution order */
6964  NULL, /* PyObject *tp_mro; */
6965  NULL, /* PyObject *tp_cache; */
6966  NULL, /* PyObject *tp_subclasses; */
6967  NULL, /* PyObject *tp_weaklist; */
6968  NULL,
6969 };
6970 
6971 /*-----------------------BPy_PropertyRNA method def------------------------------*/
6972 PyTypeObject pyrna_func_Type = {
6973  PyVarObject_HEAD_INIT(NULL, 0) "bpy_func", /* tp_name */
6974  sizeof(BPy_FunctionRNA), /* tp_basicsize */
6975  0, /* tp_itemsize */
6976  /* methods */
6977  NULL, /* tp_dealloc */
6978  0, /* tp_vectorcall_offset */
6979  NULL, /* getattrfunc tp_getattr; */
6980  NULL, /* setattrfunc tp_setattr; */
6981  NULL,
6982  /* tp_compare */ /* DEPRECATED in Python 3.0! */
6983  (reprfunc)pyrna_func_repr, /* tp_repr */
6984 
6985  /* Method suites for standard classes */
6986 
6987  NULL, /* PyNumberMethods *tp_as_number; */
6988  NULL, /* PySequenceMethods *tp_as_sequence; */
6989  NULL, /* PyMappingMethods *tp_as_mapping; */
6990 
6991  /* More standard operations (here for binary compatibility) */
6992 
6993  NULL, /* hashfunc tp_hash; */
6994  (ternaryfunc)pyrna_func_call, /* ternaryfunc tp_call; */
6995  NULL, /* reprfunc tp_str; */
6996 
6997  /* will only use these if this is a subtype of a py class */
6998  NULL, /* getattrofunc tp_getattro; */
6999  NULL, /* setattrofunc tp_setattro; */
7000 
7001  /* Functions to access object as input/output buffer */
7002  NULL, /* PyBufferProcs *tp_as_buffer; */
7003 
7004  /*** Flags to define presence of optional/expanded features ***/
7005  Py_TPFLAGS_DEFAULT, /* long tp_flags; */
7006 
7007  NULL, /* char *tp_doc; Documentation string */
7008  /*** Assigned meaning in release 2.0 ***/
7009  /* call function for all accessible objects */
7010  NULL, /* traverseproc tp_traverse; */
7011 
7012  /* delete references to contained objects */
7013  NULL, /* inquiry tp_clear; */
7014 
7015  /*** Assigned meaning in release 2.1 ***/
7016  /*** rich comparisons ***/
7017  NULL, /* richcmpfunc tp_richcompare; */
7018 
7019 /*** weak reference enabler ***/
7020 #ifdef USE_WEAKREFS
7021  offsetof(BPy_PropertyRNA, in_weakreflist), /* long tp_weaklistoffset; */
7022 #else
7023  0,
7024 #endif
7025 
7026  /*** Added in release 2.2 ***/
7027  /* Iterators */
7028  NULL, /* getiterfunc tp_iter; */
7029  NULL, /* iternextfunc tp_iternext; */
7030 
7031  /*** Attribute descriptor and subclassing stuff ***/
7032  NULL, /* struct PyMethodDef *tp_methods; */
7033  NULL, /* struct PyMemberDef *tp_members; */
7034  pyrna_func_getseters, /* struct PyGetSetDef *tp_getset; */
7035  NULL, /* struct _typeobject *tp_base; */
7036  NULL, /* PyObject *tp_dict; */
7037  NULL, /* descrgetfunc tp_descr_get; */
7038  NULL, /* descrsetfunc tp_descr_set; */
7039  0, /* long tp_dictoffset; */
7040  NULL, /* initproc tp_init; */
7041  NULL, /* allocfunc tp_alloc; */
7042  NULL, /* newfunc tp_new; */
7043  /* Low-level free-memory routine */
7044  NULL, /* freefunc tp_free; */
7045  /* For PyObject_IS_GC */
7046  NULL, /* inquiry tp_is_gc; */
7047  NULL, /* PyObject *tp_bases; */
7048  /* method resolution order */
7049  NULL, /* PyObject *tp_mro; */
7050  NULL, /* PyObject *tp_cache; */
7051  NULL, /* PyObject *tp_subclasses; */
7052  NULL, /* PyObject *tp_weaklist; */
7053  NULL,
7054 };
7055 
7056 #ifdef USE_PYRNA_ITER
7057 /* --- collection iterator: start --- */
7058 /* wrap RNA collection iterator functions */
7059 /*
7060  * RNA_property_collection_begin(...)
7061  * RNA_property_collection_next(...)
7062  * RNA_property_collection_end(...)
7063  */
7064 
7067 
7068 static PyTypeObject pyrna_prop_collection_iter_Type = {
7069  PyVarObject_HEAD_INIT(NULL, 0) "bpy_prop_collection_iter", /* tp_name */
7070  sizeof(BPy_PropertyCollectionIterRNA), /* tp_basicsize */
7071  0, /* tp_itemsize */
7072  /* methods */
7073  (destructor)pyrna_prop_collection_iter_dealloc, /* tp_dealloc */
7074  0, /* tp_vectorcall_offset */
7075  NULL, /* getattrfunc tp_getattr; */
7076  NULL, /* setattrfunc tp_setattr; */
7077  NULL,
7078  /* tp_compare */ /* DEPRECATED in Python 3.0! */
7079  NULL,
7080  /* subclassed */ /* tp_repr */
7081 
7082  /* Method suites for standard classes */
7083 
7084  NULL, /* PyNumberMethods *tp_as_number; */
7085  NULL, /* PySequenceMethods *tp_as_sequence; */
7086  NULL, /* PyMappingMethods *tp_as_mapping; */
7087 
7088  /* More standard operations (here for binary compatibility) */
7089 
7090  NULL, /* hashfunc tp_hash; */
7091  NULL, /* ternaryfunc tp_call; */
7092  NULL, /* reprfunc tp_str; */
7093 
7094  /* will only use these if this is a subtype of a py class */
7095  PyObject_GenericGetAttr, /* getattrofunc tp_getattro; */
7096  NULL, /* setattrofunc tp_setattro; */
7097 
7098  /* Functions to access object as input/output buffer */
7099  NULL, /* PyBufferProcs *tp_as_buffer; */
7100 
7101  /*** Flags to define presence of optional/expanded features ***/
7102  Py_TPFLAGS_DEFAULT, /* long tp_flags; */
7103 
7104  NULL, /* char *tp_doc; Documentation string */
7105  /*** Assigned meaning in release 2.0 ***/
7106  /* call function for all accessible objects */
7107  NULL, /* traverseproc tp_traverse; */
7108 
7109  /* delete references to contained objects */
7110  NULL, /* inquiry tp_clear; */
7111 
7112  /*** Assigned meaning in release 2.1 ***/
7113  /*** rich comparisons (subclassed) ***/
7114  NULL, /* richcmpfunc tp_richcompare; */
7115 
7116 /*** weak reference enabler ***/
7117 # ifdef USE_WEAKREFS
7118  offsetof(BPy_PropertyCollectionIterRNA, in_weakreflist), /* long tp_weaklistoffset; */
7119 # else
7120  0,
7121 # endif
7122  /*** Added in release 2.2 ***/
7123  /* Iterators */
7124  PyObject_SelfIter, /* getiterfunc tp_iter; */
7125  (iternextfunc)pyrna_prop_collection_iter_next, /* iternextfunc tp_iternext; */
7126 
7127  /*** Attribute descriptor and subclassing stuff ***/
7128  NULL, /* struct PyMethodDef *tp_methods; */
7129  NULL, /* struct PyMemberDef *tp_members; */
7130  NULL, /* struct PyGetSetDef *tp_getset; */
7131  NULL, /* struct _typeobject *tp_base; */
7132  NULL, /* PyObject *tp_dict; */
7133  NULL, /* descrgetfunc tp_descr_get; */
7134  NULL, /* descrsetfunc tp_descr_set; */
7135  0, /* long tp_dictoffset; */
7136  NULL, /* initproc tp_init; */
7137  NULL, /* allocfunc tp_alloc; */
7138  NULL, /* newfunc tp_new; */
7139  /* Low-level free-memory routine */
7140  NULL, /* freefunc tp_free; */
7141  /* For PyObject_IS_GC */
7142  NULL, /* inquiry tp_is_gc; */
7143  NULL, /* PyObject *tp_bases; */
7144  /* method resolution order */
7145  NULL, /* PyObject *tp_mro; */
7146  NULL, /* PyObject *tp_cache; */
7147  NULL, /* PyObject *tp_subclasses; */
7148  NULL, /* PyObject *tp_weaklist; */
7149  NULL,
7150 };
7151 
7153 {
7156 
7157 # ifdef USE_WEAKREFS
7158  self->in_weakreflist = NULL;
7159 # endif
7160 
7161  RNA_property_collection_begin(ptr, prop, &self->iter);
7162 
7163  return (PyObject *)self;
7164 }
7165 
7167 {
7169 }
7170 
7172 {
7173  if (self->iter.valid == false) {
7174  PyErr_SetNone(PyExc_StopIteration);
7175  return NULL;
7176  }
7177 
7179 
7180 # ifdef USE_PYRNA_STRUCT_REFERENCE
7181  if (pyrna) { /* Unlikely, but may fail. */
7182  if ((PyObject *)pyrna != Py_None) {
7183  /* hold a reference to the iterator since it may have
7184  * allocated memory 'pyrna' needs. eg: introspecting dynamic enum's */
7185  /* TODO, we could have an api call to know if this is
7186  * needed since most collections don't */
7187  pyrna_struct_reference_set(pyrna, (PyObject *)self);
7188  }
7189  }
7190 # endif /* !USE_PYRNA_STRUCT_REFERENCE */
7191 
7193 
7194  return (PyObject *)pyrna;
7195 }
7196 
7198 {
7199 # ifdef USE_WEAKREFS
7200  if (self->in_weakreflist != NULL) {
7201  PyObject_ClearWeakRefs((PyObject *)self);
7202  }
7203 # endif
7204 
7206 
7207  PyObject_DEL(self);
7208 }
7209 
7210 /* --- collection iterator: end --- */
7211 #endif /* !USE_PYRNA_ITER */
7212 
7213 static void pyrna_subtype_set_rna(PyObject *newclass, StructRNA *srna)
7214 {
7215  PointerRNA ptr;
7216  PyObject *item;
7217 
7218  Py_INCREF(newclass);
7219 
7220  if (RNA_struct_py_type_get(srna)) {
7221  PyC_ObSpit("RNA WAS SET - ", RNA_struct_py_type_get(srna));
7222  }
7223 
7224  Py_XDECREF(((PyObject *)RNA_struct_py_type_get(srna)));
7225 
7226  RNA_struct_py_type_set(srna, (void *)newclass); /* Store for later use */
7227 
7228  /* Not 100% needed, but useful,
7229  * having an instance within a type looks wrong, but this instance _is_ an RNA type. */
7230 
7231  /* Python deals with the circular reference. */
7234 
7235  /* Note, must set the class not the __dict__ else the internal slots are not updated correctly.
7236  */
7237  PyObject_SetAttr(newclass, bpy_intern_str_bl_rna, item);
7238  Py_DECREF(item);
7239 
7240  /* Add staticmethods and classmethods. */
7241  {
7242  const PointerRNA func_ptr = {NULL, srna, NULL};
7243  const ListBase *lb;
7244  Link *link;
7245 
7246  lb = RNA_struct_type_functions(srna);
7247  for (link = lb->first; link; link = link->next) {
7248  FunctionRNA *func = (FunctionRNA *)link;
7249  const int flag = RNA_function_flag(func);
7250  if ((flag & FUNC_NO_SELF) && /* Is staticmethod or classmethod. */
7251  (flag & FUNC_REGISTER) == false) /* Is not for registration. */
7252  {
7253  /* We may want to set the type of this later. */
7254  PyObject *func_py = pyrna_func_to_py(&func_ptr, func);
7255  PyObject_SetAttrString(newclass, RNA_function_identifier(func), func_py);
7256  Py_DECREF(func_py);
7257  }
7258  }
7259  }
7260 
7261  /* Done with RNA instance. */
7262 }
7263 
7264 static PyObject *pyrna_srna_Subtype(StructRNA *srna);
7265 
7266 /* Return a borrowed reference. */
7267 static PyObject *pyrna_srna_PyBase(StructRNA *srna) //, PyObject *bpy_types_dict)
7268 {
7269  /* Assume RNA_struct_py_type_get(srna) was already checked. */
7270  StructRNA *base;
7271 
7272  PyObject *py_base = NULL;
7273 
7274  /* Get the base type. */
7275  base = RNA_struct_base(srna);
7276 
7277  if (base && base != srna) {
7278  // printf("debug subtype %s %p\n", RNA_struct_identifier(srna), srna);
7279  py_base = pyrna_srna_Subtype(base); //, bpy_types_dict);
7280  Py_DECREF(py_base); /* Srna owns, this is only to pass as an arg. */
7281  }
7282 
7283  if (py_base == NULL) {
7284  py_base = (PyObject *)&pyrna_struct_Type;
7285  }
7286 
7287  return py_base;
7288 }
7289 
7290 /* Check if we have a native Python subclass, use it when it exists
7291  * return a borrowed reference. */
7292 static PyObject *bpy_types_dict = NULL;
7293 
7294 static PyObject *pyrna_srna_ExternalType(StructRNA *srna)
7295 {
7296  const char *idname = RNA_struct_identifier(srna);
7297  PyObject *newclass;
7298 
7299  if (bpy_types_dict == NULL) {
7300  PyObject *bpy_types = PyImport_ImportModuleLevel("bpy_types", NULL, NULL, NULL, 0);
7301 
7302  if (bpy_types == NULL) {
7303  PyErr_Print();
7304  PyErr_Clear();
7305  CLOG_ERROR(BPY_LOG_RNA, "failed to find 'bpy_types' module");
7306  return NULL;
7307  }
7308  bpy_types_dict = PyModule_GetDict(bpy_types); /* Borrow. */
7309  Py_DECREF(bpy_types); /* Fairly safe to assume the dict is kept. */
7310  }
7311 
7312  newclass = PyDict_GetItemString(bpy_types_dict, idname);
7313 
7314  /* Sanity check, could skip this unless in debug mode. */
7315  if (newclass) {
7316  PyObject *base_compare = pyrna_srna_PyBase(srna);
7317  /* Can't do this because it gets superclasses values! */
7318  // PyObject *slots = PyObject_GetAttrString(newclass, "__slots__");
7319  /* Can do this, but faster not to. */
7320  // PyObject *bases = PyObject_GetAttrString(newclass, "__bases__");
7321  PyObject *tp_bases = ((PyTypeObject *)newclass)->tp_bases;
7322  PyObject *tp_slots = PyDict_GetItem(((PyTypeObject *)newclass)->tp_dict,
7324 
7325  if (tp_slots == NULL) {
7326  CLOG_ERROR(
7327  BPY_LOG_RNA, "expected class '%s' to have __slots__ defined, see bpy_types.py", idname);
7328  newclass = NULL;
7329  }
7330  else if (PyTuple_GET_SIZE(tp_bases)) {
7331  PyObject *base = PyTuple_GET_ITEM(tp_bases, 0);
7332 
7333  if (base_compare != base) {
7334  char pyob_info[256];
7335  PyC_ObSpitStr(pyob_info, sizeof(pyob_info), base_compare);
7337  "incorrect subclassing of SRNA '%s', expected '%s', see bpy_types.py",
7338  idname,
7339  pyob_info);
7340  newclass = NULL;
7341  }
7342  else {
7343  CLOG_INFO(BPY_LOG_RNA, 2, "SRNA sub-classed: '%s'", idname);
7344  }
7345  }
7346  }
7347 
7348  return newclass;
7349 }
7350 
7351 static PyObject *pyrna_srna_Subtype(StructRNA *srna)
7352 {
7353  PyObject *newclass = NULL;
7354 
7355  /* Stupid/simple case. */
7356  if (srna == NULL) {
7357  newclass = NULL; /* Nothing to do. */
7358  } /* The class may have already been declared & allocated. */
7359  else if ((newclass = RNA_struct_py_type_get(srna))) {
7360  Py_INCREF(newclass);
7361  } /* Check if bpy_types.py module has the class defined in it. */
7362  else if ((newclass = pyrna_srna_ExternalType(srna))) {
7363  pyrna_subtype_set_rna(newclass, srna);
7364  Py_INCREF(newclass);
7365  } /* create a new class instance with the C api
7366  * mainly for the purposing of matching the C/RNA type hierarchy */
7367  else {
7368  /* subclass equivalents
7369  * - class myClass(myBase):
7370  * some = 'value' # or ...
7371  * - myClass = type(
7372  * name='myClass',
7373  * bases=(myBase,), dict={'__module__': 'bpy.types', '__slots__': ()}
7374  * )
7375  */
7376 
7377  /* Assume RNA_struct_py_type_get(srna) was already checked. */
7378  PyObject *py_base = pyrna_srna_PyBase(srna);
7379  PyObject *metaclass;
7380  const char *idname = RNA_struct_identifier(srna);
7381 
7382  /* Remove __doc__ for now. */
7383  // const char *descr = RNA_struct_ui_description(srna);
7384  // if (!descr) descr = "(no docs)";
7385  // "__doc__", descr
7386 
7387  if (RNA_struct_idprops_check(srna) &&
7388  !PyObject_IsSubclass(py_base, (PyObject *)&pyrna_struct_meta_idprop_Type)) {
7389  metaclass = (PyObject *)&pyrna_struct_meta_idprop_Type;
7390  }
7391  else {
7392  metaclass = (PyObject *)&PyType_Type;
7393  }
7394 
7395  /* Always use O not N when calling, N causes refcount errors. */
7396 #if 0
7397  newclass = PyObject_CallFunction(
7398  metaclass, "s(O) {sss()}", idname, py_base, "__module__", "bpy.types", "__slots__");
7399 #else
7400  {
7401  /* Longhand of the call above. */
7402  PyObject *args, *item, *value;
7403  int ok;
7404 
7405  args = PyTuple_New(3);
7406 
7407  /* arg[0] (name=...) */
7408  PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(idname));
7409 
7410  /* arg[1] (bases=...) */
7411  PyTuple_SET_ITEM(args, 1, item = PyTuple_New(1));
7412  PyTuple_SET_ITEM(item, 0, Py_INCREF_RET(py_base));
7413 
7414  /* arg[2] (dict=...) */
7415  PyTuple_SET_ITEM(args, 2, item = PyDict_New());
7416  ok = PyDict_SetItem(item, bpy_intern_str___module__, bpy_intern_str_bpy_types);
7417  BLI_assert(ok != -1);
7418  ok = PyDict_SetItem(item, bpy_intern_str___slots__, value = PyTuple_New(0));
7419  Py_DECREF(value);
7420  BLI_assert(ok != -1);
7421 
7422  newclass = PyObject_CallObject(metaclass, args);
7423  Py_DECREF(args);
7424 
7425  (void)ok;
7426  }
7427 #endif
7428 
7429  /* Newclass will now have 2 ref's, ???,
7430  * probably 1 is internal since #Py_DECREF here segfaults. */
7431 
7432  /* PyC_ObSpit("new class ref", newclass); */
7433 
7434  if (newclass) {
7435  /* srna owns one, and the other is owned by the caller. */
7436  pyrna_subtype_set_rna(newclass, srna);
7437 
7438  /* XXX, adding this back segfaults Blender on load. */
7439  // Py_DECREF(newclass); /* let srna own */
7440  }
7441  else {
7442  /* This should not happen. */
7443  CLOG_ERROR(BPY_LOG_RNA, "failed to register '%s'", idname);
7444  PyErr_Print();
7445  PyErr_Clear();
7446  }
7447  }
7448 
7449  return newclass;
7450 }
7451 
7452 /* Use for subtyping so we know which srna is used for a PointerRNA. */
7454 {
7455  if (ptr->type == &RNA_Struct) {
7456  return ptr->data;
7457  }
7458 
7459  return ptr->type;
7460 }
7461 
7462 /* Always returns a new ref, be sure to decref when done. */
7464 {
7466 }
7467 
7468 /*-----------------------CreatePyObject---------------------------------*/
7470 {
7471  BPy_StructRNA *pyrna = NULL;
7472 
7473  /* Note: don't rely on this to return None since NULL data with a valid type can often crash. */
7474  if (ptr->data == NULL && ptr->type == NULL) { /* Operator RNA has NULL data. */
7475  Py_RETURN_NONE;
7476  }
7477 
7478  /* New in 2.8x, since not many types support instancing
7479  * we may want to use a flag to avoid looping over all classes. - campbell */
7480  void **instance = ptr->data ? RNA_struct_instance(ptr) : NULL;
7481  if (instance && *instance) {
7482  pyrna = *instance;
7483 
7484  /* Refine may have changed types after the first instance was created. */
7485  if (ptr->type == pyrna->ptr.type) {
7486  Py_INCREF(pyrna);
7487  return (PyObject *)pyrna;
7488  }
7489 
7490  /* Existing users will need to use 'type_recast' method. */
7491  Py_DECREF(pyrna);
7492  *instance = NULL;
7493  /* Continue as if no instance was made. */
7494 #if 0 /* No need to assign, will be written to next... */
7495  pyrna = NULL;
7496 #endif
7497  }
7498 
7499  {
7500  PyTypeObject *tp = (PyTypeObject *)pyrna_struct_Subtype(ptr);
7501 
7502  if (tp) {
7503  pyrna = (BPy_StructRNA *)tp->tp_alloc(tp, 0);
7504 #ifdef USE_PYRNA_STRUCT_REFERENCE
7505  /* #PyType_GenericAlloc will have set tracking.
7506  * We only want tracking when `StructRNA.reference` has been set. */
7507  if (pyrna != NULL) {
7508  PyObject_GC_UnTrack(pyrna);
7509  }
7510 #endif
7511  Py_DECREF(tp); /* srna owns, can't hold a reference. */
7512  }
7513  else {
7514  CLOG_WARN(BPY_LOG_RNA, "could not make type '%s'", RNA_struct_identifier(ptr->type));
7515 
7516 #ifdef USE_PYRNA_STRUCT_REFERENCE
7517  pyrna = (BPy_StructRNA *)PyObject_GC_New(BPy_StructRNA, &pyrna_struct_Type);
7518 #else
7519  pyrna = (BPy_StructRNA *)PyObject_New(BPy_StructRNA, &pyrna_struct_Type);
7520 #endif
7521 
7522 #ifdef USE_WEAKREFS
7523  if (pyrna != NULL) {
7524  pyrna->in_weakreflist = NULL;
7525  }
7526 #endif
7527  }
7528  }
7529 
7530  if (pyrna == NULL) {
7531  PyErr_SetString(PyExc_MemoryError, "couldn't create bpy_struct object");
7532  return NULL;
7533  }
7534 
7535  /* Blender's instance owns a reference (to avoid Python freeing it). */
7536  if (instance) {
7537  *instance = pyrna;
7538  Py_INCREF(pyrna);
7539  }
7540 
7541  pyrna->ptr = *ptr;
7542 #ifdef PYRNA_FREE_SUPPORT
7543  pyrna->freeptr = false;
7544 #endif
7545 
7546 #ifdef USE_PYRNA_STRUCT_REFERENCE
7547  pyrna->reference = NULL;
7548 #endif
7549 
7550  // PyC_ObSpit("NewStructRNA: ", (PyObject *)pyrna);
7551 
7552 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
7553  if (ptr->owner_id) {
7554  id_weakref_pool_add(ptr->owner_id, (BPy_DummyPointerRNA *)pyrna);
7555  }
7556 #endif
7557  return (PyObject *)pyrna;
7558 }
7559 
7561 {
7562  BPy_PropertyRNA *pyrna;
7563 
7564  if (RNA_property_array_check(prop) == 0) {
7565  PyTypeObject *type;
7566 
7567  if (RNA_property_type(prop) != PROP_COLLECTION) {
7568  type = &pyrna_prop_Type;
7569  }
7570  else {
7571  if ((RNA_property_flag(prop) & PROP_IDPROPERTY) == 0) {
7573  }
7574  else {
7576  }
7577  }
7578 
7579  pyrna = (BPy_PropertyRNA *)PyObject_NEW(BPy_PropertyRNA, type);
7580 #ifdef USE_WEAKREFS
7581  pyrna->in_weakreflist = NULL;
7582 #endif
7583  }
7584  else {
7585  pyrna = (BPy_PropertyRNA *)PyObject_NEW(BPy_PropertyArrayRNA, &pyrna_prop_array_Type);
7586  ((BPy_PropertyArrayRNA *)pyrna)->arraydim = 0;
7587  ((BPy_PropertyArrayRNA *)pyrna)->arrayoffset = 0;
7588 #ifdef USE_WEAKREFS
7589  ((BPy_PropertyArrayRNA *)pyrna)->in_weakreflist = NULL;
7590 #endif
7591  }
7592 
7593  if (pyrna == NULL) {
7594  PyErr_SetString(PyExc_MemoryError, "couldn't create BPy_rna object");
7595  return NULL;
7596  }
7597 
7598  pyrna->ptr = *ptr;
7599  pyrna->prop = prop;
7600 
7601 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
7602  if (ptr->owner_id) {
7603  id_weakref_pool_add(ptr->owner_id, (BPy_DummyPointerRNA *)pyrna);
7604  }
7605 #endif
7606 
7607  return (PyObject *)pyrna;
7608 }
7609 
7610 /* Utility func to be used by external modules, sneaky! */
7612 {
7613  if (id) {
7614  PointerRNA ptr;
7615  RNA_id_pointer_create(id, &ptr);
7617  }
7618 
7619  Py_RETURN_NONE;
7620 }
7621 
7622 bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
7623 {
7624  if (pyrna_id_CheckPyObject(obj)) {
7625  *id = ((BPy_StructRNA *)obj)->ptr.owner_id;
7626  return true;
7627  }
7628 
7629  *id = NULL;
7630  return false;
7631 }
7632 
7633 bool pyrna_id_CheckPyObject(PyObject *obj)
7634 {
7635  return BPy_StructRNA_Check(obj) && (RNA_struct_is_ID(((BPy_StructRNA *)obj)->ptr.type));
7636 }
7637 
7638 void BPY_rna_init(void)
7639 {
7640 #ifdef USE_MATHUTILS /* Register mathutils callbacks, ok to run more than once. */
7643 #endif
7644 
7645  /* For some reason MSVC complains of these. */
7646 #if defined(_MSC_VER)
7647  pyrna_struct_meta_idprop_Type.tp_base = &PyType_Type;
7648 #endif
7649 
7650  /* metaclass */
7651  if (PyType_Ready(&pyrna_struct_meta_idprop_Type) < 0) {
7652  return;
7653  }
7654 
7655  if (PyType_Ready(&pyrna_struct_Type) < 0) {
7656  return;
7657  }
7658 
7659  if (PyType_Ready(&pyrna_prop_Type) < 0) {
7660  return;
7661  }
7662 
7663  if (PyType_Ready(&pyrna_prop_array_Type) < 0) {
7664  return;
7665  }
7666 
7667  if (PyType_Ready(&pyrna_prop_collection_Type) < 0) {
7668  return;
7669  }
7670 
7671  if (PyType_Ready(&pyrna_prop_collection_idprop_Type) < 0) {
7672  return;
7673  }
7674 
7675  if (PyType_Ready(&pyrna_func_Type) < 0) {
7676  return;
7677  }
7678 
7679 #ifdef USE_PYRNA_ITER
7680  if (PyType_Ready(&pyrna_prop_collection_iter_Type) < 0) {
7681  return;
7682  }
7683 #endif
7684 }
7685 
7686 /* 'bpy.data' from Python. */
7688 PyObject *BPY_rna_module(void)
7689 {
7690  BPy_StructRNA *pyrna;
7691  PointerRNA ptr;
7692 
7693  /* For now, return the base RNA type rather than a real module. */
7696 
7697  rna_module_ptr = &pyrna->ptr;
7698  return (PyObject *)pyrna;
7699 }
7700 
7702 {
7703  if (rna_module_ptr) {
7704 #if 0
7706 #else
7707  rna_module_ptr->data = G_MAIN; /* Just set data is enough. */
7708 #endif
7709  }
7710 }
7711 
7712 #if 0
7713 /* This is a way we can access doc-strings for RNA types
7714  * without having the data-types in Blender. */
7715 PyObject *BPY_rna_doc(void)
7716 {
7717  PointerRNA ptr;
7718 
7719  /* For now, return the base RNA type rather than a real module. */
7721 
7723 }
7724 #endif
7725 
7726 /* -------------------------------------------------------------------- */
7741 };
7742 
7743 static PyObject *bpy_types_module_getattro(PyObject *self, PyObject *pyname)
7744 {
7745  struct BPy_TypesModule_State *state = PyModule_GetState(self);
7746  PointerRNA newptr;
7747  PyObject *ret;
7748  const char *name = PyUnicode_AsUTF8(pyname);
7749 
7750  if (name == NULL) {
7751  PyErr_SetString(PyExc_AttributeError, "bpy.types: __getattr__ must be a string");
7752  ret = NULL;
7753  }
7754  else if (RNA_property_collection_lookup_string(&state->ptr, state->prop, name, &newptr)) {
7755  ret = pyrna_struct_Subtype(&newptr);
7756  if (ret == NULL) {
7757  PyErr_Format(PyExc_RuntimeError,
7758  "bpy.types.%.200s subtype could not be generated, this is a bug!",
7759  PyUnicode_AsUTF8(pyname));
7760  }
7761  }
7762  else {
7763 #if 0
7764  PyErr_Format(PyExc_AttributeError,
7765  "bpy.types.%.200s RNA_Struct does not exist",
7766  PyUnicode_AsUTF8(pyname));
7767  return NULL;
7768 #endif
7769  /* The error raised here will be displayed. */
7770  ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
7771  }
7772 
7773  return ret;
7774 }
7775 
7776 static PyObject *bpy_types_module_dir(PyObject *self)
7777 {
7778  struct BPy_TypesModule_State *state = PyModule_GetState(self);
7779  PyObject *ret = PyList_New(0);
7780 
7781  RNA_PROP_BEGIN (&state->ptr, itemptr, state->prop) {
7782  StructRNA *srna = itemptr.data;
7783  PyList_APPEND(ret, PyUnicode_FromString(RNA_struct_identifier(srna)));
7784  }
7785  RNA_PROP_END;
7786 
7787  /* Include the modules `__dict__` for Python only types. */
7788  PyObject *submodule_dict = PyModule_GetDict(self);
7789  PyObject *key, *value;
7790  Py_ssize_t pos = 0;
7791  while (PyDict_Next(submodule_dict, &pos, &key, &value)) {
7792  PyList_Append(ret, key);
7793  }
7794  return ret;
7795 }
7796 
7797 static struct PyMethodDef bpy_types_module_methods[] = {
7798  {"__getattr__", (PyCFunction)bpy_types_module_getattro, METH_O, NULL},
7799  {"__dir__", (PyCFunction)bpy_types_module_dir, METH_NOARGS, NULL},
7800  {NULL, NULL, 0, NULL},
7801 };
7802 
7803 PyDoc_STRVAR(bpy_types_module_doc, "Access to internal Blender types");
7804 static struct PyModuleDef bpy_types_module_def = {
7805  PyModuleDef_HEAD_INIT,
7806  "bpy.types", /* m_name */
7807  bpy_types_module_doc, /* m_doc */
7808  sizeof(struct BPy_TypesModule_State), /* m_size */
7809  bpy_types_module_methods, /* m_methods */
7810  NULL, /* m_reload */
7811  NULL, /* m_traverse */
7812  NULL, /* m_clear */
7813  NULL, /* m_free */
7814 };
7815 
7819 PyObject *BPY_rna_types(void)
7820 {
7821  PyObject *submodule = PyModule_Create(&bpy_types_module_def);
7822  struct BPy_TypesModule_State *state = PyModule_GetState(submodule);
7823 
7825  state->prop = RNA_struct_find_property(&state->ptr, "structs");
7826 
7827  /* Internal base types we have no other accessors for. */
7828  {
7829  static PyTypeObject *pyrna_types[] = {
7832  &pyrna_prop_Type,
7835  &pyrna_func_Type,
7836  };
7837 
7838  PyObject *submodule_dict = PyModule_GetDict(submodule);
7839  for (int i = 0; i < ARRAY_SIZE(pyrna_types); i += 1) {
7840  PyDict_SetItemString(submodule_dict, pyrna_types[i]->tp_name, (PyObject *)pyrna_types[i]);
7841  }
7842  }
7843 
7844  return submodule;
7845 }
7846 
7849 StructRNA *pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
7850 {
7851  BPy_StructRNA *py_srna = NULL;
7852  StructRNA *srna;
7853 
7854  /* Unfortunately PyObject_GetAttrString wont look up this types tp_dict first :/ */
7855  if (PyType_Check(self)) {
7856  py_srna = (BPy_StructRNA *)PyDict_GetItem(((PyTypeObject *)self)->tp_dict,
7858  Py_XINCREF(py_srna);
7859  }
7860 
7861  if (parent) {
7862  /* be very careful with this since it will return a parent classes srna.
7863  * modifying this will do confusing stuff! */
7864  if (py_srna == NULL) {
7865  py_srna = (BPy_StructRNA *)PyObject_GetAttr(self, bpy_intern_str_bl_rna);
7866  }
7867  }
7868 
7869  if (py_srna == NULL) {
7870  PyErr_Format(PyExc_RuntimeError,
7871  "%.200s, missing bl_rna attribute from '%.200s' instance (may not be registered)",
7872  error_prefix,
7873  Py_TYPE(self)->tp_name);
7874  return NULL;
7875  }
7876 
7877  if (!BPy_StructRNA_Check(py_srna)) {
7878  PyErr_Format(PyExc_TypeError,
7879  "%.200s, bl_rna attribute wrong type '%.200s' on '%.200s'' instance",
7880  error_prefix,
7881  Py_TYPE(py_srna)->tp_name,
7882  Py_TYPE(self)->tp_name);
7883  Py_DECREF(py_srna);
7884  return NULL;
7885  }
7886 
7887  if (py_srna->ptr.type != &RNA_Struct) {
7888  PyErr_Format(PyExc_TypeError,
7889  "%.200s, bl_rna attribute not a RNA_Struct, on '%.200s'' instance",
7890  error_prefix,
7891  Py_TYPE(self)->tp_name);
7892  Py_DECREF(py_srna);
7893  return NULL;
7894  }
7895 
7896  srna = py_srna->ptr.data;
7897  Py_DECREF(py_srna);
7898 
7899  return srna;
7900 }
7901 
7902 /* Orphan functions, not sure where they should go. */
7903 /* Get the srna for methods attached to types. */
7904 /*
7905  * Caller needs to raise error.*/
7906 StructRNA *srna_from_self(PyObject *self, const char *error_prefix)
7907 {
7908 
7909  if (self == NULL) {
7910  return NULL;
7911  }
7912  if (PyCapsule_CheckExact(self)) {
7913  return PyCapsule_GetPointer(self, NULL);
7914  }
7915  if (PyType_Check(self) == 0) {
7916  return NULL;
7917  }
7918 
7919  /* These cases above not errors, they just mean the type was not compatible
7920  * After this any errors will be raised in the script */
7921 
7922  PyObject *error_type, *error_value, *error_traceback;
7923  StructRNA *srna;
7924 
7925  PyErr_Fetch(&error_type, &error_value, &error_traceback);
7926  PyErr_Clear();
7927 
7928  srna = pyrna_struct_as_srna(self, false, error_prefix);
7929 
7930  if (!PyErr_Occurred()) {
7931  PyErr_Restore(error_type, error_value, error_traceback);
7932  }
7933 
7934  return srna;
7935 }
7936 
7937 static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item)
7938 {
7939  if (!BPy_PropDeferred_CheckTypeExact(item)) {
7940  /* No error, ignoring. */
7941  return 0;
7942  }
7943 
7944  /* We only care about results from C which
7945  * are for sure types, save some time with error */
7946  PyObject *py_func = ((BPy_PropDeferred *)item)->fn;
7947  PyObject *py_kw = ((BPy_PropDeferred *)item)->kw;
7948  PyObject *py_srna_cobject, *py_ret;
7949 
7950  PyObject *args_fake;
7951 
7952  if (*PyUnicode_AsUTF8(key) == '_') {
7953  PyErr_Format(PyExc_ValueError,
7954  "bpy_struct \"%.200s\" registration error: "
7955  "%.200s could not register because the property starts with an '_'\n",
7956  RNA_struct_identifier(srna),
7957  PyUnicode_AsUTF8(key));
7958  return -1;
7959  }
7960  py_srna_cobject = PyCapsule_New(srna, NULL, NULL);
7961 
7962  /* Not 100% nice :/, modifies the dict passed, should be ok. */
7963  PyDict_SetItem(py_kw, bpy_intern_str_attr, key);
7964 
7965  args_fake = PyTuple_New(1);
7966  PyTuple_SET_ITEM(args_fake, 0, py_srna_cobject);
7967 
7968  PyObject *type = PyDict_GetItemString(py_kw, "type");
7969  StructRNA *type_srna = srna_from_self(type, "");
7970  if (type_srna) {
7972  (*(PyCFunctionWithKeywords)PyCFunction_GET_FUNCTION(py_func) == BPy_PointerProperty ||
7973  *(PyCFunctionWithKeywords)PyCFunction_GET_FUNCTION(py_func) == BPy_CollectionProperty) &&
7975  PyErr_Format(PyExc_ValueError,
7976  "bpy_struct \"%.200s\" doesn't support datablock properties\n",
7977  RNA_struct_identifier(srna));
7978  return -1;
7979  }
7980  }
7981 
7982  py_ret = PyObject_Call(py_func, args_fake, py_kw);
7983 
7984  if (py_ret) {
7985  Py_DECREF(py_ret);
7986  Py_DECREF(args_fake); /* Free's py_srna_cobject too. */
7987  }
7988  else {
7989  /* _must_ print before decreffing args_fake. */
7990  PyErr_Print();
7991  PyErr_Clear();
7992 
7993  Py_DECREF(args_fake); /* Free's py_srna_cobject too. */
7994 
7995  // PyC_LineSpit();
7996  PyErr_Format(PyExc_ValueError,
7997  "bpy_struct \"%.200s\" registration error: "
7998  "%.200s could not register\n",
7999  RNA_struct_identifier(srna),
8000  PyUnicode_AsUTF8(key));
8001  return -1;
8002  }
8003 
8004  return 0;
8005 }
8006 
8010 static int pyrna_deferred_register_class_from_type_hints(StructRNA *srna, PyTypeObject *py_class)
8011 {
8012  PyObject *annotations_dict = NULL;
8013 
8014  /* `typing.get_type_hints(py_class)` */
8015  {
8016  PyObject *typing_mod = PyImport_ImportModuleLevel("typing", NULL, NULL, NULL, 0);
8017  if (typing_mod != NULL) {
8018  PyObject *get_type_hints_fn = PyObject_GetAttrString(typing_mod, "get_type_hints");
8019  if (get_type_hints_fn != NULL) {
8020  PyObject *args = PyTuple_New(1);
8021 
8022  PyTuple_SET_ITEM(args, 0, (PyObject *)py_class);
8023  Py_INCREF(py_class);
8024 
8025  annotations_dict = PyObject_CallObject(get_type_hints_fn, args);
8026 
8027  Py_DECREF(args);
8028  Py_DECREF(get_type_hints_fn);
8029  }
8030  Py_DECREF(typing_mod);
8031  }
8032  }
8033 
8034  int ret = 0;
8035  if (annotations_dict != NULL) {
8036  if (PyDict_CheckExact(annotations_dict)) {
8037  PyObject *item, *key;
8038  Py_ssize_t pos = 0;
8039 
8040  while (PyDict_Next(annotations_dict, &pos, &key, &item)) {
8041  ret = deferred_register_prop(srna, key, item);
8042  if (ret != 0) {
8043  break;
8044  }
8045  }
8046  }
8047  else {
8048  /* Should never happen, an error wont have been raised, so raise one. */
8049  PyErr_Format(PyExc_TypeError,
8050  "typing.get_type_hints returned: %.200s, expected dict\n",
8051  Py_TYPE(annotations_dict)->tp_name);
8052  ret = -1;
8053  }
8054 
8055  Py_DECREF(annotations_dict);
8056  }
8057  else {
8058  BLI_assert(PyErr_Occurred());
8059  fprintf(stderr, "typing.get_type_hints failed with: %.200s\n", py_class->tp_name);
8060  ret = -1;
8061  }
8062 
8063  return ret;
8064 }
8065 
8066 static int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
8067 {
8068  PyObject *annotations_dict;
8069  PyObject *item, *key;
8070  Py_ssize_t pos = 0;
8071  int ret = 0;
8072 
8073  /* in both cases PyDict_CheckExact(class_dict) will be true even
8074  * though Operators have a metaclass dict namespace */
8075  if ((annotations_dict = PyDict_GetItem(class_dict, bpy_intern_str___annotations__)) &&
8076  PyDict_CheckExact(annotations_dict)) {
8077  while (PyDict_Next(annotations_dict, &pos, &key, &item)) {
8078  ret = deferred_register_prop(srna, key, item);
8079 
8080  if (ret != 0) {
8081  break;
8082  }
8083  }
8084  }
8085 
8086  return ret;
8087 }
8088 
8089 static int pyrna_deferred_register_class_recursive(StructRNA *srna, PyTypeObject *py_class)
8090 {
8091  const int len = PyTuple_GET_SIZE(py_class->tp_bases);
8092  int i, ret;
8093 
8094  /* First scan base classes for registerable properties. */
8095  for (i = 0; i < len; i++) {
8096  PyTypeObject *py_superclass = (PyTypeObject *)PyTuple_GET_ITEM(py_class->tp_bases, i);
8097 
8098  /* the rules for using these base classes are not clear,
8099  * 'object' is of course not worth looking into and
8100  * existing subclasses of RNA would cause a lot more dictionary
8101  * looping then is needed (SomeOperator would scan Operator.__dict__)
8102  * which is harmless, but not at all useful.
8103  *
8104  * So only scan base classes which are not subclasses if blender types.
8105  * This best fits having 'mix-in' classes for operators and render engines.
8106  */
8107  if (py_superclass != &PyBaseObject_Type &&
8108  !PyObject_IsSubclass((PyObject *)py_superclass, (PyObject *)&pyrna_struct_Type)) {
8109  ret = pyrna_deferred_register_class_recursive(srna, py_superclass);
8110 
8111  if (ret != 0) {
8112  return ret;
8113  }
8114  }
8115  }
8116 
8117  /* Not register out own properties. */
8118  /* getattr(..., "__dict__") returns a proxy. */
8119  return pyrna_deferred_register_props(srna, py_class->tp_dict);
8120 }
8121 
8122 int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
8123 {
8124  /* Panels and Menus don't need this
8125  * save some time and skip the checks here */
8126  if (!RNA_struct_idprops_register_check(srna)) {
8127  return 0;
8128  }
8129 
8130 #ifdef USE_POSTPONED_ANNOTATIONS
8131  const bool use_postponed_annotations = true;
8132 #else
8133  const bool use_postponed_annotations = false;
8134 #endif
8135 
8136  if (use_postponed_annotations) {
8137  return pyrna_deferred_register_class_from_type_hints(srna, py_class);
8138  }
8139  return pyrna_deferred_register_class_recursive(srna, py_class);
8140 }
8141 
8142 /*-------------------- Type Registration ------------------------*/
8143 
8144 static int rna_function_arg_count(FunctionRNA *func, int *min_count)
8145 {
8146  const ListBase *lb = RNA_function_defined_parameters(func);
8147  PropertyRNA *parm;
8148  Link *link;
8149  const int flag = RNA_function_flag(func);
8150  const bool is_staticmethod = (flag & FUNC_NO_SELF) && !(flag & FUNC_USE_SELF_TYPE);
8151  int count = is_staticmethod ? 0 : 1;
8152  bool done_min_count = false;
8153 
8154  for (link = lb->first; link; link = link->next) {
8155  parm = (PropertyRNA *)link;
8156  if (!(RNA_parameter_flag(parm) & PARM_OUTPUT)) {
8157  if (!done_min_count && (RNA_parameter_flag(parm) & PARM_PYFUNC_OPTIONAL)) {
8158  /* From now on, the following parameters are optional in a Python function. */
8159  if (min_count) {
8160  *min_count = count;
8161  }
8162  done_min_count = true;
8163  }
8164  count++;
8165  }
8166  }
8167 
8168  if (!done_min_count && min_count) {
8169  *min_count = count;
8170  }
8171  return count;
8172 }
8173 
8175  StructRNA *srna,
8176  void *py_data,
8177  int *have_function)
8178 {
8179  const ListBase *lb;
8180  Link *link;
8181  const char *class_type = RNA_struct_identifier(srna);
8182  StructRNA *srna_base = RNA_struct_base(srna);
8183  PyObject *py_class = (PyObject *)py_data;
8184  PyObject *base_class = RNA_struct_py_type_get(srna);
8185  PyObject *item;
8186  int i, arg_count, func_arg_count, func_arg_min_count = 0;
8187  const char *py_class_name = ((PyTypeObject *)py_class)->tp_name; /* __name__ */
8188 
8189  if (srna_base) {
8190  if (bpy_class_validate_recursive(dummyptr, srna_base, py_data, have_function) != 0) {
8191  return -1;
8192  }
8193  }
8194 
8195  if (base_class) {
8196  if (!PyObject_IsSubclass(py_class, base_class)) {
8197  PyErr_Format(PyExc_TypeError,
8198  "expected %.200s subclass of class \"%.200s\"",
8199  class_type,
8200  py_class_name);
8201  return -1;
8202  }
8203  }
8204 
8205  /* Verify callback functions. */
8206  lb = RNA_struct_type_functions(srna);
8207  i = 0;
8208  for (link = lb->first; link; link = link->next) {
8209  FunctionRNA *func = (FunctionRNA *)link;
8210  const int flag = RNA_function_flag(func);
8211  if (!(flag & FUNC_REGISTER)) {
8212  continue;
8213  }
8214 
8215  item = PyObject_GetAttrString(py_class, RNA_function_identifier(func));
8216  have_function[i] = (item != NULL);
8217  i++;
8218 
8219  if (item == NULL) {
8220  if ((flag & (FUNC_REGISTER_OPTIONAL & ~FUNC_REGISTER)) == 0) {
8221  PyErr_Format(PyExc_AttributeError,
8222  "expected %.200s, %.200s class to have an \"%.200s\" attribute",
8223  class_type,
8224  py_class_name,
8225  RNA_function_identifier(func));
8226  return -1;
8227  }
8228  PyErr_Clear();
8229 
8230  continue;
8231  }
8232 
8233  /* TODO(campbell): this is used for classmethod's too,
8234  * even though class methods should have 'FUNC_USE_SELF_TYPE' set, see Operator.poll for eg.
8235  * Keep this as-is since it's working, but we should be using
8236  * 'FUNC_USE_SELF_TYPE' for many functions. */
8237  const bool is_staticmethod = (flag & FUNC_NO_SELF) && !(flag & FUNC_USE_SELF_TYPE);
8238 
8239  /* Store original so we can decrement its reference before returning. */
8240  PyObject *item_orig = item;
8241 
8242  if (is_staticmethod) {
8243  if (PyMethod_Check(item) == 0) {
8244  PyErr_Format(PyExc_TypeError,
8245  "expected %.200s, %.200s class \"%.200s\" "
8246  "attribute to be a static/class method, not a %.200s",
8247  class_type,
8248  py_class_name,
8250  Py_TYPE(item)->tp_name);
8251  Py_DECREF(item_orig);
8252  return -1;
8253  }
8254  item = ((PyMethodObject *)item)->im_func;
8255  }
8256  else {
8257  if (PyFunction_Check(item) == 0) {
8258  PyErr_Format(PyExc_TypeError,
8259  "expected %.200s, %.200s class \"%.200s\" "
8260  "attribute to be a function, not a %.200s",
8261  class_type,
8262  py_class_name,
8264  Py_TYPE(item)->tp_name);
8265  Py_DECREF(item_orig);
8266  return -1;
8267  }
8268  }
8269 
8270  func_arg_count = rna_function_arg_count(func, &func_arg_min_count);
8271 
8272  if (func_arg_count >= 0) { /* -1 if we don't care. */
8273  arg_count = ((PyCodeObject *)PyFunction_GET_CODE(item))->co_argcount;
8274 
8275  /* note, the number of args we check for and the number of args we give to
8276  * '@staticmethods' are different (quirk of Python),
8277  * this is why rna_function_arg_count() doesn't return the value -1*/
8278  if (is_staticmethod) {
8279  func_arg_count++;
8280  func_arg_min_count++;
8281  }
8282 
8283  if (arg_count < func_arg_min_count || arg_count > func_arg_count) {
8284  if (func_arg_min_count != func_arg_count) {
8285  PyErr_Format(
8286  PyExc_ValueError,
8287  "expected %.200s, %.200s class \"%.200s\" function to have between %d and %d "
8288  "args, found %d",
8289  class_type,
8290  py_class_name,
8292  func_arg_count,
8293  func_arg_min_count,
8294  arg_count);
8295  }
8296  else {
8297  PyErr_Format(
8298  PyExc_ValueError,
8299  "expected %.200s, %.200s class \"%.200s\" function to have %d args, found %d",
8300  class_type,
8301  py_class_name,
8303  func_arg_count,
8304  arg_count);
8305  }
8306  Py_DECREF(item_orig);
8307  return -1;
8308  }
8309  }
8310  Py_DECREF(item_orig);
8311  }
8312 
8313  /* Verify properties. */
8314  lb = RNA_struct_type_properties(srna);
8315  for (link = lb->first; link; link = link->next) {
8316  const char *identifier;
8317  PropertyRNA *prop = (PropertyRNA *)link;
8318  const int flag = RNA_property_flag(prop);
8319 
8320  if (!(flag & PROP_REGISTER)) {
8321  continue;
8322  }
8323 
8324  /* TODO(campbell): Use Python3.7x _PyObject_LookupAttr(), also in the macro below. */
8325  identifier = RNA_property_identifier(prop);
8326  item = PyObject_GetAttrString(py_class, identifier);
8327 
8328  if (item == NULL) {
8329  PyErr_Clear();
8330  /* Sneaky workaround to use the class name as the bl_idname. */
8331 
8332 #define BPY_REPLACEMENT_STRING(rna_attr, py_attr) \
8333  else if (STREQ(identifier, rna_attr)) \
8334  { \
8335  if ((item = PyObject_GetAttr(py_class, py_attr))) { \
8336  if (item != Py_None) { \
8337  if (pyrna_py_to_prop(dummyptr, prop, NULL, item, "validating class:") != 0) { \
8338  Py_DECREF(item); \
8339  return -1; \
8340  } \
8341  } \
8342  Py_DECREF(item); \
8343  } \
8344  else { \
8345  PyErr_Clear(); \
8346  } \
8347  } /* Intentionally allow else here. */
8348 
8349  if (false) {
8350  } /* Needed for macro. */
8353 
8354 #undef BPY_REPLACEMENT_STRING
8355 
8356  if (item == NULL && (((flag & PROP_REGISTER_OPTIONAL) != PROP_REGISTER_OPTIONAL))) {
8357  PyErr_Format(PyExc_AttributeError,
8358  "expected %.200s, %.200s class to have an \"%.200s\" attribute",
8359  class_type,
8360  py_class_name,
8361  identifier);
8362  return -1;
8363  }
8364 
8365  PyErr_Clear();
8366  }
8367  else {
8368  if (pyrna_py_to_prop(dummyptr, prop, NULL, item, "validating class:") != 0) {
8369  Py_DECREF(item);
8370  return -1;
8371  }
8372  Py_DECREF(item);
8373  }
8374  }
8375 
8376  return 0;
8377 }
8378 
8379 static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_function)
8380 {
8381  return bpy_class_validate_recursive(dummyptr, dummyptr->type, py_data, have_function);
8382 }
8383 
8384 /* TODO - multiple return values like with RNA functions. */
8386 {
8387  PyObject *args;
8388  PyObject *ret = NULL, *py_srna = NULL, *py_class_instance = NULL, *parmitem;
8389  PyTypeObject *py_class;
8390  PropertyRNA *parm;
8391  ParameterIterator iter;
8392  PointerRNA funcptr;
8393  int err = 0, i, ret_len = 0, arg_count;
8394  const int flag = RNA_function_flag(func);
8395  const bool is_staticmethod = (flag & FUNC_NO_SELF) && !(flag & FUNC_USE_SELF_TYPE);
8396  const bool is_classmethod = (flag & FUNC_NO_SELF) && (flag & FUNC_USE_SELF_TYPE);
8397 
8398  PropertyRNA *pret_single = NULL;
8399  void *retdata_single = NULL;
8400 
8401  PyGILState_STATE gilstate;
8402 
8403 #ifdef USE_PEDANTIC_WRITE
8404  const bool is_readonly_init = !(RNA_struct_is_a(ptr->type, &RNA_Operator) ||
8406  // const char *func_id = RNA_function_identifier(func); /* UNUSED */
8407  /* Testing, for correctness, not operator and not draw function. */
8408  const bool is_readonly = !(RNA_function_flag(func) & FUNC_ALLOW_WRITE);
8409 #endif
8410 
8411  py_class = RNA_struct_py_type_get(ptr->type);
8412  /* Rare case. can happen when registering subclasses. */
8413  if (py_class == NULL) {
8415  "unable to get Python class for RNA struct '%.200s'",
8417  return -1;
8418  }
8419 
8420  /* XXX, this is needed because render engine calls without a context
8421  * this should be supported at some point, but at the moment it's not! */
8422  if (C == NULL) {
8423  C = BPY_context_get();
8424  }
8425 
8426  /* Annoying! We need to check if the screen gets set to NULL which is a
8427  * hint that the file was actually re-loaded. */
8428  const bool is_valid_wm = (CTX_wm_manager(C) != NULL);
8429 
8430  bpy_context_set(C, &gilstate);
8431 
8432  if (!(is_staticmethod || is_classmethod)) {
8433  /* Some datatypes (operator, render engine) can store PyObjects for re-use. */
8434  if (ptr->data) {
8435  void **instance = RNA_struct_instance(ptr);
8436 
8437  if (instance) {
8438  if (*instance) {
8439  py_class_instance = *instance;
8440  Py_INCREF(py_class_instance);
8441  }
8442  }
8443  }
8444  /* End exception. */
8445 
8446  if (py_class_instance == NULL) {
8447  py_srna = pyrna_struct_CreatePyObject(ptr);
8448  }
8449 
8450  if (py_class_instance) {
8451  /* Special case, instance is cached. */
8452  }
8453  else if (py_srna == NULL) {
8454  py_class_instance = NULL;
8455  }
8456  else if (py_srna == Py_None) { /* Probably wont ever happen, but possible. */
8457  Py_DECREF(py_srna);
8458  py_class_instance = NULL;
8459  }
8460  else {
8461 #if 1
8462  /* Skip the code below and call init directly on the allocated 'py_srna'
8463  * otherwise __init__() always needs to take a second self argument, see pyrna_struct_new().
8464  * Although this is annoying to have to implement a part of Python's
8465  * typeobject.c:type_call().
8466  */
8467  if (py_class->tp_init) {
8468 # ifdef USE_PEDANTIC_WRITE
8469  const int prev_write = rna_disallow_writes;
8470  rna_disallow_writes = is_readonly_init ? false :
8471  true; /* Only operators can write on __init__. */
8472 # endif
8473 
8474  /* True in most cases even when the class itself doesn't define an __init__ function. */
8475  args = PyTuple_New(0);
8476  if (py_class->tp_init(py_srna, args, NULL) < 0) {
8477  Py_DECREF(py_srna);
8478  py_srna = NULL;
8479  /* Err set below. */
8480  }
8481  Py_DECREF(args);
8482 # ifdef USE_PEDANTIC_WRITE
8483  rna_disallow_writes = prev_write;
8484 # endif
8485  }
8486  py_class_instance = py_srna;
8487 
8488 #else
8489  const int prev_write = rna_disallow_writes;
8490  rna_disallow_writes = true;
8491 
8492  /* 'almost' all the time calling the class isn't needed.
8493  * We could just do... */
8494 # if 0
8495  py_class_instance = py_srna;
8496  Py_INCREF(py_class_instance);
8497 # endif
8498  /*
8499  * This would work fine, but means __init__ functions wouldn't run.
8500  * None of Blender's default scripts use __init__ but it's nice to call it
8501  * for general correctness. just to note why this is here when it could be safely removed.
8502  */
8503  args = PyTuple_New(1);
8504  PyTuple_SET_ITEM(args, 0, py_srna);
8505  py_class_instance = PyObject_Call(py_class, args, NULL);
8506  Py_DECREF(args);
8507 
8508  rna_disallow_writes = prev_write;
8509 
8510 #endif
8511 
8512  if (py_class_instance == NULL) {
8513  err = -1; /* So the error is not overridden below. */
8514  }
8515  }
8516  }
8517 
8518  /* Initializing the class worked, now run its invoke function. */
8519  if (err != -1 && (is_staticmethod || is_classmethod || py_class_instance)) {
8520  PyObject *item = PyObject_GetAttrString((PyObject *)py_class, RNA_function_identifier(func));
8521 
8522  if (item) {
8523  RNA_pointer_create(NULL, &RNA_Function, func, &funcptr);
8524 
8525  if (is_staticmethod) {
8526  arg_count =
8527  ((PyCodeObject *)PyFunction_GET_CODE(((PyMethodObject *)item)->im_func))->co_argcount -
8528  1;
8529  }
8530  else {
8531  arg_count = ((PyCodeObject *)PyFunction_GET_CODE(item))->co_argcount;
8532  }
8533 #if 0
8534  /* First arg is included in 'item'. */
8535  args = PyTuple_New(rna_function_arg_count(func));
8536 #endif
8537  args = PyTuple_New(arg_count); /* First arg is included in 'item'. */
8538 
8539  if (is_staticmethod) {
8540  i = 0;
8541  }
8542  else if (is_classmethod) {
8543  PyTuple_SET_ITEM(args, 0, (PyObject *)py_class);
8544  i = 1;
8545  }
8546  else {
8547  PyTuple_SET_ITEM(args, 0, py_class_instance);
8548  i = 1;
8549  }
8550 
8551  RNA_parameter_list_begin(parms, &iter);
8552 
8553  /* Parse function parameters. */
8554  for (; iter.valid; RNA_parameter_list_next(&iter)) {
8555  parm = iter.parm;
8556 
8557  /* Only useful for single argument returns, we'll need another list loop for multiple. */
8558  if (RNA_parameter_flag(parm) & PARM_OUTPUT) {
8559  ret_len++;
8560  if (pret_single == NULL) {
8561  pret_single = parm;
8562  retdata_single = iter.data;
8563  }
8564 
8565  continue;
8566  }
8567 
8568  if (i < arg_count) {
8569  parmitem = pyrna_param_to_py(&funcptr, parm, iter.data);
8570  PyTuple_SET_ITEM(args, i, parmitem);
8571  i++;
8572  }
8573  }
8574 
8575 #ifdef USE_PEDANTIC_WRITE
8576  rna_disallow_writes = is_readonly ? true : false;
8577 #endif
8578  /* *** Main Caller *** */
8579 
8580  ret = PyObject_Call(item, args, NULL);
8581 
8582  /* *** Done Calling *** */
8583 
8584 #ifdef USE_PEDANTIC_WRITE
8585  rna_disallow_writes = false;
8586 #endif
8587 
8588  RNA_parameter_list_end(&iter);
8589  Py_DECREF(item);
8590  Py_DECREF(args);
8591  }
8592  else {
8593  PyErr_Print();
8594  PyErr_Clear();
8595  PyErr_Format(PyExc_TypeError,
8596  "could not find function %.200s in %.200s to execute callback",
8599  err = -1;
8600  }
8601  }
8602  else {
8603  /* The error may be already set if the class instance couldn't be created. */
8604  if (err != -1) {
8605  PyErr_Format(PyExc_RuntimeError,
8606  "could not create instance of %.200s to call callback function %.200s",
8608  RNA_function_identifier(func));
8609  err = -1;
8610  }
8611  }
8612 
8613  if (ret == NULL) { /* Covers py_class_instance failing too. */
8614  err = -1;
8615  }
8616  else {
8617  if (ret_len == 0 && ret != Py_None) {
8618  PyErr_Format(PyExc_RuntimeError,
8619  "expected class %.200s, function %.200s to return None, not %.200s",
8622  Py_TYPE(ret)->tp_name);
8623  err = -1;
8624  }
8625  else if (ret_len == 1) {
8626  err = pyrna_py_to_prop(&funcptr, pret_single, retdata_single, ret, "");
8627 
8628  /* when calling operator funcs only gives Function.result with
8629  * no line number since the func has finished calling on error,
8630  * re-raise the exception with more info since it would be slow to
8631  * create prefix on every call (when there are no errors) */
8632  if (err == -1) {
8633  PyC_Err_Format_Prefix(PyExc_RuntimeError,
8634  "class %.200s, function %.200s: incompatible return value ",
8636  RNA_function_identifier(func));
8637  }
8638  }
8639  else if (ret_len > 1) {
8640 
8641  if (PyTuple_Check(ret) == 0) {
8642  PyErr_Format(
8643  PyExc_RuntimeError,
8644  "expected class %.200s, function %.200s to return a tuple of size %d, not %.200s",
8647  ret_len,
8648  Py_TYPE(ret)->tp_name);
8649  err = -1;
8650  }
8651  else if (PyTuple_GET_SIZE(ret) != ret_len) {
8652  PyErr_Format(PyExc_RuntimeError,
8653  "class %.200s, function %.200s to returned %d items, expected %d",
8656  PyTuple_GET_SIZE(ret),
8657  ret_len);
8658  err = -1;
8659  }
8660  else {
8661 
8662  RNA_parameter_list_begin(parms, &iter);
8663 
8664  /* Parse function parameters. */
8665  for (i = 0; iter.valid; RNA_parameter_list_next(&iter)) {
8666  parm = iter.parm;
8667 
8668  /* Only useful for single argument returns, we'll need another list loop for multiple. */
8669  if (RNA_parameter_flag(parm) & PARM_OUTPUT) {
8671  &funcptr, parm, iter.data, PyTuple_GET_ITEM(ret, i++), "calling class function:");
8672  if (err) {
8673  break;
8674  }
8675  }
8676  }
8677 
8678  RNA_parameter_list_end(&iter);
8679  }
8680  }
8681  Py_DECREF(ret);
8682  }
8683 
8684  if (err != 0) {
8685  ReportList *reports;
8686  /* Alert the user, else they wont know unless they see the console. */
8687  if ((!is_staticmethod) && (!is_classmethod) && (ptr->data) &&
8689  (is_valid_wm == (CTX_wm_manager(C) != NULL))) {
8690  wmOperator *op = ptr->data;
8691  reports = op->reports;
8692  }
8693  else {
8694  /* Wont alert users, but they can view in 'info' space. */
8695  reports = CTX_wm_reports(C);
8696  }
8697 
8698  BPy_errors_to_report(reports);
8699 
8700  /* Also print in the console for Python. */
8701  PyErr_Print();
8702  PyErr_Clear();
8703  }
8704 
8705  bpy_context_clear(C, &gilstate);
8706 
8707  return err;
8708 }
8709 
8710 static void bpy_class_free(void *pyob_ptr)
8711 {
8712  PyObject *self = (PyObject *)pyob_ptr;
8713  PyGILState_STATE gilstate;
8714 
8715  gilstate = PyGILState_Ensure();
8716 
8717  /* Breaks re-registering classes. */
8718  // PyDict_Clear(((PyTypeObject *)self)->tp_dict);
8719 
8720  /* Remove the RNA attribute instead. */
8721  PyDict_DelItem(((PyTypeObject *)self)->tp_dict, bpy_intern_str_bl_rna);
8722  if (PyErr_Occurred()) {
8723  PyErr_Clear();
8724  }
8725 
8726 #if 0 /* Needs further investigation, too annoying so quiet for now. */
8727  if (G.debug & G_DEBUG_PYTHON) {
8728  if (self->ob_refcnt > 1) {
8729  PyC_ObSpit("zombie class - reference should be 1", self);
8730  }
8731  }
8732 #endif
8733  Py_DECREF((PyObject *)pyob_ptr);
8734 
8735  PyGILState_Release(gilstate);
8736 }
8737 
8744 {
8745 #ifdef DEBUG
8746  PyGILState_STATE gilstate;
8747 
8748  PointerRNA ptr;
8749  PropertyRNA *prop;
8750 
8751  gilstate = PyGILState_Ensure();
8752 
8753  /* Avoid doing this lookup for every getattr. */
8755  prop = RNA_struct_find_property(&ptr, "structs");
8756 
8757  RNA_PROP_BEGIN (&ptr, itemptr, prop) {
8758  PyObject *item = pyrna_struct_Subtype(&itemptr);
8759  if (item == NULL) {
8760  if (PyErr_Occurred()) {
8761  PyErr_Print();
8762  PyErr_Clear();
8763  }
8764  }
8765  else {
8766  Py_DECREF(item);
8767  }
8768  }
8769  RNA_PROP_END;
8770 
8771  PyGILState_Release(gilstate);
8772 #endif /* DEBUG */
8773 }
8774 
8776 {
8777  PointerRNA ptr;
8778  PropertyRNA *prop;
8779 
8780  /* Avoid doing this lookup for every getattr. */
8782  prop = RNA_struct_find_property(&ptr, "structs");
8783 
8784  RNA_PROP_BEGIN (&ptr, itemptr, prop) {
8785  StructRNA *srna = srna_from_ptr(&itemptr);
8786  void *py_ptr = RNA_struct_py_type_get(srna);
8787 
8788  if (py_ptr) {
8789 #if 0 /* XXX - should be able to do this, but makes Python crash on exit. */
8790  bpy_class_free(py_ptr);
8791 #endif
8793  }
8794  }
8795  RNA_PROP_END;
8796 }
8797 
8809 PyDoc_STRVAR(pyrna_register_class_doc,
8810  ".. method:: register_class(cls)\n"
8811  "\n"
8812  " Register a subclass of a Blender type class.\n"
8813  "\n"
8814  " :arg cls: Blender type class in:\n"
8815  " :class:`bpy.types.Panel`, :class:`bpy.types.UIList`,\n"
8816  " :class:`bpy.types.Menu`, :class:`bpy.types.Header`,\n"
8817  " :class:`bpy.types.Operator`, :class:`bpy.types.KeyingSetInfo`,\n"
8818  " :class:`bpy.types.RenderEngine`\n"
8819  " :type cls: class\n"
8820  " :raises ValueError:\n"
8821  " if the class is not a subclass of a registerable blender class.\n"
8822  "\n"
8823  " .. note::\n"
8824  "\n"
8825  " If the class has a *register* class method it will be called\n"
8826  " before registration.\n");
8828  "register_class", pyrna_register_class, METH_O, pyrna_register_class_doc};
8829 static PyObject *pyrna_register_class(PyObject *UNUSED(self), PyObject *py_class)
8830 {
8831  bContext *C = NULL;
8832  ReportList reports;
8833  StructRegisterFunc reg;
8834  StructRNA *srna;
8835  StructRNA *srna_new;
8836  const char *identifier;
8837  PyObject *py_cls_meth;
8838  const char *error_prefix = "register_class(...):";
8839 
8840  if (!PyType_Check(py_class)) {
8841  PyErr_Format(PyExc_ValueError,
8842  "register_class(...): "
8843  "expected a class argument, not '%.200s'",
8844  Py_TYPE(py_class)->tp_name);
8845  return NULL;
8846  }
8847 
8848  if (PyDict_GetItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna)) {
8849  PyErr_Format(PyExc_ValueError,
8850  "register_class(...): "
8851  "already registered as a subclass '%.200s'",
8852  ((PyTypeObject *)py_class)->tp_name);
8853  return NULL;
8854  }
8855 
8856  if (!pyrna_write_check()) {
8857  PyErr_Format(PyExc_RuntimeError,
8858  "register_class(...): "
8859  "can't run in readonly state '%.200s'",
8860  ((PyTypeObject *)py_class)->tp_name);
8861  return NULL;
8862  }
8863 
8864  /* Warning: gets parent classes srna, only for the register function. */
8865  srna = pyrna_struct_as_srna(py_class, true, "register_class(...):");
8866  if (srna == NULL) {
8867  return NULL;
8868  }
8869 
8870  /* Fails in some cases, so can't use this check, but would like to :| */
8871 #if 0
8872  if (RNA_struct_py_type_get(srna)) {
8873  PyErr_Format(PyExc_ValueError,
8874  "register_class(...): %.200s's parent class %.200s is already registered, this "
8875  "is not allowed",
8876  ((PyTypeObject *)py_class)->tp_name,
8877  RNA_struct_identifier(srna));
8878  return NULL;
8879  }
8880 #endif
8881 
8882  /* Check that we have a register callback for this type. */
8883  reg = RNA_struct_register(srna);
8884 
8885  if (!reg) {
8886  PyErr_Format(PyExc_ValueError,
8887  "register_class(...): expected a subclass of a registerable "
8888  "RNA type (%.200s does not support registration)",
8889  RNA_struct_identifier(srna));
8890  return NULL;
8891  }
8892 
8893  /* Get the context, so register callback can do necessary refreshes. */
8894  C = BPY_context_get();
8895 
8896  /* Call the register callback with reports & identifier. */
8897  BKE_reports_init(&reports, RPT_STORE);
8898 
8899  identifier = ((PyTypeObject *)py_class)->tp_name;
8900 
8901  srna_new = reg(CTX_data_main(C),
8902  &reports,
8903  py_class,
8904  identifier,
8907  bpy_class_free);
8908 
8909  if (!BLI_listbase_is_empty(&reports.list)) {
8910  const bool has_error = BPy_reports_to_error(&reports, PyExc_RuntimeError, false);
8911  if (!has_error) {
8912  BPy_reports_write_stdout(&reports, error_prefix);
8913  }
8914  BKE_reports_clear(&reports);
8915  if (has_error) {
8916  return NULL;
8917  }
8918  }
8919 
8920  /* Python errors validating are not converted into reports so the check above will fail.
8921  * the cause for returning NULL will be printed as an error */
8922  if (srna_new == NULL) {
8923  return NULL;
8924  }
8925 
8926  /* Takes a reference to 'py_class'. */
8927  pyrna_subtype_set_rna(py_class, srna_new);
8928 
8929  /* Old srna still references us, keep the check in case registering somehow can free it. */
8930  if (RNA_struct_py_type_get(srna)) {
8932 #if 0
8933  /* Should be able to do this XXX since the old RNA adds a new ref. */
8934  Py_DECREF(py_class);
8935 #endif
8936  }
8937 
8938  /* Can't use this because it returns a dict proxy
8939  *
8940  * item = PyObject_GetAttrString(py_class, "__dict__");
8941  */
8942  if (pyrna_deferred_register_class(srna_new, (PyTypeObject *)py_class) != 0) {
8943  return NULL;
8944  }
8945 
8946  /* Call classed register method.
8947  * Note that zero falls through, no attribute, no error. */
8948  switch (_PyObject_LookupAttr(py_class, bpy_intern_str_register, &py_cls_meth)) {
8949  case 1: {
8950  PyObject *ret = PyObject_CallObject(py_cls_meth, NULL);
8951  Py_DECREF(py_cls_meth);
8952  if (ret) {
8953  Py_DECREF(ret);
8954  }
8955  else {
8956  return NULL;
8957  }
8958  break;
8959  }
8960  case -1: {
8961  return NULL;
8962  }
8963  }
8964 
8965  Py_RETURN_NONE;
8966 }
8967 
8969  StructRNA *srna,
8970  const char **r_prop_identifier)
8971 {
8972  PropertyRNA *prop;
8973  LinkData *link;
8974 
8975  /* Verify properties. */
8976  const ListBase *lb = RNA_struct_type_properties(srna);
8977 
8978  for (link = lb->first; link; link = link->next) {
8979  prop = (PropertyRNA *)link;
8981  PointerRNA tptr;
8982  RNA_pointer_create(NULL, &RNA_Struct, srna_props, &tptr);
8983 
8984  if (RNA_property_pointer_type(&tptr, prop) == srna) {
8985  *r_prop_identifier = RNA_property_identifier(prop);
8986  return 1;
8987  }
8988  }
8989  }
8990 
8991  return 0;
8992 }
8993 
8994 PyDoc_STRVAR(pyrna_unregister_class_doc,
8995  ".. method:: unregister_class(cls)\n"
8996  "\n"
8997  " Unload the Python class from blender.\n"
8998  "\n"
8999  " If the class has an *unregister* class method it will be called\n"
9000  " before unregistering.\n");
9002  "unregister_class",
9004  METH_O,
9005  pyrna_unregister_class_doc,
9006 };
9007 static PyObject *pyrna_unregister_class(PyObject *UNUSED(self), PyObject *py_class)
9008 {
9009  bContext *C = NULL;
9010  StructUnregisterFunc unreg;
9011  StructRNA *srna;
9012  PyObject *py_cls_meth;
9013 
9014  if (!PyType_Check(py_class)) {
9015  PyErr_Format(PyExc_ValueError,
9016  "register_class(...): "
9017  "expected a class argument, not '%.200s'",
9018  Py_TYPE(py_class)->tp_name);
9019  return NULL;
9020  }
9021 
9022 #if 0
9023  if (PyDict_GetItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna) == NULL) {
9024  PWM_cursor_wait(false);
9025  PyErr_SetString(PyExc_ValueError, "unregister_class(): not a registered as a subclass");
9026  return NULL;
9027  }
9028 #endif
9029 
9030  if (!pyrna_write_check()) {
9031  PyErr_Format(PyExc_RuntimeError,
9032  "unregister_class(...): "
9033  "can't run in readonly state '%.200s'",
9034  ((PyTypeObject *)py_class)->tp_name);
9035  return NULL;
9036  }
9037 
9038  srna = pyrna_struct_as_srna(py_class, false, "unregister_class(...):");
9039  if (srna == NULL) {
9040  return NULL;
9041  }
9042 
9043  /* Check that we have a unregister callback for this type. */
9044  unreg = RNA_struct_unregister(srna);
9045 
9046  if (!unreg) {
9047  PyErr_SetString(
9048  PyExc_ValueError,
9049  "unregister_class(...): "
9050  "expected a Type subclassed from a registerable RNA type (no unregister supported)");
9051  return NULL;
9052  }
9053 
9054  /* Call classed unregister method.
9055  * Note that zero falls through, no attribute, no error. */
9056  switch (_PyObject_LookupAttr(py_class, bpy_intern_str_unregister, &py_cls_meth)) {
9057  case 1: {
9058  PyObject *ret = PyObject_CallObject(py_cls_meth, NULL);
9059  Py_DECREF(py_cls_meth);
9060  if (ret) {
9061  Py_DECREF(ret);
9062  }
9063  else {
9064  return NULL;
9065  }
9066  break;
9067  }
9068  case -1: {
9069  return NULL;
9070  }
9071  }
9072 
9073  /* Should happen all the time, however it's very slow. */
9074  if (G.debug & G_DEBUG_PYTHON) {
9075  /* Remove all properties using this class. */
9076  StructRNA *srna_iter;
9077  PointerRNA ptr_rna;
9078  PropertyRNA *prop_rna;
9079  const char *prop_identifier = NULL;
9080 
9082  prop_rna = RNA_struct_find_property(&ptr_rna, "structs");
9083 
9084  /* Loop over all structs. */
9085  RNA_PROP_BEGIN (&ptr_rna, itemptr, prop_rna) {
9086  srna_iter = itemptr.data;
9087  if (pyrna_srna_contains_pointer_prop_srna(srna_iter, srna, &prop_identifier)) {
9088  break;
9089  }
9090  }
9091  RNA_PROP_END;
9092 
9093  if (prop_identifier) {
9094  PyErr_Format(PyExc_RuntimeError,
9095  "unregister_class(...): can't unregister %s because %s.%s pointer property is "
9096  "using this",
9097  RNA_struct_identifier(srna),
9098  RNA_struct_identifier(srna_iter),
9099  prop_identifier);
9100  return NULL;
9101  }
9102  }
9103 
9104  /* Get the context, so register callback can do necessary refreshes. */
9105  C = BPY_context_get();
9106 
9107  /* Call unregister. */
9108  unreg(CTX_data_main(C), srna); /* Calls bpy_class_free, this decref's py_class. */
9109 
9110  PyDict_DelItem(((PyTypeObject *)py_class)->tp_dict, bpy_intern_str_bl_rna);
9111  if (PyErr_Occurred()) {
9112  PyErr_Clear(); // return NULL;
9113  }
9114 
9115  Py_RETURN_NONE;
9116 }
9117 
9122  struct PyMethodDef *method,
9123  struct PyGetSetDef *getset)
9124 {
9125  /* See 'add_methods' in Python's 'typeobject.c'. */
9126  PyTypeObject *type = (PyTypeObject *)pyrna_srna_Subtype(srna);
9127  PyObject *dict = type->tp_dict;
9128  if (method != NULL) {
9129  for (; method->ml_name != NULL; method++) {
9130  PyObject *py_method;
9131 
9132  if (method->ml_flags & METH_CLASS) {
9133  PyObject *cfunc = PyCFunction_New(method, (PyObject *)type);
9134  py_method = PyClassMethod_New(cfunc);
9135  Py_DECREF(cfunc);
9136  }
9137  else if (method->ml_flags & METH_STATIC) {
9138  py_method = PyCFunction_New(method, NULL);
9139  }
9140  else {
9141  py_method = PyDescr_NewMethod(type, method);
9142  }
9143 
9144  const int err = PyDict_SetItemString(dict, method->ml_name, py_method);
9145  Py_DECREF(py_method);
9146  BLI_assert(!(err < 0));
9148  }
9149  }
9150 
9151  if (getset != NULL) {
9152  for (; getset->name != NULL; getset++) {
9153  PyObject *descr = PyDescr_NewGetSet(type, getset);
9154  /* Ensure we're not overwriting anything that already exists. */
9155  BLI_assert(PyDict_GetItem(dict, PyDescr_NAME(descr)) == NULL);
9156  PyDict_SetItem(dict, PyDescr_NAME(descr), descr);
9157  Py_DECREF(descr);
9158  }
9159  }
9160  Py_DECREF(type);
9161 }
9162 
9163 /* Access to 'owner_id' internal global. */
9164 
9165 static PyObject *pyrna_bl_owner_id_get(PyObject *UNUSED(self))
9166 {
9167  const char *name = RNA_struct_state_owner_get();
9168  if (name) {
9169  return PyUnicode_FromString(name);
9170  }
9171  Py_RETURN_NONE;
9172 }
9173 
9174 static PyObject *pyrna_bl_owner_id_set(PyObject *UNUSED(self), PyObject *value)
9175 {
9176  const char *name;
9177  if (value == Py_None) {
9178  name = NULL;
9179  }
9180  else if (PyUnicode_Check(value)) {
9181  name = PyUnicode_AsUTF8(value);
9182  }
9183  else {
9184  PyErr_Format(PyExc_ValueError,
9185  "owner_set(...): "
9186  "expected None or a string, not '%.200s'",
9187  Py_TYPE(value)->tp_name);
9188  return NULL;
9189  }
9191  Py_RETURN_NONE;
9192 }
9193 
9194 PyMethodDef meth_bpy_owner_id_get = {
9195  "_bl_owner_id_get",
9196  (PyCFunction)pyrna_bl_owner_id_get,
9197  METH_NOARGS,
9198  NULL,
9199 };
9200 PyMethodDef meth_bpy_owner_id_set = {
9201  "_bl_owner_id_set",
9202  (PyCFunction)pyrna_bl_owner_id_set,
9203  METH_O,
9204  NULL,
9205 };
typedef float(TangentPoint)[2]
int CTX_data_get(const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb, short *r_type)
Definition: context.c:498
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
@ CTX_DATA_TYPE_POINTER
Definition: BKE_context.h:220
@ CTX_DATA_TYPE_COLLECTION
Definition: BKE_context.h:221
ListBase CTX_data_dir_get(const bContext *C)
Definition: context.c:618
eContextResult
Definition: BKE_context.h:81
@ CTX_RESULT_OK
Definition: BKE_context.h:83
@ CTX_RESULT_NO_DATA
Definition: BKE_context.h:90
struct ReportList * CTX_wm_reports(const bContext *C)
Definition: context.c:751
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
#define G_MAIN
Definition: BKE_global.h:232
@ G_DEBUG_PYTHON
Definition: BKE_global.h:135
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
struct IDProperty * IDP_GetPropertyFromGroup(const struct IDProperty *prop, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void IDP_FreeFromGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL()
Definition: idprop.c:690
const char * BKE_idtype_idcode_to_name_plural(const short idcode)
Definition: idtype.c:182
const char * BKE_idtype_idcode_to_name(const short idcode)
Definition: idtype.c:168
struct bNodeType * nodeTypeFind(const char *idname)
Definition: node.cc:1254
void BKE_reports_clear(ReportList *reports)
Definition: report.c:84
void BKE_reports_init(ReportList *reports, int flag)
Definition: report.c:66
#define BLI_assert_unreachable()
Definition: BLI_assert.h:96
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:78
#define BLI_BITMAP_NEW(_tot, _alloc_string)
Definition: BLI_bitmap.h:50
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32
#define ATTR_FALLTHROUGH
A dynamically sized string ADT.
DynStr * BLI_dynstr_new(void) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_dynstr.c:71
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
Definition: BLI_dynstr.c:358
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
char * BLI_dynstr_get_cstring(DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: BLI_dynstr.c:323
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:146
void BLI_ghashIterator_step(GHashIterator *ghi)
Definition: BLI_ghash.c:1086
BLI_INLINE bool BLI_ghashIterator_done(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:158
unsigned int BLI_ghash_len(GHash *gh) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:744
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:900
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
GHash * BLI_ghash_ptr_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh)
Definition: BLI_ghash.c:1065
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
@ EULER_ORDER_XYZ
@ EULER_ORDER_ZYX
#define STR_ELEM(...)
Definition: BLI_string.h:218
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
unsigned short ushort
Definition: BLI_sys_types.h:84
#define ARRAY_SIZE(arr)
#define STREQLEN(a, b, n)
#define STRINGIFY(x)
#define UNUSED_VARS_NDEBUG(...)
#define UNUSED(x)
#define UNLIKELY(x)
#define ELEM(...)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
struct CLG_LogRef * BPY_LOG_RNA
typedef double(DMatrix)[4][4]
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:204
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:203
#define CLOG_INFO(clg_ref, level,...)
Definition: CLG_log.h:201
struct ID * DEG_get_original_id(struct ID *id)
@ LIB_TAG_TEMP_MAIN
Definition: DNA_ID.h:585
@ LIB_EMBEDDED_DATA
Definition: DNA_ID.h:482
@ ID_WM
Definition: DNA_ID_enums.h:84
@ ID_WS
Definition: DNA_ID_enums.h:91
@ ID_SCR
Definition: DNA_ID_enums.h:72
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint order
Read Guarded memory(de)allocation.
Group RGB to Bright Vector Camera CLAMP
#define RNA_PROP_END
Definition: RNA_access.h:1268
StructRNA RNA_Property
#define RNA_STRUCT_BEGIN(sptr, prop)
Definition: RNA_access.h:1274
StructRNA RNA_Node
StructRNA RNA_Struct
#define RNA_STRUCT_END
Definition: RNA_access.h:1294
StructRNA RNA_Function
#define RNA_POINTER_INVALIDATE(ptr)
Definition: RNA_access.h:1425
StructRNA RNA_Context
#define RNA_PROP_BEGIN(sptr, itemptr, prop)
Definition: RNA_access.h:1261
StructRNA RNA_GizmoProperties
StructRNA RNA_Gizmo
StructRNA RNA_OperatorProperties
StructRNA RNA_AnyType
StructRNA RNA_Operator
StructRNA RNA_BlendData
int RNA_def_property_free_identifier(StructOrFunctionRNA *cont_, const char *identifier)
@ PARM_RNAPTR
Definition: RNA_types.h:339
@ PARM_PYFUNC_OPTIONAL
Definition: RNA_types.h:347
@ PARM_REQUIRED
Definition: RNA_types.h:337
@ PARM_OUTPUT
Definition: RNA_types.h:338
struct StructRNA *(* StructRegisterFunc)(struct Main *bmain, struct ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
Definition: RNA_types.h:653
@ FUNC_USE_SELF_TYPE
Definition: RNA_types.h:573
@ FUNC_NO_SELF
Definition: RNA_types.h:571
@ FUNC_REGISTER
Definition: RNA_types.h:585
@ FUNC_REGISTER_OPTIONAL
Definition: RNA_types.h:587
@ FUNC_ALLOW_WRITE
Definition: RNA_types.h:593
#define RNA_ENUM_BITFLAG_SIZE
Definition: RNA_types.h:102
PropertyType
Definition: RNA_types.h:72
@ PROP_FLOAT
Definition: RNA_types.h:75
@ PROP_BOOLEAN
Definition: RNA_types.h:73
@ PROP_ENUM
Definition: RNA_types.h:77
@ PROP_INT
Definition: RNA_types.h:74
@ PROP_STRING
Definition: RNA_types.h:76
@ PROP_POINTER
Definition: RNA_types.h:78
@ PROP_COLLECTION
Definition: RNA_types.h:79
void(* StructUnregisterFunc)(struct Main *bmain, struct StructRNA *type)
Definition: RNA_types.h:661
RawPropertyType
Definition: RNA_types.h:416
@ PROP_RAW_INT
Definition: RNA_types.h:418
@ PROP_RAW_UNSET
Definition: RNA_types.h:417
@ PROP_RAW_BOOLEAN
Definition: RNA_types.h:421
@ PROP_RAW_CHAR
Definition: RNA_types.h:420
@ PROP_RAW_FLOAT
Definition: RNA_types.h:423
@ PROP_RAW_DOUBLE
Definition: RNA_types.h:422
@ PROP_RAW_SHORT
Definition: RNA_types.h:419
@ PROP_THICK_WRAP
Definition: RNA_types.h:270
@ PROP_DYNAMIC
Definition: RNA_types.h:275
@ PROP_ENUM_FLAG
Definition: RNA_types.h:251
@ PROP_REGISTER_OPTIONAL
Definition: RNA_types.h:259
@ PROP_NEVER_NULL
Definition: RNA_types.h:225
@ PROP_REGISTER
Definition: RNA_types.h:258
@ PROP_ID_SELF_CHECK
Definition: RNA_types.h:218
@ PROP_HIDDEN
Definition: RNA_types.h:202
@ PROP_IDPROPERTY
Definition: RNA_types.h:273
@ PROP_MATRIX
Definition: RNA_types.h:144
@ PROP_BYTESTRING
Definition: RNA_types.h:120
@ PROP_FILENAME
Definition: RNA_types.h:118
@ PROP_COLOR
Definition: RNA_types.h:139
@ PROP_EULER
Definition: RNA_types.h:145
@ PROP_DIRPATH
Definition: RNA_types.h:117
@ PROP_COLOR_GAMMA
Definition: RNA_types.h:151
@ PROP_UNSIGNED
Definition: RNA_types.h:129
@ PROP_QUATERNION
Definition: RNA_types.h:146
@ PROP_FILEPATH
Definition: RNA_types.h:116
#define C
Definition: RandGen.cpp:39
ATTR_WARN_UNUSED_RESULT const BMVert * v
char * BPy_enum_as_string(const EnumPropertyItem *item)
void BPy_reports_write_stdout(const ReportList *reports, const char *header)
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
bool BPy_errors_to_report(ReportList *reports)
struct bContext * BPY_context_get(void)
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate)
PyObject * self
Definition: bpy_driver.c:185
PyObject * bpy_intern_str_bpy_types
PyObject * bpy_intern_str___name__
PyObject * bpy_intern_str_register
PyObject * bpy_intern_str_bl_rna
PyObject * bpy_intern_str___module__
PyObject * bpy_intern_str___doc__
PyObject * bpy_intern_str_attr
PyObject * bpy_intern_str___annotations__
PyObject * bpy_intern_str_unregister
PyObject * bpy_intern_str___slots__
PyObject * bpy_intern_str_properties
PyObject * BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
Definition: bpy_props.c:3693
PyObject * BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
Definition: bpy_props.c:3587
#define BPy_PropDeferred_CheckTypeExact(v)
Definition: bpy_props.h:42
#define PYRNA_STACK_ARRAY
Definition: bpy_props.h:44
static PyTypeObject pyrna_prop_collection_iter_Type
Definition: bpy_rna.c:7068
static PyObject * pyrna_bl_owner_id_get(PyObject *UNUSED(self))
Definition: bpy_rna.c:9165
PyObject * pyrna_enum_bitfield_to_py(const EnumPropertyItem *items, int value)
Definition: bpy_rna.c:1444
static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
Definition: bpy_rna.c:441
PyObject * pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:7560
int pyrna_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value, const char *error_prefix)
Definition: bpy_rna.c:804
static void pyrna_struct_dealloc(BPy_StructRNA *self)
Definition: bpy_rna.c:1177
static PyObject * bpy_types_module_dir(PyObject *self)
Definition: bpy_rna.c:7776
PyTypeObject pyrna_struct_meta_idprop_Type
Definition: bpy_rna.c:6456
static PyObject * pyrna_srna_PyBase(StructRNA *srna)
Definition: bpy_rna.c:7267
PyTypeObject pyrna_prop_Type
Definition: bpy_rna.c:6633
void pyrna_invalidate(BPy_DummyPointerRNA *self)
Definition: bpy_rna.c:133
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:1537
static PyObject * pyrna_struct_property_overridable_library_set(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3805
void pyrna_struct_type_extend_capi(struct StructRNA *srna, struct PyMethodDef *method, struct PyGetSetDef *getset)
Definition: bpy_rna.c:9121
void BPY_rna_init(void)
Definition: bpy_rna.c:7638
static PyObject * pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
Definition: bpy_rna.c:2945
static int pyrna_struct_compare(BPy_StructRNA *a, BPy_StructRNA *b)
Definition: bpy_rna.c:831
static int pyrna_prop_array_contains(BPy_PropertyRNA *self, PyObject *value)
Definition: bpy_rna.c:3384
static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, PyObject *value)
Definition: bpy_rna.c:2266
static PyObject * pyrna_prop_collection_subscript_str_lib_pair(BPy_PropertyRNA *self, PyObject *key, const char *err_prefix, const bool err_not_found)
Definition: bpy_rna.c:2592
static PyObject * pyrna_prop_collection_idprop_remove(BPy_PropertyRNA *self, PyObject *value)
Definition: bpy_rna.c:4721
void pyrna_alloc_types(void)
Definition: bpy_rna.c:8743
static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self)
Definition: bpy_rna.c:2337
static PointerRNA * rna_module_ptr
Definition: bpy_rna.c:7687
static int pyrna_prop_array_ass_subscript(BPy_PropertyArrayRNA *self, PyObject *key, PyObject *value)
Definition: bpy_rna.c:3286
static PyObject * pyprop_array_foreach_getset(BPy_PropertyArrayRNA *self, PyObject *args, const bool do_set)
Definition: bpy_rna.c:5475
#define BPY_REPLACEMENT_STRING(rna_attr, py_attr)
int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
Definition: bpy_rna.c:111
static PyObject * pyrna_struct_bl_rna_get_subclass(PyObject *cls, PyObject *args)
Definition: bpy_rna.c:4106
static PyObject * pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
Definition: bpy_rna.c:2458
static PyObject * pyrna_prop_collection_getattro(BPy_PropertyRNA *self, PyObject *pyname)
Definition: bpy_rna.c:4587
static int rna_function_arg_count(FunctionRNA *func, int *min_count)
Definition: bpy_rna.c:8144
PyMethodDef meth_bpy_owner_id_set
Definition: bpy_rna.c:9200
bool pyrna_id_CheckPyObject(PyObject *obj)
Definition: bpy_rna.c:7633
static int pyrna_deferred_register_class_recursive(StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8089
static PyObject * pyrna_struct_get_rna_type(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4805
static PyObject * pyrna_prop_collection_foreach_get(BPy_PropertyRNA *self, PyObject *args)
Definition: bpy_rna.c:5457
int pyrna_set_to_enum_bitfield(const EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
Definition: bpy_rna.c:1363
bool pyrna_write_check(void)
Definition: bpy_rna.c:362
static PyObject * pyrna_prop_collection_iter_next(BPy_PropertyCollectionIterRNA *self)
Definition: bpy_rna.c:7171
static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self)
Definition: bpy_rna.c:2348
static PyObject * pyrna_srna_Subtype(StructRNA *srna)
Definition: bpy_rna.c:7351
static struct PyMethodDef pyrna_prop_collection_idprop_methods[]
Definition: bpy_rna.c:5828
static PyObject * prop_subscript_ass_array_slice__as_seq_fast(PyObject *value, int length)
Definition: bpy_rna.c:3002
static PyObject * pyrna_srna_ExternalType(StructRNA *srna)
Definition: bpy_rna.c:7294
static PyObject * pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, const char *keyname)
Definition: bpy_rna.c:2478
static PyObject * pyrna_struct_property_unset(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3687
static int pyrna_prop_compare(BPy_PropertyRNA *a, BPy_PropertyRNA *b)
Definition: bpy_rna.c:836
static PyObject * pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
Definition: bpy_rna.c:2392
static PyObject * pyrna_prop_collection_get(BPy_PropertyRNA *self, PyObject *args)
Definition: bpy_rna.c:5057
#define MATHUTILS_CB_SUBTYPE_COLOR
Definition: bpy_rna.c:408
static PyObject * pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val)
Definition: bpy_rna.c:1462
static int prop_subscript_ass_array_slice__int_recursive(PyObject **value_items, int *value, int totdim, const int dimsize[], const int range[2])
Definition: bpy_rna.c:3054
static bool rna_disallow_writes
Definition: bpy_rna.c:326
#define PROP_ALL_VECTOR_SUBTYPES
Definition: bpy_rna.c:635
static struct PyMethodDef pyrna_prop_array_methods[]
Definition: bpy_rna.c:5790
static PyObject * pyrna_prop_update(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4016
static long pyrna_prop_hash(BPy_PropertyRNA *self)
Definition: bpy_rna.c:1139
static PyObject * pyrna_struct_bl_rna_find_subclass_recursive(PyObject *cls, const char *id)
Definition: bpy_rna.c:4043
static PyObject * pyrna_prop_array_getattro(BPy_PropertyRNA *self, PyObject *pyname)
Definition: bpy_rna.c:4582
static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
Definition: bpy_rna.c:495
static const char * pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:1243
static PyObject * pyrna_struct_pop(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:4989
static int pyrna_prop_collection_ass_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum, PyObject *value)
Definition: bpy_rna.c:2425
static struct PyModuleDef bpy_types_module_def
Definition: bpy_rna.c:7804
static PyObject * pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:4946
static PyMappingMethods pyrna_struct_as_mapping
Definition: bpy_rna.c:3556
static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item)
Definition: bpy_rna.c:7937
static PyObject * pyrna_prop_dir(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4560
static PyObject * pyrna_struct_type_recast(BPy_StructRNA *self)
Definition: bpy_rna.c:4030
static PyObject * pyrna_struct_is_property_overridable_library(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3776
static void pyrna_dir_members_py__add_keys(PyObject *list, PyObject *dict)
Definition: bpy_rna.c:4141
static PyTypeObject pyrna_prop_collection_idprop_Type
Definition: bpy_rna.c:6886
static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *key)
Definition: bpy_rna.c:3389
static PyObject * pyrna_prop_path_from_id(BPy_PropertyRNA *self)
Definition: bpy_rna.c:3952
static void pyrna_prop_dealloc(BPy_PropertyRNA *self)
Definition: bpy_rna.c:1221
#define BPY_DOC_ID_PROP_TYPE_NOTE
Definition: bpy_rna.c:105
static PyObject * pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length)
Definition: bpy_rna.c:2647
static PyObject * pyrna_prop_repr_ex(BPy_PropertyRNA *self, const int index_dim, const int index)
Definition: bpy_rna.c:1066
static PyMappingMethods pyrna_prop_array_as_mapping
Definition: bpy_rna.c:3346
static PyObject * pyrna_struct_str(BPy_StructRNA *self)
Definition: bpy_rna.c:906
static void pyrna_dir_members_py(PyObject *list, PyObject *self)
Definition: bpy_rna.c:4150
PyMethodDef meth_bpy_owner_id_get
Definition: bpy_rna.c:9194
static int mathutils_rna_matrix_get(BaseMathObject *bmo, int UNUSED(subtype))
Definition: bpy_rna.c:554
static int pyrna_prop_collection_ass_subscript(BPy_PropertyRNA *self, PyObject *key, PyObject *value)
Definition: bpy_rna.c:2860
static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *r_value, const char *error_prefix)
Definition: bpy_rna.c:1264
static int foreach_parse_args(BPy_PropertyRNA *self, PyObject *args, const char **r_attr, PyObject **r_seq, int *r_tot, int *r_size, RawPropertyType *r_raw_type, int *r_attr_tot, bool *r_attr_signed)
Definition: bpy_rna.c:5169
static PyObject * pyrna_prop_collection_iter(BPy_PropertyRNA *self)
Definition: bpy_rna.c:7166
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7469
static PyObject * pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *UNUSED(kwds))
Definition: bpy_rna.c:5886
static PyObject * pyrna_struct_get_id_data(BPy_DummyPointerRNA *self)
Definition: bpy_rna.c:4785
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition: bpy_rna.c:7849
static PyNumberMethods pyrna_prop_collection_as_number
Definition: bpy_rna.c:3371
static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int start, int stop, int length, PyObject *value_orig)
Definition: bpy_rna.c:3119
static int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
Definition: bpy_rna.c:8066
static PyObject * pyrna_prop_collection_values(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4928
static int mathutils_rna_matrix_set(BaseMathObject *bmo, int UNUSED(subtype))
Definition: bpy_rna.c:568
void pyrna_free_types(void)
Definition: bpy_rna.c:8775
void BPY_update_rna_module(void)
Definition: bpy_rna.c:7701
static int pyrna_prop_collection_type_check(BPy_PropertyRNA *self, PyObject *value)
Definition: bpy_rna.c:2817
static int pyrna_struct_meta_idprop_setattro(PyObject *cls, PyObject *attr, PyObject *value)
Definition: bpy_rna.c:4432
static int pyrna_struct_setattro(BPy_StructRNA *self, PyObject *pyname, PyObject *value)
Definition: bpy_rna.c:4495
static bool foreach_attr_type(BPy_PropertyRNA *self, const char *attr, RawPropertyType *r_raw_type, int *r_attr_tot, bool *r_attr_signed)
Definition: bpy_rna.c:5137
static PyObject * pyrna_register_class(PyObject *self, PyObject *py_class)
static uchar mathutils_rna_array_cb_index
Definition: bpy_rna.c:402
static int bpy_class_call(bContext *C, PointerRNA *ptr, FunctionRNA *func, ParameterList *parms)
Definition: bpy_rna.c:8385
static PySequenceMethods pyrna_struct_as_sequence
Definition: bpy_rna.c:3470
static PyObject * pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *UNUSED(kwds))
Definition: bpy_rna.c:5838
static int pyrna_prop_array_bool(BPy_PropertyRNA *self)
Definition: bpy_rna.c:2357
#define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err)
Definition: bpy_rna.c:2381
static PyObject * pyrna_prop_collection_find(BPy_PropertyRNA *self, PyObject *key_ob)
Definition: bpy_rna.c:5103
PyObject * BPY_rna_module(void)
Definition: bpy_rna.c:7688
static PyObject * pyrna_func_doc_get(BPy_FunctionRNA *self, void *closure)
static void pyrna_subtype_set_rna(PyObject *newclass, StructRNA *srna)
Definition: bpy_rna.c:7213
static PyObject * pyrna_prop_repr(BPy_PropertyRNA *self)
Definition: bpy_rna.c:1115
static PyObject * pyrna_prop_as_bytes(BPy_PropertyRNA *self)
Definition: bpy_rna.c:3981
static int mathutils_rna_generic_check(BaseMathObject *bmo)
Definition: bpy_rna.c:410
static void pyrna_prop_collection_iter_dealloc(BPy_PropertyCollectionIterRNA *self)
Definition: bpy_rna.c:7197
static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
Definition: bpy_rna.c:4182
static struct PyMethodDef pyrna_prop_methods[]
Definition: bpy_rna.c:5779
PyTypeObject pyrna_prop_array_Type
Definition: bpy_rna.c:6717
static PyObject * pyrna_prop_collection_idprop_move(BPy_PropertyRNA *self, PyObject *args)
Definition: bpy_rna.c:4758
static int prop_subscript_ass_array_slice__bool_recursive(PyObject **value_items, bool *value, int totdim, const int dimsize[])
Definition: bpy_rna.c:3086
static StructRNA * srna_from_ptr(PointerRNA *ptr)
Definition: bpy_rna.c:7453
static PyObject * pyrna_prop_array_repr(BPy_PropertyArrayRNA *self)
Definition: bpy_rna.c:1120
static PyNumberMethods pyrna_prop_array_as_number
Definition: bpy_rna.c:3359
static PyObject * pyrna_prop_collection_idprop_add(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4701
PyObject * pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:644
static PyObject * pyrna_struct_is_property_readonly(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3747
static int prop_subscript_ass_array_int(BPy_PropertyArrayRNA *self, Py_ssize_t keynum, PyObject *value)
Definition: bpy_rna.c:3264
static PySequenceMethods pyrna_prop_collection_as_sequence
Definition: bpy_rna.c:3455
StructRNA * srna_from_self(PyObject *self, const char *error_prefix)
Definition: bpy_rna.c:7906
static Py_hash_t pyrna_struct_hash(BPy_StructRNA *self)
Definition: bpy_rna.c:1133
static int bpy_class_validate_recursive(PointerRNA *dummyptr, StructRNA *srna, void *py_data, int *have_function)
Definition: bpy_rna.c:8174
static PyObject * pyrna_prop_collection_keys(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4855
static PyGetSetDef pyrna_prop_getseters[]
Definition: bpy_rna.c:4816
static PyObject * pyrna_prop_collection_iter_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:7152
PyTypeObject pyrna_struct_Type
Definition: bpy_rna.c:6540
static PyObject * small_dict_get_item_string(PyObject *dict, const char *key_lookup)
Definition: bpy_rna.c:6088
static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_function)
Definition: bpy_rna.c:8379
static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
Definition: bpy_rna.c:419
static void bpy_class_free(void *pyob_ptr)
Definition: bpy_rna.c:8710
static PyObject * pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
Definition: bpy_rna.c:843
static uchar mathutils_rna_matrix_cb_index
Definition: bpy_rna.c:552
static PyObject * pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
Definition: bpy_rna.c:5910
static PySequenceMethods pyrna_prop_array_as_sequence
Definition: bpy_rna.c:3441
static PyObject * pyrna_prop_array_foreach_get(BPy_PropertyArrayRNA *self, PyObject *args)
Definition: bpy_rna.c:5625
static PyObject * pyrna_struct_Subtype(PointerRNA *ptr)
Definition: bpy_rna.c:7463
static bool foreach_compat_buffer(RawPropertyType raw_type, int attr_signed, const char *format)
Definition: bpy_rna.c:5252
static PyObject * pyrna_prop_collection_idprop_clear(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4745
static int pyrna_srna_contains_pointer_prop_srna(StructRNA *srna_props, StructRNA *srna, const char **r_prop_identifier)
Definition: bpy_rna.c:8968
static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
Definition: bpy_rna.c:3415
static PyObject * pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3718
PyTypeObject pyrna_func_Type
Definition: bpy_rna.c:6972
static PyObject * pyrna_prop_array_to_py_index(BPy_PropertyArrayRNA *self, int index)
Definition: bpy_rna.c:2260
static PyObject * pyrna_struct_get_data(BPy_DummyPointerRNA *self)
Definition: bpy_rna.c:4799
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8122
static PyGetSetDef pyrna_struct_getseters[]
Definition: bpy_rna.c:4831
static PyObject * pyrna_func_to_py(const PointerRNA *ptr, FunctionRNA *func)
Definition: bpy_rna.c:1694
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const bool all_args, const char *error_prefix)
Definition: bpy_rna.c:1622
static Mathutils_Callback mathutils_rna_array_cb
Definition: bpy_rna.c:543
static PyObject * pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
Definition: bpy_rna.c:874
static PyObject * pyrna_prop_array_foreach_set(BPy_PropertyArrayRNA *self, PyObject *args)
Definition: bpy_rna.c:5636
static PyObject * pyrna_struct_values(BPy_PropertyRNA *self)
Definition: bpy_rna.c:3625
static PyObject * pyrna_prop_collection_foreach_set(BPy_PropertyRNA *self, PyObject *args)
Definition: bpy_rna.c:5468
static int pyrna_prop_collection_bool(BPy_PropertyRNA *self)
Definition: bpy_rna.c:2364
static PyObject * pyrna_struct_as_pointer(BPy_StructRNA *self)
Definition: bpy_rna.c:5041
PyMethodDef meth_bpy_unregister_class
Definition: bpy_rna.c:9001
static short pyrna_rotation_euler_order_get(PointerRNA *ptr, const short order_fallback, PropertyRNA **r_prop_eul_order)
Definition: bpy_rna.c:609
static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
Definition: bpy_rna.c:509
static PyObject * pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject *kw)
Definition: bpy_rna.c:6105
static PyObject * bpy_types_dict
Definition: bpy_rna.c:7292
static int prop_subscript_ass_array_slice__float_recursive(PyObject **value_items, float *value, int totdim, const int dimsize[], const float range[2])
Definition: bpy_rna.c:3022
static struct PyMethodDef pyrna_struct_methods[]
Definition: bpy_rna.c:5688
static PyObject * pyrna_struct_items(BPy_PropertyRNA *self)
Definition: bpy_rna.c:3598
static PyObject * foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
Definition: bpy_rna.c:5291
static PyObject * pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
Definition: bpy_rna.c:2743
static void pyrna_prop_array_dealloc(BPy_PropertyRNA *self)
Definition: bpy_rna.c:1232
static PyObject * pyrna_prop_collection_subscript_slice(BPy_PropertyRNA *self, Py_ssize_t start, Py_ssize_t stop)
Definition: bpy_rna.c:2608
void pyrna_write_set(bool val)
Definition: bpy_rna.c:367
static PyObject * pyrna_struct_repr(BPy_StructRNA *self)
Definition: bpy_rna.c:942
PyMethodDef meth_bpy_register_class
Definition: bpy_rna.c:8827
static PyObject * pyrna_bl_owner_id_set(PyObject *UNUSED(self), PyObject *value)
Definition: bpy_rna.c:9174
#define MATHUTILS_CB_SUBTYPE_VEC
Definition: bpy_rna.c:406
static int pyrna_deferred_register_class_from_type_hints(StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8010
static PyObject * bpy_types_module_getattro(PyObject *self, PyObject *pyname)
Definition: bpy_rna.c:7743
static struct PyMethodDef bpy_types_module_methods[]
Definition: bpy_rna.c:7797
int pyrna_prop_validity_check(BPy_PropertyRNA *self)
Definition: bpy_rna.c:121
#define MATHUTILS_CB_SUBTYPE_EUL
Definition: bpy_rna.c:405
static PyObject * pyrna_struct_path_from_id(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3895
static PyObject * pyrna_struct_keys(BPy_PropertyRNA *self)
Definition: bpy_rna.c:3571
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition: bpy_rna.c:7611
static PyGetSetDef pyrna_func_getseters[]
Definition: bpy_rna.c:4842
static int pyrna_prop_collection_setattro(BPy_PropertyRNA *self, PyObject *pyname, PyObject *value)
Definition: bpy_rna.c:4667
static int pyrna_prop_to_enum_bitfield(PointerRNA *ptr, PropertyRNA *prop, PyObject *value, int *r_value, const char *error_prefix)
Definition: bpy_rna.c:1400
static PyMappingMethods pyrna_prop_collection_as_mapping
Definition: bpy_rna.c:3352
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition: bpy_rna.c:7622
void BPY_id_release(struct ID *id)
Definition: bpy_rna.c:306
static PyObject * pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args, PyObject *kw)
Definition: bpy_rna.c:3658
BLI_bitmap * pyrna_set_to_enum_bitmap(const EnumPropertyItem *items, PyObject *value, int type_size, bool type_convert_sign, int bitmap_size, const char *error_prefix)
Definition: bpy_rna.c:1299
static PyObject * pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna.c:3839
static PyObject * pyrna_struct_bl_rna_get_subclass_py(PyObject *cls, PyObject *args)
Definition: bpy_rna.c:4084
static int pyrna_prop_collection_subscript_str_lib_pair_ptr(BPy_PropertyRNA *self, PyObject *key, const char *err_prefix, const short err_not_found, PointerRNA *r_ptr)
Definition: bpy_rna.c:2504
#define MATHUTILS_CB_SUBTYPE_QUAT
Definition: bpy_rna.c:407
PyObject * BPY_rna_types(void)
Definition: bpy_rna.c:7819
static PyObject * pyrna_prop_str(BPy_PropertyRNA *self)
Definition: bpy_rna.c:1000
BPy_StructRNA * bpy_context_module
Definition: bpy_rna.c:97
static PyObject * pyrna_struct_getattro(BPy_StructRNA *self, PyObject *pyname)
Definition: bpy_rna.c:4274
static int pyrna_struct_ass_subscript(BPy_StructRNA *self, PyObject *key, PyObject *value)
Definition: bpy_rna.c:3520
static PyObject * pyrna_struct_subscript(BPy_StructRNA *self, PyObject *key)
Definition: bpy_rna.c:3484
static bool rna_id_write_error(PointerRNA *ptr, PyObject *key)
Definition: bpy_rna.c:328
static PyObject * pyrna_prop_collection_items(BPy_PropertyRNA *self)
Definition: bpy_rna.c:4885
static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix)
Definition: bpy_rna.c:1702
static PyObject * pyrna_unregister_class(PyObject *self, PyObject *py_class)
static struct PyMethodDef pyrna_prop_collection_methods[]
Definition: bpy_rna.c:5803
static PyObject * pyrna_struct_dir(BPy_StructRNA *self)
Definition: bpy_rna.c:4232
static PyObject * pyrna_func_repr(BPy_FunctionRNA *self)
Definition: bpy_rna.c:1125
PyTypeObject pyrna_prop_collection_Type
Definition: bpy_rna.c:6800
PyDoc_STRVAR(pyrna_struct_keys_doc, ".. method:: keys()\n" "\n" " Returns the keys of this objects custom properties (matches Python's\n" " dictionary function of the same name).\n" "\n" " :return: custom property keys.\n" " :rtype: list of strings\n" "\n" BPY_DOC_ID_PROP_TYPE_NOTE)
static PyObject * pyrna_prop_array_iter(BPy_PropertyArrayRNA *self)
Definition: bpy_rna.c:5645
static Mathutils_Callback mathutils_rna_matrix_cb
Definition: bpy_rna.c:601
int pyrna_py_to_array(PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix)
#define PYRNA_STRUCT_IS_VALID(pysrna)
Definition: bpy_rna.h:107
int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
#define BPy_StructRNA_CheckExact(v)
Definition: bpy_rna.h:81
#define PYRNA_STRUCT_CHECK_OBJ(obj)
Definition: bpy_rna.h:85
int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int index, PyObject *py, const char *error_prefix)
#define PYRNA_STRUCT_CHECK_INT(obj)
Definition: bpy_rna.h:90
#define PYRNA_PROP_CHECK_OBJ(obj)
Definition: bpy_rna.h:96
PyObject * pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index)
#define PYRNA_PROP_CHECK_INT(obj)
Definition: bpy_rna.h:101
#define BPy_PropertyRNA_CheckExact(v)
Definition: bpy_rna.h:83
#define BPy_StructRNA_Check(v)
Definition: bpy_rna.h:80
#define BPy_PropertyRNA_Check(v)
Definition: bpy_rna.h:82
PyObject * pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop)
PyObject * pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
PyObject * pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args, PyObject *kw)
Definition: bpy_rna_anim.c:312
char pyrna_struct_driver_add_doc[]
Definition: bpy_rna_anim.c:537
char pyrna_struct_keyframe_insert_doc[]
Definition: bpy_rna_anim.c:279
PyObject * pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *args, PyObject *kw)
Definition: bpy_rna_anim.c:433
char pyrna_struct_driver_remove_doc[]
Definition: bpy_rna_anim.c:619
char pyrna_struct_keyframe_delete_doc[]
Definition: bpy_rna_anim.c:414
PyObject * pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna_anim.c:631
PyObject * pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args)
Definition: bpy_rna_anim.c:549
PyObject * pyrna_callback_classmethod_remove(PyObject *UNUSED(self), PyObject *args)
PyObject * pyrna_callback_classmethod_add(PyObject *UNUSED(self), PyObject *args)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
DRWShaderLibrary * lib
static FT_Error err
Definition: freetypefont.c:52
uint pos
uint col
int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val)
PyObject * BPy_IDGroup_MapDataToPy(IDProperty *prop)
PyObject * BPy_Wrap_GetItems(ID *id, IDProperty *prop)
PyObject * BPy_Wrap_GetValues(ID *id, IDProperty *prop)
PyObject * BPy_Wrap_GetKeys(IDProperty *prop)
PyObject * BPy_IDGroup_WrapData(ID *id, IDProperty *prop, IDProperty *parent)
int count
#define GS(x)
Definition: iris.c:241
format
Definition: logImageCore.h:47
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition: mathutils.c:584
int(* BaseMathGetFunc)(BaseMathObject *, int)
Definition: mathutils.h:101
int(* BaseMathGetIndexFunc)(BaseMathObject *, int, int)
Definition: mathutils.h:105
int(* BaseMathSetIndexFunc)(BaseMathObject *, int, int)
Definition: mathutils.h:107
int(* BaseMathSetFunc)(BaseMathObject *, int)
Definition: mathutils.h:103
int(* BaseMathCheckFunc)(BaseMathObject *)
Definition: mathutils.h:99
PyObject * Color_CreatePyObject_cb(PyObject *cb_user, uchar cb_type, uchar cb_subtype)
PyObject * Color_CreatePyObject(const float col[3], PyTypeObject *base_type)
PyObject * Euler_CreatePyObject_cb(PyObject *cb_user, const short order, uchar cb_type, uchar cb_subtype)
PyObject * Euler_CreatePyObject(const float eul[3], const short order, PyTypeObject *base_type)
PyObject * Matrix_CreatePyObject_cb(PyObject *cb_user, const ushort num_col, const ushort num_row, uchar cb_type, uchar cb_subtype)
PyObject * Matrix_CreatePyObject(const float *mat, const ushort num_col, const ushort num_row, PyTypeObject *base_type)
PyObject * Quaternion_CreatePyObject(const float quat[4], PyTypeObject *base_type)
PyObject * Quaternion_CreatePyObject_cb(PyObject *cb_user, uchar cb_type, uchar cb_subtype)
PyObject * Vector_CreatePyObject(const float *vec, const int size, PyTypeObject *base_type)
PyObject * Vector_CreatePyObject_cb(PyObject *cb_user, int size, uchar cb_type, uchar cb_subtype)
static ulong state[N]
static void error(const char *str)
Definition: meshlaplacian.c:65
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
#define hash
Definition: noise.c:169
const char * PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
void PyC_ObSpit(const char *name, PyObject *var)
PyObject * PyC_UnicodeFromByteAndSize(const char *str, Py_ssize_t size)
PyObject * PyC_ExceptionBuffer(void)
int PyC_Long_AsBool(PyObject *value)
PyObject * PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format,...)
void PyC_FileAndNum(const char **r_filename, int *r_lineno)
PyObject * PyC_UnicodeFromByte(const char *str)
int PyC_ParseBool(PyObject *o, void *p)
void PyC_ObSpitStr(char *result, size_t result_len, PyObject *var)
return ret
StructUnregisterFunc RNA_struct_unregister(StructRNA *type)
Definition: rna_access.c:1005
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2941
bool RNA_property_enum_value(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *r_value)
Definition: rna_access.c:1801
StructRegisterFunc RNA_struct_register(StructRNA *type)
Definition: rna_access.c:1000
const char * RNA_struct_identifier(const StructRNA *type)
Definition: rna_access.c:723
int RNA_enum_bitflag_identifiers(const EnumPropertyItem *item, const int value, const char **r_identifier)
Definition: rna_access.c:1840
const char * RNA_function_identifier(FunctionRNA *func)
Definition: rna_access.c:7149
void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, bool value)
Definition: rna_access.c:2530
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
Definition: rna_access.c:2627
FunctionRNA * RNA_struct_find_function(StructRNA *srna, const char *identifier)
Definition: rna_access.c:958
bool RNA_property_update_check(PropertyRNA *prop)
Definition: rna_access.c:2312
bool RNA_property_array_check(PropertyRNA *prop)
Definition: rna_access.c:1223
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
Definition: rna_access.c:844
bool RNA_path_resolve_full(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
Definition: rna_access.c:5416
const struct ListBase * RNA_struct_type_properties(StructRNA *srna)
Definition: rna_access.c:948
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
Definition: rna_access.c:3033
void RNA_struct_state_owner_set(const char *name)
Definition: rna_access.c:8188
void RNA_struct_py_type_set(StructRNA *srna, void *py_type)
Definition: rna_access.c:1034
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
int RNA_property_int_clamp(PointerRNA *ptr, PropertyRNA *prop, int *value)
Definition: rna_access.c:1542
int RNA_property_float_clamp(PointerRNA *ptr, PropertyRNA *prop, float *value)
Definition: rna_access.c:1525
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
void ** RNA_struct_instance(PointerRNA *ptr)
Definition: rna_access.c:1016
bool RNA_struct_is_ID(const StructRNA *type)
Definition: rna_access.c:797
const char * RNA_property_identifier(const PropertyRNA *prop)
Definition: rna_access.c:1145
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
Definition: rna_access.c:3190
bool RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value)
Definition: rna_access.c:6474
int RNA_function_defined(FunctionRNA *func)
Definition: rna_access.c:7169
ParameterList * RNA_parameter_list_create(ParameterList *parms, PointerRNA *UNUSED(ptr), FunctionRNA *func)
Definition: rna_access.c:7207
int RNA_property_collection_assign_int(PointerRNA *ptr, PropertyRNA *prop, const int key, const PointerRNA *assign_ptr)
Definition: rna_access.c:4309
int RNA_property_array_dimension(PointerRNA *ptr, PropertyRNA *prop, int length[])
Definition: rna_access.c:1229
void RNA_blender_rna_pointer_create(PointerRNA *r_ptr)
Definition: rna_access.c:189
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
Definition: rna_access.c:1049
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
Definition: rna_access.c:3108
int RNA_parameter_list_arg_count(ParameterList *parms)
Definition: rna_access.c:7335
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
Definition: rna_access.c:3673
void RNA_property_collection_skip(CollectionPropertyIterator *iter, int num)
Definition: rna_access.c:3866
int RNA_property_collection_raw_get(ReportList *reports, PointerRNA *ptr, PropertyRNA *prop, const char *propname, void *array, RawPropertyType type, int len)
Definition: rna_access.c:4799
bool RNA_property_is_set_ex(PointerRNA *ptr, PropertyRNA *prop, bool use_ghost)
Definition: rna_access.c:6645
int RNA_property_enum_bitflag_identifiers(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier)
Definition: rna_access.c:2024
void RNA_property_collection_begin(PointerRNA *ptr, PropertyRNA *prop, CollectionPropertyIterator *iter)
Definition: rna_access.c:3816
void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin, float *hardmax)
Definition: rna_access.c:1426
void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, bool *values)
Definition: rna_access.c:2420
PropertyType RNA_property_type(PropertyRNA *prop)
Definition: rna_access.c:1155
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:71
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
Definition: rna_access.c:3562
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3641
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
Definition: rna_access.c:4212
void RNA_parameter_list_free(ParameterList *parms)
Definition: rna_access.c:7303
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2317
char * RNA_path_from_ID_to_property(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:6027
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2331
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
Definition: rna_access.c:3339
bool RNA_struct_idprops_contains_datablock(const StructRNA *type)
Definition: rna_access.c:823
void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
Definition: rna_access.c:2830
bool RNA_enum_id_from_value(const EnumPropertyItem *item, int value, const char **r_identifier)
Definition: rna_access.c:6484
void RNA_pointer_recast(PointerRNA *ptr, PointerRNA *r_ptr)
Definition: rna_access.c:217
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2607
IDProperty * RNA_struct_idprops(PointerRNA *ptr, bool create)
Definition: rna_access.c:372
void RNA_property_collection_next(CollectionPropertyIterator *iter)
Definition: rna_access.c:3850
void RNA_parameter_list_next(ParameterIterator *iter)
Definition: rna_access.c:7361
bool RNA_property_editable_flag(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2132
int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr)
Definition: rna_access.c:4299
char * RNA_path_from_real_ID_to_struct(Main *bmain, PointerRNA *ptr, struct ID **r_real)
Definition: rna_access.c:5918
PropertyRNA * RNA_struct_type_find_property(StructRNA *srna, const char *identifier)
Definition: rna_access.c:953
void RNA_parameter_list_end(ParameterIterator *UNUSED(iter))
Definition: rna_access.c:7373
int RNA_function_flag(FunctionRNA *func)
Definition: rna_access.c:7164
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1567
char * RNA_path_from_real_ID_to_property_index(Main *bmain, PointerRNA *ptr, PropertyRNA *prop, int index_dim, int index, ID **r_real_id)
Definition: rna_access.c:6032
int RNA_property_flag(PropertyRNA *prop)
Definition: rna_access.c:1192
RawPropertyType RNA_property_raw_type(PropertyRNA *prop)
Definition: rna_access.c:4778
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
Definition: rna_access.c:2358
bool RNA_property_builtin(PropertyRNA *prop)
Definition: rna_access.c:1208
bool RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier)
Definition: rna_access.c:1925
bool RNA_struct_idprops_register_check(const StructRNA *type)
Definition: rna_access.c:807
void RNA_property_string_set_bytes(PointerRNA *ptr, PropertyRNA *prop, const char *value, int len)
Definition: rna_access.c:3430
void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
Definition: rna_access.c:3966
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1218
bool RNA_property_is_idprop(const PropertyRNA *prop)
Definition: rna_access.c:6706
int RNA_property_string_maxlength(PropertyRNA *prop)
Definition: rna_access.c:1561
bool RNA_struct_idprops_datablock_allowed(const StructRNA *type)
Definition: rna_access.c:812
const ListBase * RNA_function_defined_parameters(FunctionRNA *func)
Definition: rna_access.c:7195
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
Definition: rna_access.c:3132
void RNA_property_int_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
Definition: rna_access.c:2783
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3543
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
Definition: rna_access.c:2964
void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
Definition: rna_access.c:2690
bool RNA_property_collection_move(PointerRNA *ptr, PropertyRNA *prop, int key, int pos)
Definition: rna_access.c:4114
PropertySubType RNA_property_subtype(PropertyRNA *prop)
Definition: rna_access.c:1160
void * RNA_struct_py_type_get(StructRNA *srna)
Definition: rna_access.c:1029
void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, int *hardmax)
Definition: rna_access.c:1335
int RNA_function_call(bContext *C, ReportList *reports, PointerRNA *ptr, FunctionRNA *func, ParameterList *parms)
Definition: rna_access.c:7546
void RNA_property_enum_items(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
Definition: rna_access.c:1657
void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:4157
bool RNA_property_collection_type_get(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
Definition: rna_access.c:4326
void RNA_property_collection_end(CollectionPropertyIterator *iter)
Definition: rna_access.c:3891
bool RNA_struct_idprops_check(StructRNA *srna)
Definition: rna_access.c:383
bool RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop_orig)
Definition: rna_access.c:2073
const ListBase * RNA_struct_type_functions(StructRNA *srna)
Definition: rna_access.c:995
bool RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key)
Definition: rna_access.c:4049
void RNA_main_pointer_create(struct Main *main, PointerRNA *r_ptr)
Definition: rna_access.c:115
const char * RNA_struct_state_owner_get(void)
Definition: rna_access.c:8198
char * RNA_path_from_ID_to_struct(PointerRNA *ptr)
Definition: rna_access.c:5876
void RNA_property_unset(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:6665
char * RNA_function_as_string_keywords(bContext *C, FunctionRNA *func, const bool as_function, const bool all_args, const int max_prop_length)
Definition: rna_access.c:6881
void RNA_parameter_list_begin(ParameterList *parms, ParameterIterator *iter)
Definition: rna_access.c:7345
PropertyRNA * RNA_struct_iterator_property(StructRNA *type)
Definition: rna_access.c:771
void RNA_property_enum_items_ex(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const bool use_static, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
Definition: rna_access.c:1614
int RNA_parameter_flag(PropertyRNA *prop)
Definition: rna_access.c:7202
int RNA_raw_type_sizeof(RawPropertyType type)
Definition: rna_access.c:4431
const char * RNA_function_ui_description(FunctionRNA *func)
Definition: rna_access.c:7154
int RNA_property_collection_raw_set(ReportList *reports, PointerRNA *ptr, PropertyRNA *prop, const char *propname, void *array, RawPropertyType type, int len)
Definition: rna_access.c:4810
const StructRNA * RNA_struct_base_child_of(const StructRNA *type, const StructRNA *parent_type)
Definition: rna_access.c:786
int RNA_property_multi_array_length(PointerRNA *ptr, PropertyRNA *prop, int dim)
Definition: rna_access.c:1241
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3903
void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const bool *values)
Definition: rna_access.c:2482
StructRNA * RNA_struct_base(StructRNA *type)
Definition: rna_access.c:776
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
Definition: rna_access.c:3401
bool RNA_property_overridable_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_overridable_library_set(PointerRNA *UNUSED(ptr), PropertyRNA *prop, const bool is_overridable)
const EnumPropertyItem rna_enum_property_type_items[]
Definition: rna_rna.c:56
const EnumPropertyItem DummyRNA_NULL_items[]
Definition: rna_rna.c:40
#define min(a, b)
Definition: sort.c:51
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:117
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:176
FunctionRNA * func
Definition: bpy_rna.h:177
PropertyRNA * prop
Definition: bpy_rna.h:143
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:142
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:125
PropertyRNA * prop
Definition: bpy_rna.c:7740
struct CollectionPointerLink * first
Definition: RNA_types.h:413
unsigned char order
StructRNA * srna
Definition: RNA_types.h:681
uint flag
Definition: BLI_ghash.c:114
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
struct Library * lib
Definition: DNA_ID.h:277
short flag
Definition: DNA_ID.h:288
char name[66]
Definition: DNA_ID.h:283
void * data
Definition: DNA_listBase.h:42
struct LinkData * next
Definition: DNA_listBase.h:41
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase libraries
Definition: BKE_main.h:147
intptr_t array_tot
Definition: RNA_types.h:541
PropertyRNA * parm
Definition: RNA_types.h:534
struct StructRNA * type
Definition: RNA_types.h:51
void * data
Definition: RNA_types.h:52
struct ID * owner_id
Definition: RNA_types.h:50
Defines a node type.
Definition: BKE_node.h:221
ExtensionRNA rna_ext
Definition: BKE_node.h:330
struct ReportList * reports
float max
#define G(x, y, z)
uint len
PointerRNA * ptr
Definition: wm_files.c:3157