Loading...
Loading...
Generate 3D models and objects from text prompts or images using ModelsLab's 3D API. Transform 2D images into 3D representations or create 3D objects from text descriptions.
npx skill4agent add modelslab/skills modelslab-3d-generationPOST https://modelslab.com/api/v6/3d/text_to_3dPOST https://modelslab.com/api/v6/3d/image_to_3dimport requests
import time
def text_to_3d(prompt, api_key, num_inference_steps=50):
"""Generate a 3D object from a text prompt.
Args:
prompt: Text description of the 3D object
api_key: Your ModelsLab API key
num_inference_steps: Quality (higher = better, slower)
Returns:
URL of the 3D model file
"""
response = requests.post(
"https://modelslab.com/api/v6/3d/text_to_3d",
json={
"key": api_key,
"prompt": prompt,
"num_inference_steps": num_inference_steps
}
)
data = response.json()
if data["status"] == "error":
raise Exception(f"Error: {data['message']}")
if data["status"] == "success":
return data["output"][0]
# 3D generation is async - poll for results
request_id = data["id"]
print(f"3D generation started... Request ID: {request_id}")
print(f"ETA: {data.get('eta', 'unknown')} seconds")
return poll_3d_result(request_id, api_key)
def poll_3d_result(request_id, api_key, timeout=600):
"""Poll for 3D generation results."""
start_time = time.time()
while time.time() - start_time < timeout:
fetch = requests.post(
f"https://modelslab.com/api/v6/3d/fetch/{request_id}",
json={"key": api_key}
)
result = fetch.json()
if result["status"] == "success":
return result["output"][0]
elif result["status"] == "failed":
raise Exception(result.get("message", "Generation failed"))
print(f"Status: processing... ({int(time.time() - start_time)}s elapsed)")
time.sleep(10)
raise Exception("Timeout waiting for 3D generation")
# Usage
model_url = text_to_3d(
"A medieval sword with ornate handle and detailed engravings",
"your_api_key",
num_inference_steps=50
)
print(f"3D model ready: {model_url}")def image_to_3d(image_url, api_key):
"""Convert a 2D image into a 3D model.
Args:
image_url: URL of the 2D image
api_key: Your ModelsLab API key
Returns:
URL of the generated 3D model
"""
response = requests.post(
"https://modelslab.com/api/v6/3d/image_to_3d",
json={
"key": api_key,
"image": image_url
}
)
data = response.json()
if data["status"] == "error":
raise Exception(f"Error: {data['message']}")
if data["status"] == "success":
return data["output"][0]
# Poll for results
request_id = data["id"]
print(f"Converting image to 3D... Request ID: {request_id}")
return poll_3d_result(request_id, api_key)
# Convert product photo to 3D
model_url = image_to_3d(
"https://example.com/product-photo.jpg",
"your_api_key"
)
print(f"3D model: {model_url}")def generate_3d_with_webhook(prompt, api_key, webhook_url, track_id):
"""Generate 3D model and receive results via webhook."""
response = requests.post(
"https://modelslab.com/api/v6/3d/text_to_3d",
json={
"key": api_key,
"prompt": prompt,
"num_inference_steps": 50,
"webhook": webhook_url,
"track_id": track_id
}
)
data = response.json()
print(f"Request submitted: {data['id']}")
return data["id"]
# Usage
request_id = generate_3d_with_webhook(
"A futuristic sci-fi weapon",
"your_api_key",
"https://yourserver.com/webhook/3d",
"model_001"
)| Parameter | Description | Recommended Values |
|---|---|---|
| Text description of 3D object | Be specific about shape, style, details |
| 2D image URL for conversion | Clear, well-lit product photo |
| Quality vs speed | 30 (fast), 50 (balanced), 100 (quality) |
| Async callback URL | Your server endpoint |
| Request identifier | Unique ID for tracking |
✗ Bad: "a chair"
✓ Good: "A modern office chair with ergonomic design, armrests, five wheels, mesh back"
Include: Type, style, materials, key features, details# 3D generation is ALWAYS async
if data["status"] == "processing":
result = poll_3d_result(data["id"], api_key)# Use longer timeouts for 3D
poll_3d_result(request_id, api_key, timeout=900) # 15 minutes
# Or use webhooks
generate_3d_with_webhook(prompt, api_key, webhook_url, track_id)# Generate weapon
sword = text_to_3d(
"Fantasy sword with glowing blue blade, ornate gold handle, medieval style",
api_key,
num_inference_steps=75
)
# Generate character prop
shield = text_to_3d(
"Round wooden shield with iron rim and clan emblem in center",
api_key
)# Create product mockup
prototype = text_to_3d(
"Modern wireless earbuds with charging case, minimalist design, matte white finish",
api_key,
num_inference_steps=100
)# Convert product photo to 3D
def create_product_3d_view(product_image_url, api_key):
"""Generate 3D model from product photo."""
model = image_to_3d(product_image_url, api_key)
print(f"3D product view ready: {model}")
return model
product_3d = create_product_3d_view(
"https://example.com/product.jpg",
api_key
)# Generate furniture
furniture = text_to_3d(
"Modern minimalist coffee table, glass top, wooden legs, Scandinavian design",
api_key
)
# Generate decorations
vase = text_to_3d(
"Ceramic vase with geometric pattern, blue and white colors, modern style",
api_key
)def generate_multiple_3d_models(prompts, api_key):
"""Generate multiple 3D models in batch."""
request_ids = []
# Submit all requests
for i, prompt in enumerate(prompts):
response = requests.post(
"https://modelslab.com/api/v6/3d/text_to_3d",
json={
"key": api_key,
"prompt": prompt,
"webhook": "https://yourserver.com/webhook/3d",
"track_id": f"batch_{i}"
}
)
request_ids.append(response.json()["id"])
print(f"Submitted: {prompt}")
print(f"All {len(prompts)} requests submitted")
return request_ids
# Generate multiple assets
models = generate_multiple_3d_models([
"Medieval sword",
"Knight's helmet",
"Wooden shield",
"Battle axe"
], api_key)try:
model = text_to_3d(prompt, api_key)
print(f"3D model generated: {model}")
except Exception as e:
print(f"3D generation failed: {e}")
# Log error, retry with simpler prompt, notify user.glb.obj.fbx# Enterprise endpoints
text_to_3d_url = "https://modelslab.com/api/v1/enterprise/3d/text_to_3d"
image_to_3d_url = "https://modelslab.com/api/v1/enterprise/3d/image_to_3d"modelslab-image-generationmodelslab-webhooksmodelslab-sdk-usage