Import skingroups as separate materials with skins as textures

This commit is contained in:
khreathor 2019-01-08 03:55:13 +01:00 committed by Bill Currie
parent ae86790e6c
commit 66fe862a87

View file

@ -98,6 +98,16 @@ def load_skins(mdl):
else: else:
load_skin(skin, "%s_%d" % (mdl.name, i)) load_skin(skin, "%s_%d" % (mdl.name, i))
def setup_main_material(mdl):
mat = bpy.data.materials.new(mdl.name)
mat.blend_method = 'OPAQUE'
mat.diffuse_color = (1, 1, 1)
mat.metallic = 1
mat.roughness = 1
mat.specular_intensity = 0
mat.use_nodes = True
return mat
def setup_skins(mdl, uvs): def setup_skins(mdl, uvs):
load_skins(mdl) load_skins(mdl)
# img = mdl.images[0] # use the first skin for now # img = mdl.images[0] # use the first skin for now
@ -112,34 +122,38 @@ def setup_skins(mdl, uvs):
for j,k in enumerate(poly.loop_indices): for j,k in enumerate(poly.loop_indices):
uvloop.data[k].uv = mdl_uv[j] uvloop.data[k].uv = mdl_uv[j]
# Create main material #Load all skins
mat = bpy.data.materials.new(mdl.name) for i, skin in enumerate(mdl.skins):
mat.blend_method = 'OPAQUE' if skin.type:
mat.diffuse_color = (1,1,1) mat = setup_main_material(mdl)
mat.metallic = 1 emissionNode = mat.node_tree.nodes.new("ShaderNodeEmission")
mat.roughness = 1 shaderOut = mat.node_tree.nodes["Material Output"]
mat.specular_intensity = 0 mat.node_tree.nodes.remove(mat.node_tree.nodes["Principled BSDF"])
mat.use_nodes = True
for j, subskin in enumerate(skin.skins):
tex_node = mat.node_tree.nodes.new("ShaderNodeTexImage")
tex_node.image = mdl.images[i + j]
tex_node.interpolation = "Closest"
if j == 0:
# connect only first texture (we'll need something smarter in the future)
mat.node_tree.links.new(tex_node.outputs[0], emissionNode.inputs[0])
mat.node_tree.links.new(emissionNode.outputs[0], shaderOut.inputs[0])
mdl.mesh.materials.append(mat)
else:
mat = setup_main_material(mdl)
# TODO: turn transform to True and position it properly in editor # TODO: turn transform to True and position it properly in editor
emissionNode = mat.node_tree.nodes.new("ShaderNodeEmission") emissionNode = mat.node_tree.nodes.new("ShaderNodeEmission")
shaderOut = mat.node_tree.nodes["Material Output"] shaderOut = mat.node_tree.nodes["Material Output"]
bdsf_node = mat.node_tree.nodes["Principled BSDF"] mat.node_tree.nodes.remove(mat.node_tree.nodes["Principled BSDF"])
mat.node_tree.nodes.remove(bdsf_node)
#Add skingroup
#bpy.ops.object.material_slot_add()
#bpy.ops.material.new()
#Add all existing textures to shader node
for i in range(len(mdl.images)):
tex_node = mat.node_tree.nodes.new("ShaderNodeTexImage") tex_node = mat.node_tree.nodes.new("ShaderNodeTexImage")
tex_node.image = mdl.images[i] tex_node.image = mdl.images[i]
tex_node.interpolation = "Closest" tex_node.interpolation = "Closest"
if i == 0:
# connect only first texture (we'll need something smarter in the future)
mat.node_tree.links.new(tex_node.outputs[0], emissionNode.inputs[0])
mat.node_tree.links.new(tex_node.outputs[0], emissionNode.inputs[0])
mat.node_tree.links.new(emissionNode.outputs[0], shaderOut.inputs[0]) mat.node_tree.links.new(emissionNode.outputs[0], shaderOut.inputs[0])
mdl.mesh.materials.append(mat) mdl.mesh.materials.append(mat)