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
@@ -93,9 +93,115 @@ def linked_image(s):
|
||||
return "?" + n.bl_idname
|
||||
|
||||
|
||||
PASS = {"NodeReroute": ["Input"], "ShaderNodeMix": ["A_Color", "B_Color"], "ShaderNodeMixRGB": ["Color1", "Color2"],
|
||||
"ShaderNodeHueSaturation": ["Color"], "ShaderNodeBrightContrast": ["Color"], "ShaderNodeGamma": ["Color"],
|
||||
"ShaderNodeInvert": ["Color"], "ShaderNodeRGBCurve": ["Color"], "ShaderNodeValToRGB": ["Fac"]}
|
||||
|
||||
|
||||
def by_identifier(sockets, ident):
|
||||
for s in sockets:
|
||||
if s.identifier == ident:
|
||||
return s
|
||||
return None
|
||||
|
||||
|
||||
def trace(sock, via, stack, depth=0):
|
||||
"""bpy's view of the reader's rules: (end node, from-socket name, group stack) of the first image, else the last node"""
|
||||
if depth > 64 or not sock.is_linked:
|
||||
return None, None
|
||||
link = sock.links[0]
|
||||
n, out = link.from_node, link.from_socket
|
||||
if n.bl_idname == "ShaderNodeTexImage":
|
||||
return n, out.name
|
||||
if n.bl_idname == "ShaderNodeGroup" and n.node_tree:
|
||||
outs = [x for x in n.node_tree.nodes if x.bl_idname == "NodeGroupOutput"]
|
||||
if outs:
|
||||
inner = by_identifier(outs[0].inputs, out.identifier)
|
||||
if inner is not None:
|
||||
via.append(n.bl_idname)
|
||||
r, nm = trace(inner, via, stack + [n], depth + 1)
|
||||
if r is not None and r.bl_idname == "ShaderNodeTexImage":
|
||||
return r, nm
|
||||
via.pop()
|
||||
return n, out.name
|
||||
if n.bl_idname == "NodeGroupInput" and stack:
|
||||
g = stack[-1]
|
||||
outer = by_identifier(g.inputs, out.identifier)
|
||||
if outer is not None:
|
||||
return trace(outer, via, stack[:-1], depth + 1)
|
||||
return n, out.name
|
||||
if n.bl_idname in PASS:
|
||||
last = n
|
||||
for ident in PASS[n.bl_idname]:
|
||||
s = by_identifier(n.inputs, ident) or (n.inputs.get(ident) if hasattr(n.inputs, "get") else None)
|
||||
if s is None and n.bl_idname == "NodeReroute":
|
||||
s = n.inputs[0]
|
||||
if s is None:
|
||||
continue
|
||||
sub = []
|
||||
r, nm = trace(s, sub, stack, depth + 1)
|
||||
if r is not None and r.bl_idname == "ShaderNodeTexImage":
|
||||
via.append(n.bl_idname)
|
||||
via.extend(sub)
|
||||
return r, nm
|
||||
if n.bl_idname == "NodeReroute":
|
||||
return (r, nm) if r is not None else (n, out.name)
|
||||
return last, out.name
|
||||
return n, out.name
|
||||
|
||||
|
||||
def image_info(img):
|
||||
info = {"name": img.name, "source": img.source, "filepath": img.filepath}
|
||||
if img.source == "TILED":
|
||||
info["tiles"] = [{"number": t.number, "label": t.label} for t in img.tiles]
|
||||
return info
|
||||
|
||||
|
||||
def graph_info(m):
|
||||
out = {}
|
||||
if not m.node_tree:
|
||||
return out
|
||||
bsdf = None # the reader's choice: the Principled BSDF linked to an output's Surface, else the first
|
||||
for n in m.node_tree.nodes:
|
||||
if n.bl_idname == "ShaderNodeOutputMaterial" and n.inputs["Surface"].is_linked:
|
||||
f = n.inputs["Surface"].links[0].from_node
|
||||
if f.bl_idname == "ShaderNodeBsdfPrincipled":
|
||||
bsdf = f
|
||||
if bsdf is None:
|
||||
bsdf = next((n for n in m.node_tree.nodes if n.bl_idname == "ShaderNodeBsdfPrincipled"), None)
|
||||
if bsdf is None:
|
||||
return out
|
||||
for name in ("Base Color", "Metallic", "Roughness", "Normal", "Alpha", "Emission Color", "Emission"):
|
||||
s = bsdf.inputs.get(name)
|
||||
if s is None or not s.is_linked:
|
||||
continue
|
||||
sock = s
|
||||
e = {"linked": s.links[0].from_node.bl_idname}
|
||||
if name == "Normal" and e["linked"] in ("ShaderNodeNormalMap", "ShaderNodeBump"):
|
||||
nn = s.links[0].from_node
|
||||
sock = nn.inputs["Color"] if e["linked"] == "ShaderNodeNormalMap" else nn.inputs["Height"]
|
||||
if e["linked"] == "ShaderNodeBump":
|
||||
e["strength"] = r6(nn.inputs["Strength"].default_value)
|
||||
e["distance"] = r6(nn.inputs["Distance"].default_value)
|
||||
via = []
|
||||
n, outname = trace(sock, via, [])
|
||||
if n is not None and n.bl_idname == "ShaderNodeTexImage" and n.image:
|
||||
e["image"] = image_info(n.image)
|
||||
e["via"] = via
|
||||
e["output"] = outname
|
||||
elif n is not None:
|
||||
e["end"] = n.bl_idname
|
||||
if n.bl_idname == "ShaderNodeVertexColor":
|
||||
e["vertexColor"] = n.layer_name
|
||||
elif n.bl_idname == "ShaderNodeAttribute":
|
||||
e["vertexColor"] = n.attribute_name
|
||||
out[name] = e
|
||||
return out
|
||||
|
||||
|
||||
def material_info(m):
|
||||
info = {"name": m.name, "diffuse": [r6(x) for x in m.diffuse_color], "metallic": r6(m.metallic), "roughness": r6(m.roughness),
|
||||
"use_nodes": bool(m.use_nodes), "cull": bool(m.use_backface_culling)}
|
||||
"use_nodes": bool(m.use_nodes), "cull": bool(m.use_backface_culling), "graph": graph_info(m)}
|
||||
if m.node_tree:
|
||||
for n in m.node_tree.nodes:
|
||||
if n.bl_idname == "ShaderNodeBsdfPrincipled":
|
||||
|
||||
Reference in new issue
Block a user