Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting examples. #5059

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Sverchok Scripting

This directory contains some examples of Sverchok scripting:

* `blender_python.sh` — script to run any python script with blender's builtin python
* `generate_api_documentation.py` — script to generate API documentation (similar to what we have in Github Actions, but to launch locally)
* `run_sverchok_script.py` — script to run any python script, which probably uses Sverchok API. Similar to blender_python.sh, but does not depend on OS / shell.
* `script_example.py` — example of script which can be run by run_sverchok_script.py.
10 changes: 10 additions & 0 deletions scripts/blender_python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -e

BLENDER=${BLENDER:-blender}
MODULE=$1
shift

$BLENDER -b --addons sverchok --python $MODULE --python-exit-code 1 -- $@

19 changes: 19 additions & 0 deletions scripts/generate_api_documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/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-2.79/blender ./generate_api_documentation.py
#

import os
import sys

BLENDER = os.environ.get('BLENDER', 'blender')

argv = sys.argv[1:]
argv = ['"' + arg + '"' for arg in argv]
args = " ".join(argv)

os.system(f"{BLENDER} -b --addons sverchok --python utils/apidoc.py --python-exit-code 1 -- {args}")

34 changes: 34 additions & 0 deletions scripts/run_sverchok_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/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)

34 changes: 34 additions & 0 deletions scripts/script_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# 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'))