Headless Sverchok #4792
Replies: 3 comments
-
What you can do is to run Blender without GUI. I think something like this is possible:
Sverchok uses similar technic to perform some tests. It requires some knowlege about Sverchok internals. |
Beta Was this translation helpful? Give feedback.
-
Yeah, exactly like @Durman described. One thing i would like to mention. After loading a node tree I had to update the node tree to get Sverchok's data. I fixed it with: node_tree.process_ani(True, False). It will update all nodes immediately. |
Beta Was this translation helpful? Give feedback.
-
Script to run any script under Blender, #!/usr/bin/python3
# If your blender is not available as just "blender" command, then you need
# to specify path to blender when running this script, e.g.
#
# $ BLENDER=~/soft/blender-3.3.1/blender ./run_sverchok_script.py
#
import os
import sys
import argparse
BLENDER = os.environ.get('BLENDER', 'blender')
parser = argparse.ArgumentParser(description = "Run Python script which uses Sverchok API")
parser.add_argument('script', metavar="SCRIPT.PY", help = "Path to Python script file")
parser.add_argument('-b', '--blender', metavar="/PATH/TO/BLENDER", help = "Path to Blender executable", default = BLENDER)
argv = sys.argv[1:]
try:
delimiter_idx = argv.index("--")
my_arguments = argv[:delimiter_idx]
script_arguments = argv[delimiter_idx+1:]
script_args = " ".join(script_arguments)
except ValueError:
my_arguments = argv
script_args = ""
args = parser.parse_args(my_arguments)
command = f"{args.blender} -b --addons sverchok --python {args.script} --python-exit-code 1 -- {script_args}"
print(command)
os.system(command) A simple script demonstrating usage of Sverchok API — # We can import usual python modules here
import numpy as np
# Also we can import Sverchok API modules
from sverchok.utils.curve.bezier import SvBezierCurve
cpts = np.array([
[-2.0, 0.0, 0.0],
[-1.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[2.0, 1.0, 0.0]
])
curve = SvBezierCurve(cpts)
t_min, t_max = curve.get_u_bounds()
ts = np.linspace(t_min, t_max, num=50)
curve_points = curve.evaluate_array(ts)
print(curve_points) This prints output to stdout. A bit more complicated example — # We can import usual python modules here
import sys
import numpy as np
# We can import Blender API
import bpy
# Also we can import Sverchok API modules
from sverchok.utils.testing import link_node_tree, get_node_tree
# Use only arguments which come after --
# ones which come earlier are intended for Blender itself, not for this script
argv = sys.argv
delimiter_idx = argv.index("--")
argv = argv[delimiter_idx+1:]
if len(argv) < 2:
print("Usage: run_sverchok_script.py script_example.py -- FILE.blend TreeName")
sys.exit(1)
blend_path = argv[0]
tree_name = argv[1]
# Load node tree. It will be processed upon loading.
link_node_tree(blend_path, tree_name)
tree = get_node_tree(tree_name)
out_buffer = bpy.data.texts['output.txt']
output = out_buffer.as_string()
with open('output.txt', 'wb') as f:
f.write(output.encode('utf-8')) This prints output to |
Beta Was this translation helpful? Give feedback.
-
Is it possible to execute sverchok scripts headlessly? I'd like to write a python script that takes user input, loads a sverchok tree based on the input, executes it and writes the output.
Beta Was this translation helpful? Give feedback.
All reactions