Skip to content

Instantly share code, notes, and snippets.

@donmahallem
Last active September 24, 2023 02:29
Show Gist options
  • Save donmahallem/a05100077ec1327268f28f0b2bd8da60 to your computer and use it in GitHub Desktop.
Save donmahallem/a05100077ec1327268f28f0b2bd8da60 to your computer and use it in GitHub Desktop.

Run Blender on Google Colab

Please Read

This is a automatically updated GIST copied from this repository. If you have any issues, suggestions or improvements please file those to that repository! It's far easier to keep track of conversations in their own dedicated issues/PRs.

Instructions

Please refer to the repositories instructions! For a quick start click the following badge: Open In Colab

{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "blender_script.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "MNd7jnnSkuUD",
"colab_type": "text"
},
"source": [
"#Setup\n",
"Specify your desired blender version and the path to your blend file within google drive.\n",
"\n",
"###Info\n",
"If you do need more information on parameters etc. look here: [Blender CLI Wiki](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8dFNjYGTgNjR",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Setup\n",
"#@markdown Please configure your setup\n",
"\n",
"blender_version = 'blender2.91.2' #@param [\"blender2.80\", \"blender2.81\", \"blender2.82\", \"blender2.83\", \"blender2.90.1\", \"blender2.91.2\"] {allow-input: false}\n",
"path_to_blend = 'path/to/blend.blend' #@param {type: \"string\"}\n",
"output_path = 'output/dir/in/drive/output####.png' #@param {type: \"string\"}\n",
"gpu_enabled = True #@param {type:\"boolean\"}\n",
"cpu_enabled = False #@param {type:\"boolean\"}\n",
"#@markdown ---\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GPJ9fmoB6PWM",
"colab_type": "code",
"colab": {}
},
"source": [
"if blender_version == \"blender2.80\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.80/blender-2.80-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.81\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.81/blender-2.81-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.82\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.82/blender-2.82-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.83\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.83/blender-2.83.0-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.90.1\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.90/blender-2.90.1-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.91.2\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.91/blender-2.91.2-linux64.tar.xz\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OQ54OjLVjb26",
"colab_type": "code",
"colab": {}
},
"source": [
"!mkdir $blender_version\n",
"if blender_version == \"blender2.81\" or \"blender2.80\":\n",
" !wget -O '{blender_version}.tar.bz2' -nc $download_path\n",
" !tar -xf '{blender_version}.tar.bz2' -C ./$blender_version --strip-components=1\n",
"else:\n",
" !wget -O '{blender_version}.tar.xz' -nc $download_path\n",
" !tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "s3uIDMZLbYOE",
"colab_type": "text"
},
"source": [
"This Block is required as some weird behaviors with libtcmalloc appeared in the colab VM"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h6vohA2q2BDF",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"\n",
"os.environ[\"LD_PRELOAD\"] = \"\"\n",
"\n",
"!apt update\n",
"!apt remove libtcmalloc-minimal4\n",
"!apt install libtcmalloc-minimal4\n",
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n",
"\n",
"!echo $LD_PRELOAD"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UUwwvaq5BxzN",
"colab_type": "text"
},
"source": [
"Unpack and move blender"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RU3p_G4hDk97",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt install libboost-all-dev\n",
"!apt install libgl1-mesa-dev\n",
"!apt install libglu1-mesa libsm-dev"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wh4i8msvAanq",
"colab_type": "text"
},
"source": [
"Required for Blender to use the GPU as expected"
]
},
{
"cell_type": "code",
"metadata": {
"id": "30Tv3lIeVaC6",
"colab_type": "code",
"colab": {}
},
"source": [
"data = \"import re\\n\"+\\\n",
" \"import bpy\\n\"+\\\n",
" \"scene = bpy.context.scene\\n\"+\\\n",
" \"scene.cycles.device = 'GPU'\\n\"+\\\n",
" \"prefs = bpy.context.preferences\\n\"+\\\n",
" \"prefs.addons['cycles'].preferences.get_devices()\\n\"+\\\n",
" \"cprefs = prefs.addons['cycles'].preferences\\n\"+\\\n",
" \"print(cprefs)\\n\"+\\\n",
" \"# Attempt to set GPU device types if available\\n\"+\\\n",
" \"for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):\\n\"+\\\n",
" \" try:\\n\"+\\\n",
" \" cprefs.compute_device_type = compute_device_type\\n\"+\\\n",
" \" print('Device found',compute_device_type)\\n\"+\\\n",
" \" break\\n\"+\\\n",
" \" except TypeError:\\n\"+\\\n",
" \" pass\\n\"+\\\n",
" \"#for scene in bpy.data.scenes:\\n\"+\\\n",
" \"# scene.render.tile_x = 64\\n\"+\\\n",
" \"# scene.render.tile_y = 64\\n\"+\\\n",
" \"# Enable all CPU and GPU devices\\n\"+\\\n",
" \"for device in cprefs.devices:\\n\"+\\\n",
" \" if not re.match('intel', device.name, re.I):\\n\"+\\\n",
" \" print('Activating',device)\\n\"+\\\n",
" \" device.use = \"+str(gpu_enabled)+\"\\n\"+\\\n",
" \" else:\\n\"+\\\n",
" \" device.use = \"+str(cpu_enabled)+\"\\n\"\n",
"with open('setgpu.py', 'w') as f:\n",
" f.write(data)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QRzNmiHN8Xr0",
"colab_type": "code",
"colab": {}
},
"source": [
"from google.colab import drive\n",
"drive.mount('/gdrive')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RPiQkHVbCDu2",
"colab_type": "text"
},
"source": [
"Use this if you want to render all Frames\n",
"\n",
"Use -s to speficy the start frame.\n",
"eg: -s 10\n",
"\n",
"Use -e to speficy the end frame.\n",
"eg: -e 20\n",
"\n",
"**THE ORDER IS IMPORTANT. BOTH -s AND -e MUST BE SPEFICIED BEFORE -a**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Fpk2w3yM8XqQ",
"colab_type": "code",
"colab": {}
},
"source": [
"!sudo ./$blender_version/blender -P './setgpu.py' -b '/gdrive/My Drive/{path_to_blend}' -E CYCLES -o '/gdrive/My Drive/{output_path}' -a"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "qmZg_8Eylgxe",
"colab_type": "text"
},
"source": [
"Use to render a single frame.\n",
"Specify the frame with -f *frame_number*"
]
},
{
"cell_type": "code",
"metadata": {
"id": "VFVx8omJCGBN",
"colab_type": "code",
"colab": {}
},
"source": [
"!sudo ./$blender_version/blender -P 'setgpu.py' -b '/gdrive/My Drive/{path_to_blend}' -P 'setgpu.py' -o '/gdrive/My Drive/{output_path}' -f 1"
],
"execution_count": 0,
"outputs": []
}
]
}
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "blender_script.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "MNd7jnnSkuUD",
"colab_type": "text"
},
"source": [
"#Setup\n",
"Specify your desired blender version and the path to your blend file within google drive or colab local storage.\n",
"\n",
"###Info\n",
"If you do need more information on parameters etc. look here: [Blender CLI Wiki](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8dFNjYGTgNjR",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Setup\n",
"#@markdown Please configure your setup\n",
"\n",
"blender_version = 'blender2.91.2' #@param [\"blender2.80\", \"blender2.81\", \"blender2.82\", \"blender2.83\", \"blender2.90.1\", \"blender2.91.2\"] {allow-input: false}\n",
"#@markdown You can right click a file and click copy path\n",
"path_to_blend = '/content/blendfile.blend' #@param {type: \"string\"}\n",
"output_path = '/content/output' #@param {type: \"string\"}\n",
"gpu_enabled = True #@param {type:\"boolean\"}\n",
"cpu_enabled = False #@param {type:\"boolean\"}\n",
"use_drive = False #@param {type:\"boolean\"}\n",
"#@markdown ---\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QRzNmiHN8Xr0",
"colab_type": "code",
"colab": {}
},
"source": [
"if use_drive:\n",
" from google.colab import drive\n",
" drive.mount('/content/drive')\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GPJ9fmoB6PWM",
"colab_type": "code",
"colab": {}
},
"source": [
"if blender_version == \"blender2.80\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.80/blender-2.80-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.81\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.81/blender-2.81-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.82\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.82/blender-2.82-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.83\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.83/blender-2.83.0-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.90.1\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.90/blender-2.90.1-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.91.2\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.91/blender-2.91.2-linux64.tar.xz\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OQ54OjLVjb26",
"colab_type": "code",
"colab": {}
},
"source": [
"!mkdir $blender_version\n",
"if blender_version == \"blender2.81\" or \"blender2.80\":\n",
" !wget -O '{blender_version}.tar.bz2' -nc $download_path\n",
" !tar -xf '{blender_version}.tar.bz2' -C ./$blender_version --strip-components=1\n",
"else:\n",
" !wget -O '{blender_version}.tar.xz' -nc $download_path\n",
" !tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "s3uIDMZLbYOE",
"colab_type": "text"
},
"source": [
"This Block is required as some weird behaviors with libtcmalloc appeared in the colab VM"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h6vohA2q2BDF",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"\n",
"os.environ[\"LD_PRELOAD\"] = \"\"\n",
"\n",
"!apt update\n",
"!apt remove libtcmalloc-minimal4\n",
"!apt install libtcmalloc-minimal4\n",
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n",
"\n",
"!echo $LD_PRELOAD"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UUwwvaq5BxzN",
"colab_type": "text"
},
"source": [
"Unpack and move blender"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RU3p_G4hDk97",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt install libboost-all-dev\n",
"!apt install libgl1-mesa-dev\n",
"!apt install libglu1-mesa libsm-dev"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wh4i8msvAanq",
"colab_type": "text"
},
"source": [
"Required for Blender to use the GPU as expected"
]
},
{
"cell_type": "code",
"metadata": {
"id": "30Tv3lIeVaC6",
"colab_type": "code",
"colab": {}
},
"source": [
"data = \"import re\\n\"+\\\n",
" \"import bpy\\n\"+\\\n",
" \"scene = bpy.context.scene\\n\"+\\\n",
" \"scene.cycles.device = 'GPU'\\n\"+\\\n",
" \"prefs = bpy.context.preferences\\n\"+\\\n",
" \"prefs.addons['cycles'].preferences.get_devices()\\n\"+\\\n",
" \"cprefs = prefs.addons['cycles'].preferences\\n\"+\\\n",
" \"print(cprefs)\\n\"+\\\n",
" \"# Attempt to set GPU device types if available\\n\"+\\\n",
" \"for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):\\n\"+\\\n",
" \" try:\\n\"+\\\n",
" \" cprefs.compute_device_type = compute_device_type\\n\"+\\\n",
" \" print('Device found',compute_device_type)\\n\"+\\\n",
" \" break\\n\"+\\\n",
" \" except TypeError:\\n\"+\\\n",
" \" pass\\n\"+\\\n",
" \"#for scene in bpy.data.scenes:\\n\"+\\\n",
" \"# scene.render.tile_x = 64\\n\"+\\\n",
" \"# scene.render.tile_y = 64\\n\"+\\\n",
" \"# Enable all CPU and GPU devices\\n\"+\\\n",
" \"for device in cprefs.devices:\\n\"+\\\n",
" \" if not re.match('intel', device.name, re.I):\\n\"+\\\n",
" \" print('Activating',device)\\n\"+\\\n",
" \" device.use = \"+str(gpu_enabled)+\"\\n\"+\\\n",
" \" else:\\n\"+\\\n",
" \" device.use = \"+str(cpu_enabled)+\"\\n\"\n",
"with open('setgpu.py', 'w') as f:\n",
" f.write(data)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RPiQkHVbCDu2",
"colab_type": "text"
},
"source": [
"Use this if you want to render all Frames\n",
"\n",
"Use -s to speficy the start frame.\n",
"eg: -s 10\n",
"\n",
"Use -e to speficy the end frame.\n",
"eg: -e 20\n",
"\n",
"**THE ORDER IS IMPORTANT. BOTH -s AND -e MUST BE SPEFICIED BEFORE -a**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h9xSisNXBIbH",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Render Setup\r\n",
"#@markdow\r\n",
"whatToRender = 'renderOneFrame' #@param [\"renderOneFrame\", \"renderAllFrame\", \"renderWithinRange\"] {allow-input: false}\r\n",
"#@markdown StartFrame = the frame you want to start with or the frame number for the renderOneFrame option\r\n",
"startFrame = 1#@param {type: \"integer\"}\r\n",
"endFrame = 20 #@param {type: \"integer\"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BIk15rdnCuVu",
"colab_type": "code",
"colab": {}
},
"source": [
"if whatToRender == 'renderOneFrame':\r\n",
" !sudo ./$blender_version/blender -P 'setgpu.py' -b '{path_to_blend}' -P 'setgpu.py' -o '{output_path}' -f {startFrame}\r\n",
"elif whatToRender == 'renderAllFrame':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -a\r\n",
"elif whatToRender == 'renderWithinRange':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -s {startFrame} -e {endFrame} -a"
],
"execution_count": null,
"outputs": []
}
]
}
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "blender_script.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "MNd7jnnSkuUD",
"colab_type": "text"
},
"source": [
"#Setup\n",
"Specify your desired blender version and the path to your blend file within google drive or colab local storage.\n",
"\n",
"###Info\n",
"If you do need more information on parameters etc. look here: [Blender CLI Wiki](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8dFNjYGTgNjR",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Setup\n",
"#@markdown Please configure your setup\n",
"\n",
"blender_version = 'blender2.91.2' #@param [\"blender2.80\", \"blender2.81\", \"blender2.82\", \"blender2.83\", \"blender2.90.1\", \"blender2.91.2\"] {allow-input: false}\n",
"#@markdown You can right click a file and click copy path\n",
"path_to_blend = '/content/blendfile.blend' #@param {type: \"string\"}\n",
"output_path = '/content/output' #@param {type: \"string\"}\n",
"gpu_enabled = True #@param {type:\"boolean\"}\n",
"cpu_enabled = False #@param {type:\"boolean\"}\n",
"use_drive = False #@param {type:\"boolean\"}\n",
"#@markdown ---\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QRzNmiHN8Xr0",
"colab_type": "code",
"colab": {}
},
"source": [
"if use_drive:\n",
" from google.colab import drive\n",
" drive.mount('/content/drive')\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GPJ9fmoB6PWM",
"colab_type": "code",
"colab": {}
},
"source": [
"if blender_version == \"blender2.80\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.80/blender-2.80-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.81\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.81/blender-2.81-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.82\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.82/blender-2.82-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.83\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.83/blender-2.83.0-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.90.1\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.90/blender-2.90.1-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.91.2\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.91/blender-2.91.2-linux64.tar.xz\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OQ54OjLVjb26",
"colab_type": "code",
"colab": {}
},
"source": [
"!mkdir $blender_version\n",
"if blender_version == \"blender2.81\" or \"blender2.80\":\n",
" !wget -O '{blender_version}.tar.bz2' -nc $download_path\n",
" !tar -xf '{blender_version}.tar.bz2' -C ./$blender_version --strip-components=1\n",
"else:\n",
" !wget -O '{blender_version}.tar.xz' -nc $download_path\n",
" !tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "s3uIDMZLbYOE",
"colab_type": "text"
},
"source": [
"This Block is required as some weird behaviors with libtcmalloc appeared in the colab VM"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h6vohA2q2BDF",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"\n",
"os.environ[\"LD_PRELOAD\"] = \"\"\n",
"\n",
"!apt update\n",
"!apt remove libtcmalloc-minimal4\n",
"!apt install libtcmalloc-minimal4\n",
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n",
"\n",
"!echo $LD_PRELOAD"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UUwwvaq5BxzN",
"colab_type": "text"
},
"source": [
"Unpack and move blender"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RU3p_G4hDk97",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt install libboost-all-dev\n",
"!apt install libgl1-mesa-dev\n",
"!apt install libglu1-mesa libsm-dev"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wh4i8msvAanq",
"colab_type": "text"
},
"source": [
"Required for Blender to use the GPU as expected"
]
},
{
"cell_type": "code",
"metadata": {
"id": "30Tv3lIeVaC6",
"colab_type": "code",
"colab": {}
},
"source": [
"data = \"import re\\n\"+\\\n",
" \"import bpy\\n\"+\\\n",
" \"scene = bpy.context.scene\\n\"+\\\n",
" \"scene.cycles.device = 'GPU'\\n\"+\\\n",
" \"prefs = bpy.context.preferences\\n\"+\\\n",
" \"prefs.addons['cycles'].preferences.get_devices()\\n\"+\\\n",
" \"cprefs = prefs.addons['cycles'].preferences\\n\"+\\\n",
" \"print(cprefs)\\n\"+\\\n",
" \"# Attempt to set GPU device types if available\\n\"+\\\n",
" \"for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):\\n\"+\\\n",
" \" try:\\n\"+\\\n",
" \" cprefs.compute_device_type = compute_device_type\\n\"+\\\n",
" \" print('Device found',compute_device_type)\\n\"+\\\n",
" \" break\\n\"+\\\n",
" \" except TypeError:\\n\"+\\\n",
" \" pass\\n\"+\\\n",
" \"#for scene in bpy.data.scenes:\\n\"+\\\n",
" \"# scene.render.tile_x = 64\\n\"+\\\n",
" \"# scene.render.tile_y = 64\\n\"+\\\n",
" \"# Enable all CPU and GPU devices\\n\"+\\\n",
" \"for device in cprefs.devices:\\n\"+\\\n",
" \" if not re.match('intel', device.name, re.I):\\n\"+\\\n",
" \" print('Activating',device)\\n\"+\\\n",
" \" device.use = \"+str(gpu_enabled)+\"\\n\"+\\\n",
" \" else:\\n\"+\\\n",
" \" device.use = \"+str(cpu_enabled)+\"\\n\"\n",
"with open('setgpu.py', 'w') as f:\n",
" f.write(data)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RPiQkHVbCDu2",
"colab_type": "text"
},
"source": [
"Use this if you want to render all Frames\n",
"\n",
"Use -s to speficy the start frame.\n",
"eg: -s 10\n",
"\n",
"Use -e to speficy the end frame.\n",
"eg: -e 20\n",
"\n",
"**THE ORDER IS IMPORTANT. BOTH -s AND -e MUST BE SPEFICIED BEFORE -a**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h9xSisNXBIbH",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Render Setup\r\n",
"#@markdown\r\n",
"whatToRender = 'renderOneFrame' #@param [\"renderOneFrame\", \"renderAllFrame\", \"renderWithinRange\"] {allow-input: false}\r\n",
"#@markdown StartFrame = the frame you want to start with or the frame number for the renderOneFrame option\r\n",
"startFrame = 1#@param {type: \"integer\"}\r\n",
"endFrame = 20 #@param {type: \"integer\"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BIk15rdnCuVu",
"colab_type": "code",
"colab": {}
},
"source": [
"if whatToRender == 'renderOneFrame':\r\n",
" !sudo ./$blender_version/blender -P 'setgpu.py' -b '{path_to_blend}' -P 'setgpu.py' -o '{output_path}' -f {startFrame}\r\n",
"elif whatToRender == 'renderAllFrame':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -a\r\n",
"elif whatToRender == 'renderWithinRange':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -s {startFrame} -e {endFrame} -a"
],
"execution_count": null,
"outputs": []
}
]
}
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "blender_script.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "MNd7jnnSkuUD",
"colab_type": "text"
},
"source": [
"#Setup\n",
"Specify your desired blender version and the path to your blend file within google drive or colab local storage.\n",
"\n",
"###Info\n",
"If you do need more information on parameters etc. look here: [Blender CLI Wiki](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8dFNjYGTgNjR",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Setup\n",
"#@markdown Please configure your setup\n",
"\n",
"blender_version = 'blender2.91.2' #@param [\"blender2.80\", \"blender2.81\", \"blender2.82\", \"blender2.83\", \"blender2.90.1\", \"blender2.91.2\"] {allow-input: false}\n",
"#@markdown You can right click a file and click copy path\n",
"path_to_blend = '/content/blendfile.blend' #@param {type: \"string\"}\n",
"output_path = '/content/output' #@param {type: \"string\"}\n",
"gpu_enabled = True #@param {type:\"boolean\"}\n",
"cpu_enabled = False #@param {type:\"boolean\"}\n",
"use_drive = False #@param {type:\"boolean\"}\n",
"#@markdown ---\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QRzNmiHN8Xr0",
"colab_type": "code",
"colab": {}
},
"source": [
"if use_drive:\n",
" from google.colab import drive\n",
" drive.mount('/content/drive')\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GPJ9fmoB6PWM",
"colab_type": "code",
"colab": {}
},
"source": [
"if blender_version == \"blender2.80\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.80/blender-2.80-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.81\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.81/blender-2.81-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.82\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.82/blender-2.82-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.83\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.83/blender-2.83.0-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.90.1\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.90/blender-2.90.1-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.91.2\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.91/blender-2.91.2-linux64.tar.xz\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OQ54OjLVjb26",
"colab_type": "code",
"colab": {}
},
"source": [
"!mkdir $blender_version\n",
"if blender_version == \"blender2.81\" or \"blender2.80\":\n",
" !wget -O '{blender_version}.tar.bz2' -nc $download_path\n",
" !tar -xf '{blender_version}.tar.bz2' -C ./$blender_version --strip-components=1\n",
"else:\n",
" !wget -O '{blender_version}.tar.xz' -nc $download_path\n",
" !tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "s3uIDMZLbYOE",
"colab_type": "text"
},
"source": [
"This Block is required as some weird behaviors with libtcmalloc appeared in the colab VM"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h6vohA2q2BDF",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"\n",
"os.environ[\"LD_PRELOAD\"] = \"\"\n",
"\n",
"!apt update\n",
"!apt remove libtcmalloc-minimal4\n",
"!apt install libtcmalloc-minimal4\n",
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n",
"\n",
"!echo $LD_PRELOAD"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UUwwvaq5BxzN",
"colab_type": "text"
},
"source": [
"Unpack and move blender"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RU3p_G4hDk97",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt install libboost-all-dev\n",
"!apt install libgl1-mesa-dev\n",
"!apt install libglu1-mesa libsm-dev"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wh4i8msvAanq",
"colab_type": "text"
},
"source": [
"Required for Blender to use the GPU as expected"
]
},
{
"cell_type": "code",
"metadata": {
"id": "30Tv3lIeVaC6",
"colab_type": "code",
"colab": {}
},
"source": [
"data = \"import re\\n\"+\\\n",
" \"import bpy\\n\"+\\\n",
" \"scene = bpy.context.scene\\n\"+\\\n",
" \"scene.cycles.device = 'GPU'\\n\"+\\\n",
" \"prefs = bpy.context.preferences\\n\"+\\\n",
" \"prefs.addons['cycles'].preferences.get_devices()\\n\"+\\\n",
" \"cprefs = prefs.addons['cycles'].preferences\\n\"+\\\n",
" \"print(cprefs)\\n\"+\\\n",
" \"# Attempt to set GPU device types if available\\n\"+\\\n",
" \"for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):\\n\"+\\\n",
" \" try:\\n\"+\\\n",
" \" cprefs.compute_device_type = compute_device_type\\n\"+\\\n",
" \" print('Device found',compute_device_type)\\n\"+\\\n",
" \" break\\n\"+\\\n",
" \" except TypeError:\\n\"+\\\n",
" \" pass\\n\"+\\\n",
" \"#for scene in bpy.data.scenes:\\n\"+\\\n",
" \"# scene.render.tile_x = 64\\n\"+\\\n",
" \"# scene.render.tile_y = 64\\n\"+\\\n",
" \"# Enable all CPU and GPU devices\\n\"+\\\n",
" \"for device in cprefs.devices:\\n\"+\\\n",
" \" if not re.match('intel', device.name, re.I):\\n\"+\\\n",
" \" print('Activating',device)\\n\"+\\\n",
" \" device.use = \"+str(gpu_enabled)+\"\\n\"+\\\n",
" \" else:\\n\"+\\\n",
" \" device.use = \"+str(cpu_enabled)+\"\\n\"\n",
"with open('setgpu.py', 'w') as f:\n",
" f.write(data)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RPiQkHVbCDu2",
"colab_type": "text"
},
"source": [
"Use this if you want to render all Frames\n",
"\n",
"Use -s to speficy the start frame.\n",
"eg: -s 10\n",
"\n",
"Use -e to speficy the end frame.\n",
"eg: -e 20\n",
"\n",
"**THE ORDER IS IMPORTANT. BOTH -s AND -e MUST BE SPEFICIED BEFORE -a**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h9xSisNXBIbH",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Render Setup\r\n",
"#@markdown\r\n",
"whatToRender = 'renderOneFrame' #@param [\"renderOneFrame\", \"renderAllFrame\", \"renderWithinRange\"] {allow-input: false}\r\n",
"#@markdown StartFrame = the frame you want to start with or the frame number for the renderOneFrame option\r\n",
"startFrame = 1#@param {type: \"integer\"}\r\n",
"endFrame = 20 #@param {type: \"integer\"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BIk15rdnCuVu",
"colab_type": "code",
"colab": {}
},
"source": [
"if whatToRender == 'renderOneFrame':\r\n",
" !sudo ./$blender_version/blender -P 'setgpu.py' -b '{path_to_blend}' -P 'setgpu.py' -o '{output_path}' -f {startFrame}\r\n",
"elif whatToRender == 'renderAllFrame':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -a\r\n",
"elif whatToRender == 'renderWithinRange':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -s {startFrame} -e {endFrame} -a"
],
"execution_count": null,
"outputs": []
}
]
}
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "blender_script.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "MNd7jnnSkuUD",
"colab_type": "text"
},
"source": [
"#Setup\n",
"Specify your desired blender version and the path to your blend file within google drive or colab local storage.\n",
"\n",
"###Info\n",
"If you do need more information on parameters etc. look here: [Blender CLI Wiki](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8dFNjYGTgNjR",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Setup\n",
"#@markdown Please configure your setup\n",
"\n",
"blender_version = 'blender2.91.2' #@param [\"blender2.80\", \"blender2.81\", \"blender2.82\", \"blender2.83\", \"blender2.90.1\", \"blender2.91.2\"] {allow-input: false}\n",
"#@markdown You can right click a file and click copy path\n",
"path_to_blend = '/content/blendfile.blend' #@param {type: \"string\"}\n",
"output_path = '/content/output' #@param {type: \"string\"}\n",
"gpu_enabled = True #@param {type:\"boolean\"}\n",
"cpu_enabled = False #@param {type:\"boolean\"}\n",
"use_drive = False #@param {type:\"boolean\"}\n",
"#@markdown ---\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QRzNmiHN8Xr0",
"colab_type": "code",
"colab": {}
},
"source": [
"if use_drive:\n",
" from google.colab import drive\n",
" drive.mount('/content/drive')\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GPJ9fmoB6PWM",
"colab_type": "code",
"colab": {}
},
"source": [
"if blender_version == \"blender2.80\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.80/blender-2.80-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.81\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.81/blender-2.81-linux-glibc217-x86_64.tar.bz2\"\n",
"elif blender_version == \"blender2.82\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.82/blender-2.82-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.83\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.83/blender-2.83.0-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.90.1\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.90/blender-2.90.1-linux64.tar.xz\"\n",
"elif blender_version == \"blender2.91.2\":\n",
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.91/blender-2.91.2-linux64.tar.xz\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OQ54OjLVjb26",
"colab_type": "code",
"colab": {}
},
"source": [
"!mkdir $blender_version\n",
"if blender_version == \"blender2.81\" or \"blender2.80\":\n",
" !wget -O '{blender_version}.tar.bz2' -nc $download_path\n",
" !tar -xf '{blender_version}.tar.bz2' -C ./$blender_version --strip-components=1\n",
"else:\n",
" !wget -O '{blender_version}.tar.xz' -nc $download_path\n",
" !tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "s3uIDMZLbYOE",
"colab_type": "text"
},
"source": [
"This Block is required as some weird behaviors with libtcmalloc appeared in the colab VM"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h6vohA2q2BDF",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"\n",
"os.environ[\"LD_PRELOAD\"] = \"\"\n",
"\n",
"!apt update\n",
"!apt remove libtcmalloc-minimal4\n",
"!apt install libtcmalloc-minimal4\n",
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n",
"\n",
"!echo $LD_PRELOAD"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UUwwvaq5BxzN",
"colab_type": "text"
},
"source": [
"Unpack and move blender"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RU3p_G4hDk97",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt install libboost-all-dev\n",
"!apt install libgl1-mesa-dev\n",
"!apt install libglu1-mesa libsm-dev"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wh4i8msvAanq",
"colab_type": "text"
},
"source": [
"Required for Blender to use the GPU as expected"
]
},
{
"cell_type": "code",
"metadata": {
"id": "30Tv3lIeVaC6",
"colab_type": "code",
"colab": {}
},
"source": [
"data = \"import re\\n\"+\\\n",
" \"import bpy\\n\"+\\\n",
" \"scene = bpy.context.scene\\n\"+\\\n",
" \"scene.cycles.device = 'GPU'\\n\"+\\\n",
" \"prefs = bpy.context.preferences\\n\"+\\\n",
" \"prefs.addons['cycles'].preferences.get_devices()\\n\"+\\\n",
" \"cprefs = prefs.addons['cycles'].preferences\\n\"+\\\n",
" \"print(cprefs)\\n\"+\\\n",
" \"# Attempt to set GPU device types if available\\n\"+\\\n",
" \"for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):\\n\"+\\\n",
" \" try:\\n\"+\\\n",
" \" cprefs.compute_device_type = compute_device_type\\n\"+\\\n",
" \" print('Device found',compute_device_type)\\n\"+\\\n",
" \" break\\n\"+\\\n",
" \" except TypeError:\\n\"+\\\n",
" \" pass\\n\"+\\\n",
" \"#for scene in bpy.data.scenes:\\n\"+\\\n",
" \"# scene.render.tile_x = 64\\n\"+\\\n",
" \"# scene.render.tile_y = 64\\n\"+\\\n",
" \"# Enable all CPU and GPU devices\\n\"+\\\n",
" \"for device in cprefs.devices:\\n\"+\\\n",
" \" if not re.match('intel', device.name, re.I):\\n\"+\\\n",
" \" print('Activating',device)\\n\"+\\\n",
" \" device.use = \"+str(gpu_enabled)+\"\\n\"+\\\n",
" \" else:\\n\"+\\\n",
" \" device.use = \"+str(cpu_enabled)+\"\\n\"\n",
"with open('setgpu.py', 'w') as f:\n",
" f.write(data)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RPiQkHVbCDu2",
"colab_type": "text"
},
"source": [
"Use this if you want to render all Frames\n",
"\n",
"Use -s to speficy the start frame.\n",
"eg: -s 10\n",
"\n",
"Use -e to speficy the end frame.\n",
"eg: -e 20\n",
"\n",
"**THE ORDER IS IMPORTANT. BOTH -s AND -e MUST BE SPEFICIED BEFORE -a**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h9xSisNXBIbH",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Render Setup\r\n",
"#@markdown\r\n",
"whatToRender = 'renderOneFrame' #@param [\"renderOneFrame\", \"renderAllFrame\", \"renderWithinRange\"] {allow-input: false}\r\n",
"#@markdown StartFrame = the frame you want to start with or the frame number for the renderOneFrame option\r\n",
"startFrame = 1#@param {type: \"integer\"}\r\n",
"endFrame = 20 #@param {type: \"integer\"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BIk15rdnCuVu",
"colab_type": "code",
"colab": {}
},
"source": [
"if whatToRender == 'renderOneFrame':\r\n",
" !sudo ./$blender_version/blender -P 'setgpu.py' -b '{path_to_blend}' -P 'setgpu.py' -o '{output_path}' -f {startFrame}\r\n",
"elif whatToRender == 'renderAllFrame':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -a\r\n",
"elif whatToRender == 'renderWithinRange':\r\n",
" !sudo ./$blender_version/blender -P './setgpu.py' -b '{path_to_blend}' -E CYCLES -o '{output_path}' -s {startFrame} -e {endFrame} -a"
],
"execution_count": null,
"outputs": []
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@donmahallem
Copy link
Author

@exuanbo Thanks for the info. Will try to clean this up asap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment