From 7b6df29203ea2b093bc68bc4f9aec0fa6a167b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Wed, 26 Apr 2023 12:48:51 +0200 Subject: [PATCH 1/9] Feature: Blender hook to execute python scripts at launch --- .../hooks/pre_add_run_python_script_arg.py | 61 +++++++++++++++++++ website/docs/dev_blender.md | 61 +++++++++++++++++++ website/sidebars.js | 1 + 3 files changed, 123 insertions(+) create mode 100644 openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py create mode 100644 website/docs/dev_blender.md diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py new file mode 100644 index 0000000000..7cf7b0f852 --- /dev/null +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -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", [])] + ) diff --git a/website/docs/dev_blender.md b/website/docs/dev_blender.md new file mode 100644 index 0000000000..228447fb64 --- /dev/null +++ b/website/docs/dev_blender.md @@ -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) +``` diff --git a/website/sidebars.js b/website/sidebars.js index 93887e00f6..c204c3fb45 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -179,6 +179,7 @@ module.exports = { ] }, "dev_deadline", + "dev_blender", "dev_colorspace" ] }; From 6de1710810b028949c86276088a988ccb83f06e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Wed, 26 Apr 2023 12:53:20 +0200 Subject: [PATCH 2/9] clean Path --- .../hosts/blender/hooks/pre_add_run_python_script_arg.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index 7cf7b0f852..9ae96327ca 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -31,7 +31,8 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): f"Adding python script {python_script_path} to launch" ) # Test script path exists - if not Path(python_script_path).exists(): + python_script_path = Path(python_script_path) + if not python_script_path.exists(): raise ValueError( f"Python script {python_script_path} doesn't exist." ) @@ -45,11 +46,11 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): ) self.launch_context.launch_args.insert( separator_index + 1, - Path(python_script_path).as_posix(), + python_script_path.as_posix(), ) else: self.launch_context.launch_args.extend( - ["-P", Path(python_script_path).as_posix()] + ["-P", python_script_path.as_posix()] ) # Ensure separator From 610a65420d521b26a3c44792368cbd4b9cec6219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Wed, 26 Apr 2023 14:02:18 +0200 Subject: [PATCH 3/9] remove useless settings --- .../hosts/blender/hooks/pre_add_run_python_script_arg.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index 9ae96327ca..ff3683baa9 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -14,15 +14,6 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): ] 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", [] From 4fe7ce64a2a834adcdd2763a22b8c93d532c0ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Wed, 26 Apr 2023 14:05:39 +0200 Subject: [PATCH 4/9] changes from comments --- .../hosts/blender/hooks/pre_add_run_python_script_arg.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index ff3683baa9..0f959b8f54 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -1,7 +1,6 @@ from pathlib import Path from openpype.lib import PreLaunchHook -from openpype.settings.lib import get_project_settings class AddPythonScriptToLaunchArgs(PreLaunchHook): @@ -14,10 +13,11 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): ] def execute(self): + if not self.launch_context.data.get("python_scripts"): + return + # Add path to workfile to arguments - for python_script_path in self.launch_context.data.get( - "python_scripts", [] - ): + for python_script_path in self.launch_context.data["python_scripts"]: self.log.info( f"Adding python script {python_script_path} to launch" ) From 6252e4b6c5c57e2a0f9f60fd0ad53cdcb7d26c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Wed, 10 May 2023 10:07:12 +0200 Subject: [PATCH 5/9] skipping if python script doesn't exist --- openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index 0f959b8f54..8015a15de8 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -24,8 +24,8 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): # Test script path exists python_script_path = Path(python_script_path) if not python_script_path.exists(): - raise ValueError( - f"Python script {python_script_path} doesn't exist." + raise self.log.warning( + f"Python script {python_script_path} doesn't exist. Skipped..." ) if "--" in self.launch_context.launch_args: From 52ad442a9cacdd7675bb49c9fbcd144a2400df51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Wed, 10 May 2023 10:10:12 +0200 Subject: [PATCH 6/9] lint --- openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index 8015a15de8..2d1b773c5f 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -25,7 +25,8 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): python_script_path = Path(python_script_path) if not python_script_path.exists(): raise self.log.warning( - f"Python script {python_script_path} doesn't exist. Skipped..." + f"Python script {python_script_path} doesn't exist. " + "Skipped..." ) if "--" in self.launch_context.launch_args: From 2e7b0b9d954c606220f8ad89fdf86549dc38e3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Fri, 19 May 2023 18:20:03 +0200 Subject: [PATCH 7/9] fix raise --- openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index 2d1b773c5f..525905f1ab 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -24,7 +24,7 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): # Test script path exists python_script_path = Path(python_script_path) if not python_script_path.exists(): - raise self.log.warning( + self.log.warning( f"Python script {python_script_path} doesn't exist. " "Skipped..." ) From dc7373408f8d586f854b99da7c9eb799441a3fec Mon Sep 17 00:00:00 2001 From: Felix David Date: Mon, 22 May 2023 10:39:30 +0200 Subject: [PATCH 8/9] continue if not python script path --- openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py index 525905f1ab..559e9ae0ce 100644 --- a/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py +++ b/openpype/hosts/blender/hooks/pre_add_run_python_script_arg.py @@ -28,6 +28,7 @@ class AddPythonScriptToLaunchArgs(PreLaunchHook): f"Python script {python_script_path} doesn't exist. " "Skipped..." ) + continue if "--" in self.launch_context.launch_args: # Insert before separator From d0886e43fe8efc8b675d9da5ccc9c37c459408d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20David?= Date: Thu, 1 Jun 2023 16:42:42 +0200 Subject: [PATCH 9/9] fix doc --- website/docs/dev_blender.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/dev_blender.md b/website/docs/dev_blender.md index 228447fb64..bed0e4a09d 100644 --- a/website/docs/dev_blender.md +++ b/website/docs/dev_blender.md @@ -9,14 +9,14 @@ toc_max_heading_level: 4 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.hosts.blender.hooks import pre_add_run_python_script_arg from openpype.lib import PreLaunchHook class MyHook(PreLaunchHook): """Add python script to be executed before Blender launch.""" - order = AddPythonScriptToLaunchArgs.order - 1 + order = pre_add_run_python_script_arg.AddPythonScriptToLaunchArgs.order - 1 app_groups = [ "blender", ]