From 429771fed55119df4dfebcf1c8c3666f017fc0eb Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 30 Nov 2022 16:52:10 +0100 Subject: [PATCH] Add 'work around' to accessing data from volatile iterators in py API. Re T102550. --- doc/python_api/rst/info_gotcha.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/doc/python_api/rst/info_gotcha.rst b/doc/python_api/rst/info_gotcha.rst index 2dd6c3c92b1..e2a49898a4b 100644 --- a/doc/python_api/rst/info_gotcha.rst +++ b/doc/python_api/rst/info_gotcha.rst @@ -870,6 +870,26 @@ an issue but, due to internal implementation details, currently are: thus breaking any current iteration over ``Collection.all_objects``. +.. rubric:: Do not: + +.. code-block:: python + + # `all_objects` is an iterator. Using it directly while performing operations on its members that will update + # the memory accessed by the `all_objects` iterator will lead to invalid memory accesses and crashes. + for object in bpy.data.collections["Collection"].all_objects: + object.hide_viewport = True + + +.. rubric:: Do: + +.. code-block:: python + + # `all_objects[:]` is an independent list generated from the iterator. As long as no objects are deleted, + # its content will remain valid even if the data accessed by the `all_objects` iterator is modified. + for object in bpy.data.collections["Collection"].all_objects[:]: + object.hide_viewport = True + + sys.exit ========