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()