Image-to-3d
0.3 Quick Start Script Turbo Nodejs
// Install axios with npm install axios
import axios from 'axios';
const baseUrl = "https://api.csm.ai/";

function sleep(seconds) {
    const ms = seconds * 1000;
    return new Promise(resolve => setTimeout(resolve, ms));
}

const headers = {
    'Content-Type': 'application/json',
    'x-api-key': '6bCfF4467bXXXXXX4E6B271BeC5'
};

// Upload Stage
const uploadObj = {
    "image_url": "https://via.placeholder.com/300/09f/fff.png",
    "preview_mesh": "turbo",
    "pixel_alignment": "highest",
    "model_resolution": "high",
    "resolution": "high_poly",
    "diffusion_time_steps": 75,
    "auto_gen_3d": true,
    "topology": "quads",
    "texture_resolution": 2048
}

// Create the session, this will automatically upload the image and start the spin generation process.
const response = await axios.post(`${baseUrl}image-to-3d-sessions`, uploadObj, { headers: headers });

let imageTo3dSession = response.data.data;

const session_code = imageTo3dSession.session_code;

// console.log(imageTo3dSession);
// {
//     error: '',
//     message: 'Created',
//     statusCode: 201,
//     data: {
//       user_id: 'XXXXXXX',
//       session_code: 'SESSION_1713430448_7229295',
//       image_url: 'https://via.placeholder.com/300/09f/fff.png',
//       status: 'spin_generate_processing',
//       topology: 'quads',
//       resolution: 'high_poly',
//       source: 'external',
//       diffusion_time_steps: 75,
//       auto_gen_3d: true,
//       preview_mesh: 'turbo',
//       model_resolution: 'high',
//       texture_resolution: 2048,
//       pixel_alignment: 'highest',
//       created_at: '2024-04-18T08:54:08.575Z',
//       updated_at: '2024-04-18T08:54:09.095Z'
//     }
//   }


// Sleep for a minute befire making the call to check spin status
// Check the status of the session
do{
    await sleep(20);
    const response = await axios.get(`${baseUrl}image-to-3d-sessions/${session_code}`, { headers: headers });
    imageTo3dSession = response.data.data;
}while (imageTo3dSession.status !== 'spin_generate_done') ;

console.log("spinResponse");


// Select Spin
const chosen_spin_id = 0;
const spinObj = {
    "selected_spin_index": chosen_spin_id,
    "selected_spin": imageTo3dSession.spins[chosen_spin_id].image_url
 }

// Make the preview and refine job call
const previewResponse = await axios.post(`${baseUrl}image-to-3d-sessions/get-3d/preview/${session_code}`, spinObj, { headers: headers });

imageTo3dSession = previewResponse.data.data;

// Sleep for a minute befire making the call to check preview status
const previewAndRefineStatusCodes = ['preview_done', 'training_refine', 'refine_done'];

await sleep(40);

// Check the status of the session
do{
    await sleep(20);
    const response = await axios.get(`${baseUrl}image-to-3d-sessions/${session_code}`, { headers: headers });
    imageTo3dSession = response.data.data;
    console.log(imageTo3dSession);
}while (!previewAndRefineStatusCodes.includes(imageTo3dSession.status)) ;




// console.log(imageTo3dSession);
// {
//     _id: '66210ae1f37ba79ae6ff4a22',
//     user_id: 'XXXXXXX',
//     session_code: 'SESSION_1713441503_6143414',
//     image_url: 'https://via.placeholder.com/300/09f/fff.png',
//     preview_mesh_url: 'https://XXXX.XXXXXXXXXX/mesh.zip?se=YYYYYYYYYYYYYYYYYYY',
//     preview_mesh_url_zip: 'https://XXXX.XXXXXXXXXX/mesh.zip?se=YYYYYYYYYYYYYYYYYYY',
//     preview_mesh_url_glb: 'https://XXXX.XXXXXXXXXX/mesh.glb?se=YYYYYYYYYYYYYYYYYYY',
//     preview_mesh_url_usdz: 'https://XXXX.XXXXXXXXXX/mesh.usdz?se=YYYYYYYYYYYYYYYYYYY',
//     preview_mesh_url_fbx: '',
//     status: 'training_refine',
//     topology: 'quads',
//     resolution: 'high_poly',
//     source: 'external',
//     auto_gen_3d: true,
//     model_version: '2.0-turbo',
//     selected_spin: 'https://XXXX.XXXXXXXXXX/spins/spin_00.mp4?se=YYYYYYYYYYYYYYYYYYY',
//     is_deleted: false,
//     manual_segmentation: false,
//     preview_mesh: 'turbo',
//     pixel_alignment: 'highest',
//     model_resolution: 'high',
//     diffusion_time_steps: 75,
//     selected_spin_index: 0,
//     user_id_in_string: 'XXXXXXX',
//     spins: [
     
//     ],
//     created_at: '2024-04-18T11:58:25.287Z',
//     updated_at: '2024-04-18T12:01:03.539Z',
//   }