Last active
November 2, 2023 10:41
-
-
Save Raimund58/88061f785ea2563d345914d196c15b87 to your computer and use it in GitHub Desktop.
Blender_script_for_Google_Colab_using_the_GPU.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Blender_script_for_Google_Colab_using_the_GPU.ipynb", | |
"private_outputs": true, | |
"provenance": [], | |
"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/Raimund58/88061f785ea2563d345914d196c15b87/blender_script_for_google_colab_using_the_gpu.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "F0lUNu1a_bJc" | |
}, | |
"source": [ | |
"#Script to render with Blender on Google Colab" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "JhKyQE_C19a4" | |
}, | |
"source": [ | |
"#First we need to connect to Google Drive." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "KBF6fUHMqm-_" | |
}, | |
"source": [ | |
"#Connect to your drive\n", | |
"#from google.colab import drive\n", | |
"#drive.mount('/content/drive')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "bN7GH5sr-Zb0" | |
}, | |
"source": [ | |
"Specify your desired blender version and the path to your blend file within google drive." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "hfq5e2Rb-Woj" | |
}, | |
"source": [ | |
"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" | |
}, | |
"source": [ | |
"#@title Blender setup\n", | |
"#@markdown Please configure your setup\n", | |
"\n", | |
"blender_version = 'Custom' #@param [\"blender2.83.12\", \"blender2.92.0\", \"blender2.93.0\", \"blender2.93.1\", \"blender2.93.2\", \"blender2.93.4\", \"blender2.93.5\", \"blender3.0\", \"Custom\"] {allow-input: false}\n", | |
"path_to_blend = '/content/drive/My Drive/Raimund PC/Colab_Arbeitsverzeichnis/blenderfile/MT.blend' #@param {type: \"string\"}\n", | |
"output_path = '/content/drive/My Drive/Raimund PC/Colab_Arbeitsverzeichnis/Blender_Output/MT_####.png' #@param {type: \"string\"}\n", | |
"gpu_enabled = True #@param {type:\"boolean\"}\n", | |
"cpu_enabled = False #@param {type:\"boolean\"}\n", | |
"#@markdown ---\n" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "GPJ9fmoB6PWM" | |
}, | |
"source": [ | |
"if blender_version == \"blender2.83.12\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.83/blender-2.83.12-linux64.tar.xz\"\n", | |
"elif blender_version == \"blender2.92.0\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.92/blender-2.92.0-linux64.tar.xz\"\n", | |
"elif blender_version == \"blender2.93.0\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.93/blender-2.93.0-linux-x64.tar.xz\"\n", | |
"elif blender_version == \"blender2.93.1\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.93/blender-2.93.1-linux-x64.tar.xz\"\n", | |
"elif blender_version == \"blender2.93.2\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.93/blender-2.93.2-linux-x64.tar.xz\"\n", | |
"elif blender_version == \"blender2.93.4\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.93/blender-2.93.4-linux-x64.tar.xz\"\n", | |
"elif blender_version == \"blender2.93.5\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender2.93/blender-2.93.5-linux-x64.tar.xz\"\n", | |
"elif blender_version == \"blender3.0\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender3.0/blender-3.0.1-linux-x64.tar.xz\"\n", | |
"elif blender_version == \"Custom\":\n", | |
" download_path=\"https://ftp.halifax.rwth-aachen.de/blender/release/Blender3.0/blender-3.0.1-linux-x64.tar.xz\"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"#This first Block is required as some weird behaviors with libtcmalloc appeared in the colab VM.\n", | |
"import os\n", | |
"os.environ[\"LD_PRELOAD\"] = \"\"\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.5.3\"\n", | |
"!echo $LD_PRELOAD" | |
], | |
"metadata": { | |
"id": "2oew_WOSg_HG" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "UUwwvaq5BxzN" | |
}, | |
"source": [ | |
"Unpack and move blender" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "OQ54OjLVjb26" | |
}, | |
"source": [ | |
"!mkdir $blender_version\n", | |
"!wget -O '{blender_version}.tar.xz' -nc $download_path\n", | |
"!tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RU3p_G4hDk97" | |
}, | |
"source": [ | |
"!apt update\n", | |
"!apt install libboost-all-dev\n", | |
"!apt install libgl1-mesa-dev\n", | |
"!apt install libglu1-mesa\n", | |
"!apt install libsm-dev\n", | |
"!apt install libxkbcommon0" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Wh4i8msvAanq" | |
}, | |
"source": [ | |
"Required for Blender to use the GPU as expected" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "30Tv3lIeVaC6" | |
}, | |
"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.refresh_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', 'OPTIX', '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 = 32\\n\"+\\\n", | |
" \"# scene.render.tile_y = 32\\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": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "EVTxsg6J8E1A" | |
}, | |
"source": [ | |
"#Let's render a animation\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7ByOzaUDOoKJ" | |
}, | |
"source": [ | |
"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) and [Blender CLI rendering](https://docs.blender.org/manual/en/latest/advanced/command_line/render.html#cycles)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "RPiQkHVbCDu2" | |
}, | |
"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": "n7FTOiV357jg" | |
}, | |
"source": [ | |
"!cat /etc/os-release\n", | |
"!nvidia-smi\n", | |
"!nvcc --version" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Fpk2w3yM8XqQ" | |
}, | |
"source": [ | |
"###FOR OLDER BLENDER VERSIONS <2.91 YOU HAVE TO USE -P './setgpu.py'\n", | |
"!./'$blender_version'/blender --debug --debug-cycles --debug-gpu -b -E CYCLES -o '{output_path}' -f 1 -- --cycles-device OPTIX\n", | |
"#!./'$blender_version'/blender -P './setgpu.py' -b -noaudio '{path_to_blend}' -E CYCLES -o '{output_path}' -a" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "zMuGCuoX8o2x" | |
}, | |
"source": [ | |
"#Let's render a frame" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "qmZg_8Eylgxe" | |
}, | |
"source": [ | |
"Use to render a single frame.\n", | |
"Specify the frame with -f *frame_number*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VFVx8omJCGBN" | |
}, | |
"source": [ | |
"#!./$blender_version/blender -P './setgpu.py' -b -noaudio '{path_to_blend}' -E CYCLES -o '{output_path}' -f 51 -- --cycles-device OPTIX" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "vEKwOiwQo-cF" | |
}, | |
"source": [ | |
"Credits go to https://github.com/donmahallem. He is the original creator/developer. <3" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment