Blender is losing reference to collection property item when used in a recursive function? #61297
Labels
No Label
Interest
Animation & Rigging
Interest
Blender Cloud
Interest
Collada
Interest
Core
Interest
Documentation
Interest
Eevee & Viewport
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
Import and Export
Interest
Modeling
Interest
Modifiers
Interest
Nodes & Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds, Tests & Devices
Interest
Python API
Interest
Rendering & Cycles
Interest
Sculpt, Paint & Texture
Interest
Translations
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Meta
Good First Issue
Meta
Papercut
Module
Add-ons (BF-Blender)
Module
Add-ons (Community)
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender-addons#61297
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
System Information
Operating system: Ubuntu 18.04.1 LTS
Graphics card: GeForce GT 610. But I am not doing any graphics, so it's unrelated
Blender Version
Broken:
2.79b release
and
blender-2.80-9c68ac0448b6-linux-glibc224-x86_64 from 2019-02-07
Worked: none
Short description of error
I am creating a new item in collection property, and I am adding data to this item. After a recursive call of the same function, this item is not accessible. Details of errors are in the python code comments.
Exact steps for others to reproduce the error
Base on the default startup blend file, load my python script to a text editor and run a script. No errors yet. Find the button "Load tree from json" (properties in 3d viewport) and press it.
An error does not appear when pressed again. You will have to restart blender to repeat the error.
Script
error2.8.py
Crash report from blender 2.8
blender.crash.txt
Added subscriber: @dopiotrko
Added subscriber: @lichtwerk
Changed status from 'Open' to: 'Resolved'
RNA_def_property: runtime property identifier "TreeItems.items" - this keyword is reserved by python
Since this is a reserved keyword, try renaming your property to something else:
and change that elsewhere in the code as well, should be good to go then, see
T61297_phi.blend
Will close this as resolved, but feel free to comment again if issues persist...
I regret to write: it is not it :(.
I changed init to sub_init and I steel get TypeError: unsubscriptable object.
error2.8_sub_item.py
Regards
Changed status from 'Resolved' to: 'Open'
That was probably a bit too quick on my side, need to look at that again (on Monday)...
?
Added subscribers: @mont29, @ideasman42
Can confirm the crash (when accessing/printing the
current_item
after the recursive call)However, this might be expected [to get into trouble after a recursive call], since the local variable is just a reference to the same object?
https://stackoverflow.com/questions/24572089/trouble-with-variable-scope-in-recursive-function-python
You are also using
enumerate
here [which is different from just looping over a list, but I am uncertain about the python internals...]I made a 2nd try here:
sub_branch
prior to passing it to the recursive call#61297.blend
Since I am really on shaky ground here, I would like to ask:
@mont29, @ideasman42 : is this expected? [to get into trouble when using enumerate + recursive function calls?] Is dropping
enumerate
(in favor ofin list
) plus making a deepcopy the desired/correct solution here?Will mark this as
Normal
priority for now...This .blend file You attached is (as far as I know) default init blend file, so I can't check what You have changed. Please attach the changed .py file.
I followed Your directions:
Regards.
Sorry, Your blend file was not empty, my mistake. But the error still occurs.
T61297_____.blend
@dopiotrko : changed py is in that file (just didnt rename it -- e.g. look for
_sub_branch = deepcopy(sub_branch)
)and yes, that solved the error on my side...
I can only repeat myself: I am on shaky ground here, if the above file doesnt work for you, I am probably out of ideas...
Changed status from 'Open' to: 'Archived'
First of all, this is not a bug report at all, and should not be handled here. We have a site for that kind of question: https://devtalk.blender.org/
Would be a bug report if you had done investigation, and generated from it a very simple demo script of the issue, instead of giving us your full project one:
name: bpy.props.StringProperty()
counter -= 1
if counter == 0:
return
curr_item = context.scene.tree_items.add()
recursive_idprop_user(context, counter)
curr_item.name = str(counter)
print(curr_item.values())
bl_idname = "foobar.test"
bl_label = "Test recursive IDProp handling"
def execute(self, context):
recursive_idprop_user(context, 10)
return {"FINISHED"}
bpy.utils.register_class(TreeItems)
bpy.utils.register_class(FooBar_OT_test)
bpy.types.Scene.tree_items = bpy.props.CollectionProperty(type=TreeItems)
register()
That should also have made it obvious that there is no bug at all here, you are just keeping reference of some Blender data while modifying container of that data, which is always dangerous. That simple modification is enough to make example work:
name: bpy.props.StringProperty()
counter -= 1
if counter == 0:
return
curr_item = context.scene.tree_items.add()
curr_item_idx = len(context.scene.tree_items) - 1
recursive_idprop_user(context, counter)
curr_item = context.scene.tree_items[curr_item_idx]
curr_item.name = str(counter)
print(curr_item.values())
bl_idname = "foobar.test"
bl_label = "Test recursive IDProp handling"
def execute(self, context):
recursive_idprop_user(context, 10)
return {"FINISHED"}
bpy.utils.register_class(TreeItems)
bpy.utils.register_class(FooBar_OT_test)
bpy.types.Scene.tree_items = bpy.props.CollectionProperty(type=TreeItems)
register()
Did not check your actual code, but pretty sure this will fix it. Also, will update the doc the make this point more obvious, we describe several common case of that issue but it's not enough standing out that this can happen with pretty much any data owned by Blender…
This comment was removed by @dopiotrko
thanx @mont29 for clearing that up! (sorry for my uneducated fiddeling here...)
Ok. Thanks for Your help, and sorry for posting it here.