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
# ------------------------------------------------------------------------
# TODO: RENAME TO SEARCH
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()