├── README.md ├── GPU.py ├── ColabTimeoutJavaScript ├── LICENSE ├── FluidPython.py ├── Blender_Eevee_Colab.ipynb └── BlenderColab.ipynb /README.md: -------------------------------------------------------------------------------- 1 | This is the official code repository for the tutorials of microSingularity 2 | 3 | https://www.youtube.com/c/MicroSingularity/ 4 | 5 | There are multiple tutorials on this channel which use this or similar code. All Google Colab Blender tutorials can be found here: https://www.youtube.com/playlist?list=PLV0Zsi5ZYUasqIKTYo7et4iJuaSIArSy5 6 | 7 | https://youtu.be/eOxawEi5cNk https://youtu.be/lEW7efy_aiY 8 | -------------------------------------------------------------------------------- /GPU.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | 4 | def enable_gpus(device_type, use_cpus=False): 5 | preferences = bpy.context.preferences 6 | cycles_preferences = preferences.addons["cycles"].preferences 7 | cuda_devices, opencl_devices = cycles_preferences.get_devices() 8 | 9 | if device_type == "CUDA": 10 | devices = cuda_devices 11 | elif device_type == "OPENCL": 12 | devices = opencl_devices 13 | else: 14 | raise RuntimeError("Unsupported device type") 15 | 16 | activated_gpus = [] 17 | 18 | for device in devices: 19 | if device.type == "CPU": 20 | device.use = use_cpus 21 | else: 22 | device.use = True 23 | activated_gpus.append(device.name) 24 | 25 | cycles_preferences.compute_device_type = device_type 26 | bpy.context.scene.cycles.device = "GPU" 27 | 28 | return activated_gpus 29 | 30 | 31 | enable_gpus("CUDA") 32 | -------------------------------------------------------------------------------- /ColabTimeoutJavaScript: -------------------------------------------------------------------------------- 1 | ######################################### 2 | #Javascript Code to Prevent Colab from 3 | #timing out while rendering Blender scenes 4 | ######################################### 5 | 6 | ######################################### 7 | #Press "Control + Shift + I" while in the 8 | #browser (command + option + c in Safari) 9 | and paste the following code into the console tab. 10 | #The 60,000 is how many milliseconds to wait before clicking 11 | ######################################### 12 | 13 | 14 | function ClickConnect(){ 15 | console.log("Working"); 16 | document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click(); 17 | } 18 | var clicker = setInterval(ClickConnect,60000); 19 | 20 | 21 | ########################################## 22 | #and then type the following code to stop 23 | #the clicker code 24 | ######################################### 25 | 26 | 27 | clearInterval(clicker); 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 microSingularity 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /FluidPython.py: -------------------------------------------------------------------------------- 1 | #Bake Blender Fluids on Google Colab 2 | #Created by microSingularity 3 | 4 | https://www.youtube.com/c/MicroSingularity 5 | 6 | 7 | import bpy 8 | 9 | #Change Fluid Domain Type: Can be "GAS" or "LIQUID". Domain needs to be changed to name of fluid domain object. 10 | bpy.data.objects["Domain"].modifiers["Fluid"].domain_settings.domain_type = 'LIQUID' 11 | 12 | #Change Output Directory of Fluid Cache: "//test" means render in folder called "test" which is in the same folder as blender file 13 | bpy.data.objects["Domain"].modifiers["Fluid"].domain_settings.cache_directory = "//test" 14 | 15 | #Change Cache Type 16 | bpy.data.objects["Domain"].modifiers["Fluid"].domain_settings.cache_type = 'ALL' 17 | 18 | #Bake all Fluid Simulations in the scene 19 | bpy.ops.fluid.bake_all() 20 | 21 | #Free all Fluid Simulations in the scene 22 | bpy.ops.fluid.free_all() 23 | 24 | 25 | 26 | #Some other useful commands: 27 | ############################ 28 | 29 | #Select Domain 30 | #bpy.data.objects['Domain'].select_set(1) 31 | 32 | 33 | #Change Cache End Frame 34 | #bpy.data.objects['Domain'].modifiers["Fluid"].domain_settings.cache_frame_end = 100 35 | #Change Domain Resolution 36 | #bpy.data.objects['Domain'].modifiers["Fluid"].domain_settings.resolution_max = 32 37 | 38 | #Enable Particles, Foam and Bubbles 39 | #bpy.data.objects['Cube'].modifiers["Fluid"].domain_settings.use_spray_particles = True 40 | #bpy.data.objects['Cube'].modifiers["Fluid"].domain_settings.use_foam_particles = True 41 | #bpy.data.objects['Cube'].modifiers["Fluid"].domain_settings.use_bubble_particles = True 42 | 43 | 44 | #Change Mesh and Particle Scale 45 | #bpy.data.objects['Domain'].modifiers["Fluid"].domain_settings.particle_scale = 2 46 | #bpy.data.objects['Domain'].modifiers["Fluid"].domain_settings.mesh_scale = 2 47 | 48 | 49 | #Bake Individual Elements 50 | #bpy.ops.fluid.bake_data() 51 | #bpy.ops.fluid.bake_particles() 52 | #bpy.ops.fluid.bake_mesh() 53 | 54 | #Bake All 55 | #bpy.ops.fluid.bake_all() 56 | 57 | #Free Individual Elements 58 | #bpy.ops.fluid.free_data() 59 | #bpy.ops.fluid.free_mesh() 60 | #bpy.ops.fluid.free_particles() 61 | 62 | -------------------------------------------------------------------------------- /Blender_Eevee_Colab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "private_outputs": true, 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "accelerator": "GPU" 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "CwraDEw7cENB" 21 | }, 22 | "source": [ 23 | "# Render Eevee Blender Scenes on Google Colab\n", 24 | "##Created by microSingularity \n", 25 | "https://www.youtube.com/c/MicroSingularity" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "source": [ 31 | "import os\n", 32 | "\n", 33 | "def find(name, path):\n", 34 | " for root, dirs, files in os.walk(path):\n", 35 | " if name in files:\n", 36 | " return os.path.join(os.path.relpath(root,start = os.curdir), name)" 37 | ], 38 | "metadata": { 39 | "id": "Q2aCyB_a9Kb1" 40 | }, 41 | "execution_count": null, 42 | "outputs": [] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "metadata": { 47 | "id": "h_E1kl9W4iV8" 48 | }, 49 | "source": [ 50 | "#Connect Google Drive to Google Colab\n", 51 | "#=====================================\n", 52 | "from google.colab import drive\n", 53 | "drive.mount('/content/drive')" 54 | ], 55 | "execution_count": null, 56 | "outputs": [] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "metadata": { 61 | "id": "ldykR0fMftHt" 62 | }, 63 | "source": [ 64 | "#Download Blender 3.4 from Blender Repository (Must be Blender 3.4 or higher). \n", 65 | "#Other Versions are here: https://download.blender.org/release/\n", 66 | "#=====================================\n", 67 | "!wget https://builder.blender.org/download/daily/blender-3.4.0-alpha+master.5a90bbf71675-linux.x86_64-release.tar.xz -O BlenderDownload.tar.xz" 68 | ], 69 | "execution_count": null, 70 | "outputs": [] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "metadata": { 75 | "id": "ANYFn5tFH_PE" 76 | }, 77 | "source": [ 78 | "#Use these commands to move Blender zip file to Google Drive and then back to working directory so you don't have to download Blender each time\n", 79 | "\n", 80 | "#Copy Blender Zip File to Google Drive (once downloaded) - Change file name to match the Blender version downloaded\n", 81 | "#=====================================\n", 82 | "#!cp /content/BlenderDownload.tar.xz /content/drive/MyDrive/Blender/BlenderDownload.tar.xz\n", 83 | "\n", 84 | "#Copy Blender Zip File from Google Drive\n", 85 | "#=====================================\n", 86 | "!cp /content/drive/MyDrive/Blender/BlenderDownload.tar.xz /content/BlenderDownload.tar.xz" 87 | ], 88 | "execution_count": null, 89 | "outputs": [] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "metadata": { 94 | "id": "rQ-CKpS5hBFo" 95 | }, 96 | "source": [ 97 | "#Unzip Blender (change file name to match downloaded version)\n", 98 | "#=====================================\n", 99 | "#!mkdir Blender\n", 100 | "!tar xf /content/BlenderDownload.tar.xz\n" 101 | ], 102 | "execution_count": null, 103 | "outputs": [] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "source": [ 108 | "blender_path = find('blender', '/content/')\n", 109 | "print(blender_path)" 110 | ], 111 | "metadata": { 112 | "id": "XsrRk7lgTHma" 113 | }, 114 | "execution_count": null, 115 | "outputs": [] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "metadata": { 120 | "id": "AYLt4dk18KPS" 121 | }, 122 | "source": [ 123 | "#Path of Blender File to be Rendered (change path to your Blender file)\n", 124 | "#===========================\n", 125 | "filename = '/content/drive/MyDrive/forest.blend'" 126 | ], 127 | "execution_count": null, 128 | "outputs": [] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "metadata": { 133 | "id": "_p77a65khTRB" 134 | }, 135 | "source": [ 136 | "#Render Single Frame: Change -f 1 to -f 100 to render frame 100 for example\n", 137 | "#===========================================================================\n", 138 | "!./$blender_path -b $filename -noaudio -o \"/content/drive/MyDrive/\" -f 1 -F 'PNG'\n", 139 | "#!./blender-3.4.0-alpha+master.7f6cd405b99c-linux.x86_64-release/blender -b $filename -noaudio -o \"/content/drive/MyDrive/\" -s 1 -e 100 -a" 140 | ], 141 | "execution_count": null, 142 | "outputs": [] 143 | } 144 | ] 145 | } -------------------------------------------------------------------------------- /BlenderColab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "BlenderColab_Nov_2021.ipynb", 7 | "private_outputs": true, 8 | "provenance": [], 9 | "collapsed_sections": [] 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "accelerator": "GPU" 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "CwraDEw7cENB" 22 | }, 23 | "source": [ 24 | "# Render Blender Files on Google Colab\n", 25 | "##Created by microSingularity \n", 26 | "https://www.youtube.com/c/MicroSingularity" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "h_E1kl9W4iV8" 33 | }, 34 | "source": [ 35 | "#Connect Google Drive to Google Colab\n", 36 | "#=====================================\n", 37 | "from google.colab import drive\n", 38 | "drive.mount('/content/drive')" 39 | ], 40 | "execution_count": null, 41 | "outputs": [] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "metadata": { 46 | "id": "ldykR0fMftHt" 47 | }, 48 | "source": [ 49 | "#Download Blender from Blender Repository. \n", 50 | "#Other Versions are here: https://download.blender.org/release/\n", 51 | "#=====================================\n", 52 | "!wget https://download.blender.org/release/Blender3.0/blender-3.0.0-linux-x64.tar.xz" 53 | ], 54 | "execution_count": null, 55 | "outputs": [] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "metadata": { 60 | "id": "ANYFn5tFH_PE" 61 | }, 62 | "source": [ 63 | "#Use these commands to move Blender zip file to Google Drive and then back to working directory so you don't have to download Blender each time\n", 64 | "\n", 65 | "#Copy Blender Zip File to Google Drive\n", 66 | "#=====================================\n", 67 | "#!cp /content/blender-2.93.5-linux-x64.tar.xz /content/drive/MyDrive/Blender/blender-2.93.5-linux-x64.tar.xz\n", 68 | "\n", 69 | "#Copy Blender Zip File from Google Drive\n", 70 | "#=====================================\n", 71 | "#!cp /content/drive/MyDrive/Blender/blender-2.93.5-linux-x64.tar.xz /content/blender-2.93.5-linux-x64.tar.xz" 72 | ], 73 | "execution_count": null, 74 | "outputs": [] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "metadata": { 79 | "id": "rQ-CKpS5hBFo" 80 | }, 81 | "source": [ 82 | "#Unzip Blender \n", 83 | "#=====================================\n", 84 | "!tar xf blender-2.93.5-linux-x64.tar.xz\n" 85 | ], 86 | "execution_count": null, 87 | "outputs": [] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "metadata": { 92 | "id": "d20avlrs8oWf" 93 | }, 94 | "source": [ 95 | "#Deletes the Default libtcmalloc-minimal4 version and installs the Ubuntu default version\n", 96 | "import os\n", 97 | "\n", 98 | "os.environ[\"LD_PRELOAD\"] = \"\"\n", 99 | "\n", 100 | "#Deletes wrong Version of libtcmalloc-minimal4\n", 101 | "!apt remove libtcmalloc-minimal4\n", 102 | "#Installs correct version of libtcmalloc-minimal4\n", 103 | "!apt install libtcmalloc-minimal4\n", 104 | "\n", 105 | "#Adds this library to the user environment\n", 106 | "os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n" 107 | ], 108 | "execution_count": null, 109 | "outputs": [] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "metadata": { 114 | "id": "AYLt4dk18KPS" 115 | }, 116 | "source": [ 117 | "#Path of Blender File to be Rendered \n", 118 | "#===========================\n", 119 | "filename = '/content/drive/MyDrive/BlenderColab.blend'" 120 | ], 121 | "execution_count": null, 122 | "outputs": [] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "metadata": { 127 | "id": "_p77a65khTRB" 128 | }, 129 | "source": [ 130 | "#Render Single Frame: Change -f 1 to -f 100 to render frame 100 for example\n", 131 | "#===========================================================================\n", 132 | "#!./blender-2.93.5-linux-x64/blender -b $filename -noaudio -E 'CYCLES' --debug-all -o \"/content/drive/MyDrive/Blender\" -f 1 -F 'PNG' -- --cycles-device CUDA\n", 133 | "\n", 134 | "#Render Single Frame using OPTIX, if GPU supports it, otherwise use CUDA. \n", 135 | "#===========================================================================\n", 136 | "#!./blender-2.93.5-linux-x64/blender -b $filename -noaudio -E 'CYCLES' -o \"/content/drive/MyDrive/Blender\" -f 1 -F 'PNG' -- --cycles-device OPTIX\n", 137 | "\n", 138 | "#Render Animation: -s 1 and -e 250 means, render frames 1 to 250 of animation, -a means render animation. Animation will be rendered in whatever format was chosen in blender file. i.e. MP4\n", 139 | "#===========================================================================\n", 140 | "!./blender-2.93.5-linux-x64/blender -b $filename -noaudio -E 'CYCLES' -o \"/content/drive/MyDrive/Blender\" -s 1 -e 250 -a -- --cycles-device CUDA\n" 141 | ], 142 | "execution_count": null, 143 | "outputs": [] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "metadata": { 148 | "id": "UcXH9y2fG0Ub" 149 | }, 150 | "source": [ 151 | "#Uncomment to Check NVidia GPU Status. i.e. What GPU you are using\n", 152 | "#=================================================================\n", 153 | "#!nvidia-smi" 154 | ], 155 | "execution_count": null, 156 | "outputs": [] 157 | } 158 | ] 159 | } 160 | --------------------------------------------------------------------------------