{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import k3d\n", "from k3d.headless import k3d_remote, get_headless_driver\n", "import numpy as np\n", "from IPython.display import Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = k3d.plot(screenshot_scale=1.0)\n", "headless = k3d_remote(plot, get_headless_driver())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "T = 1.618033988749895\n", "r = 4.77\n", "zmin, zmax = -r, r\n", "xmin, xmax = -r, r\n", "ymin, ymax = -r, r\n", "Nx, Ny, Nz = 77, 77, 77\n", "\n", "x = np.linspace(xmin, xmax, Nx)\n", "y = np.linspace(ymin, ymax, Ny)\n", "z = np.linspace(zmin, zmax, Nz)\n", "x, y, z = np.meshgrid(x, y, z, indexing='ij')\n", "p = 2 - (np.cos(x + T * y) + np.cos(x - T * y) + \n", " np.cos(y + T * z) + np.cos(y - T * z) + \n", " np.cos(z - T * x) + np.cos(z + T * x)).astype(np.float32)\n", "iso = k3d.marching_cubes(p, level=0.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot += iso" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "headless.sync()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Image(headless.get_browser_screenshot()) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Image(headless.get_screenshot(True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('screenshot.png', 'wb') as f:\n", " f.write(headless.get_screenshot())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# movie creator\n", "\n", "import cv2\n", "import tqdm\n", "\n", "fourcc = cv2.VideoWriter_fourcc(*'MP4V')\n", "video = cv2.VideoWriter(\"headless_movie.mp4\", fourcc, 60.0, (1280, 720))\n", "r = 2\n", "\n", "for fi in tqdm.tqdm(np.linspace(0, 2 * np.pi, 60 * 4)):\n", " plot.camera = [\n", " np.sin(fi) * r, np.cos(fi) * r, 0.5,\n", " 0, 0, 0,\n", " 0, 0, 1\n", " ]\n", " \n", " headless.sync()\n", " \n", " file_bytes = np.asarray(bytearray(headless.get_screenshot(True)), dtype=np.uint8) \n", " img = cv2.imdecode(file_bytes, cv2.IMREAD_UNCHANGED)[:,:,0:3]\n", " video.write(img) \n", " \n", "video.release()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "headless.close()" ] }, { "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }