Skip to content

Instantly share code, notes, and snippets.

@chunibyo-wly
Created June 17, 2024 13:42
Show Gist options
  • Save chunibyo-wly/60d5fa7bb1c69500f8c06d5a9155b168 to your computer and use it in GitHub Desktop.
Save chunibyo-wly/60d5fa7bb1c69500f8c06d5a9155b168 to your computer and use it in GitHub Desktop.
blender 启动 webserver
import bpy
import threading
import uvicorn
from fastapi import FastAPI
# Define the FastAPI application
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/blend")
def blend_operation():
# Add your Blender-related operations here
return {"status": "Blender operation successful"}
# Function to run the FastAPI server
def run_fastapi():
uvicorn.run(app, host="0.0.0.0", port=8000)
# Blender Operator to start the FastAPI server
class StartFastAPIServer(bpy.types.Operator):
bl_idname = "wm.start_fastapi_server"
bl_label = "Start FastAPI Server"
def execute(self, context):
# Start the FastAPI server in a separate thread
server_thread = threading.Thread(target=run_fastapi, daemon=True)
server_thread.start()
self.report({'INFO'}, "FastAPI server started")
return {'FINISHED'}
# Register the operator
def register():
bpy.utils.register_class(StartFastAPIServer)
def unregister():
bpy.utils.unregister_class(StartFastAPIServer)
if __name__ == "__main__":
register()
@chunibyo-wly
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment