MCPcopy Create free account
hub / github.com/AcademySoftwareFoundation/MaterialX / buildDocument

Function buildDocument

python/Scripts/creatematerial.py:132–213  ·  view source on GitHub ↗

Build a MaterialX document from the given textures and shading model.

(textureFiles, mtlxFile, shadingModel, colorspace, useTiledImage)

Source from the content-addressed store, hash-verified

130 return shaderInputs[idx]
131
132def buildDocument(textureFiles, mtlxFile, shadingModel, colorspace, useTiledImage):
133 '''
134 Build a MaterialX document from the given textures and shading model.
135 '''
136
137 # Find the default library nodedef, if any, for the requested shading model.
138 stdlib = mx.createDocument()
139 mx.loadLibraries(mx.getDefaultDataLibraryFolders(), mx.getDefaultDataSearchPath(), stdlib)
140 matchingNodeDefs = stdlib.getMatchingNodeDefs(shadingModel)
141 if not matchingNodeDefs:
142 print('Shading model', shadingModel, 'not found in the MaterialX data libraries')
143 return None
144 shadingModelNodeDef = matchingNodeDefs[0]
145 for nodeDef in matchingNodeDefs:
146 if nodeDef.getAttribute('isdefaultversion') == 'true':
147 shadingModelNodeDef = nodeDef
148
149 # Create content document.
150 doc = mx.createDocument()
151 materialName = mx.createValidName(mtlxFile.getBaseName().rsplit('.', 1)[0])
152 nodeGraph = doc.addNodeGraph('NG_' + materialName)
153 shaderNode = doc.addNode(shadingModel, 'SR_' + materialName, 'surfaceshader')
154 doc.addMaterialNode('M_' + materialName, shaderNode)
155
156 # Iterate over texture files.
157 imageNodeCategory = 'tiledimage' if useTiledImage else 'image'
158 udimNumbers = set()
159 for textureFile in textureFiles:
160 textureName = textureFile.getNameWithoutExtension()
161 shaderInput = findBestMatch(textureName, shadingModelNodeDef)
162
163 if not shaderInput:
164 print('Skipping', textureFile.getBaseName(), 'which does not match any', shadingModel, 'input')
165 continue
166
167 inputName = shaderInput.getName()
168 inputType = shaderInput.getType()
169
170 # Skip inputs that have already been created, e.g. in multi-UDIM materials.
171 if shaderNode.getInput(inputName) or nodeGraph.getChild(textureName):
172 continue
173
174 mtlInput = shaderNode.addInput(inputName)
175 textureName = nodeGraph.createValidChildName(textureName)
176 imageNode = nodeGraph.addNode(imageNodeCategory, textureName, inputType)
177
178 # Set color space.
179 if shaderInput.isColorType():
180 imageNode.setColorSpace(colorspace)
181
182 # Set file path.
183 filePathString = os.path.relpath(textureFile.asPattern(), mtlxFile.getParentPath().asString())
184 imageNode.setInputValue('file', filePathString, 'filename')
185
186 # Apply special cases for normal maps.
187 inputNode = imageNode
188 connNode = imageNode
189 inBetweenNodes = []

Callers 1

mainFunction · 0.85

Calls 15

printFunction · 0.85
findBestMatchFunction · 0.85
addMaterialNodeMethod · 0.80
getChildMethod · 0.80
addInputMethod · 0.80
setColorSpaceMethod · 0.80
asPatternMethod · 0.80
setInputValueMethod · 0.80
addOutputMethod · 0.80
setConnectedOutputMethod · 0.80
isUdimMethod · 0.80

Tested by

no test coverage detected