bpy.data.libraries.load returns bpy_lib and not 2 objects #96212

Closed
opened 2022-03-07 12:19:42 +01:00 by Daniel Santana · 14 comments

System Information
Operating system: Windows-10-10.0.22000-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 3060/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 511.79

Blender Version
Broken: version: 3.2.0 Alpha, branch: master, commit date: 2022-03-07 06:49, hash: ad2948face
Worked: (newest version of Blender that worked as expected)

Short description of error
When doing a link library load, the return python object is bpy_lib and not 2 objects. Also this objects doesn't seem to be fully exposed to python since it doesn't have any methods.

Exact steps for others to reproduce the error

  1. Open factory default blender.
  2. Go to Scripting tab and create the following script:
import bpy
import os
filename =  os.path.join(os.path.expandvars('$TMP'), 'test.blend')

bpy.data.libraries.write(filename, set(bpy.context.selected_objects), fake_user=True)

with bpy.data.libraries.load(filename, link=True) as (data_from, data_to):
    data_to.objects = data_from.objects
  1. Execute and an error should occur, stating that bpy_lib is not iterable

PS: This happens with any file

**System Information** Operating system: Windows-10-10.0.22000-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 3060/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 511.79 **Blender Version** Broken: version: 3.2.0 Alpha, branch: master, commit date: 2022-03-07 06:49, hash: `ad2948face` Worked: (newest version of Blender that worked as expected) **Short description of error** When doing a link library load, the return python object is bpy_lib and not 2 objects. Also this objects doesn't seem to be fully exposed to python since it doesn't have any methods. **Exact steps for others to reproduce the error** 1. Open factory default blender. 2. Go to Scripting tab and create the following script: ``` import bpy import os filename = os.path.join(os.path.expandvars('$TMP'), 'test.blend') bpy.data.libraries.write(filename, set(bpy.context.selected_objects), fake_user=True) with bpy.data.libraries.load(filename, link=True) as (data_from, data_to): data_to.objects = data_from.objects ``` 3. Execute and an error should occur, stating that bpy_lib is not iterable PS: This happens with any file
Author

Added subscriber: @dgsantana

Added subscriber: @dgsantana
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

Just checking if the filename path really exists? (on my linux, I dont have $TMP defined).
Does the following equally fail for you? (this is working fine on my end -- meaning it does not error and prints the selected objects in the end...)

import bpy
import os
filename =  os.path.join(bpy.app.tempdir, 'test.blend')
print(filename)

bpy.data.libraries.write(filename, set(bpy.context.selected_objects), fake_user=True)

with bpy.data.libraries.load(filename, link=True) as (data_from, data_to):
    data_to.objects = data_from.objects
    
print(data_to.objects)
Just checking if the filename path really exists? (on my linux, I dont have $TMP defined). Does the following equally fail for you? (this is working fine on my end -- meaning it does not error and prints the selected objects in the end...) ``` import bpy import os filename = os.path.join(bpy.app.tempdir, 'test.blend') print(filename) bpy.data.libraries.write(filename, set(bpy.context.selected_objects), fake_user=True) with bpy.data.libraries.load(filename, link=True) as (data_from, data_to): data_to.objects = data_from.objects print(data_to.objects) ```
Author

Sorry, my bad about the path I was using windows. Weird that worked, my blender should have been in a weird state.
But still happens in a larger script.

import os
import bpy
bl_info = {
    "name": "Export and Link",
    "author": "Daniel Santana",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "File > Export > Export and Link",
    "description": "Export and links the selected objects",
    "warning": "",
    "doc_url": "",
    "category": "Import-Export",
}

def write_lib_link(context, filepath:str, link:bool):
    
    bpy.data.libraries.write(filepath, set(context.selected_objects), fake_user=True)
    if link:
        bpy.ops.object.delete(use_global=False)
        with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
            data_to.objects = data_from.objects
        for obj in data_to:
            context.view_layer.active_layer_collection.collection.objects.link(obj)

    return {'FINISHED'}


- ExportHelper is a helper class, defines filename and
- invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator


class ExportAndLink(Operator, ExportHelper):
    """This appears in the tooltip of the operator and in the generated docs"""
    bl_idname = "export.export_and_link"  # important since its how bpy.ops.import_test.some_data is constructed
    bl_label = "Export And Link"

    # ExportHelper mixin class uses this
    filename_ext = ".blend"

    filter_glob: StringProperty(
        default="*.blend",
        options={'HIDDEN'},
        maxlen=255,  # Max internal buffer length, longer would be clamped.
    )

    - List of operator properties, the attributes will be assigned
    - to the class instance from the operator settings before calling.
    use_setting: BoolProperty(
        name="Export and Link",
        description="Exports and links the selected objects.",
        default=True,
    )

    type: EnumProperty(
        name="Example Enum",
        description="Choose between two items",
        items=(
            ('OPT_A', "First Option", "Description one"),
            ('OPT_B', "Second Option", "Description two"),
        ),
        default='OPT_A',
    )

    def execute(self, context):
        return write_lib_link(context, self.filepath, self.use_setting)


# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    self.layout.operator(ExportAndLink.bl_idname, text="Export and Link")

# Register and add to the "file selector" menu (required to use F3 search "Text Export Operator" for quick access)
def register():
    bpy.utils.register_class(ExportAndLink)
    bpy.types.TOPBAR_MT_file_export.append(menu_func_export)


def unregister():
    bpy.utils.unregister_class(ExportAndLink)
    bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.export.export_and_link('INVOKE_DEFAULT')
Sorry, my bad about the path I was using windows. Weird that worked, my blender should have been in a weird state. But still happens in a larger script. ``` import os import bpy bl_info = { "name": "Export and Link", "author": "Daniel Santana", "version": (1, 0), "blender": (2, 80, 0), "location": "File > Export > Export and Link", "description": "Export and links the selected objects", "warning": "", "doc_url": "", "category": "Import-Export", } def write_lib_link(context, filepath:str, link:bool): bpy.data.libraries.write(filepath, set(context.selected_objects), fake_user=True) if link: bpy.ops.object.delete(use_global=False) with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to): data_to.objects = data_from.objects for obj in data_to: context.view_layer.active_layer_collection.collection.objects.link(obj) return {'FINISHED'} - ExportHelper is a helper class, defines filename and - invoke() function which calls the file selector. from bpy_extras.io_utils import ExportHelper from bpy.props import StringProperty, BoolProperty, EnumProperty from bpy.types import Operator class ExportAndLink(Operator, ExportHelper): """This appears in the tooltip of the operator and in the generated docs""" bl_idname = "export.export_and_link" # important since its how bpy.ops.import_test.some_data is constructed bl_label = "Export And Link" # ExportHelper mixin class uses this filename_ext = ".blend" filter_glob: StringProperty( default="*.blend", options={'HIDDEN'}, maxlen=255, # Max internal buffer length, longer would be clamped. ) - List of operator properties, the attributes will be assigned - to the class instance from the operator settings before calling. use_setting: BoolProperty( name="Export and Link", description="Exports and links the selected objects.", default=True, ) type: EnumProperty( name="Example Enum", description="Choose between two items", items=( ('OPT_A', "First Option", "Description one"), ('OPT_B', "Second Option", "Description two"), ), default='OPT_A', ) def execute(self, context): return write_lib_link(context, self.filepath, self.use_setting) # Only needed if you want to add into a dynamic menu def menu_func_export(self, context): self.layout.operator(ExportAndLink.bl_idname, text="Export and Link") # Register and add to the "file selector" menu (required to use F3 search "Text Export Operator" for quick access) def register(): bpy.utils.register_class(ExportAndLink) bpy.types.TOPBAR_MT_file_export.append(menu_func_export) def unregister(): bpy.utils.unregister_class(ExportAndLink) bpy.types.TOPBAR_MT_file_export.remove(menu_func_export) if __name__ == "__main__": register() # test call bpy.ops.export.export_and_link('INVOKE_DEFAULT') ```
Member

Added subscriber: @PratikPB2123

Added subscriber: @PratikPB2123

Added subscriber: @ideasman42

Added subscriber: @ideasman42

Changed status from 'Needs User Info' to: 'Archived'

Changed status from 'Needs User Info' to: 'Archived'

This isn't a bug for obj in data_to: should be for obj in data_to.objects:.

This isn't a bug `for obj in data_to:` should be `for obj in data_to.objects:`.
Author

Thanks Campbell,
Sorry for my mistake. Even though it shouldn't leave blender in a weird state, because any subsequent calls to bpy.data.libraries.load stop working.

Thanks Campbell, Sorry for my mistake. Even though it shouldn't leave blender in a weird state, because any subsequent calls to `bpy.data.libraries.load` stop working.

@dgsantana could you please report a bug with a script to redo the error.

@dgsantana could you please report a bug with a script to redo the error.
Author

Ok course.

Ok course.
Author

Ok, maybe it's just bad usage on my part, I was doing this on the console, which gives the error, so it only works with with context:

import os
bpy.data.libraries.write(filename, set(bpy.context.selected_objects), fake_user=True)
d,f = bpy.data.libraries.load(filename, link=True)
Ok, maybe it's just bad usage on my part, I was doing this on the console, which gives the error, so it only works with `with` context: ``` import os bpy.data.libraries.write(filename, set(bpy.context.selected_objects), fake_user=True) d,f = bpy.data.libraries.load(filename, link=True) ```

Right, you'll need to use a context manager.

Right, you'll need to use a context manager.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#96212
No description provided.