do you know sketchfab and sketchfab api ?
Oct 18, 9:07am

Yes! Sketchfab is a popular platform for publishing, sharing, and discovering 3D, VR, and AR content. It allows users to upload 3D models, view them in a browser, and embed them in websites. Sketchfab is widely used by artists, game developers, architects, and educators.

Sketchfab API

Sketchfab also provides an API (Application Programming Interface) that lets developers interact with Sketchfab programmatically. With the API, you can:

  • Upload, download, and manage 3D models.
  • Fetch model metadata, comments, and likes.
  • Integrate Sketchfab functionality into your own applications or workflows.

The API uses RESTful endpoints and requires authentication (usually via an API key). You can find the official documentation and details on how to use it on Sketchfab’s Developer Portal.


Do you have a specific question about Sketchfab or its API? For example:

  • Are you looking to upload models programmatically?
  • Do you want to embed models in a custom app?
  • Are you curious about authentication or rate limits?
Oct 18, 9:07am
do you know rhino python ?
Oct 18, 9:09am

Yes! Rhino Python refers to the use of the Python scripting language within Rhinoceros 3D (commonly called Rhino), a popular 3D modeling software. Rhino provides a powerful Python scripting interface through RhinoScriptSyntax and the rhino3dm library, allowing users to automate tasks, create custom tools, and extend Rhino’s functionality.


Key Features of Rhino Python

  1. RhinoScriptSyntax

    • A Python module that mimics the older RhinoScript (VBScript) syntax, making it easier for users familiar with RhinoScript to transition to Python.
    • Provides access to Rhino’s geometry, objects, layers, and more.
  2. rhino3dm

    • A Python package for working with Rhino’s 3DM file format outside of Rhino (e.g., in Jupyter notebooks or standalone scripts).
    • Useful for reading, writing, and manipulating 3D models programmatically.
  3. Integration with Grasshopper

    • Python scripts can be used within Grasshopper (Rhino’s visual programming plugin) via the Python component or ghpython.
  4. Automation

    • Automate repetitive tasks, batch process files, or generate complex geometry.

Example: Simple Rhino Python Script

Here’s a basic example of creating a sphere in Rhino using Python:

python
import rhinoscriptsyntax as rs

# Create a sphere with radius 5 at the origin
sphere_id = rs.AddSphere([0, 0, 0], 5)
rs.ObjectColor(sphere_id, (255, 0, 0))  # Color it red

How to Get Started

  • In Rhino: Open the Python editor via EditPythonScript or use the PythonScript command.
  • Documentation: Check out the Rhino Python documentation for tutorials, API references, and examples.
  • Community: The McNeel Forum is a great place to ask questions and share scripts.

Are you looking to automate something in Rhino, or do you have a specific task in mind? I can help with code snippets, best practices, or troubleshooting!

Oct 18, 9:09am
can you write a script to upload data yo sketchfab in python. data is present as obj files with a main descriptor in a toml file
Oct 18, 9:15am

Absolutely! To upload .obj files to Sketchfab using Python, you’ll need to:

  1. Use the Sketchfab API (v3) for authentication and uploads.
  2. Read the TOML file to get metadata or descriptors for your models.
  3. Upload the .obj file along with any required metadata.

Below is a step-by-step script to achieve this. Make sure you have the required libraries installed:

bash
pip install requests tomli

Python Script: Upload OBJ to Sketchfab

python
import os
import requests
import tomli
from pathlib import Path

# 1. Load Sketchfab API credentials and TOML descriptor
SKETCHFAB_API_TOKEN = "your_sketchfab_api_token"  # Replace with your token
TOML_PATH = "path/to/your/descriptor.toml"        # Path to your TOML file
OBJ_DIR = "path/to/your/obj/files"                # Directory containing OBJ files

# 2. Read the TOML descriptor
def load_toml(toml_path):
    with open(toml_path, "rb") as f:
        return tomli.load(f)

# 3. Upload a single OBJ file to Sketchfab
def upload_to_sketchfab(obj_path, metadata):
    url = "https://api.sketchfab.com/v3/models"
    headers = {
        "Authorization": f"Token {SKETCHFAB_API_TOKEN}",
        "Content-Type": "multipart/form-data",
    }
    files = {
        "modelFile": open(obj_path, "rb"),
    }
    data = {
        "name": metadata.get("name", Path(obj_path).stem),
        "description": metadata.get("description", ""),
        "tags": metadata.get("tags", []),
        "isPublished": metadata.get("isPublished", False),
    }

    response = requests.post(url, headers=headers, files=files, data=data)
    return response.json()

# 4. Main function
def main():
    descriptor = load_toml(TOML_PATH)
    obj_files = [f for f in os.listdir(OBJ_DIR) if f.endswith(".obj")]

    for obj_file in obj_files:
        obj_path = os.path.join(OBJ_DIR, obj_file)
        model_name = Path(obj_file).stem
        metadata = descriptor.get(model_name, {})
        result = upload_to_sketchfab(obj_path, metadata)
        print(f"Uploaded {obj_file}: {result.get('uid', 'Failed')}")

if __name__ == "__main__":
    main()

How It Works

  1. TOML Descriptor: The script reads a TOML file that describes each model (e.g., name, description, tags).
    Example descriptor.toml:

    toml
    [model1]
    name = "My Model 1"
    description = "A 3D model of a chair"
    tags = ["furniture", "chair"]
    isPublished = true
    
    [model2]
    name = "My Model 2"
    description = "A 3D model of a table"
    tags = ["furniture", "table"]
    isPublished = false
  2. Sketchfab API: The script uses the Sketchfab API to upload each .obj file, along with metadata from the TOML file.

  3. Output: It prints the Sketchfab model UID for each successful upload.


Prerequisites

  • A Sketchfab API token (get it from your Sketchfab account).
  • .obj files and a descriptor.toml file in the specified paths.

Let me know if you need help customizing the script or setting up the TOML file! Would you like to add more features, like error handling or progress tracking?

Oct 18, 9:15am
can you explain how to use it from grasshopper
Oct 18, 9:16am

Using Python scripts from Grasshopper (the visual programming interface for Rhino) is a powerful way to automate tasks, including uploading models to Sketchfab. Here’s how you can integrate the Sketchfab upload script into Grasshopper:


Step 1: Prepare Your Grasshopper Definition

You’ll use the Python component in Grasshopper to run your script. Here’s how to set it up:

  1. Open Grasshopper in Rhino.
  2. Add a Python component:
    • Drag a Python component from the Params tab > Script section.
  3. Add inputs (optional):
    • You can pass file paths, model names, or other parameters as inputs to the Python component.

Step 2: Adapt the Python Script for Grasshopper

The script needs to be slightly modified to work within Grasshopper’s environment. Grasshopper’s Python component uses the ghpythonlib library and has access to Rhino’s geometry and file system.

Modified Script for Grasshopper

python
import os
import requests
import ghpythonlib.components as ghcomp
import tomli
from pathlib import Path

# 1. Inputs (connected to Grasshopper sliders or panels)
obj_path = str(obj_path)  # Path to OBJ file (input from Grasshopper)
toml_path = str(toml_path)  # Path to TOML descriptor (input from Grasshopper)
api_token = "your_sketchfab_api_token"  # Replace with your token

# 2. Read the TOML descriptor
def load_toml(toml_path):
    with open(toml_path, "rb") as f:
        return tomli.load(f)

# 3. Upload to Sketchfab
def upload_to_sketchfab(obj_path, metadata):
    url = "https://api.sketchfab.com/v3/models"
    headers = {
        "Authorization": f"Token {api_token}",
        "Content-Type": "multipart/form-data",
    }
    files = {
        "modelFile": open(obj_path, "rb"),
    }
    data = {
        "name": metadata.get("name", Path(obj_path).stem),
        "description": metadata.get("description", ""),
        "tags": metadata.get("tags", []),
        "isPublished": metadata.get("isPublished", False),
    }

    response = requests.post(url, headers=headers, files=files, data=data)
    return response.json()

# 4. Main function
def main():
    descriptor = load_toml(toml_path)
    model_name = Path(obj_path).stem
    metadata = descriptor.get(model_name, {})
    result = upload_to_sketchfab(obj_path, metadata)
    return result.get("uid", "Failed")

# 5. Output
upload_result = main()

Step 3: Connect Inputs in Grasshopper

  1. Add File Path components:

    • Use File Path components to select the .obj file and the descriptor.toml file.
    • Connect these to the obj_path and toml_path inputs of the Python component.
  2. Add a Panel for Output:

    • Connect a Panel to the upload_result output to display the Sketchfab model UID or error message.

Step 4: Run the Script

  1. Double-click the Python component to open the editor and paste the script.
  2. Set the API token in the script.
  3. Run the Grasshopper definition by toggling the preview or pressing the "Run" button.

Example Grasshopper Workflow

  1. User selects an OBJ file using a File Path component.
  2. User selects the TOML descriptor using another File Path component.
  3. Grasshopper runs the Python script and uploads the model to Sketchfab.
  4. The Sketchfab model UID is displayed in a Panel.

Notes

  • Error Handling: Add try-except blocks to handle errors (e.g., file not found, API errors).
  • Dependencies: Ensure requests and tomli are installed in Rhino’s Python environment.
  • API Token: Never hardcode sensitive tokens in shared files. Use Grasshopper’s Text Input for the token if needed.

Would you like help setting up the Grasshopper file or troubleshooting the script? Let me know how you’d like to proceed!

Oct 18, 9:16am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.