Adding assets (modifiers, such as Smooth by Angle) not working in background Python script because Asset list might not be fully loaded yet #117399

Open
opened 2024-01-22 10:58:18 +01:00 by Matthias_Schaefer · 7 comments

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: Quadro RTX 3000/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 528.02

Blender Version
Broken: version: 4.1.0 Alpha, branch: main, commit date: 2024-01-17 00:18, hash: 35028d9480b0
Worked: None as it is a new feature

Short description of error
I try to apply the new "Smooth by Angle" operator from a Python script that runs in a Blender background process. But while it works from inside of Blender it fails when running from the command line as background with the message:

Warning: Asset loading is unfinished

Exact steps for others to reproduce the error

  1. Created a copy of the default .blend file and saved it as test_simple.blend
  2. Create a small Python file test_simple.py with this content:
import bpy
this_object = bpy.data.objects["Cube"]
modifier_status = bpy.ops.object.modifier_add_node_group(asset_library_type="ESSENTIALS", asset_library_identifier="", relative_asset_identifier="geometry_nodes\\smooth_by_angle.blend\\NodeTree\\Smooth by Angle")
  1. Call Blender from the command line (in the directory with the blend and py files) with blender test_simple.blend --background -P test_simple.py

As result I get Warning: Asset loading is unfinished. If the modified blend file is saved to the disk and opened in blender one can see that a GeoemetryNodes group was created for the cube but that it is empty.

Running exactly the same script from inside Blender while the test_simple.blend is opened works fine and produces the expected modifier.

**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: Quadro RTX 3000/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 528.02 **Blender Version** Broken: version: 4.1.0 Alpha, branch: main, commit date: 2024-01-17 00:18, hash: `35028d9480b0` Worked: None as it is a new feature **Short description of error** I try to apply the new "Smooth by Angle" operator from a Python script that runs in a Blender background process. But while it works from inside of Blender it fails when running from the command line as background with the message: `Warning: Asset loading is unfinished` **Exact steps for others to reproduce the error** 1. Created a copy of the default .blend file and saved it as `test_simple.blend` 2. Create a small Python file `test_simple.py` with this content: ```Py import bpy this_object = bpy.data.objects["Cube"] modifier_status = bpy.ops.object.modifier_add_node_group(asset_library_type="ESSENTIALS", asset_library_identifier="", relative_asset_identifier="geometry_nodes\\smooth_by_angle.blend\\NodeTree\\Smooth by Angle") ``` 3. Call Blender from the command line (in the directory with the blend and py files) with `blender test_simple.blend --background -P test_simple.py` As result I get `Warning: Asset loading is unfinished`. If the modified blend file is saved to the disk and opened in blender one can see that a GeoemetryNodes group was created for the cube but that it is empty. Running exactly the same script from inside Blender while the `test_simple.blend` is opened works fine and produces the expected modifier.
Matthias_Schaefer added the
Type
Report
Priority
Normal
Status
Needs Triage
labels 2024-01-22 10:58:18 +01:00
Member

Can confirm.

Not sure if there is a way to ensure the Asset list is loaded from python, will ask around.

Note this might be on the border of a feature request if such functionality/handler/callback does not exist (yet).

Can confirm. Not sure if there is a way to ensure the Asset list is loaded from python, will ask around. Note this might be on the border of a feature request if such functionality/handler/callback does not exist (yet).
Philipp Oeser added
Status
Confirmed
Module
Pipeline, Assets & IO
and removed
Status
Needs Triage
labels 2024-01-23 16:35:22 +01:00
Member

Maybe it would be ideal if this operator was blocking when called with exec rather than invoke. I'm not sure how to load assets in a blocking way though, I haven't seen the asset system expose that functionality.

Maybe it would be ideal if this operator was blocking when called with `exec` rather than `invoke`. I'm not sure how to load assets in a blocking way though, I haven't seen the asset system expose that functionality.
Member

Might be overkill, but maybe add a handler (for when all the loading has finished)?

Might be overkill, but maybe add a handler (for when all the loading has finished)?

Thank you for checking and confirming. Unfortunately this effect avoids usage of the "Smooth by Angle" from background Python scripts while it was no problem at all with the old "Auto Smooth" option. I fully understand the idea behind introducing the new way with the GeometryNodes but this is limiting in this application.

I thought about using timers to delay the execution when I have to apply the "Smooth by Angle" but then found out that this is limited in background Python scripts when the UI is not loaded.

I think for now I will try to rework my script to first load all geometry and then step through all objects and apply the modifier. Hopefully loading all geometry first keeps Blender busy until the required assets are loaded. But depending on the size of the assets and the amount of data that I have to load this will be a random behavior.

I like the idea of a handler etc. that indicates that loading has finished.

Thank you for checking and confirming. Unfortunately this effect avoids usage of the "Smooth by Angle" from background Python scripts while it was no problem at all with the old "Auto Smooth" option. I fully understand the idea behind introducing the new way with the GeometryNodes but this is limiting in this application. I thought about using timers to delay the execution when I have to apply the "Smooth by Angle" but then found out that this is limited in background Python scripts when the UI is not loaded. I think for now I will try to rework my script to first load all geometry and then step through all objects and apply the modifier. Hopefully loading all geometry first keeps Blender busy until the required assets are loaded. But depending on the size of the assets and the amount of data that I have to load this will be a random behavior. I like the idea of a handler etc. that indicates that loading has finished.
Philipp Oeser changed title from Smooth by Angle not working in background Python script to Adding assets (modifiers, such as Smooth by Angle) not working in background Python script because Asset list might not be fully loaded yet 2024-01-24 09:15:44 +01:00
Member

Got confirmation from @JulianEisel that there is no such system in place (yet).

For now, delaying the procedure of loading asset (modifiers) in some other way seems the best choice.

Got confirmation from @JulianEisel that there is no such system in place (yet). For now, delaying the procedure of loading asset (modifiers) in some other way seems the best choice.
Philipp Oeser added
Type
Known Issue
and removed
Type
Report
labels 2024-01-25 08:37:43 +01:00
Member

The asset loading is currently written to be non-blocking and happen in the background as it's first needed. There is no blocking version of the code, so the first time an operator like this is run, it will just trigger the loading of assets.

The reason having an object in the scene makes this work is that the assets are loaded for node tools.

I think we should just load the all library as soon as Blender starts, we aren't going to be able to avoid that much longer. It will already be needed for node tools in object mode, and brush assets. And node tools should work without an active object in the future too.
@JulianEisel What do you think about that?

The asset loading is currently written to be non-blocking and happen in the background as it's first needed. There is no blocking version of the code, so the first time an operator like this is run, it will just trigger the loading of assets. The reason having an object in the scene makes this work is that the assets are loaded for node tools. I think we should just load the all library as soon as Blender starts, we aren't going to be able to avoid that much longer. It will already be needed for node tools in object mode, and brush assets. And node tools should work without an active object in the future too. @JulianEisel What do you think about that?
Contributor

I just ran into this issue while updating my pcb2blender addon for Blender 4.1. I've found that as a workaround for now (althought this is obviously not very ideal), loading the asset library manually seems to do the trick.

Here's the helper function I use now:

(Edit: Seems like the location of the internal assets is system dependant.)

def get_internal_asset_path():
    for path_type in ("LOCAL", "SYSTEM", "USER"):
        path = Path(bpy.utils.resource_path(path_type)) / "datafiles" / "assets"
        if path.exists():
            return path
    assert False

SMOOTH_BY_ANGLE_ASSET_PATH = str(
    get_internal_asset_path() / "geometry_nodes" / "smooth_by_angle.blend"
)
SMOOTH_BY_ANGLE_NODE_GROUP_NAME = "Smooth by Angle"
def add_smooth_by_angle_modifier(obj):
    global SMOOTH_BY_ANGLE_NODE_GROUP_NAME

    smooth_by_angle_node_group = bpy.data.node_groups.get(SMOOTH_BY_ANGLE_NODE_GROUP_NAME)
    if not smooth_by_angle_node_group or smooth_by_angle_node_group.type != "GEOMETRY":
        with bpy.data.libraries.load(SMOOTH_BY_ANGLE_ASSET_PATH) as (data_from, data_to):
            data_to.node_groups = [SMOOTH_BY_ANGLE_NODE_GROUP_NAME]
        smooth_by_angle_node_group = data_to.node_groups[0]
        SMOOTH_BY_ANGLE_NODE_GROUP_NAME = smooth_by_angle_node_group.name

    modifier = obj.modifiers.new("Smooth by Angle", "NODES")
    modifier.node_group = smooth_by_angle_node_group
I just ran into this issue while updating my [pcb2blender addon](https://github.com/30350n/pcb2blender) for Blender 4.1. I've found that as a workaround for now (althought this is obviously not very ideal), loading the asset library manually seems to do the trick. Here's the helper function I use now: (Edit: Seems like the location of the internal assets is system dependant.) ```py def get_internal_asset_path(): for path_type in ("LOCAL", "SYSTEM", "USER"): path = Path(bpy.utils.resource_path(path_type)) / "datafiles" / "assets" if path.exists(): return path assert False SMOOTH_BY_ANGLE_ASSET_PATH = str( get_internal_asset_path() / "geometry_nodes" / "smooth_by_angle.blend" ) SMOOTH_BY_ANGLE_NODE_GROUP_NAME = "Smooth by Angle" def add_smooth_by_angle_modifier(obj): global SMOOTH_BY_ANGLE_NODE_GROUP_NAME smooth_by_angle_node_group = bpy.data.node_groups.get(SMOOTH_BY_ANGLE_NODE_GROUP_NAME) if not smooth_by_angle_node_group or smooth_by_angle_node_group.type != "GEOMETRY": with bpy.data.libraries.load(SMOOTH_BY_ANGLE_ASSET_PATH) as (data_from, data_to): data_to.node_groups = [SMOOTH_BY_ANGLE_NODE_GROUP_NAME] smooth_by_angle_node_group = data_to.node_groups[0] SMOOTH_BY_ANGLE_NODE_GROUP_NAME = smooth_by_angle_node_group.name modifier = obj.modifiers.new("Smooth by Angle", "NODES") modifier.node_group = smooth_by_angle_node_group ```
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#117399
No description provided.