45 lines
2.1 KiB
Python
45 lines
2.1 KiB
Python
"""Carry v022 equipment PBR and authored CAD viewport colors into editable groups."""
|
|
import hashlib
|
|
import re
|
|
import bpy
|
|
from materials import specs
|
|
|
|
PRINTED_MATERIALS={"V2 · base", "V2 · holder", "V2 · cap", "Graphite"}
|
|
DARK_PLASTIC_MATERIALS={"R01 | X4 габаритный макет"}
|
|
|
|
|
|
def equipment_material(source):
|
|
name = re.sub(r'\.\d{3}$', '', source.name)
|
|
key = 'equipment-' + hashlib.sha256(name.encode()).hexdigest()[:12]
|
|
node = source.node_tree.nodes.get('Principled BSDF') if source.use_nodes else None
|
|
color = list(node.inputs['Base Color'].default_value) if node else list(source.diffuse_color)
|
|
# Several manufacturing parts only set diffuse_color; their untouched node
|
|
# still has Blender's default 0.8 gray. Do not export those parts as white.
|
|
viewport = list(source.diffuse_color)
|
|
fallback = node is not None and all(abs(v - .8) < 1e-6 for v in color[:3]) and any(abs(v - .8) > 1e-6 for v in viewport[:3])
|
|
if fallback:
|
|
color = viewport
|
|
metal = float(source.metallic if fallback or node is None else node.inputs['Metallic'].default_value)
|
|
rough = float(source.roughness if fallback or node is None else node.inputs['Roughness'].default_value)
|
|
color_source = 'viewport' if fallback else 'shader'
|
|
# Owner's physical finish: printed CAM/K1 parts match the frame; X4 is dark plastic.
|
|
finish = 'body' if name in PRINTED_MATERIALS else 'polymer' if name in DARK_PLASTIC_MATERIALS else None
|
|
if finish:
|
|
_, rgb, metal, rough = specs[finish]
|
|
color = [*rgb, 1]
|
|
color_source = 'owner/' + finish
|
|
material = bpy.data.materials.new('rover/' + key)
|
|
material.use_nodes = True
|
|
bs = material.node_tree.nodes.get('Principled BSDF')
|
|
bs.inputs['Base Color'].default_value = color
|
|
bs.inputs['Metallic'].default_value = metal
|
|
bs.inputs['Roughness'].default_value = rough
|
|
return key, (name, tuple(color[:3]), metal, rough), material, color_source
|
|
|
|
|
|
def srgb_hex(color):
|
|
def channel(c):
|
|
v = 12.92 * c if c <= .0031308 else 1.055 * c ** (1 / 2.4) - .055
|
|
return round(max(0, min(1, v)) * 255)
|
|
return '#' + ''.join(f'{channel(c):02x}' for c in color)
|