{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "813d2960",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "A simple example showing how to save a 3D object as an image\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "183b68be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "import trimesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba25b4d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load a file by name\n",
    "mesh = trimesh.load_mesh(\"../models/featuretype.STL\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adb4a249",
   "metadata": {},
   "outputs": [],
   "source": [
    "# preview mesh in a pyglet window from a terminal, or inline in a notebook\n",
    "mesh.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb628a7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a scene\n",
    "scene = trimesh.Scene()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4dc9de77",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add the chosen geometry to the scene\n",
    "scene.add_geometry(mesh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61e7427f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the bounds corners for the camera transform\n",
    "corners = scene.bounds_corners"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a61704bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get isometric view\n",
    "r_e = trimesh.transformations.euler_matrix(\n",
    "    math.radians(45),\n",
    "    math.radians(45),\n",
    "    math.radians(45),\n",
    "    \"ryxz\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d36e4a59",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get camera transform to look at geometry with isometric view\n",
    "t_r = scene.camera.look_at(corners, rotation=r_e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f8dc471",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set camera transform\n",
    "scene.camera_transform = t_r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c32d0d6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Render of scene as a PNG bytes\n",
    "png = scene.save_image()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2791b600",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write the bytes to file\n",
    "with open(\"../models/featuretype.png\", \"wb\") as f:\n",
    "    f.write(png)\n",
    "    f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
