DIYer22 / bpycv

Computer vision utils for Blender (generate instance annoatation, depth and 6D pose by one line code)
MIT License
464 stars 58 forks source link

Global variables break undo functionality #10

Closed ggalan87 closed 3 years ago

ggalan87 commented 3 years ago

I want to use render_data function within an undo() context because otherwise the annotation rendering modified the material of several background objects in my scene. I want to run it in a loop for several modifications of some objects. The script run for only the first time and then I got an error "ReferenceError: StructRNA of type Scene has been removed" in https://github.com/DIYer22/bpycv/blob/0ff76443642544e7459024e37b28f2bc1d8cce90/bpycv/render_utils.py#L35

As I saw from your code in https://github.com/DIYer22/bpycv/blob/0ff76443642544e7459024e37b28f2bc1d8cce90/bpycv/render_utils.py#L17 you use some global variables, e.g. for the scene. While for other cases you don't https://github.com/DIYer22/bpycv/blob/0ff76443642544e7459024e37b28f2bc1d8cce90/bpycv/pose_utils.py#L24

The error I got above was due to the global scene variable which refers to deleted data, caused due to the undo operation. Such behavior is described in Blender documentation https://docs.blender.org/api/blender2.8/info_gotcha.html#undo-redo

A working modfication is to override the global scene variable and re-obtain it from the context within here https://github.com/DIYer22/bpycv/blob/0ff76443642544e7459024e37b28f2bc1d8cce90/bpycv/render_utils.py#L25.

I don't know however where else these global variables are used.

DIYer22 commented 3 years ago

Thank you for feedback. I have fix it by this commit . You can upgrade bpycv to version 0.2.21 and try again.