Blender Finder: allow passing the directory instead of the executable

Blender Finder now understands that directory paths should be suffixed
with `blender` (Linux, macOS) or `blender.exe` (Windows).

Giving the Setup Assistant a path like `C:\Program files\Blender
Foundation\Blender 3.2` will now just work. This is considerably simpler
for many users, as copy-pasting a directory from a file explorer is
simpler than obtaining/typing the path to the executable.
This commit is contained in:
Sybren A. Stüvel 2022-07-26 18:18:02 +02:00
parent 1e3a2b5480
commit 4cb0a6fb14
4 changed files with 28 additions and 3 deletions

View File

@ -6,7 +6,9 @@ import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
@ -58,8 +60,8 @@ func CheckBlender(ctx context.Context, exename string) (CheckBlenderResult, erro
}
if crosspath.Dir(exename) != "." {
// exename is some form of path, see if it works directly as executable.
return getResultWithVersion(ctx, exename, exename, api.BlenderPathSourceInputPath)
// exename is some form of path, see if it works for us.
return checkBlenderAtPath(ctx, exename)
}
// Try to find exename on $PATH
@ -70,6 +72,25 @@ func CheckBlender(ctx context.Context, exename string) (CheckBlenderResult, erro
return getResultWithVersion(ctx, exename, fullPath, api.BlenderPathSourcePathEnvvar)
}
func checkBlenderAtPath(ctx context.Context, path string) (CheckBlenderResult, error) {
stat, err := os.Stat(path)
if err != nil {
return CheckBlenderResult{}, err
}
if !stat.IsDir() {
// Simple case, it's not a directory so let's just try and execute it.
return getResultWithVersion(ctx, path, path, api.BlenderPathSourceInputPath)
}
// Try appending the Blender executable name.
log.Debug().
Str("path", path).
Str("executable", blenderExeName).
Msg("blender finder: given path is directory, going to find Blender executable")
exepath := filepath.Join(path, blenderExeName)
return getResultWithVersion(ctx, path, exepath, api.BlenderPathSourceInputPath)
}
// getResultWithVersion tries to run the command to get Blender's version.
// The result is returned as a `CheckBlenderResult` struct.
func getResultWithVersion(

View File

@ -4,6 +4,8 @@ package find_blender
// SPDX-License-Identifier: GPL-3.0-or-later
const blenderExeName = "blender"
// fileAssociation isn't implemented on non-Windows platforms.
func fileAssociation() (string, error) {
return "", ErrNotAvailable

View File

@ -14,6 +14,8 @@ import (
"unsafe"
)
const blenderExeName = "blender.exe"
// fileAssociation returns the full path of `blender.exe` associated with ".blend" files.
func fileAssociation() (string, error) {
exe, err := getFileAssociation(".blend")

View File

@ -182,7 +182,7 @@
v-model="customBlenderExe"
:class="{'is-invalid': blenderExeCheckResult != null && !blenderExeCheckResult.is_usable}"
type="text"
placeholder="Path to Blender executable"
placeholder="Path to Blender"
>
<p v-if="isBlenderExeChecking" class="is-in-progress">Checking...</p>
<p v-if="blenderExeCheckResult != null && !blenderExeCheckResult.is_usable" class="check-failed">