Skip to content

Instantly share code, notes, and snippets.

@hogjonny
Created February 25, 2015 18:13
Show Gist options
  • Save hogjonny/5709033ecd616db64721 to your computer and use it in GitHub Desktop.
Save hogjonny/5709033ecd616db64721 to your computer and use it in GitHub Desktop.
Basic python formatting for a working example of accessing Allegorithmic Substance BatchTools via script.
#!/usr/bin/env python
#coding:utf-8
# -- This line is 75 characters -------------------------------------------
__author__ = 'HogJonny'
import os, sys
import subprocess
from bp_files import Path # path convenience class
## Documentation ##########################################################
## if you want additional info, you can use the included substance CMD for batch tools
## and retreive help from the various .exe and command args
## the command line is here (should in in the star menu):
## %COMSPEC% /k "E:\Program Files\Allegorithmic\Substance\BatchTools\4.x\btvars.bat"
## for example, running this command gives you a bit of useful information about the mutator:
## sbsmutator.exe --help specialization
## path to substance resources
# if a package has core dependancies (like blend.sbs, etc.) this is REQUIRED so it can find them
resourcesPath = Path(r"E:\Program Files\Allegorithmic\Substance\Designer\4.x\resources\packages")
## TOOLS ##################################################################
## the tools we might be wanting to run
sbsBaker = Path(r"E:\Program Files\Allegorithmic\Substance\BatchTools\4.x\sbsbaker.exe") # bakes images from mesh, like cavity maps
sbsCooker = Path(r"E:\Program Files\Allegorithmic\Substance\BatchTools\4.x\sbscooker.exe") # cooks sbs to sbsar materials
sbsMutator = Path(r"E:\Program Files\Allegorithmic\Substance\BatchTools\4.x\sbsmutator.exe") # mutates a sbs (modified clone)
sbsRender = Path(r"E:\Program Files\Allegorithmic\Substance\BatchTools\4.x\sbsrender.exe") # renders output images from sbs/sbsar
## BAKER ##################################################################
### some values ... these are only used for sbsbaker I think
## Size of generated bitmaps 1='64x64' 2= 128x128' 3='256x256' 4='512x512' 5='1024x1024' 6='2048x2048'
#outputSize = 5
## Format of the baked texture (bmp,jpeg,png,etc.)
#outputFormat = "png"
## Normal Maps format (0=OpenGL 1=DirectX)
#normalFormat = 0 # <-- i am pretty sure that ND uses the openGL format on this project
### here is an example for generating commanArgs fof the baker
#commandArgs = ('curvature "{0}"'.format(meshPath.fbx)+
#' --normal "{0}"'.format(imgNormalPath.tga)+
#' --normal-format = {0}'.format(normalFormat)+
#' --output-size {0}'.format(outputSize)+
#' --output-path "{0}"'.format(outputPath)+
#' --output-name {inputName}_Curvature'+
#" --output-format {0}".format(outputFormat))
#result = subprocess.call( '"{0}" {1}'.format(sbsBaker, commandArgs) )
## PRESETS ################################################################
## input / output paths
# we should put all of our presets in this directory
sbs_bpPresets = Path(r'G:\DEV\CP9\BP\ArtTools\SubstanceShared\packages\bpPresets')
# configure one of our presets for mutation
inputSBS = Path(sbs_bpPresets + '\\batchTest.sbs').expand()
## SETUP INPUTS ###########################################################
# input images
inputDiffuse = Path(r'M:\CP9\ND\drop20140715_synced\big2\art\levels\worldCrack\textures\wood\wc-woodtiles-color.tga')
# I as also trying a .PNG, because I was having issues ... but it turned out to be the wrong command args for --image-connect
# See the commented out line below, this tag seems to be the issue: '@format@PNG' and this tag might be depricated?
#inputDiffuse = Path(r"G:\DEV\CP9\Data\Fools\AssetOverride\art\levels\worldCrack\textures\wood\WIP\wc-woodtiles\wc-woodtiles-color.png")
# asset override, output path
outputPath = Path(r'G:\DEV\CP9\Data\Fools\AssetOverride\art\levels\worldCrack\textures\wood\WIP\wc-woodtiles', storeCaseSensitive=True)
#foo = inputDiffuse.splitext()[0].basename() # <- this will give you just the name of input
## SETUP COMMAND ##########################################################
commandArgs = ('specialization '+
'--input "{0}" '.format(inputSBS)+
'--presets-path "{0}" '.format(resourcesPath)+
#'--connect-image "{0}" '.format('input_diffuse'+'@path@{0}'.format(inputDiffuse)+'@tiling@1'+'@format@PNG'+'@level@0')+
'--connect-image "{0}" '.format('input_diffuse'+'@path@{0}'.format(inputDiffuse)+'@tiling@1')+
'--output-name "{0}" '.format(inputDiffuse.splitext()[0].basename())+
'--output-graph-name "{0}" '.format(inputDiffuse.splitext()[0].basename())+
'--output-path "{0}"'.format(outputPath))
## DEBUG COMMAND DUMP #####################################################
## Test code ... writes out the command string to a text file for debugging
## so it can be copied and pasted to a commandline...
outputPathDEBUG = Path(outputPath +'\\DebugCommandDump.txt').expand()
outputPathDEBUG.makedirs()
outputPathDEBUG.touch()
outputPathDEBUG.exists()
text_file = open(outputPathFOO, "w")
text_file.write('"{0}" {1}'.format(sbsMutator, commandArgs))
text_file.close()
## RUN COMMAND ############################################################
## The above command will mutate the inputSBS ...
## Attach input files, then save the mutated .sbs variant into the outputPath
result = subprocess.call( '"{0}" {1}'.format(sbsMutator, commandArgs) )
print result
@hogjonny
Copy link
Author

Substance packages written out as .sbs files, are basically .xml files.
Currently, one of the main fallbacks I see to .sbs batchtool automation, is that you basically have to know beforehand all of the names (identifiers) of the input nodes. There really isn't any sort of API for handling them.

So, my thoughts, are that if should be pretty easy, to make a class to parse .sbs files and access/retrieve all the input/output nodes, etc., to make it easier to work with them in script. Ideally, you'd also be able to access switches and other settings - but I don't think that the batchtools give the user any of this sort of access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment