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