It will use an ior value if it is there in the json file
It mixes diffuse and transparent and then the resulting mix with gloss so that transparency doesn't remove gloss
It uses specular_color instead of diffuse_color as the color input to the gloss node
It uses opacity as the default mix factor for transparency
- Code: Select all
def buildMaterialCycles(mat, mhMat, scn, cfg):
print("Creating CYCLES material", mat.name)
mat.use_nodes= True
mat.node_tree.nodes.clear()
tree = NodeTree(mat.node_tree)
links = mat.node_tree.links
texco = tree.addNode(1, 'ShaderNodeTexCoord')
fresnel = tree.addNode(4, 'ShaderNodeFresnel')
ior = mhMat.get('ior', 1.45)
fresnel.inputs["IOR"].default_value = ior
mixTrans = tree.addNode(4, 'ShaderNodeMixShader')
glossy = tree.addNode(4, 'ShaderNodeBsdfGlossy')
glossy.inputs["Color"].default_value[0:3] = mhMat["specular_color"]
glossy.inputs["Roughness"].default_value = 1.0 - mhMat["shininess"]
glossyTex = tree.addTexImageNode(mhMat, texco, "specular_map_texture", cfg)
if glossyTex:
links.new(glossyTex.outputs['Color'], glossy.inputs['Color'])
mixGloss = tree.addNode(5, 'ShaderNodeMixShader')
links.new(fresnel.outputs['Fac'], mixGloss.inputs['Fac'])
links.new(glossy.outputs['BSDF'], mixGloss.inputs[2])
transparent = tree.addNode(3, 'ShaderNodeBsdfTransparent')
diffuse = tree.addNode(3, 'ShaderNodeBsdfDiffuse')
diffuse.inputs["Color"].default_value[0:3] = mhMat["diffuse_color"]
diffuse.inputs["Roughness"].default_value = 1.0 - mhMat["shininess"]
diffuseTex = tree.addTexImageNode(mhMat, texco, "diffuse_texture", cfg)
if diffuseTex:
links.new(diffuseTex.outputs['Color'], diffuse.inputs['Color'])
links.new(diffuseTex.outputs['Alpha'], mixTrans.inputs['Fac'])
links.new(transparent.outputs['BSDF'], mixTrans.inputs[1])
links.new(diffuse.outputs['BSDF'], mixTrans.inputs[2])
links.new(mixTrans.outputs['Shader'], mixGloss.inputs[1])
opacity = mhMat["opacity"]
if opacity < 1:
mixTrans.inputs["Fac"].default_value = opacity
normalTex = tree.addTexImageNode(mhMat, texco, "normal_map_texture", cfg)
if normalTex:
normalTex.color_space = 'NONE'
normalMap = tree.addNode(2, 'ShaderNodeNormalMap')
normalMap.space = 'TANGENT'
normalMap.uv_map = "UVMap"
links.new(normalTex.outputs['Color'], normalMap.inputs['Color'])
links.new(normalMap.outputs['Normal'], fresnel.inputs['Normal'])
links.new(normalMap.outputs['Normal'], diffuse.inputs['Normal'])
links.new(normalMap.outputs['Normal'], glossy.inputs['Normal'])
else:
normalMap = None
output = tree.addNode(6, 'ShaderNodeOutputMaterial')
links.new(mixGloss.outputs['Shader'], output.inputs['Surface'])
The biggest issue with it is that invisible materials will have gloss unless the ior on the Fresnel node is set to 1, which makes sense, because invisible materials shouldn't refract light, pretty much by definition. Unfortunately the only way to set the ior is in Blender or by editing of the json/mhx2 file. I was hoping that I could just put it in the mhmat material file but doing so doesn't get it set in the exported file. I looked at the export code and it looks like it just writes out the internal data structures, so if the ior isn't read in by MakeHuman then it won't get exported.
- Code: Select all
def addMaterial(mhMaterials, mat, mname, texhandler):
mhMat = OrderedDict()
mhMaterials.append(mhMat)
mhMat["name"] = mname
mhMat["diffuse_color"] = mat.diffuseColor.asTuple()
mhMat["diffuse_map_intensity"] = 1.0
mhMat["specular_color"] = mat.specularColor.asTuple()
mhMat["specular_map_intensity"] = mat.specularMapIntensity
mhMat["shininess"] = mat.shininess
mhMat["opacity"] = mat.opacity
mhMat["translucency"] = mat.translucency
mhMat["emissive_color"] = mat.emissiveColor.asTuple()
mhMat["ambient_color"] = mat.ambientColor.asTuple()
mhMat["transparency_map_intensity"] = mat.transparencyMapIntensity
mhMat["shadeless"] = mat.shadeless
mhMat["wireframe"] = mat.wireframe
mhMat["transparent"] = mat.transparent
mhMat["alphaToCoverage"] = mat.alphaToCoverage
mhMat["backfaceCull"] = mat.backfaceCull
mhMat["depthless"] = mat.depthless
mhMat["castShadows"] = mat.castShadows
mhMat["receiveShadows"] = mat.receiveShadows
mhMat["sssEnabled"] = mat.sssEnabled
mhMat["sssRScale"] = mat.sssRScale
mhMat["sssGScale"] = mat.sssBScale
mhMat["sssBScale"] = mat.sssBScale
texhandler.addTexture(mhMat, "diffuse_texture", mat.diffuseTexture)
texhandler.addTexture(mhMat, "specular_map_texture", mat.specularMapTexture)
texhandler.addTexture(mhMat, "normal_map_texture", mat.normalMapTexture)
texhandler.addTexture(mhMat, "transparency_map_texture", mat.transparencyMapTexture)
texhandler.addTexture(mhMat, "bump_map_texture", mat.bumpMapTexture)
texhandler.addTexture(mhMat, "displacement_map_texture", mat.displacementMapTexture)
texhandler.addTexture(mhMat, "ao_map_texture", mat.aoMapTexture)