Posts

Showing posts with the label Tech Stuff

Calling Python from Substance Painter

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 Designer foray

Image
I'm pretty late to the whole substance craze, having spent three years in a mobile development environment where textures larger than 512x512 caused eyebrows to raise within the engineering team. But I kept hearing things about it being amazing, so I thought I'd give it a shot. Here is my attempt at a "hand painted" look, based off of similar processes I'd use in Photoshop. The texture is generated using an exported sbsar material and... well, thats it. I saw a nice example online of using a switch to swap out gradient maps in a material which allowed me to create a bunch of looks in the one material. This tool makes me happy.

Google Spreadsheet: Script to Change Row Background Color on Cell Edit

Now for something a little different! Google Spreadsheets! Here is a little script I cooked up today for changing a row color in Google Spreadsheet when you change the contents of a drop down menu. When you change the contents of a cell in the designated "status" column the entire row will change it's background color to match whatever rule you have defined in the switch statement. A useful addition to this would be the ability to automatically find the status column in whatever sheet you have open, but it was beyond the scope of what the script was meant for, so here is it in the raw! /** Google Sheets Script to modify a row's color based on the 'Status' entry. This script uses the onEdit() method, which is called every time a cell is edited. The statusColumn variable's cells are assumed to be a drop-down with the following options: Complete In Progress Not Started Revisions Req Blocked The script modifies a row's color based on the contents- leaving ...

New Photoshop Exporter- ExportThis!

Image
After a long and (mostly) successful run of controlling Photoshop with Python, I decided to take a stab at writing a fully integrated Photoshop Extension, this time using the native Javascript based API. The end result is a cleaner, smaller and all round better Photoshop tool that does a number of kinda cool things. Export This! Some of the User-facing features Because its written to take advantage of the native API there is a significant difference in app size- Texture Monkey being almost 40mb when including all the additional GUI and Python library files it required, vs a 29kb .ZXP installer for ExportThis. The ZXP is easily distributed, and uses Adobe Extension Manager CC to install/update/remove itself.  Within the tool, the document template system was added to allow artists to easily make documents that would be compatible with the exporter (as a lot of it is group name/hierachy based) but I expanded on it to allow artists to make and save their own particular document setups...

Perforce: Setting up Ignore lists

Image
Sometimes your local workspace will have files that you don't want to check in. Examples include Maya swatches files, if you are using Unity, the library folder, and anything called tmp. You can set up P4 to ignore files and folders using the P4IGNORE environment variable, and a text file in your perforce root called .p4ignore.txt Here is how you set it up: An example of the contents of an ignore file. Comments are added using the # sign.  Create a file in the root directory of your workspace (eg: C:/Projects/Perforce/) called .p4ignore.txt Inside this file, define which file types to ignore. For example: The .swatches file, which is generated by Maya.  The Library folder, generated by Unity.  Any other folder that contains source file work that should remain local to people's workstations (texture bakes, render data etc). Once this file is set up, open the command console (windows key + r then type in cmd) and type in: p4 set P4IGNORE = .p4ignore.txt Setting the environm...

Maya: Animation Event Definition Tool

Image
I've been working on a new set of tools to define animation event data in Maya and export it into Unity for consumption by the AssetPostProcessor. This has been kind of a cool learning experience, because before now I really didn't have much to do with the Animation pipe, which has been an entirely manual process for the animators. Previously clips were cut up and events were defined by hand in Unity once the .fbx files had been exported from Maya. This wasn't such a bad thing, except that the tools in Unity for defining events were very un-intuitive. The aim of doing all this was to allow the animators the ability to work in a tool they were familiar with, and get the AssetPostProcessor to do most of the boring legwork on the Unity side. The Maya interface.  The Maya tool is pretty simple right now, and responsive enough that the animators don't hate me. Navigating to different clips or events will move to that time range on the slider. The event list is directly corre...

Perforce: Delete empty change lists using Python

Image
Some of the tools I've written allow a user to check out chains of files- like Maya files as well as any exported FBX files and their unity metadata etc, all in one nice pass. This is great, but it has one annoying side effect. If the user presses the button twice the files are added to another change list, but the one they were just in stays there, empty. Press it a number of times and suddenly you have an army of zombie phantom changelists whose only purpose is to clutter up your pending list and annoy you.  I have been using the P4Python API and found it to be... well... its kinda crap. It would be great if it had a 'delete all empty pending changelists' function, but it doesn't seem to. So I wrote one of my own.  Beware! If you don't like hacking output out of strings the following code will make you cringe...  There's my target... import subprocess """Get a list of changelists from the command line. Try to delete them (will delete if empty)...

Use Python to use JavaScript to get Photoshop to do stuff... and tell you about it!

Its been pretty well established that you can send a .jsx file to Photoshop using a subprocess call in Python. The tricky part is then getting Photoshop to send some information back. This is my awesome hacky method of passing information from Photoshop to Python. It relies on being able to write to a temporary text file from Photoshop and then reading that information back into Python. This method relies on actually being able to write data to disk... if that's a problem I suspect you could do the same to the console standard output and get the same results... somehow? Maybe? Because writing to the disk was not a problem in this situation, I went with a temp file solution. The only kinda tricky part was making my program wait for the return value to be passed. Subproces.call() returns a value of 1 or 0 from the shell, but this only indicates that the program successfully (or not) opened. Its highly likely that whatever script you passed to Photoshop as part of the Subprocess cal...

Texture Monkey: Starting to look like a real tool.

Image
Stuff is happening... After a little while in the field it came to be pretty apparent that the win32com module  can't  be relied upon to consistently work on everyone's machines. I spent a good amount of time trying to work out why , but came up with no solution that worked consistently on everyone's workstations. Errors like ('Member not found, None, None) would constantly pop up in their debug feedback, but not on mine. Talk about frustrating. So I got pretty annoyed and just ripped the win32com components out.  This was a pretty big undertaking, because apart from the GUI, win32com stuff made up about 80% of the remaining code. The P4 functionality wasn't touched, because its operating through it's own native API.  The solution: Photoshop Automation without win32com! The solution I went with was to write a wrapper around the JavaScript equivalent of what I was using the win32com module for. Each of these commands would be put into a list, which is then compi...

Texture Monkey... version"some big number"

Image
We are now getting Perforce. Oh how I  missed thee Perforce! And what a fantastic opportunity to revisit my favorite pet project, Texture Monkey! Be be honest, I'm kinda sick of re-writing this tool, but I've got it to a point where it meets a couple of extra prerequisites that it was sucking at before, mainly: It's written at a point where I actually feel I can competently write functional and readable code.   It's UI uses the QT framework. Which it MUCH nicer to work with than the previous WX.  It's been built around the premise that it  should easily be portable across projects without having to change any of the source code- provided afew assumptions about project structure are met.  It does more than texture exporting, and now supports Perforce integration as well.  Most importantly, it is written in a modular fashion, and can be expanded upon or cut back without too much trouble.  Now with extra stuff! Behind the scenes: It stores and loads tool ...

Writing Metadata to a PSD file using Python

Image
I want to save some tool specific information about a PSD file, but I don't want to have another pesky metadata file floating about to bloat my source texture folder. Luckily, the PSD file format supports writing custom MetaData within it, which is perfect for what I want to do. In this particular example, I want my tool to be able to remember which folder the flattened image associated with this PSD will eventually be put into, the format the image will be in and the resolution. There are many fields you can write to, but I have chosen to write to the Instructions information, because that just makes the most sense. Usually in Photoshop, you can see this fields available by going to the file info panel and going to the advanced tab: In Python, we can access and write to the PSD's metadata very easily. import win32com.client.dynamic as w32dynamic w32 = w32dynamic.Dispatch psApp = w32('Photoshop.Application') doc = psApp.activeDocument # When I pull this info out of the ...

Model Monkey: Now with Configs!

Image
I've decided to use ini files to make it easier to port my Maya exporter across different projects with as little fuss as possible, using the standard Python ConfigParser module. The last time I really played around with .ini files was to mess with Command and Conquer Red Alert, and that was some time ago. It's really awesome that the .ini file format is still worth using today. There is a bug where you can add new file rules until the tool grows so long it is bigger than the screen. Time to add a scroll bar... Part of getting this to work includes creating a GUI interface to minimize the manual file editing that you might expect. Being my first major use of an .ini for a tool (I did use a teeny tiny .ini on Texture Monkey) I suddenly realized that I had to re-work a lot of my previous configuration code in order to make my tool flexible enough to be re-configurable without breaking into a million different lame parts. BUT! It's working now, whoo! A couple of things have to...

Maya Exporter: Tabs and Tools!

Image
I've been chipping away at my Maya Exporter and have a few Photoshop functions tied into it now, along side the original exporter functionality. Behind the scenes the Photoshop commands are built on Standard modules, using Subprocess and some string manipulation in order to get JavaScript commands to the Photoshop application. As the tool has expanded beyond it's initial export-only functionality, there has been a decent amount of clean up and re-factoring behind the scenes to make sure that it doesn't just turn into a mega-script. Maya-side functions and Photoshop functions have been split into separate classes and files from the GUI, which has made keeping track of the code a lot easier. The GUI is built using the Pyside QT Libraries, which comes standard with Maya 2014. Next on the list, adding the option to load different project environments.

Python Photoshop Automation without win32com- The Example

Image
So here is a working example of what I was talking about in my last blog post- making Photoshop automation possible without needing to use the win32com module. This specific example is not cross-platform compatible. It relies on getting the Photoshop application through it's registry entry, but the subprocess module is cross platform so it shouldn't be too hard to hammer this into a shape that can be used on Mac and PC. Or just Mac. Or whatever. Essentially there is no real magic going on here, just lots of string manipulation behind the scenes. Like I said in my last post, the biggest hole in this right now is the lack of feedback from Photoshop, but that's something I'm looking into when I get a few spare minutes here and there. Here is the example. When you run it, you should come up with something that looks like this: What an Amazing document.  And the code! This is a particularly long snippet. Hang in there! Essentially when the commands are called, like add_new_...

Python Photoshop Automation without win32com.

Clunky but kinda fun, its possible to automate Photoshop using Javascript files passed as arguments to the subprocess.  Although this essentially means you have to write your script in two languages (PS's native JS and Python for pipeline) the practical aspect of this is that it allows scripts to be written that do not require people to have the win32com/comtypes modules installed, and paves the way for cross platform scripts that aren't tied to windows only modules.  I've had some success with creating a dynamic JavaScript builder that wraps Python functions around Photoshop's native Javascript commands. When the list of commands is executed it is compiled into a jsx file and then passed on to the Photoshop subprocess as an argument. The only short coming right now is obtaining the output from Photoshop and making it into a two-way street. I have some ideas on how to do this, and will update with the solution I end up using. 

Model Monkey: A Maya Exporter

Image
When I started on the project I have been working on for the last few months, it lacked any kind of automation or pipeline tools for the Art department. There was no kind of convention in the names, and no kind of structure to the project. It was a nightmare! Did I mention the pipeline had artists working on two different platforms? Compensating within the tools for the different folder structures on Mac and PC computers also became a concern, although it did not end up being as much of a problem as I thought it would be. From day one it was a goal of mine to introduce naming conventions, and restructure the project to be more pipeline friendly. It was a crazy-big job, but now its starting to pay off with some art tools. To address the lack of exporting tools I wrote a Maya exporter that takes advantage of the new naming conventions, predictable folder structure and Python's awesome ability to manipulate strings. Behind the scenes it has a dictionary that matches the scene file...

PySide and Maya2014

I've been working with PySide to make some tools for our Animation department. Part of that process involves a UI that is generated on the fly based on data read in from a spreadsheet. It was important that each UI component had a unique and predictable object name so that I could access the information it contained and save it out later. In case anyone else needs to do it, or has a better way, here is an example of the process I use.  Thanks to Nathan Horne and Chris Zurbrigg  for their very useful examples! # System # System import sys import os # GUI Modules from PySide import QtCore, QtGui import maya.OpenMayaUI as apiUI # Allows converting pointers to Python objects from shiboken import wrapInstance def maya_main_window(): main_win_ptr = apiUI.MQtUtil.mainWindow() return wrapInstance(long(main_win_ptr), QtGui.QWidget) # Create out Dialog class, inheriting from QDialog class Dialog(QtGui.QDialog): # Call the maya_main_window command to parent it def _...

Extending the Unity Editor

Image
Its been a while since my last post, as I have been pretty busy with a new job and learning new tools. After going through a bunch of tutorials I have familiarized myself with the Unity3d workflow and C#, and busied myself writing art tools for my new workplace. The Unity Editor is pretty fun to work with, and has a very extendable interface. With just a few lines of code (C# by preference) it's possible to create a customized interface window. Here is a bare bones example of how to make a custom window in Unity 4.2, and make it do stuff. The example is written in CSharp and does a very simple task, makes a window and tells you the name of whatever you have selected. // Example Window- Get the name of whatever game object // that is currently selected using UnityEngine; using UnityEditor; public class ExampleWindow : EditorWindow { // Strings string objectName = ""; // Update specific variables // Using the repaint flag, GUI updates are done once, rath...

Sliced Py- Cheat sheet for Python Slicing

I'm constantly using slicing in my Python scripts, but I am also constantly scratching my head about exactly where to put the little colon to get the slice that I'm after. After consulting the stackoverflow gurus I wrote a little cheat sheet using a string as an example.   foo = "Monty Python's Flying Circus" #Print the middle of foo- slice from the 7th-1 character end at the 15th-1 character print foo[6:14] #Use a slice to get the first six characters print foo[:6] #>>> Monty #Slice after the first six characters of foo print foo[6:] #>>> Python's Flying Circus #Print everthing in foo print foo[:] #>>> Monty Python's Flying Circus #Every second character print foo[0:27:2] #>>> MnyPto' ligCru #The last character of foo print foo[-1] #>>> s #The last two characters of foo print foo[-2:] #>>> us #Everything but the last seven charachers of foo print foo[:-7] #>>> Monty Python's Flying

Maya, Python and Wings IDE

I've recently moved away from the Maya Script Editor for my Maya scripting and started using Wing IDE, which is great in that it has full code auto-complete, debugging support and can be rigged up to execute code in Maya straight from the IDE window using a simple server setup. Here are a couple of guides I found handy in getting Wings to work with my Maya install: To set up Auto Complete for MEL and Python. To execute code in Maya from the Wing IDE