Skip to main content
Known Participant
July 18, 2023
Question

How to run a python script without - 'def start_plugin' ?

  • July 18, 2023
  • 2 replies
  • 2294 views

Hello, 

I was hoping someone could tell me how I could run/trigger python scripts without using the start and close definitions shown in the examples. 

Currently, I am launching painter via an external python script using  subprocess.call([substance_painter_path])

What I would like to do now is run/trigger my other python scripts for painter directly from that external python script (Open Project, Rebake Textures, Export etc.) 

But so far I have only been able to run those kind of scripts via the Python drop down, and using  start/close plugin definitions. 

If someone could explain this to me I would really appreciate it! 

Thank you! 

This topic has been closed for replies.

2 replies

Participating Frequently
July 28, 2023

Here is some info that might be helpful to run your external python scripts:

You can submit multiple commands/lines to python console, just separate them with a semicolon.

For example I use pycharm, and when testing code I don't want to constantly copy files to painters plugin folder. Here is what I do: 
- In painters python console set the path to pycharm folder such as 

path = r"D:\_WORK\PycharmProject1\code\"

- Then I have this line ready that I simply copy paste:

import sys; sys.path.append(path); import substance_functions as funcs;

- This adds my pycharm project directory to painters system path (only valid for current session) and allows me to import and run any python script  from that folder

Then I can call functions like this:

funcs.some_function()


If I you make some changes to substance_functions.py, you need to reload it in painter by using 

imp.reload(funcs)

 

Known Participant
July 31, 2023

Ah this is a great method! - Thanks for sharing this with me! 


Inspiring
July 18, 2023

Hi there.

 

You should be able to run any script on start up as long as it goes through the start_plugin function. Also you can have multiple python files with start_plugin defined and substance will run them all on startup as long as they are in the proper search path.

 

For example if I have a python script that opens a project, bakes textures, and exports called rebake_and_export.py, then in my main py file I could do:

 

from rebake_and_export import run

def start_plugin():
    run()

 

Then this will be run whenever substance starts up.

 

If you only want to run this SOMETIMES and not every time substance starts up, then you can alter the environment variable SUBSTANCE_PAINTER_PLUGINS_PATH right before you call substance painter:

 

import subprocess, os

my_env = os.environ.copy()
my_env["SUBSTANCE_PAINTER_PLUGINS_PATH"] = f"/path/to/custom/start/script.py:{my_env['SUBSTANCE_PAINTER_PLUGINS_PATH']}"
subprocess.Popen(substance_painter_path, env=my_env)

 

Keep in mind I have used Popen rather than call.

 

Does this make sense?

Inspiring
July 18, 2023

Sorry the SUBSTANCE_PAINTER_PLUGINS_PATH env variable wouldn't point to a python file but rather a directory that contains a "plugins" sub directory, and in the sub directory would be the custom script to run.