Add UserAgent header as required by Poly Haven's API terms

This commit is contained in:
Greg Zaal 2025-08-12 10:48:59 +02:00
parent 9b9a14f7c7
commit 5a7765c2a8

View File

@ -28,6 +28,10 @@ bl_info = {
RODIN_FREE_TRIAL_KEY = "k9TcfFoEhNd9cCPP2guHAHHHkctZHIRhZDywZ1euGUXwihbYLpOjQhofby80NJez"
# Add User-Agent as required by Poly Haven API
REQ_HEADERS = requests.utils.default_headers()
REQ_HEADERS.update({"User-Agent": "blender-mcp"})
class BlenderMCPServer:
def __init__(self, host='localhost', port=9876):
self.host = host
@ -423,7 +427,7 @@ class BlenderMCPServer:
if asset_type not in ["hdris", "textures", "models", "all"]:
return {"error": f"Invalid asset type: {asset_type}. Must be one of: hdris, textures, models, all"}
response = requests.get(f"https://api.polyhaven.com/categories/{asset_type}")
response = requests.get(f"https://api.polyhaven.com/categories/{asset_type}", headers=REQ_HEADERS)
if response.status_code == 200:
return {"categories": response.json()}
else:
@ -445,7 +449,7 @@ class BlenderMCPServer:
if categories:
params["categories"] = categories
response = requests.get(url, params=params)
response = requests.get(url, params=params, headers=REQ_HEADERS)
if response.status_code == 200:
# Limit the response size to avoid overwhelming Blender
assets = response.json()
@ -465,7 +469,7 @@ class BlenderMCPServer:
def download_polyhaven_asset(self, asset_id, asset_type, resolution="1k", file_format=None):
try:
# First get the files information
files_response = requests.get(f"https://api.polyhaven.com/files/{asset_id}")
files_response = requests.get(f"https://api.polyhaven.com/files/{asset_id}", headers=REQ_HEADERS)
if files_response.status_code != 200:
return {"error": f"Failed to get asset files: {files_response.status_code}"}
@ -485,7 +489,7 @@ class BlenderMCPServer:
# since Blender can't properly load HDR data directly from memory
with tempfile.NamedTemporaryFile(suffix=f".{file_format}", delete=False) as tmp_file:
# Download the file
response = requests.get(file_url)
response = requests.get(file_url, headers=REQ_HEADERS)
if response.status_code != 200:
return {"error": f"Failed to download HDRI: {response.status_code}"}
@ -581,7 +585,7 @@ class BlenderMCPServer:
# Use NamedTemporaryFile like we do for HDRIs
with tempfile.NamedTemporaryFile(suffix=f".{file_format}", delete=False) as tmp_file:
# Download the file
response = requests.get(file_url)
response = requests.get(file_url, headers=REQ_HEADERS)
if response.status_code == 200:
tmp_file.write(response.content)
tmp_path = tmp_file.name
@ -718,7 +722,7 @@ class BlenderMCPServer:
main_file_name = file_url.split("/")[-1]
main_file_path = os.path.join(temp_dir, main_file_name)
response = requests.get(file_url)
response = requests.get(file_url, headers=REQ_HEADERS)
if response.status_code != 200:
return {"error": f"Failed to download model: {response.status_code}"}
@ -736,7 +740,7 @@ class BlenderMCPServer:
os.makedirs(os.path.dirname(include_file_path), exist_ok=True)
# Download the included file
include_response = requests.get(include_url)
include_response = requests.get(include_url, headers=REQ_HEADERS)
if include_response.status_code == 200:
with open(include_file_path, "wb") as f:
f.write(include_response.content)