Now that I've gotten to grips with making Python scripts that work, I'm paying more attention to readability and style. I've had the PEP 8 Style guide for Python Code recommended to me as a good standard to follow.
Here is a quick code snippet for calling a Python script from Substance Painter and parsing the results. The in/out is very simple, but serves as an example of using the alg.subprocess.check_output function to bridge the JS api and your own Python scripts. // This can be called from the QML UI, or elsewhere in the plugin code. function GetAllFilesInDirectory(root) { if (root == undefined) return; // I put my scripts in a relative path to keep my plugin tidy. var script_path = "Scripts/FileUtils.py"; // Gathering files var ret = "NOSTRING"; try { // The arguments are used as parameter inputs to the Python script. This requires some planning // and well communicated conventions, but works well enough. ret = alg.subprocess.check_output( [ pypath, // Absolute path to interpreter. script_path, // Relative path to the py script. "log_files_of_type", // sys.argv[1], in this case the python ...
Substance Painter plugins use the QT Meta Language , or QML files to build their interface. From Wikipedia: It is a JSON-like declarative language for designing user interface–centric applications. Inline JavaScript code handles imperative aspects. It is part of Qt Quick, the UI creation kit developed by Nokia within the Qt framework. Using this information, we can start building a window with a button to execute our script. Visual Studio Code For this tutorial I’m going to be using Visual Studio Code , a lightweight IDE from Microsoft. It’s not the same thing as Visual Studio, but it’s a nice scripting editor that’s available on Mac, Linux and PC. Other text editors like Sublime will do the job quite well. Installing QML Syntax Highlighting By default, QML files will appear as ordinary text files in Visual Studio Code. While this won’t stop you from being able to write the plugin, adding some QML support will make reading, organizing and editing our script easier. To do this, I’...
I thought I might make a little list of commands that I use *alot* in my Photoshop Python scripts in case they might be useful to other people out there trying to do the same thing. These examples are written using Python 2.7. An invaluable help is the Photoshop Cs4 Visual basic scripting guide . Although the scripting guide does not specifically deal with Python, the majority of the VB code works straight out of the box. Before anything else, you need to set up the COM interface! I recommend using the comtypes module . The win32com module can also do the job, but you will find it very limiting if you want to do anything with paths or selection arrays . This is how I usually create my photoshop COM reference in my scripts: import comtypes.client psApp = comtypes.client.CreateObject('Photoshop.Application') The psApp object that I have created is my reference to the photoshop application. From now on, I can call psApp to execute various actions. It's worth noting that if y...
Comments
Post a Comment