Last active
November 20, 2024 12:39
-
-
Save Wahooney/5f8ec5857eb46c5ca35148e75dd9f17b to your computer and use it in GitHub Desktop.
Blender auto-updater Powershell script. Check the Readme section (after license) for instructions. Use at your own risk.
This file contains hidden or 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
# MIT License | |
# Copyright (c) 2019 Keith Boshoff (Wahooney) | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# READ ME | |
# History | |
# 2019/09/05 - Fixed download issue related to Blender changing the name of the zip | |
# Place in a non-root folder (ie. C:\Apps) for best results. | |
# This requires 7-zip | |
# The process: | |
# * Download the latest blender-4.4x zip from the nightly builder site. | |
# * Close all open blender instances (this is essential) | |
# * Delete old blender version | |
# * Unzip new blender version | |
######################################################################################################################## | |
# change these variables to suit your needs | |
# 7-ZIP location | |
$pathTo7Zip = 'C:\Program Files\7-zip' | |
# current version do check for | |
$blenderVersion = 'blender-4.4' | |
$blenderMinorVersion = '' | |
# folder to install blender into, ie. C:\blender-4.4 | |
$blenderInstallLocation = 'C:\' | |
# folder this script will operate in | |
$blenderWorkingPath = 'C:\Apps\' | |
######################################################################################################################## | |
# Here be dragons: don't touch these unless you know what you're doing | |
# establish environment | |
$env:Path = $env:Path + ';' + $pathTo7Zip | |
# Let's get a window foregrounder going | |
Add-Type @" | |
using System; using System.Runtime.InteropServices; | |
public class SFW { [DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool SetForegroundWindow(IntPtr hWnd); } | |
"@ | |
$blenderZip = $blenderVersion + '.zip' | |
$blenderZipFolderPath = $blenderVersion + "*" | |
$blenderInstallPath = $blenderInstallLocation + $blenderVersion | |
$blenderZipPath = $blenderWorkingPath + $blenderZip | |
$blenderSearchString = '*' + $blenderVersion + $blenderMinorVersion + '*-*-win*amd64*.zip?' | |
# set working folder | |
Set-Location -Path $blenderWorkingPath | |
# get blender download page | |
$blender = Invoke-WebRequest -uri https://builder.blender.org/download/ | |
Write-Host $blenderSearchString; | |
# find win64 build | |
$links = $blender.Links | select href | Where-Object { $_ -like $blenderSearchString } | |
# remove old zip file | |
if (Test-Path $blenderZip) { Remove-Item -Path $blenderZip } | |
Write-Host ""; | |
Write-Host -NoNewLine " Downloading "; | |
Write-Host $links[0].href; | |
Write-Host ""; | |
# download file | |
Invoke-WebRequest -uri ($links[-1].href) -OutFile $blenderZipPath | |
# Invoke-WebRequest -uri ('https://builder.blender.org' + $links[-1].href) -OutFile $blenderZipPath | |
# kill all running blender instances | |
$blenderProcess = Get-Process blender -ErrorAction SilentlyContinue | |
while ($blenderProcess) | |
{ | |
$bid = $blenderProcess.id | |
[SFW]::SetForegroundWindow($blenderProcess.MainWindowHandle) > $out | |
$blenderProcess.CloseMainWindow() > $out | |
Write-Host '[WAITING] Close all instances of Blender to continue...'; | |
Wait-Process -Id $bid | |
[SFW]::SetForegroundWindow((Get-Process -Id $pid).MainWindowHandle) > $out | |
$blenderProcess = Get-Process blender -ErrorAction SilentlyContinue | |
} | |
# unzip file | |
7z x $blenderZipPath $blenderZipFolderPath | |
# find new folder | |
$folder = (Get-ChildItem | Where-Object {$_.Name -like 'blender*' -and ($_.Mode -eq 'd-----')} | select Name)[0].Name | |
# remove old folder | |
if (Test-Path $blenderInstallPath) { Remove-Item -Path $blenderInstallPath -Force -Recurse } | |
# sanitize current folder | |
if ($folder -ne $blenderVersion) { Rename-Item -Path $folder -NewName $blenderVersion } | |
# move new folder | |
Move-Item -Path $blenderVersion -Destination $blenderInstallLocation | |
# wait for input | |
Write-Host -NoNewLine 'Press any key to continue...'; | |
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment