Page MenuHome

PyAPI: add bpy.types.BlendFile.temp_data for temporary library loading
ClosedPublic

Authored by Campbell Barton (campbellbarton) on Mar 4 2021, 8:12 AM.

Details

Summary

This adds support for creating a BlendFile (internally called Main),
which is limited to a context.

This can now be passed to bpy.data.libraries.load, supporting loading temporary data.

To prevent errors caused by mixing the temporary ID's with data in bpy.data
they are tagged as temporary so they can't be assigned to properties,
however they can be passed as arguments to functions.


  • This adds id_tag_extra to support adding extra ID tags at load time. This will be split off into a separate commit.

This is one of the requirements for T86183: Pose Library: temporarily load Action asset.

Example usage:

1import bpy
2lib = "/path/to/file.blend"
3with bpy.types.BlendData.temp_data() as temp_data:
4 with temp_data.libraries.load(lib) as (data_from, data_to):
5 data_to.objects = data_from.objects
6 print(data_from.objects)
7 print(temp_data.objects[:])
8
9 # This would raise an error, good!
10 # "TypeError: bpy_struct: item.attr = val: Object.parent ID type assignment is temporary, can't assign"
11 # bpy.data.objects[0].parent = temp_data.objects[0]
12
13# This would raise an error, also good!
14# "ReferenceError: StructRNA of type BlendData has been removed"
15# print(temp_data.objects[:])


Other Topics

These topics could be investigated later.

  • Support writing temporary Main from bpy.data.libraries.write(..).
  • Support merging the temporary Main into G.main (or other instances of Main).

Diff Detail

Repository
rB Blender
Branch
TEMP-DATA-API (branched from master)
Build Status
Buildable 13294
Build 13294: arc lint + arc unit

Event Timeline

Campbell Barton (campbellbarton) requested review of this revision.Mar 4 2021, 8:12 AM
Campbell Barton (campbellbarton) created this revision.
Campbell Barton (campbellbarton) retitled this revision from Python API for temporary library loading to PyAPI: add bpy.types.BlendFile.temp_data for temporary library loading.Mar 4 2021, 8:38 AM
Campbell Barton (campbellbarton) edited the summary of this revision. (Show Details)
Campbell Barton (campbellbarton) edited the summary of this revision. (Show Details)
Campbell Barton (campbellbarton) edited the summary of this revision. (Show Details)
Sybren A. Stüvel (sybren) requested changes to this revision.Mar 4 2021, 11:44 AM

LGTM except for one little thing.

source/blender/python/intern/bpy_library_load.c
218

Doesn't this violate the "Only set when this doesn't match #G_MAIN" comment in the BPy_Library struct?

It's probably more correct to swap this line with the next and do ret->bmain = ret->bmain_is_temp ? bmain : NULL. On the other hand, that would require adding NULL-checks in other areas of the code. Maybe it's better to update the comment itself then, to read something like:

Set to #G_MAIN when bmain_is_temp=false, or to the temporary main when bmain_is_temp=true

This revision is now accepted and ready to land.Mar 4 2021, 11:52 AM

Added regular method support to RNA collections (in master rBd9e567d36573: PyAPI: support methods for collection properties).

Use methods to avoid passing the BlendData as an argument to 'load'.

with bpy.types.BlendData.temp_data() as temp_data:
    with bpy.data.libraries.load(lib, data=temp_data) as (data_from, data_to)

Is now written as:

with bpy.types.BlendData.temp_data() as temp_data:
    with temp_data.libraries.load(lib) as (data_from, data_to)
Bastien Montagne (mont29) requested changes to this revision.Mar 4 2021, 2:28 PM
Bastien Montagne (mont29) added inline comments.
source/blender/python/intern/bpy_library_load.c
378–379

I think the code behind this code (in readfile.c) needs to be updated to ensure no session uuid is generated (and global counter is not increased) when bmain_is_temp is True?

This revision now requires changes to proceed.Mar 4 2021, 2:28 PM
Campbell Barton (campbellbarton) edited the summary of this revision. (Show Details)
  • Add a generic way to set the TAG flag when loading ID's.
  • Don't set session UUID for temporary ID's
  • Add assert if any *unsafe* tags are set as extra tags.
  • Add comment explaining why LibraryLink_Params.id_tag_extra is duplicated in FileData.
Bastien Montagne (mont29) requested changes to this revision.Mar 8 2021, 12:33 PM
Bastien Montagne (mont29) added inline comments.
source/blender/blenloader/intern/readfile.c
2427–2430

Think this is also needed in create_placeholder ? Loading IDs from a lib, even temporarily, may also imply loading IDs from other libs, which could be missing too.

This revision now requires changes to proceed.Mar 8 2021, 12:33 PM

Disable setting the UUID for create_placeholder() & in versioning code, also add assert to ensure this never happens.

This revision is now accepted and ready to land.Mar 8 2021, 2:02 PM