{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "#%load_ext wurlitzer\n", "#^^^ the wurlitzer extension is used to capture C/C++ output to be displayed in the notebook\n", "#^^^ this is very useful for debugging, but it doesn't work on windows\n", "\n", "#Python 2/3 compatibility\n", "from __future__ import print_function,division\n", "\n", "import time\n", "from klampt import *\n", "from klampt.math import vectorops,so3,se3\n", "from klampt import vis\n", "from klampt.model.create import primitives\n", "from IPython.display import clear_output\n", "from klampt.vis.ipython import EditConfig,EditPoint,EditTransform\n", "\n", "world = WorldModel()\n", "world.loadFile(\"../data/athlete_plane.xml\")\n", "vis.add(\"world\",world)\n", "\n", "vis.add(\"sphere\",[1,0,2],color=(0,1,0),size=0.5)\n", "#vis.add(\"sphere\",primitives.sphere(0.5,[1,0,2]))\n", "#vis.setColor(\"sphere\",0,1,0)\n", "vis.add(\"ghost\",world.robot(0).getConfig(),color=(0,1,0,0.5))\n", "\n", "vis.show()\n", "\n", "#Controls:\n", "#left mouse click to rotate the view\n", "#right click or ctrl+click to pan the view\n", "#mouse wheel or shift+click to zoom the view" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q = world.robot(0).getConfig()\n", "#getConfig() returns a copy of the robot's current configuration, so these changes\n", "#aren't reflected in the WorldModel...\n", "q[6] = 2\n", "q[2] = 0.5\n", "#until this call here\n", "world.robot(0).setConfig(q)\n", "\n", "#WAIT? why didn't the visualization change?\n", "#... Because the WorldModel knows nothing about the visualization..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#... so we need to do this manually.\n", "#Use a kvis.update() call to push the current state of the world to the visualization\n", "vis.update()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#do a little animation... note the vis.update calls\n", "q = world.robot(0).getConfig()\n", "for i in range(20):\n", " q[2] = i*0.1\n", " world.robot(0).setConfig(q)\n", " vis.update()\n", " time.sleep(0.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#can move the camera using setViewport() and setViewport()\n", "cam = vis.getViewport()\n", "print(cam)\n", "cam['position']['x'] += 1.5\n", "vis.setViewport(cam)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#add some \"extras\"\n", "\n", "vis.addText(\"some_text\",text=\"hello\",position=(10,10))\n", "vis.add(\"sphere1\",[1,0,0.5],size=0.3)\n", "vis.setColor(\"sphere1\",1,0,0,0.25)\n", "q = [0.0]*world.robot(0).numLinks()\n", "vis.add('ghost',q)\n", "vis.setColor('ghost',0,1,0,0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#items can be updated by calling add again, setItemConfig, or setAttribute\n", "vis.addText(\"some_text\",text=\"hello modified\")\n", "vis.setAttribute(\"sphere1\",'size',0.5)\n", "vis.add(\"xform\",(so3.identity(),[1,0,0]),length=0.5,width=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "#you can create multiple KlamptWidget objects\n", "window2 = vis.createWindow()\n", "\n", "vis.add(\"world\",world)\n", "vis.show()\n", "print([a['name'] for a in vis.nativeWindow().scene['object']['children']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis.lock()\n", "vis.add('ghost',[0.0]*len(world.robot(0).getConfig()))\n", "vis.setColor('ghost',0,1,0,0.5)\n", "vis.add(\"xform\",(so3.identity(),[1,0,0.5]))\n", "vis.add(\"point\",[0.5,0,1],color=(1,0,0))\n", "vis.unlock()\n", "\n", "print([a['name'] for a in vis.nativeWindow().scene['object']['children']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#and you can add editors\n", "vis.edit('ghost')\n", "vis.edit('point')\n", "vis.edit('xform')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "#you can even load multiple worlds\n", "world2 = WorldModel()\n", "world2.loadFile('../data/hubo_plane.xml')\n", "window4 = vis.createWindow()\n", "vis.add(\"world\",world2)\n", "\n", "#here's how you show a playback widget \n", "from klampt.vis.ipython import Playback\n", "playback_widget3 = Playback(vis.nativeWindow())\n", "display(playback_widget3)\n", "vis.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#you can play around with these hooks to the playback widget\n", "framerate = 30\n", "movespeed = 1.0\n", "robot = world2.robot(0)\n", "q0 = robot.getConfig()\n", "def advance():\n", " q = robot.getConfig()\n", " q[9] += movespeed/framerate\n", " robot.setConfig(q)\n", "def reset():\n", " robot.setConfig(q0)\n", "playback_widget3.advance = advance\n", "playback_widget3.reset = reset\n", "playback_widget3.framerate = framerate\n", "playback_widget3.maxframes = 30" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#If the Jupyter notebook is running on your own computer, and you have OpenGL, you can\n", "#still use the standard Klamp't vis functions\n", "vis.kill()\n", "vis.init([\"PyQt\",\"GLUT\"])\n", "vis.add(\"world\",world)\n", "vis.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }