Add UserAgent header as required by Poly Haven's API terms
This commit is contained in:
parent
9b9a14f7c7
commit
5a7765c2a8
18
addon.py
18
addon.py
@ -28,6 +28,10 @@ bl_info = {
|
|||||||
|
|
||||||
RODIN_FREE_TRIAL_KEY = "k9TcfFoEhNd9cCPP2guHAHHHkctZHIRhZDywZ1euGUXwihbYLpOjQhofby80NJez"
|
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:
|
class BlenderMCPServer:
|
||||||
def __init__(self, host='localhost', port=9876):
|
def __init__(self, host='localhost', port=9876):
|
||||||
self.host = host
|
self.host = host
|
||||||
@ -423,7 +427,7 @@ class BlenderMCPServer:
|
|||||||
if asset_type not in ["hdris", "textures", "models", "all"]:
|
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"}
|
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:
|
if response.status_code == 200:
|
||||||
return {"categories": response.json()}
|
return {"categories": response.json()}
|
||||||
else:
|
else:
|
||||||
@ -445,7 +449,7 @@ class BlenderMCPServer:
|
|||||||
if categories:
|
if categories:
|
||||||
params["categories"] = categories
|
params["categories"] = categories
|
||||||
|
|
||||||
response = requests.get(url, params=params)
|
response = requests.get(url, params=params, headers=REQ_HEADERS)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
# Limit the response size to avoid overwhelming Blender
|
# Limit the response size to avoid overwhelming Blender
|
||||||
assets = response.json()
|
assets = response.json()
|
||||||
@ -465,7 +469,7 @@ class BlenderMCPServer:
|
|||||||
def download_polyhaven_asset(self, asset_id, asset_type, resolution="1k", file_format=None):
|
def download_polyhaven_asset(self, asset_id, asset_type, resolution="1k", file_format=None):
|
||||||
try:
|
try:
|
||||||
# First get the files information
|
# 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:
|
if files_response.status_code != 200:
|
||||||
return {"error": f"Failed to get asset files: {files_response.status_code}"}
|
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
|
# since Blender can't properly load HDR data directly from memory
|
||||||
with tempfile.NamedTemporaryFile(suffix=f".{file_format}", delete=False) as tmp_file:
|
with tempfile.NamedTemporaryFile(suffix=f".{file_format}", delete=False) as tmp_file:
|
||||||
# Download the file
|
# Download the file
|
||||||
response = requests.get(file_url)
|
response = requests.get(file_url, headers=REQ_HEADERS)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return {"error": f"Failed to download HDRI: {response.status_code}"}
|
return {"error": f"Failed to download HDRI: {response.status_code}"}
|
||||||
|
|
||||||
@ -581,7 +585,7 @@ class BlenderMCPServer:
|
|||||||
# Use NamedTemporaryFile like we do for HDRIs
|
# Use NamedTemporaryFile like we do for HDRIs
|
||||||
with tempfile.NamedTemporaryFile(suffix=f".{file_format}", delete=False) as tmp_file:
|
with tempfile.NamedTemporaryFile(suffix=f".{file_format}", delete=False) as tmp_file:
|
||||||
# Download the file
|
# Download the file
|
||||||
response = requests.get(file_url)
|
response = requests.get(file_url, headers=REQ_HEADERS)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
tmp_file.write(response.content)
|
tmp_file.write(response.content)
|
||||||
tmp_path = tmp_file.name
|
tmp_path = tmp_file.name
|
||||||
@ -718,7 +722,7 @@ class BlenderMCPServer:
|
|||||||
main_file_name = file_url.split("/")[-1]
|
main_file_name = file_url.split("/")[-1]
|
||||||
main_file_path = os.path.join(temp_dir, main_file_name)
|
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:
|
if response.status_code != 200:
|
||||||
return {"error": f"Failed to download model: {response.status_code}"}
|
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)
|
os.makedirs(os.path.dirname(include_file_path), exist_ok=True)
|
||||||
|
|
||||||
# Download the included file
|
# 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:
|
if include_response.status_code == 200:
|
||||||
with open(include_file_path, "wb") as f:
|
with open(include_file_path, "wb") as f:
|
||||||
f.write(include_response.content)
|
f.write(include_response.content)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user