1
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ It doesn't really surprise me much, with makes it an inner scope than the global scope, and you're accessing data_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 the with scope closes. Basically it's a wrapper like python decorators that executes some arbitrary code written elsewhere when its scope finishes. $\endgroup$
    – Gorgious
    Commented Jan 16, 2023 at 20:09
  • $\begingroup$ @Gorgious hey that's actually a very good point, i didn't realize it indeed is out of scope and python didn't throw an error. $\endgroup$
    – Harry McKenzie
    Commented Jan 17, 2023 at 7:26

1 Answer 1

2
$\begingroup$

Exactly that happens: "The sudden change in data type"

Sybren explains it a bit in his "scripting for artists" series (asset linking around 8:40 https://studio.blender.org/training/scripting-for-artists/5eabe54d521eafd0953f6d45/). Ok, not really explains, but Blender does this "magic" -> it exchanges the value from a string list to a collection. So Blender assigns just a new value to this variable. That's all.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .