{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# pythreejs\n", "\n", "Eine Python/[ThreeJS](https://threejs.org/)-Bridge, die die Jupyter-Widget-Infrastruktur verwendet." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Beispiele" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Animation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ipywidgets\n", "\n", "from IPython.display import display\n", "from pythreejs import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "view_width = 600\n", "view_height = 400" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "sphere = Mesh(\n", " SphereBufferGeometry(1, 32, 16), MeshStandardMaterial(color=\"red\")\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cube = Mesh(\n", " BoxBufferGeometry(1, 1, 1),\n", " MeshPhysicalMaterial(color=\"green\"),\n", " position=[2, 0, 4],\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "camera = PerspectiveCamera(\n", " position=[10, 6, 10], aspect=view_width / view_height\n", ")\n", "key_light = DirectionalLight(position=[0, 10, 10])\n", "ambient_light = AmbientLight()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "positon_track = VectorKeyframeTrack(\n", " name=\".position\",\n", " times=[0, 2, 5],\n", " values=[\n", " 10,\n", " 6,\n", " 10,\n", " 6.3,\n", " 3.78,\n", " 6.3,\n", " -2.98,\n", " 0.84,\n", " 9.2,\n", " ],\n", ")\n", "rotation_track = QuaternionKeyframeTrack(\n", " name=\".quaternion\",\n", " times=[0, 2, 5],\n", " values=[\n", " -0.184,\n", " 0.375,\n", " 0.0762,\n", " 0.905,\n", " -0.184,\n", " 0.375,\n", " 0.0762,\n", " 0.905,\n", " -0.0430,\n", " -0.156,\n", " -0.00681,\n", " 0.987,\n", " ],\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "camera_clip = AnimationClip(tracks=[positon_track, rotation_track])\n", "camera_action = AnimationAction(AnimationMixer(camera), camera_clip, camera)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "scene = Scene(children=[sphere, cube, camera, key_light, ambient_light])\n", "controller = OrbitControls(controlling=camera)\n", "renderer = Renderer(\n", " camera=camera,\n", " scene=scene,\n", " controls=[controller],\n", " width=view_width,\n", " height=view_height,\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "96ae07b8a1d34c75bb00a5df1fecaa39", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Renderer(camera=PerspectiveCamera(aspect=1.5, position=(10.0, 6.0, 10.0), projectionMatrix=(1.0, 0.0, 0.0, 0.0…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "renderer" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "129cd18ea62d402487487514c76a60b3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AnimationAction(clip=AnimationClip(tracks=(VectorKeyframeTrack(name='.position', times=array([0, 2, 5], dtype=…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "camera_action" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "