I have the following script. What exactly is happening in the background, why does the element type in data_from.scenes[0]
change from class 'str'
to class 'bpy.types.Scene'
. From this thread I quote:
It's just a special case of reusing a variable, which is a bad idea - mainly because it makes it hard to understand what a variable contains at any given point in the program flow. See e.g. Should I reuse variables?
import bpy
filepath = "/path/to/blend/file.blend"
with bpy.data.libraries.load(filepath) as (data_from, data_to):
data_to.scenes = data_from.scenes
print("T1:", type(data_from.scenes[0])) # prints class 'str'
print("T2:", type(data_from.scenes[0])) # prints class 'bpy.types.Scene'
The sudden change in data type confused me.
with
makes it an inner scope than the global scope, and you're accessingdata_from
when this scope has been closed, which is not prevented by python, but can lead to funky behaviour, because you don't have a say in what happens when thewith
scope closes. Basically it's a wrapper like python decorators that executes some arbitrary code written elsewhere when its scope finishes. $\endgroup$