{ "cells": [ { "cell_type": "markdown", "id": "61538355-4b00-44bb-82cc-c46b3911acd3", "metadata": {}, "source": [ "© Copyright, 2025 G. Schaer.\n", "\n", "SPDX-License-Identifier: GPL-3.0-only" ] }, { "cell_type": "markdown", "id": "82bf0179-33b2-441a-9b3c-73633afc8c79", "metadata": {}, "source": [ "# Visualizer Example Usage" ] }, { "cell_type": "markdown", "id": "d4bebcc8-b973-4d88-9e3b-18446ba12230", "metadata": {}, "source": [ "This module gives example usage of the Visualizer class. In this, we load a\n", "cube into the visualizer and update its position and color while tracking it\n", "with the camera." ] }, { "cell_type": "code", "execution_count": 1, "id": "9dc1fc8e-c6b8-49f6-a200-866d4131a1a3", "metadata": {}, "outputs": [], "source": [ "import time\n", "import condynsate\n", "from condynsate import __assets__ as assets\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "d0332278-7a93-42c2-816b-9afabb226eb7", "metadata": {}, "source": [ "First we create an instance of the Visualizer class that is running at 60 fps and not recording." ] }, { "cell_type": "code", "execution_count": 2, "id": "6ada1c26-8391-410d-a639-4055d1afd9e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You can open the visualizer by visiting the following URL:\n", "http://127.0.0.1:7000/static/\n" ] } ], "source": [ "# Create an instance of the visualizer\n", "vis = condynsate.Visualizer(frame_rate=60.0, record=False)" ] }, { "cell_type": "markdown", "id": "7c3735a6-ce8f-4849-aa5c-6643a8f18b84", "metadata": {}, "source": [ "Now that we have the visualizer window open, we can begin to manipulate what the scene looks like. In this case, let's turn off the grid plane and the axes." ] }, { "cell_type": "code", "execution_count": 3, "id": "36fed904-3d3e-46c7-b526-66d90ac939e3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Turn off the axes and the ground plane\n", "vis.set_axes(False) #Returns 0 on success\n", "vis.set_grid(False )#Returns 0 on success" ] }, { "cell_type": "markdown", "id": "0c94f90c-a679-4e1b-b23d-fccc5fa6726d", "metadata": {}, "source": [ "We can also move the camera around in the scene. Doing so will cause the camera to automatically pan to focus on its previous target. In the default case, the target is (0, 0, 0)." ] }, { "cell_type": "code", "execution_count": 4, "id": "03a22714-f367-483f-bcd1-d54ea066d413", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set the initial position of the camera\n", "vis.set_cam_position((0.0, -3.0, 3.0)) #Returns 0 on success" ] }, { "cell_type": "markdown", "id": "401c15b2-2b47-40ef-b4f2-ea62ce621bff", "metadata": {}, "source": [ "Next, we will add objects to the visualzier." ] }, { "cell_type": "code", "execution_count": 6, "id": "1e79a66b-355d-4b79-b7e2-12020a032b46", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add a plane as the ground from an obj file and apply a texture\n", "vis.add_object('Ground', # Name of the object in the scene\n", " assets['plane.obj'], # The path to the file describing its geometryu\n", " scale=(10., 10., 1.), # We scale up the ground plane to 10x10\n", " tex_path=assets['concrete.png'] # We can also apply textures to objects defined via obj files\n", " ) #Returns 0 on success" ] }, { "cell_type": "code", "execution_count": 7, "id": "6e7433d0-3858-4a1e-9575-84046868fc21", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add a cube from an stl file.\n", "vis.add_object('Cube',\n", " assets['cube.stl'],\n", " color=(0.121, 0.403, 0.749), # Set initial color\n", " scale=(0.5, 0.5, 0.5), # Scale cube to 0.5x0.5x0.5\n", " position=(0., 0., 0.25) # Set initial position on ground\n", " ) #Returns 0 on success" ] }, { "cell_type": "markdown", "id": "f52f086d-4443-419d-8915-4c22584159db", "metadata": {}, "source": [ "Next, let's run a visualization loop. In this loop we will \n", "1. Move the cube object around using the `set_transform` function\n", "2. Change the color of the cube using the `set_material` function\n", "3. Set the camera's target to look at the cube's new position\n", "4. Move the camera around in space to circle the object" ] }, { "cell_type": "code", "execution_count": 8, "id": "05035048-5a9b-405b-9e75-8430f533ef85", "metadata": {}, "outputs": [], "source": [ "N = 1750\n", "P0 = np.array([0., 0., 0.25])\n", "P1 = np.array([-0.5, 0.5, 4])\n", "for i in range(N):\n", " t = i / (N-1)\n", "\n", " # Get the position and orientation to set the cube to\n", " p = (1-t)*P0 + t*P1\n", " roll = np.sin(5*t)\n", " pitch = np.sin(7*t)\n", " yaw = np.sin(11*t)\n", "\n", " # Transform the cube to the desired position and orientation\n", " vis.set_transform('Cube',\n", " position=p,\n", " roll=roll,\n", " pitch=pitch,\n", " yaw=yaw,\n", " scale=(0.5, 0.5, 0.5), # You must reset the scale\n", " # lest it return to default (1,1,1)\n", " )\n", "\n", " # Select a new color to apply to the cube and apply it\n", " color = (0.121+np.sin(5*t),\n", " 0.403+np.cos(7*t),\n", " 0.749+np.cos(11*t))\n", " color = tuple(float(max(0,min(c,1))) for c in color)\n", " vis.set_material('Cube', color=color)\n", "\n", " # Set the camera target to the new position of the cube\n", " vis.set_cam_target(p)\n", "\n", " # Move the camera's position in a dramatic way\n", " vis.set_cam_position((3*np.sin(4*t), -3*np.cos(4*t), 3.+4.*t))\n", "\n", " # Run the updates at about triple the frame rate\n", " time.sleep(0.0056)" ] }, { "cell_type": "markdown", "id": "36444970-71d5-47e6-87ae-2f79d85d8b13", "metadata": {}, "source": [ "When done, terminate ensure all children threads exit gracefully." ] }, { "cell_type": "code", "execution_count": 10, "id": "c30ea7c4-3fc4-4118-93f2-1dd8dc30427e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vis.terminate() #Returns 0 on success" ] }, { "cell_type": "code", "execution_count": null, "id": "53beb4f1-d37f-4b2c-8ef9-c96cfcfc8633", "metadata": {}, "outputs": [], "source": [] } ], "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.14.0" } }, "nbformat": 4, "nbformat_minor": 5 }