Skip to main content
added 390 characters in body; edited tags
Source Link
Harry McKenzie
  • 14.5k
  • 11
  • 34
  • 74

bpy.types.Operator on execution blocks How can I prevent operator from blocking Blender interactions while running - HelpInteractions during execution?

I amI'm new to Blender pythonPython programming, and for a personal project am playing around, I'm experimenting with creating a Blender pythonPython addon where. I can doaim to execute an async getasynchronous GET request when I select a button in a panel (custom operator). SeeYou can find an example code snippet below. My current issue is that when I selectupon selecting the operator, it seemsappears to be blockingblock any other Blender interactions until the asyncasynchronous call has finished runningcompletes. AnyDo you have any suggestions/examples for or examples on how I can haveprevent the operatoroperator's logic not blockfrom blocking? I’dI still like the userwant users to be able to continue with editing their models, etc., while these apiAPI calls/downloads happenoccur.

import bpy
import json
import asyncio
import aiohttp

from bpy.types import Operator, Panel
      
# ------------------------------------------------------------------------
#    Operators
# ------------------------------------------------------------------------                
class ADDON_OT_test(Operator): 
    bl_idname = "addon.search"
    bl_label = "Search" 
                
    def execute(self, context):
        scene = context.scene
        mytool = scene.my_properties
            
             
        # Endpoint Resource Path 
        assets_url = "someurl"
                
        # Request Body
        assets_query_body = { }
    
        # Request Headers
        headers = { }
        
        x = asyncio.run(fetch_data(assets_url, assets_query_body, headers))

        if x is None:
            self.report({'ERROR'}, "something went wrong")
        else:
            beautify_data = json.dumps(x, indent=4)
            print(beautify_data)
            self.report({'INFO'}, "all done")
        
        return {'FINISHED'} 
      
# ------------------------------------------------------------------------
#    Panels
# ------------------------------------------------------------------------

class ADDON_PT_browse_search_panel(Panel):
    bl_idname = "ADDON_PT_Search_Results"
    bl_label = "Search Results"

    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    
    def draw  (self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_properties
        
        layout.operator("addon.search", text="Search")
        
# ------------------------------------------------------------------------
#    Additional Functionality
# ------------------------------------------------------------------------          
async def fetch_data(url, query_params=None, headers=None):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, params=query_params, headers=headers) as response:
            print("Status:", response.status)
            if response.status == 200:
                print(response.status)
                return await response.json()
            else:
                print(response.status)
                return None    

# ------------------------------------------------------------------------
#    Register/Deregister
# ------------------------------------------------------------------------
classes = [
    ADDON_PT_browse_search_panel,
    ADDON_OT_test
]

def register():
    for cls in classes: 
        bpy.utils.register_class(cls)

def unregister():
    for cls in reversed(classes): 
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
```

bpy.types.Operator on execution blocks Blender interactions while running - Help?

I am new to Blender python programming, and for a personal project am playing around with creating a Blender python addon where I can do an async get request when I select a button in a panel (custom operator). See example code below. My current issue is that when I select the operator it seems to be blocking any other Blender interactions until the async call has finished running. Any suggestions/examples for how I can have the operator logic not block? I’d still like the user to be able to continue with editing their models, etc while these api calls/downloads happen.

import bpy
import json
import asyncio
import aiohttp

from bpy.types import Operator, Panel
      
# ------------------------------------------------------------------------
#    Operators
# ------------------------------------------------------------------------                
class ADDON_OT_test(Operator): 
    bl_idname = "addon.search"
    bl_label = "Search" 
                
    def execute(self, context):
        scene = context.scene
        mytool = scene.my_properties
            
             
        # Endpoint Resource Path 
        assets_url = "someurl"
                
        # Request Body
        assets_query_body = { }
    
        # Request Headers
        headers = { }
        
        x = asyncio.run(fetch_data(assets_url, assets_query_body, headers))

        if x is None:
            self.report({'ERROR'}, "something went wrong")
        else:
            beautify_data = json.dumps(x, indent=4)
            print(beautify_data)
            self.report({'INFO'}, "all done")
        
        return {'FINISHED'} 
      
# ------------------------------------------------------------------------
#    Panels
# ------------------------------------------------------------------------

class ADDON_PT_browse_search_panel(Panel):
    bl_idname = "ADDON_PT_Search_Results"
    bl_label = "Search Results"

    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    
    def draw  (self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_properties
        
        layout.operator("addon.search", text="Search")
        
# ------------------------------------------------------------------------
#    Additional Functionality
# ------------------------------------------------------------------------          
async def fetch_data(url, query_params=None, headers=None):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, params=query_params, headers=headers) as response:
            print("Status:", response.status)
            if response.status == 200:
                print(response.status)
                return await response.json()
            else:
                print(response.status)
                return None    

# ------------------------------------------------------------------------
#    Register/Deregister
# ------------------------------------------------------------------------
classes = [
    ADDON_PT_browse_search_panel,
    ADDON_OT_test
]

def register():
    for cls in classes: 
        bpy.utils.register_class(cls)

def unregister():
    for cls in reversed(classes): 
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
```

How can I prevent operator from blocking Blender Interactions during execution?

I'm new to Blender Python programming, and for a personal project, I'm experimenting with creating a Blender Python addon. I aim to execute an asynchronous GET request when I select a button in a panel (custom operator). You can find an example code snippet below. My current issue is that upon selecting the operator, it appears to block any other Blender interactions until the asynchronous call completes. Do you have any suggestions or examples on how I can prevent the operator's logic from blocking? I still want users to be able to continue editing their models, etc., while these API calls/downloads occur.

import bpy
import json
import asyncio
import aiohttp

from bpy.types import Operator, Panel
      
# ------------------------------------------------------------------------
#    Operators
# ------------------------------------------------------------------------                
class ADDON_OT_test(Operator): 
    bl_idname = "addon.search"
    bl_label = "Search" 
                
    def execute(self, context):
        scene = context.scene
        mytool = scene.my_properties
            
             
        # Endpoint Resource Path 
        assets_url = "someurl"
                
        # Request Body
        assets_query_body = { }
    
        # Request Headers
        headers = { }
        
        x = asyncio.run(fetch_data(assets_url, assets_query_body, headers))

        if x is None:
            self.report({'ERROR'}, "something went wrong")
        else:
            beautify_data = json.dumps(x, indent=4)
            print(beautify_data)
            self.report({'INFO'}, "all done")
        
        return {'FINISHED'} 
      
# ------------------------------------------------------------------------
#    Panels
# ------------------------------------------------------------------------

class ADDON_PT_browse_search_panel(Panel):
    bl_idname = "ADDON_PT_Search_Results"
    bl_label = "Search Results"

    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    
    def draw  (self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_properties
        
        layout.operator("addon.search", text="Search")
        
# ------------------------------------------------------------------------
#    Additional Functionality
# ------------------------------------------------------------------------          
async def fetch_data(url, query_params=None, headers=None):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, params=query_params, headers=headers) as response:
            print("Status:", response.status)
            if response.status == 200:
                print(response.status)
                return await response.json()
            else:
                print(response.status)
                return None    

# ------------------------------------------------------------------------
#    Register/Deregister
# ------------------------------------------------------------------------
classes = [
    ADDON_PT_browse_search_panel,
    ADDON_OT_test
]

def register():
    for cls in classes: 
        bpy.utils.register_class(cls)

def unregister():
    for cls in reversed(classes): 
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
Source Link

bpy.types.Operator on execution blocks Blender interactions while running - Help?

I am new to Blender python programming, and for a personal project am playing around with creating a Blender python addon where I can do an async get request when I select a button in a panel (custom operator). See example code below. My current issue is that when I select the operator it seems to be blocking any other Blender interactions until the async call has finished running. Any suggestions/examples for how I can have the operator logic not block? I’d still like the user to be able to continue with editing their models, etc while these api calls/downloads happen.

import bpy
import json
import asyncio
import aiohttp

from bpy.types import Operator, Panel
      
# ------------------------------------------------------------------------
#    Operators
# ------------------------------------------------------------------------                
class ADDON_OT_test(Operator): 
    bl_idname = "addon.search"
    bl_label = "Search" 
                
    def execute(self, context):
        scene = context.scene
        mytool = scene.my_properties
            
             
        # Endpoint Resource Path 
        assets_url = "someurl"
                
        # Request Body
        assets_query_body = { }
    
        # Request Headers
        headers = { }
        
        x = asyncio.run(fetch_data(assets_url, assets_query_body, headers))

        if x is None:
            self.report({'ERROR'}, "something went wrong")
        else:
            beautify_data = json.dumps(x, indent=4)
            print(beautify_data)
            self.report({'INFO'}, "all done")
        
        return {'FINISHED'} 
      
# ------------------------------------------------------------------------
#    Panels
# ------------------------------------------------------------------------

class ADDON_PT_browse_search_panel(Panel):
    bl_idname = "ADDON_PT_Search_Results"
    bl_label = "Search Results"

    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    
    def draw  (self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_properties
        
        layout.operator("addon.search", text="Search")
        
# ------------------------------------------------------------------------
#    Additional Functionality
# ------------------------------------------------------------------------          
async def fetch_data(url, query_params=None, headers=None):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, params=query_params, headers=headers) as response:
            print("Status:", response.status)
            if response.status == 200:
                print(response.status)
                return await response.json()
            else:
                print(response.status)
                return None    

# ------------------------------------------------------------------------
#    Register/Deregister
# ------------------------------------------------------------------------
classes = [
    ADDON_PT_browse_search_panel,
    ADDON_OT_test
]

def register():
    for cls in classes: 
        bpy.utils.register_class(cls)

def unregister():
    for cls in reversed(classes): 
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
```