model/blend: material inputs traced through reroutes, mix, colour adjustments, colour ramps and node groups; unresolved links flagged with neutral values; roughness / metallic image textures with their channel; vertex colours; bump; UDIM images with the <UDIM> token and their tiles
This commit is contained in:
1 parent
8fd5a4c3f2
commit
3a56a66ac1
13 files changed
+1781
-531
No files matched your search
@@ -16,6 +16,10 @@
|
||||
# track constraints; bone inheritance and b-bone settings; a saved pose basis; drivers
|
||||
# (every variable type, keys, modifiers) on the rig, its data and the shape keys; custom
|
||||
# properties on pose bones, bones and objects
|
||||
# mat_<ver>.blend material graphs: images through Mix / MixRGB, Hue-Saturation, Bright-Contrast, Gamma,
|
||||
# Invert, RGB Curves, Color Ramp, reroutes and node groups; roughness images linked directly
|
||||
# or through a ramp; vertex colours; bump; unresolved links; a UDIM image with two tiles
|
||||
# (2.82+, the tiles as udim.1001.png / udim.1002.png next to the file)
|
||||
import bpy, sys, os, math
|
||||
|
||||
out = sys.argv[sys.argv.index("--") + 1]
|
||||
@@ -824,9 +828,169 @@ def ctl_file():
|
||||
save(os.path.join(out, "ctl_%s.blend" % TAG))
|
||||
|
||||
|
||||
def mat_file():
|
||||
reset()
|
||||
old_mix = V < (3, 4)
|
||||
diffuse = image("diffuse", 4, 4, lambda x, y: [x / 3, y / 3, 0.5, 1.0])
|
||||
rough = image("rough", 4, 4, lambda x, y: [0.25 + x / 8] * 3 + [1.0])
|
||||
bump = image("height", 4, 4, lambda x, y: [(x + y) / 6] * 3 + [1.0])
|
||||
nrm = image("nrm", 4, 4, lambda x, y: [0.5, 0.5, 1.0, 1.0])
|
||||
udim = None
|
||||
if V >= (2, 82):
|
||||
paths = []
|
||||
for tile, col in ((1001, (1.0, 0.0, 0.0)), (1002, (0.0, 0.0, 1.0))):
|
||||
p = os.path.join(out, "udim.%d.png" % tile)
|
||||
t = bpy.data.images.new("udim_%d" % tile, 2, 2)
|
||||
t.pixels = list(col + (1.0,)) * 4
|
||||
t.filepath_raw = p
|
||||
t.file_format = "PNG"
|
||||
t.save()
|
||||
bpy.data.images.remove(t)
|
||||
paths.append(p)
|
||||
udim = bpy.data.images.load(paths[0])
|
||||
udim.name = "udim"
|
||||
udim.source = "TILED"
|
||||
if len(udim.tiles) < 2:
|
||||
udim.tiles.new(1002)
|
||||
udim.tiles[0].label = "front"
|
||||
udim.filepath = "//" + os.path.basename(udim.filepath.replace("1001", "<UDIM>" if V >= (3, 0) else "1001"))
|
||||
|
||||
def material(name):
|
||||
m = bpy.data.materials.new(name)
|
||||
bsdf = principled(m, (0.8, 0.8, 0.8, 1.0), 0.0, 0.5)
|
||||
return m, m.node_tree, bsdf
|
||||
|
||||
def tex(nt, img):
|
||||
t = nt.nodes.new("ShaderNodeTexImage")
|
||||
t.image = img
|
||||
return t
|
||||
|
||||
def mix(nt, a, b, fac):
|
||||
if old_mix:
|
||||
n = nt.nodes.new("ShaderNodeMixRGB")
|
||||
ia, ib, f, o = n.inputs["Color1"], n.inputs["Color2"], n.inputs["Fac"], n.outputs["Color"]
|
||||
else:
|
||||
n = nt.nodes.new("ShaderNodeMix")
|
||||
n.data_type = "RGBA"
|
||||
ia, ib, f, o = n.inputs[6], n.inputs[7], n.inputs[0], n.outputs[2]
|
||||
if a:
|
||||
nt.links.new(a, ia)
|
||||
if b:
|
||||
nt.links.new(b, ib)
|
||||
if fac:
|
||||
nt.links.new(fac, f)
|
||||
return o
|
||||
|
||||
def vcol(nt):
|
||||
if V >= (2, 81):
|
||||
n = nt.nodes.new("ShaderNodeVertexColor")
|
||||
n.layer_name = "Col"
|
||||
else:
|
||||
n = nt.nodes.new("ShaderNodeAttribute")
|
||||
n.attribute_name = "Col"
|
||||
return n.outputs["Color"]
|
||||
|
||||
def ramp(nt, src):
|
||||
n = nt.nodes.new("ShaderNodeValToRGB")
|
||||
nt.links.new(src, n.inputs["Fac"])
|
||||
return n.outputs["Color"]
|
||||
|
||||
def reroute(nt, src):
|
||||
n = nt.nodes.new("NodeReroute")
|
||||
nt.links.new(src, n.inputs[0])
|
||||
return n.outputs[0]
|
||||
|
||||
mats = []
|
||||
# body: Mix(A = image, B = HueSat(image), Fac = ramp(vertex colour)); roughness image linked directly
|
||||
m, nt, b = material("MatBody")
|
||||
img = udim or diffuse
|
||||
hs = nt.nodes.new("ShaderNodeHueSaturation")
|
||||
nt.links.new(tex(nt, img).outputs["Color"], hs.inputs["Color"])
|
||||
nt.links.new(mix(nt, tex(nt, img).outputs["Color"], hs.outputs["Color"], ramp(nt, vcol(nt))), b.inputs["Base Color"])
|
||||
nt.links.new(tex(nt, rough).outputs["Color"], b.inputs["Roughness"])
|
||||
mats.append(m)
|
||||
# hands: Mix(A = image, Fac = ramp(vertex colour)), B unlinked
|
||||
m, nt, b = material("MatHands")
|
||||
nt.links.new(mix(nt, tex(nt, diffuse).outputs["Color"], None, ramp(nt, vcol(nt))), b.inputs["Base Color"])
|
||||
nt.links.new(tex(nt, rough).outputs["Color"], b.inputs["Roughness"])
|
||||
mats.append(m)
|
||||
# hair: reroutes; roughness through a reroute and a ramp
|
||||
m, nt, b = material("MatHair")
|
||||
nt.links.new(reroute(nt, reroute(nt, tex(nt, diffuse).outputs["Color"])), b.inputs["Base Color"])
|
||||
nt.links.new(reroute(nt, ramp(nt, tex(nt, rough).outputs["Color"])), b.inputs["Roughness"])
|
||||
mats.append(m)
|
||||
# eyebrows: vertex colour
|
||||
m, nt, b = material("MatVcol")
|
||||
nt.links.new(vcol(nt), b.inputs["Base Color"])
|
||||
mats.append(m)
|
||||
# jeans: image, normal map, roughness image linked directly, metallic through Separate RGB (packed)
|
||||
m, nt, b = material("MatJeans")
|
||||
nt.links.new(tex(nt, diffuse).outputs["Color"], b.inputs["Base Color"])
|
||||
nm = nt.nodes.new("ShaderNodeNormalMap")
|
||||
nt.links.new(tex(nt, nrm).outputs["Color"], nm.inputs["Color"])
|
||||
nt.links.new(nm.outputs["Normal"], b.inputs["Normal"])
|
||||
nt.links.new(tex(nt, rough).outputs["Color"], b.inputs["Roughness"])
|
||||
mats.append(m)
|
||||
# a chain of colour adjustments
|
||||
m, nt, b = material("MatChain")
|
||||
src = tex(nt, diffuse).outputs["Color"]
|
||||
for idname in ("ShaderNodeRGBCurve", "ShaderNodeInvert", "ShaderNodeGamma", "ShaderNodeBrightContrast"):
|
||||
n = nt.nodes.new(idname)
|
||||
nt.links.new(src, n.inputs["Color"])
|
||||
src = n.outputs["Color"]
|
||||
nt.links.new(src, b.inputs["Base Color"])
|
||||
mats.append(m)
|
||||
# a node group: an image inside it, and a pass-through of an outside image
|
||||
grp = bpy.data.node_groups.new("TexGroup", "ShaderNodeTree")
|
||||
gi, go = grp.nodes.new("NodeGroupInput"), grp.nodes.new("NodeGroupOutput")
|
||||
if hasattr(grp, "interface"):
|
||||
grp.interface.new_socket("Tint", in_out="INPUT", socket_type="NodeSocketColor")
|
||||
grp.interface.new_socket("Color", in_out="OUTPUT", socket_type="NodeSocketColor")
|
||||
grp.interface.new_socket("Through", in_out="OUTPUT", socket_type="NodeSocketColor")
|
||||
else:
|
||||
grp.inputs.new("NodeSocketColor", "Tint")
|
||||
grp.outputs.new("NodeSocketColor", "Color")
|
||||
grp.outputs.new("NodeSocketColor", "Through")
|
||||
inner = grp.nodes.new("ShaderNodeTexImage")
|
||||
inner.image = rough
|
||||
grp.links.new(inner.outputs["Color"], go.inputs[0])
|
||||
grp.links.new(gi.outputs[0], go.inputs[1])
|
||||
m, nt, b = material("MatGroup")
|
||||
gn = nt.nodes.new("ShaderNodeGroup")
|
||||
gn.node_tree = grp
|
||||
nt.links.new(tex(nt, diffuse).outputs["Color"], gn.inputs[0])
|
||||
nt.links.new(gn.outputs[1], b.inputs["Base Color"])
|
||||
nt.links.new(gn.outputs[0], b.inputs["Roughness"])
|
||||
mats.append(m)
|
||||
# bump, and unresolved links (a noise texture, a math node)
|
||||
m, nt, b = material("MatBump")
|
||||
bp = nt.nodes.new("ShaderNodeBump")
|
||||
bp.inputs["Strength"].default_value = 0.5
|
||||
bp.inputs["Distance"].default_value = 0.2
|
||||
nt.links.new(tex(nt, bump).outputs["Color"], bp.inputs["Height"])
|
||||
nt.links.new(bp.outputs["Normal"], b.inputs["Normal"])
|
||||
nz = nt.nodes.new("ShaderNodeTexNoise")
|
||||
nt.links.new(nz.outputs["Color"], b.inputs["Base Color"])
|
||||
b.inputs["Base Color"].default_value = (0.0, 0.0, 0.0, 1.0)
|
||||
mt = nt.nodes.new("ShaderNodeMath")
|
||||
nt.links.new(mt.outputs[0], b.inputs["Metallic"])
|
||||
b.inputs["Metallic"].default_value = 0.9
|
||||
mats.append(m)
|
||||
for i, m in enumerate(mats):
|
||||
me = mesh("Quad" + m.name, [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)], [(0, 1, 2, 3)])
|
||||
uv(me, "UVMap", lambda k: ((k % 2) * 1.0, (k // 2) * 1.0))
|
||||
byte_colors(me, "Col")
|
||||
me.materials.append(m)
|
||||
ob = link(bpy.data.objects.new("Quad" + m.name, me))
|
||||
ob.location = (i * 1.5, 0, 0)
|
||||
update()
|
||||
save(os.path.join(out, "mat_%s.blend" % TAG))
|
||||
|
||||
|
||||
scene_file()
|
||||
rig_file()
|
||||
ctl_file()
|
||||
mat_file()
|
||||
if OLD:
|
||||
scene_file()
|
||||
save(os.path.join(out, "compat_%s.blend" % TAG), compat=True)
|
||||
|
||||
Reference in new issue
Block a user