mirror of
https://github.com/ynput/ayon-core.git
synced 2025-12-24 21:04:40 +01:00
Feature: Blender hook to execute python scripts at launch
This commit is contained in:
parent
dc527e3c5e
commit
7b6df29203
3 changed files with 123 additions and 0 deletions
|
|
@ -0,0 +1,61 @@
|
|||
from pathlib import Path
|
||||
|
||||
from openpype.lib import PreLaunchHook
|
||||
from openpype.settings.lib import get_project_settings
|
||||
|
||||
|
||||
class AddPythonScriptToLaunchArgs(PreLaunchHook):
|
||||
"""Add python script to be executed before Blender launch."""
|
||||
|
||||
# Append after file argument
|
||||
order = 15
|
||||
app_groups = [
|
||||
"blender",
|
||||
]
|
||||
|
||||
def execute(self):
|
||||
# Check enabled in settings
|
||||
project_name = self.data["project_name"]
|
||||
project_settings = get_project_settings(project_name)
|
||||
host_name = self.application.host_name
|
||||
host_settings = project_settings.get(host_name)
|
||||
if not host_settings:
|
||||
self.log.info(f"""Host "{host_name}" doesn\'t have settings""")
|
||||
return None
|
||||
|
||||
# Add path to workfile to arguments
|
||||
for python_script_path in self.launch_context.data.get(
|
||||
"python_scripts", []
|
||||
):
|
||||
self.log.info(
|
||||
f"Adding python script {python_script_path} to launch"
|
||||
)
|
||||
# Test script path exists
|
||||
if not Path(python_script_path).exists():
|
||||
raise ValueError(
|
||||
f"Python script {python_script_path} doesn't exist."
|
||||
)
|
||||
|
||||
if "--" in self.launch_context.launch_args:
|
||||
# Insert before separator
|
||||
separator_index = self.launch_context.launch_args.index("--")
|
||||
self.launch_context.launch_args.insert(
|
||||
separator_index,
|
||||
"-P",
|
||||
)
|
||||
self.launch_context.launch_args.insert(
|
||||
separator_index + 1,
|
||||
Path(python_script_path).as_posix(),
|
||||
)
|
||||
else:
|
||||
self.launch_context.launch_args.extend(
|
||||
["-P", Path(python_script_path).as_posix()]
|
||||
)
|
||||
|
||||
# Ensure separator
|
||||
if "--" not in self.launch_context.launch_args:
|
||||
self.launch_context.launch_args.append("--")
|
||||
|
||||
self.launch_context.launch_args.extend(
|
||||
[*self.launch_context.data.get("script_args", [])]
|
||||
)
|
||||
61
website/docs/dev_blender.md
Normal file
61
website/docs/dev_blender.md
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
---
|
||||
id: dev_blender
|
||||
title: Blender integration
|
||||
sidebar_label: Blender integration
|
||||
toc_max_heading_level: 4
|
||||
---
|
||||
|
||||
## Run python script at launch
|
||||
In case you need to execute a python script when Blender is started (aka [`-P`](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#python-options)), for example to programmatically modify a blender file for conformation, you can create an OpenPype hook as follows:
|
||||
|
||||
```python
|
||||
from openpype.hosts.blender.hooks.pre_add_run_python_script_arg import AddPythonScriptToLaunchArgs
|
||||
from openpype.lib import PreLaunchHook
|
||||
|
||||
|
||||
class MyHook(PreLaunchHook):
|
||||
"""Add python script to be executed before Blender launch."""
|
||||
|
||||
order = AddPythonScriptToLaunchArgs.order - 1
|
||||
app_groups = [
|
||||
"blender",
|
||||
]
|
||||
|
||||
def execute(self):
|
||||
self.launch_context.data.setdefault("python_scripts", []).append(
|
||||
"/path/to/my_script.py"
|
||||
)
|
||||
```
|
||||
|
||||
You can write a bare python script, as you could run into the [Text Editor](https://docs.blender.org/manual/en/latest/editors/text_editor.html).
|
||||
|
||||
### Python script with arguments
|
||||
#### Adding arguments
|
||||
In case you need to pass arguments to your script, you can append them to `self.launch_context.data["script_args"]`:
|
||||
|
||||
```python
|
||||
self.launch_context.data.setdefault("script_args", []).append(
|
||||
"--my-arg",
|
||||
"value",
|
||||
)
|
||||
```
|
||||
|
||||
#### Parsing arguments
|
||||
You can parse arguments in your script using [argparse](https://docs.python.org/3/library/argparse.html) as follows:
|
||||
|
||||
```python
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Parsing arguments for my_script.py"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--my-arg",
|
||||
nargs="?",
|
||||
help="My argument",
|
||||
)
|
||||
args, unknown = arg_parser.parse_known_args(
|
||||
sys.argv[sys.argv.index("--") + 1 :]
|
||||
)
|
||||
print(args.my_arg)
|
||||
```
|
||||
|
|
@ -179,6 +179,7 @@ module.exports = {
|
|||
]
|
||||
},
|
||||
"dev_deadline",
|
||||
"dev_blender",
|
||||
"dev_colorspace"
|
||||
]
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue