Created
June 17, 2024 13:42
-
-
Save chunibyo-wly/60d5fa7bb1c69500f8c06d5a9155b168 to your computer and use it in GitHub Desktop.
blender 启动 webserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
chunibyo-wly
commented
Jun 17, 2024

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