If, after opening one .blend file, we need to view data from another .blend file, we can use the temporary data (temp_data) context.
First, let’s determine the path to the file being viewed:
1 |
filepath = bpy.path.abspath("//") + 'external_blend_file.blend' |
Create a temporary data context “temp_data” for the specified file:
1 |
with bpy.data.temp_data(filepath=filepath) as temp_data: |
However, if we now try to view the contents of the temporary file, we will get an empty list:
1 2 3 |
print(temp_data.objects[:]) # [] |
To access the contents of the “temp_data”, it must be loaded using the temp_data.libraries.load() function:
1 2 |
with temp_data.libraries.load(filepath) as (data_from, data_to): data_to.objects = data_from.objects |
Now we can view the contents of the “temp_data”:
1 2 3 |
print(temp_data.objects[:]) # [bpy.data.objects['Cube'], bpy.data.objects['Empty'], bpy.data.objects['Suzanne']] |
After the block “with bpy.data.temp_data(filepath=filepath) as temp_data” completes, all temporary data and temporary context will be automatically cleared.
Full code example:
1 2 3 4 5 6 7 8 9 10 |
filepath = bpy.path.abspath("//") + 'external_blend_file.blend' # using temp data context with bpy.data.temp_data(filepath=filepath) as temp_data: # empty at first print(temp_data.objects[:]) with temp_data.libraries.load(filepath) as (data_from, data_to): data_to.objects = data_from.objects # full of data after reading print(temp_data.objects[:]) |