Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save unwave/9421a0e3b57626f6ac06c61bd6f2581d to your computer and use it in GitHub Desktop.
Save unwave/9421a0e3b57626f6ac06c61bd6f2581d to your computer and use it in GitHub Desktop.
# test blender property default retrieval
def ensure_site_packages(packages: list, directory: str):
"""
`packages`: list of tuples (<import name>, <pip name>)
`directory`: a folder for site packages, will be created if does not exist and added to `sys.path`
"""
if not packages:
return
import bpy
import os
import sys
import importlib
import importlib.util
os.makedirs(directory, exist_ok = True)
if not directory in sys.path:
sys.path.append(directory)
modules_to_install = [module[1] for module in packages if not importlib.util.find_spec(module[0])]
if not modules_to_install:
return
if modules_to_install:
result = input(f'Install {modules_to_install} to: {directory}? Y/N:')
if not result.lower() == 'y':
quit()
if bpy.app.version < (2,91,0):
python_binary = bpy.app.binary_path_python
else:
python_binary = sys.executable
import subprocess
subprocess.run([python_binary, '-m', 'ensurepip'], check=True)
subprocess.run([python_binary, '-m', 'pip', 'install', *modules_to_install, "--target", directory], check=True)
importlib.invalidate_caches()
def is_in_blender():
try:
import bpy
return bool(bpy.app.version) and bool(bpy.app.binary_path)
except:
return False
def test_cycles_samples_default():
print()
import bpy
print(bpy.data.scenes["Scene"].cycles.samples)
assert bpy.data.scenes["Scene"].cycles.samples
assert bpy.data.scenes["Scene"].cycles.bl_rna.properties['samples'].default == bpy.data.scenes["Scene"].cycles.samples
print(bpy.data.scenes["Scene"].cycles.preview_samples)
assert bpy.data.scenes["Scene"].cycles.preview_samples
assert bpy.data.scenes["Scene"].cycles.bl_rna.properties['preview_samples'].default == bpy.data.scenes["Scene"].cycles.preview_samples
def test_socket():
import bpy
import math
print()
material = bpy.data.materials['Material']
if not material.use_nodes: #2.79-
material.use_nodes = True
material.node_tree.nodes.new('ShaderNodeBsdfPrincipled')
default_value = material.node_tree.nodes['Principled BSDF'].inputs['IOR'].default_value
print(default_value)
assert math.isclose(default_value, 1.45, abs_tol=1e-6)
default = material.node_tree.nodes['Principled BSDF'].inputs['IOR'].bl_rna.properties['default_value'].default
print(default)
assert default == 0.0
assert not math.isclose(default, 1.45, abs_tol=1e-6)
if __name__ == "__main__" and is_in_blender():
import site
ensure_site_packages([("pytest", "pytest")], directory = site.getusersitepackages())
import pytest
raise SystemExit(pytest.main([
'-svv',
__file__
]))
blenders = [
r'blender',
r'C:\blender\blender-3.1.2-windows-x64\blender.exe',
r'C:\blender\blender-3.6.0-alpha+main.e078419c9c12-windows.amd64-release\blender.exe',
r'C:\Program Files (x86)\Steam\steamapps\common\Blender\blender.exe',
r'D:\games\SteamLibrary\steamapps\common\blender_293\blender.exe',
r'D:\games\SteamLibrary\steamapps\common\blender-2.83.13-windows64\blender.exe',
r'D:\source\software\blender\blender-2.82-windows64\blender.exe',
r'D:\games\SteamLibrary\steamapps\common\blender-2.79.0-git.e045fe53f1b0-windows64\blender.exe',
]
if __name__ == '__main__' and not is_in_blender():
import subprocess
import os
for blender in blenders:
print('\n')
print('#' * os.get_terminal_size()[0])
# print(subprocess.check_output([blender, '--version']).splitlines()[0].decode())
subprocess.run([blender, '--python-exit-code', '1', '-b', '-noaudio', '--factory-startup', '--python', __file__])
if os.name == 'nt':
os.system('pause')
else:
input('Press Enter to exit...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment