调用Meshy的接口,使用文本生成3D模型
接口
接口分3步调用:
每一步都返回一个任务id,用于下一步任务使用
生成粗模
import requests
YOUR_API_KEY = '*********************'
payload = {
"mode": "preview",
"prompt": "a fox mask",
"art_style": "realistic",
"negative_prompt": "low quality, low resolution, low poly, ugly"
}
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}
response = requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=headers,
json=payload,
)
response.raise_for_status()
print(response.json())
细化
import requests
YOUR_API_KEY = '**************'
payload = {
"mode": "refine",
"preview_task_id": "018ea235-c062-788e-bbec-a54f6291818d"
}
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}
response = requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=headers,
json=payload,
)
response.raise_for_status()
print(response.json())
生成并获取链接
import requests
task_id = "018e9e6a-51c9-7cd7-9d17-b1494707ac45"
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}
response = requests.get(
f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
headers=headers,
)
response.raise_for_status()
print(response.json())
实现
问题
需要注意的是,第二步和第三步需要等待Meshy服务器执行完毕,如果远端服务器未完成任务,就区调接口的话:
- 对refine过程来说,会报bad request错误
- 对第三步来说,会产生一个空的链接
解决方案
采用轮询的思想,等待一段时间后重复请求;
但是要注意:
- 后端和前端有请求超时的时间限制,而这个接口需要等待的时间较长(对于免费用户来说大概在5min左右),需要设置超时的时间
- 设置最大请求次数,避免死循环
工作流程
由于三次请求过于复杂,不方便Unity(客户端)使用,采用后端进行整合代理的方式,简化了接口的调用
代码
使用了Flask
框架
service.py
内容如下:
import requests
import time
YOUR_API_KEY = '**************'
def preview(prompt):
payload = {
"mode": "preview",
"prompt": prompt,
"art_style": "realistic",
"negative_prompt": "low quality, low resolution, low poly, ugly"
}
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}
response = requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=headers,
json=payload,
)
response.raise_for_status()
preview_task_id = response.json()['result']
return preview_task_id
def refine(preview_task_id):
time.sleep(5)
payload = {
"mode": "refine",
"preview_task_id": preview_task_id
}
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}
response = requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=headers,
json=payload,
)
try:
response.raise_for_status()
return response.json()['result']
except Exception:
print('retry refine...')
return refine(preview_task_id)
def getUrl(task_id):
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}
response = requests.get(
f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
headers=headers,
)
response.raise_for_status()
res = response.json()
try:
model_urls = res['model_urls']
fbx = model_urls['fbx']
return fbx
except Exception:
return 0
def tryGetUrl(task_id):
time.sleep(30)
fbx = getUrl(task_id)
print('retry generate...')
if fbx==0 or fbx=='':
fbx = tryGetUrl(task_id)
return fbx
server.py
内容如下:
from flask import Flask, request
import service
app = Flask(__name__)
app.config["timeout"] = 1800
@app.route('/getModelUrl', methods=['POST'])
def sign():
prompt = request.form.get('prompt')
preview_task_id = service.preview(prompt)
print(preview_task_id)
task_id = service.refine(preview_task_id)
print(task_id)
url = service.tryGetUrl(task_id)
return url
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8087)