{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Error handling\n", "\n", "This test covers typical runtime error outputs from Lightmetrica." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "import traceback\n", "import imageio\n", "import pandas as pd\n", "import numpy as np\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.axes_grid1 import make_axes_locatable\n", "import lmfunctest as ft\n", "import lightmetrica as lm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%load_ext lightmetrica_jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calling Lightmetrica API without initialization\n", "\n", "Calling Lightmetrica API without initialization causes an exception." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"\", line 3, in \n", " lm.info()\n", "RuntimeError: Uninitialized subsystem. Possible failure to call *::init() function.\n" ] } ], "source": [ "try:\n", " # This causes an exception\n", " lm.info()\n", "except Exception:\n", " traceback.print_exc()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Initializing Lightmetrica fixes the problem\n", "lm.init('user::default', {})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### No outputs in Jupyter notebook\n", "\n", "Lightmetrica initializes default logger that outputs messages into standard output of the console. If you are using Jupyter notebook, you need to initialize the logger using `logger::jupyter`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# No output in Jupyter notebook\n", "lm.info()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Initialize the logger with logger::jupyter\n", "lm.log.init('logger::jupyter', {})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.016|114@user ] Lightmetrica -- Version 3.0.0 (rev. fe30e7c) Linux x64\n" ] } ], "source": [ "lm.info()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wrong asset name\n", "\n", "If you specify the wrong asset name, the framework will rause an exception." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.034|48@assets ] Loading asset [name='film1']\n", "[E|0.035|145@compon] Missing component [key='film:bitmap']. Check if\n", "[E|0.035|146@compon] - Key is wrong\n", "[E|0.035|147@compon] - Component with the key is not registered\n", "[E|0.035|148@compon] - Plugin containing the component is not loaded\n", "[E|0.035|67@assets ] Failed to create an asset [name='film1', key='film:bitmap']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"\", line 3, in \n", " lm.asset('film1', 'film:bitmap', {'w': 1920, 'h': 1080})\n", "RuntimeError: Consult log outputs for detailed error messages\n" ] } ], "source": [ "try:\n", " # Wrong: film:bitmap\n", " lm.asset('film1', 'film:bitmap', {'w': 1920, 'h': 1080})\n", "except Exception:\n", " traceback.print_exc()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.054|48@assets ] Loading asset [name='film1']\n" ] }, { "data": { "text/plain": [ "'$.assets.film1'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Correct: film::bitmap\n", "lm.asset('film1', 'film::bitmap', {'w': 1920, 'h': 1080})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Invalid parameter\n", "\n", "The framework will cause an exception if you try to create an asset with invalid parameters.\n", "The framework will *not* generate an error for the unnecessasry parameters." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.165|48@assets ] Loading asset [name='camera1']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"\", line 6, in \n", " 'up': [0,1,0]\n", "RuntimeError: Missing property [name='vfov']\n" ] } ], "source": [ "try:\n", " # vfov is missing\n", " lm.asset('camera1', 'camera::pinhole', {\n", " 'position': [0,0,5],\n", " 'center': [0,0,0],\n", " 'up': [0,1,0]\n", " })\n", "except Exception:\n", " traceback.print_exc()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.185|48@assets ] Loading asset [name='camera1']\n", "[I|0.185|61@assets ] Asset [name='camera1'] has been already loaded. Replacing..\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"\", line 7, in \n", " 'fov': 30\n", "RuntimeError: Invalid JSON type [expected='array', actual='number']\n" ] } ], "source": [ "try:\n", " lm.asset('camera1', 'camera::pinhole', {\n", " # Parameter type is wrong. position must be an array.\n", " 'position': 5,\n", " 'center': [0,0,0],\n", " 'up': [0,1,0],\n", " 'fov': 30\n", " })\n", "except Exception:\n", " traceback.print_exc()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.205|48@assets ] Loading asset [name='camera1']\n", "[I|0.205|61@assets ] Asset [name='camera1'] has been already loaded. Replacing..\n" ] }, { "data": { "text/plain": [ "'$.assets.camera1'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This is correct\n", "lm.asset('camera1', 'camera::pinhole', {\n", " # Parameter type is wrong. position must be an array.\n", " 'position': [0,0,5],\n", " 'center': [0,0,0],\n", " 'up': [0,1,0],\n", " 'vfov': 30\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Missing reference" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.227|48@assets ] Loading asset [name='mesh1']\n" ] }, { "data": { "text/plain": [ "'$.assets.mesh1'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lm.asset('mesh1', 'mesh::raw', {\n", " 'ps': [-1,-1,-1,1,-1,-1,1,1,-1,-1,1,-1],\n", " 'ns': [0,0,1],\n", " 'ts': [0,0,1,0,1,1,0,1],\n", " 'fs': {\n", " 'p': [0,1,2,0,2,3],\n", " 'n': [0,0,0,0,0,0],\n", " 't': [0,1,2,0,2,3]\n", " }\n", "})" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[E|0.247|41@assets ] Invalid asset name [name='material1']\n", "[E|0.247|284@compon] Failed to find a component with locator [loc='$.assets.material1']\n" ] } ], "source": [ "# material1 is undefined\n", "lm.primitive(lm.identity(), {\n", " 'mesh': lm.asset('mesh1'),\n", " 'material': lm.asset('material1')\n", "})" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.265|48@assets ] Loading asset [name='material1']\n" ] }, { "data": { "text/plain": [ "'$.assets.material1'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define a missing asset\n", "lm.asset('material1', 'material::diffuse', {\n", " 'Kd': [1,1,1]\n", "})" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[E|0.285|272@compon] Locator must start with '$' [loc='material1'].\n" ] } ], "source": [ "# 'material1' is not a valid locator use lm.asset()\n", "lm.primitive(lm.identity(), {\n", " 'mesh': lm.asset('mesh1'),\n", " 'material': 'material1'\n", "})" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# This is correct\n", "lm.primitive(lm.identity(), {\n", " 'mesh': lm.asset('mesh1'),\n", " 'material': lm.asset('material1')\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rendering with invalid scene" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.321|151@user ] Starting render [name='renderer::raycast']\n", "[E|0.321|80@scene ] Missing camera primitive. Use lm::primitive() function to add camera primitive.\n" ] } ], "source": [ "# Without camera\n", "lm.render('renderer::raycast', {\n", " 'output': lm.asset('film1'),\n", " 'color': [0,0,0]\n", "})" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "lm.primitive(lm.identity(), {\n", " 'camera': lm.asset('camera1')\n", "})" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.356|151@user ] Starting render [name='renderer::raycast']\n", "[E|0.356|84@scene ] Missing acceleration structure. Use lm::build() function before rendering.\n" ] } ], "source": [ "# Without acceleration structure\n", "lm.render('renderer::raycast', {\n", " 'output': lm.asset('film1'),\n", " 'color': [0,0,0]\n", "})" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.374|246@scene ] Building acceleration structure [name='accel::sahbvh']\n", "[I|0.374|131@accel_] Flattening scene\n", "[I|0.374|261@accel_] Building\n" ] } ], "source": [ "lm.build('accel::sahbvh', {})" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.394|151@user ] Starting render [name='renderer::raycast']\n", "[I|0.895|125@progre] Processing [iter=1654000/2073600, progress=79.8%, ETA=0.1s]\n", "[I|1.015|132@progre] Processing [completed]\n" ] } ], "source": [ "lm.render('renderer::raycast', {\n", " 'output': lm.asset('film1'),\n", " 'color': [0,0,0]\n", "})" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }