def cooksbsar(src_file, output_path): """ Cooks an sbsfile to an sbsar file :param src_file: path to the sbs file to cook :type src_file: string :param resources_path: The path to the substance resources :type resources_path: string :param output_path: The directory to put the result :type output_path: string :return: None """ print('Operating {}'.format(src_file)) myContext = context.Context() sbsDoc = substance.SBSDocument(myContext,src_file) sbsDoc.parseDoc() graph = sbsDoc.getSBSGraphList()[0] batchtools.sbscooker(inputs=src_file, output_path=output_path, expose_random_seed=False, quiet=True, verbose=False, includes='/opt/Allegorithmic/Substance_Designer/resources/packages' ).wait()
def get_sbsDoc(sbs_path, material): # Create path to .sbs file sbs_file = material + ".sbs" aSBSFileAbsPath = os.path.join(sbs_path, sbs_file) # Creation of a SBSDocument object from an existing .sbs file, parsing and writing aContext = context.Context() sbsDoc = substance.SBSDocument(aContext, aSBSFileAbsPath) sbsDoc.parseDoc() sbsDoc.writeDoc() return sbsDoc
def read_sbs(sbsfile): # Documentation about SBSGraph : https://support.allegorithmic.com/documentation/display/SAT/graph#graph.graph.SBSGraph content = [] myContext = context.Context() sbsDoc = substance.SBSDocument(myContext, sbsfile) sbsDoc.parseDoc() graphs = sbsDoc.getSBSGraphList() for item in graphs: graph = {} # Doc : https://support.allegorithmic.com/documentation/display/SAT/inputparameters#graph.inputparameters.SBSParamInput graph['identifier'] = item.mIdentifier graph['label'] = item.mAttributes.mLabel graph['description'] = item.mAttributes.mDescription graph['author'] = item.mAttributes.mAuthor graph['usertags'] = item.mAttributes.mUserTags graph['parameters'] = {} for param in item.mParamInputs: graph['parameters'][param.mIdentifier] = {} graph['parameters'][param.mIdentifier]['identifier'] = param.mIdentifier graph['parameters'][param.mIdentifier]['defaultvalue'] = param.mDefaultValue.getValue() content.append(graph) return content
def create_mat(aDestFileAbsPath, basec_in, rough_in, normal_in, height_in, ao_in): aContext = context.Context() #aContext.getUrlAliasMgr().setAliasAbsPath(aAliasName = 'myAlias', aAbsPath = 'myAliasAbsolutePath') print("create_mat:", aDestFileAbsPath, basec_in, rough_in, normal_in, height_in) startPos = [48, 48, 0] xOffset = [192, 0, 0] yOffset = [0, 192, 0] try: # Create a new SBSDocument from scratch, with a graph named 'Material' sbsDoc = sbsgenerator.createSBSDocument(aContext, aFileAbsPath=aDestFileAbsPath, aGraphIdentifier='Material') # Get the graph 'Material' aGraph = sbsDoc.getSBSGraph(aGraphIdentifier='Material') if basec_in is not None: #basecImg = sbsDoc.createLinkedResource(aResourcePath = basec_in, # aResourceTypeEnum = sbsenum.ResourceTypeEnum.BITMAP) #print(basecImg.getPkgResourcePath()) baseColor = aGraph.createBitmapNode( aSBSDocument=sbsDoc, #aResourcePath = basecImg.getPkgResourcePath(), aResourcePath=basec_in, aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.COLOR }, aGUIPos=startPos) outBaseColor = aGraph.createOutputNode( aIdentifier='BaseColor', aGUIPos=baseColor.getOffsetPosition(xOffset), aUsages={ sbsenum.UsageEnum.BASECOLOR: sbsenum.ComponentsEnum.RGBA }) aGraph.connectNodes(aLeftNode=baseColor, aRightNode=outBaseColor) startPos = baseColor.getOffsetPosition(yOffset) if rough_in is not None: #roughImg = sbsDoc.createLinkedResource(aResourcePath = rough_in, # aResourceTypeEnum = sbsenum.ResourceTypeEnum.BITMAP) roughness = aGraph.createBitmapNode( aSBSDocument=sbsDoc, #aResourcePath = roughImg.getPkgResourcePath(), aResourcePath=rough_in, aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.GRAYSCALE }, aGUIPos=startPos) outRoughness = aGraph.createOutputNode( aIdentifier='Roughness', aGUIPos=roughness.getOffsetPosition(xOffset), aUsages={ sbsenum.UsageEnum.ROUGHNESS: sbsenum.ComponentsEnum.RGBA }) aGraph.connectNodes(aLeftNode=roughness, aRightNode=outRoughness) startPos = roughness.getOffsetPosition(yOffset) metallic = aGraph.createCompFilterNode( aFilter=sbsenum.FilterEnum.UNIFORM, aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.GRAYSCALE, sbsenum.CompNodeParamEnum.OUTPUT_COLOR: 0.0 }, aGUIPos=startPos) outMetallic = aGraph.createOutputNode( aIdentifier='Metallic', aGUIPos=metallic.getOffsetPosition(xOffset), aUsages={sbsenum.UsageEnum.METALLIC: sbsenum.ComponentsEnum.RGBA}) aGraph.connectNodes(aLeftNode=metallic, aRightNode=outMetallic) startPos = metallic.getOffsetPosition(yOffset) if normal_in is not None: #normalImg = sbsDoc.createLinkedResource(aResourcePath = normal_in, # aResourceTypeEnum = sbsenum.ResourceTypeEnum.BITMAP) normals = aGraph.createBitmapNode( aSBSDocument=sbsDoc, #aResourcePath = normalImg.getPkgResourcePath(), aResourcePath=normal_in, aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.COLOR }, aGUIPos=startPos) outNormal = aGraph.createOutputNode( aIdentifier='Normal', aGUIPos=normals.getOffsetPosition(xOffset), aUsages={ sbsenum.UsageEnum.NORMAL: sbsenum.ComponentsEnum.RGBA }) aGraph.connectNodes(aLeftNode=normals, aRightNode=outNormal) startPos = normals.getOffsetPosition(yOffset) if height_in is not None: height = aGraph.createBitmapNode( aSBSDocument=sbsDoc, aResourcePath=height_in, aGUIPos=startPos, aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.GRAYSCALE }) outHeight = aGraph.createOutputNode( aIdentifier='Height', aGUIPos=height.getOffsetPosition(xOffset), aUsages={ sbsenum.UsageEnum.HEIGHT: sbsenum.ComponentsEnum.RGBA }) aGraph.connectNodes(aLeftNode=height, aRightNode=outHeight) startPos = height.getOffsetPosition(yOffset) if ao_in is not None: ao = aGraph.createBitmapNode( aSBSDocument=sbsDoc, aResourcePath=ao_in, aGUIPos=startPos, aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.GRAYSCALE }) outAO = aGraph.createOutputNode( aIdentifier='AO', aGUIPos=ao.getOffsetPosition(xOffset), aUsages={ sbsenum.UsageEnum.AMBIENT_OCCLUSION: sbsenum.ComponentsEnum.RGBA }) aGraph.connectNodes(aLeftNode=ao, aRightNode=outAO) # Write back the document structure into the destination .sbs file sbsDoc.writeDoc() log.info("=> Resulting substance saved at %s", aDestFileAbsPath) return True except BaseException as error: log.error("!!! Failed to create the new package") raise error
import sys import xml.etree.ElementTree as et import logging as log import re import ntpath from pysbs import context, sbsenum, sbsgenerator import substance_wrapper as wsbs wsbs.tools_path = "C:\Program Files\Allegorithmic\Substance Automation Toolkit" # Init the context myContext = context.Context() def create_mat(aDestFileAbsPath, basec_in, rough_in, normal_in, height_in, ao_in): aContext = context.Context() #aContext.getUrlAliasMgr().setAliasAbsPath(aAliasName = 'myAlias', aAbsPath = 'myAliasAbsolutePath') print("create_mat:", aDestFileAbsPath, basec_in, rough_in, normal_in, height_in) startPos = [48, 48, 0] xOffset = [192, 0, 0] yOffset = [0, 192, 0] try: # Create a new SBSDocument from scratch, with a graph named 'Material' sbsDoc = sbsgenerator.createSBSDocument(aContext, aFileAbsPath=aDestFileAbsPath, aGraphIdentifier='Material')
def createMultiSwitchGraph(sbsname, graphname, foldername): # Create a list of all the files to include in the Switch ######################################################### filelist = [] choicevalues = {} fullsbsname = os.path.abspath('{}.sbs'.format(sbsname)) fullfoldername = os.path.abspath(foldername) fileindex = 1 for file in os.listdir(fullfoldername): if file.endswith(".png"): filelist.append(file) choicevalues[fileindex] = file fileindex += 1 # Define Base Graph offsets ########################### xOffset = [192, 0, 0] # Init the context ################## myContext = context.Context() # Substance Creation #################### # Create a new Substance with a graph called graphname sbsDoc = sbsgenerator.createSBSDocument(aContext=myContext, aFileAbsPath=fullsbsname, aGraphIdentifier=graphname) # Graph edition ############### # Get the graph created with the document usedGraph = sbsDoc.getSBSGraph(aGraphIdentifier=graphname) # Define its parameter aParam = usedGraph.addInputParameter( aIdentifier='_picChoice', aWidget=sbsenum.WidgetEnum.DROPDOWN_INT1, aLabel='Picture Choice') aParam.setDropDownList(aValueMap=choicevalues) aParam.setDefaultValue(1) # - Instance of Multi-Switch from the default package library multiSwitchNode = usedGraph.createCompInstanceNodeFromPath( aSBSDocument=sbsDoc, aPath='sbs://blend_switch.sbs/multi_switch_grayscale', aParameters={ 'input_number': len(filelist), 'input_selection': 1 }) # Link its paramater to the one exposed in the Graph aDynFunction = multiSwitchNode.setDynamicParameter('input_selection') aDynFunction.setToInputParam(aParentGraph=usedGraph, aInputParamIdentifier='_picChoice') # - Output node with the usage baseColor outputNode = usedGraph.createOutputNode( aIdentifier='DemoOutput', aGUIPos=multiSwitchNode.getOffsetPosition(xOffset), aOutputFormat=sbsenum.TextureFormatEnum.DEFAULT_FORMAT, aUsages={sbsenum.UsageEnum.BASECOLOR: sbsenum.ComponentsEnum.RGBA}) usedGraph.connectNodes(aLeftNode=multiSwitchNode, aRightNode=outputNode) # Start at first row row = 0 # Create a row for each file, connect to the switch for picture in filelist: # Resource creation picRes = sbsDoc.createLinkedResource( aResourcePath=os.path.join(fullfoldername, picture), aResourceTypeEnum=sbsenum.ResourceTypeEnum.BITMAP) # - Bitmap node that uses the previously created resource bitmapNode = usedGraph.createBitmapNode( aSBSDocument=sbsDoc, aGUIPos=multiSwitchNode.getOffsetPosition([-192 * 4, row * 128, 0]), aResourcePath=picRes.getPkgResourcePath(), aParameters={ sbsenum.CompNodeParamEnum.COLOR_MODE: sbsenum.ColorModeEnum.COLOR }) # - Instance of a Greyscale conversion to transform Bitmap to greyscale grayNode = usedGraph.createCompFilterNode( aFilter=sbsenum.FilterEnum.GRAYSCALECONVERSION, aGUIPos=bitmapNode.getOffsetPosition(xOffset), aParameters={sbsenum.CompNodeParamEnum.FLATTEN_ALPHA: 1}) # Connect the nodes between them usedGraph.connectNodes(aLeftNode=bitmapNode, aRightNode=grayNode) usedGraph.connectNodes(aLeftNode=grayNode, aRightNode=multiSwitchNode, aRightNodeInput="input_{}".format(row + 1)) # Pass to the following row of image row += 1 # Write the document sbsDoc.writeDoc()