Skip to content

Instantly share code, notes, and snippets.

@AnnikenYT
Last active April 19, 2023 08:52
Show Gist options
  • Save AnnikenYT/02a830df592678cac2da224abadefd8d to your computer and use it in GitHub Desktop.
Save AnnikenYT/02a830df592678cac2da224abadefd8d to your computer and use it in GitHub Desktop.
Blender Render.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Blender Render.ipynb",
"private_outputs": true,
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/AnnikenYT/02a830df592678cac2da224abadefd8d/blender-render.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Blender Render\n",
"Made by [Anniken](https://anniken.codes). IF you have any questions, contact me on Discord: `Anniken#0001`. \\\n",
"\n",
"**I you are currently on github:**\n",
"Make sure to use [this](https://gist.github.com/AnnikenYT/02a830df592678cac2da224abadefd8d/f632dee4530c18378f648694062b0cd044976d19) revision of the file! The newer versions have been customized for my specific usecase.\n",
"## **⚠️ Make sure to run ALL CELLS before asking for support**.\n",
"## Preparing your blend file\n",
"There is not a lot of preparation needed, the only configuration necassary is to **pack external files into your blend file**.\n",
"To do that, go into blender and then to <kbd>File</kbd> > <kbd>External Data</kbd> > <kbd>Pack all into .blend</kbd>. You can also enable `Automatically Pack into .blend`."
],
"metadata": {
"id": "7ZYAPbp5sj0Y"
}
},
{
"cell_type": "markdown",
"source": [
"# **Setup**"
],
"metadata": {
"id": "yM4hhNwxloRH"
}
},
{
"cell_type": "markdown",
"source": [
"## Checking your GPU\n",
"Colab randomly assigns you a GPU. This cell allows you to check which one you have.\n",
"If you get `Not connected to a GPU`, make sure you have GPU enabled in Edit > Notebook Settings > Hardware Accelerator.\n",
"There are 3 types of GPUs: `K80`, `T4` and `P100` (Only available in Pro).\n",
"`K80`s are pretty slow for rendering, so I would recommend reconnecting your session until you get a `T4`."
],
"metadata": {
"id": "I7mN2ySItD7O"
}
},
{
"cell_type": "code",
"metadata": {
"id": "UcXH9y2fG0Ub"
},
"source": [
"def info(msg):\n",
" print('='*40, msg, '='*40)\n",
"# Check NVidia GPU Status. i.e. What GPU you are using\n",
"#=================================================================\n",
"gpu_info = !nvidia-smi -L\n",
"gpu_info = '\\n'.join(gpu_info)\n",
"if gpu_info.find('failed') >= 0:\n",
" print('Not connected to a GPU')\n",
"else:\n",
" info(\"GPU INFO\")\n",
" print(gpu_info)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Setting up Blender & Dependencys.\n",
"⚠️ **Make sure to set all variables before running the cell**. \\\n",
"This Cell does multiple things:\n",
"1. Connect to Google Drive. Google drive will be used to store your blend file, as well as the output.\n",
"2. Download Blender. Im using Blender 3.0.0, but you can choose more versions from the dropdown. If you need another version, contact me on Discord to add it, or get the link yourself from [here](https://download.blender.org/release/) if you know what you are doing.\n",
"3. Installing Blender. This will install blender to your colab machine.\n",
"4. Apply a workaround. I have found some issues with the preinstalled verison of a standart library. This workaround reinstalles the library with the latest official version."
],
"metadata": {
"id": "2XT08l8nuIuT"
}
},
{
"cell_type": "code",
"metadata": {
"id": "ldykR0fMftHt"
},
"source": [
"#Connect Google Drive to Google Colab\n",
"#=====================================\n",
"info(\"Mounting Drive. Please confirm in Popup.\")\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')\n",
"#Download Blender from Blender Repository. \n",
"#Other Versions are here: https://download.blender.org/release/\n",
"#=====================================\n",
"#@markdown untick this box if you've run this notebook before\n",
"download = True #@param {type:'boolean'}\n",
"#@markdown *if `download` is unticked, you can skip this*\n",
"version = \"3.0.0\" #@param ['2.93.0', \"3.0.0\", \"3.2.0\"]\n",
"download_links = {\n",
" \"2.93.0\": [\"https://download.blender.org/release/Blender2.93/blender-2.93.0-linux-x64.tar.xz\", \"blender-2.93.0-stable+blender-v293-release.84da05a8b806-linux.x86_64-release\"],\n",
" \"3.0.0\": [\"https://download.blender.org/release/Blender3.0/blender-3.0.0-linux-x64.tar.xz\", \"blender-3.0.0-linux-x64\"],\n",
" \"3.2.0\": [\"https://download.blender.org/release/Blender3.2/blender-3.2.0-linux-x64.tar.xz\", \"blender-3.2.0-linux-x64\"]\n",
"}\n",
"info(\"Setting files for v\" + version)\n",
"link = download_links[version][0]\n",
"blenderdir = download_links[version][1]\n",
"blenderarch = download_links[version][0].split(\"/\")[-1]\n",
"if download:\n",
" info(\"Downloading Blender v\" + version)\n",
" print(\"Downloading from\", link)\n",
" !wget $link\n",
"#Unzip Blender \n",
"#=====================================\n",
" info(\"Unzipping Blender\")\n",
" !tar xf $blenderarch\n",
"# Copy blender to Google Drive\n",
" !mkdir /content/drive/MyDrive/Animations/\n",
" !mv $blenderdir /content/drive/MyDrive/Animations/$blenderdir\n",
"#Deletes the Default libtcmalloc-minimal4 version and installs the Ubuntu default version\n",
"#=====================================\n",
"info(\"Applying workaround for libtcmalloc. Please wait.\")\n",
"import os\n",
"\n",
"os.environ[\"LD_PRELOAD\"] = \"\"\n",
"\n",
"#Deletes wrong Version of libtcmalloc-minimal4\n",
"!apt remove libtcmalloc-minimal4\n",
"#Installs correct version of libtcmalloc-minimal4\n",
"!apt install libtcmalloc-minimal4\n",
"\n",
"#Adds this library to the user environment\n",
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Render**\n",
"Before rendering, you'll need to set some Parameters. After configuring, **run the cell to save the values**."
],
"metadata": {
"id": "TXIrPe9zl2AM"
}
},
{
"cell_type": "code",
"source": [
"#@markdown While you can render as a video, I recommend rendering as a series of images. This allows you to continue the render if it crashes.\n",
"format = 'PNG' #@param ['PNG', 'JPG']\n",
"#@markdown To easily get your file path, open the explorer on the left sidebar and go to drive/MyDrive. Then, find your blend file and <kbd>Right Click</kbd> > <kbd>Copy Path</kbd>. Then paste it here.\n",
"filename = '/content/drive/MyDrive/Animations/MovingMeditations/MM_Blender_Template_v1.blend' #@param {type:\"string\"}\n",
"#@markdown For the output, do the same. The script will automatically append `/out_####.[png|jpg]` to the end of your path, so only select a folder, not a file.\n",
"output = '/content/drive/MyDrive/Animations/' #@param {type: \"string\"}\n",
"output += 'out_####.' + format.lower() if output.endswith('/') else '/out_####.' + format.lower()\n",
"print(\"Saving output to\", output)\n",
"#@markdown ---\n",
"#@markdown If you are using EEVEE, you can change this here. However, I have experienced errors with EEVEE that I dont have a fix for.\n",
"engine = 'CYCLES' #@param ['CYCLES', 'BLENDER_EEVEE', 'BLENDER_WORKBENCH'] \n",
"#@markdown ⚠️ **Only edit this if you experience errors!**\n",
"#@markdown Some GPUs dont have OPTIX support. If you get an error, change this to CUDA. Hybrid renders (`+CPU` appended) tend to be slower than pure GPU renders. You can still try using thm, but you have been warned.\n",
"device = 'CUDA' #@param ['OPTIX', 'CUDA', 'OPTIX+CPU', 'CUDA+CPU', 'CPU']"
],
"metadata": {
"id": "F-rVRvtFxsE8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Rendering an Image\n",
"I highly recommend rendering a single Frame before actually rendering the full video. This allows you to check quality before rendering the entire thing.\n",
"For some reason, this renders as specified in the render setings of the project and not as a PNG/JPG. I have yet to find a fix for this, if you have one please contact me on [Discord](#scrollTo=7ZYAPbp5sj0Y)!"
],
"metadata": {
"id": "0-mX9x1JxMwv"
}
},
{
"cell_type": "code",
"source": [
"#Render Single Frame\n",
"#===========================================================================\n",
"frame = 59#@param {type:\"number\"}\n",
"print(filename, engine, output, format, frame, device)\n",
"!chmod +x /content/drive/MyDrive/Animations/$blenderdir/blender\n",
"!/content/drive/MyDrive/Animations/$blenderdir/blender -b \"$filename\" -noaudio -E \"$engine\" -o \"$output\" -F \"$format\" -f \"$frame\" -- --cycles-device \"$device\""
],
"metadata": {
"id": "XWxyQce2xpft"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Rendering the animation\n",
"**❗ The below cell will render the entire animation.** \\\n",
"Before running, you should:\n",
"- Double check all variables\n",
"- Check the single frame result for errors\n",
"- Make sure you have a GPU allocated in colab\n",
"- Don't have any errors in previous cells."
],
"metadata": {
"id": "Bnv20lkO_OdE"
}
},
{
"cell_type": "code",
"metadata": {
"id": "_p77a65khTRB"
},
"source": [
"start = 65 #@param {type:\"number\"}\n",
"end = 144 #@param {type:\"number\"}\n",
"print(filename, engine, output, format, start, end, device)\n",
"!chmod +x /content/drive/MyDrive/Animations/$blenderdir/blender\n",
"!/content/drive/MyDrive/Animations/$blenderdir/blender -b \"$filename\" -noaudio -E \"$engine\" -o \"$output\" -F \"$format\" -s \"$start\" -e \"$end\" -a -- --cycles-device \"$device\"\n"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment