-
-
Save wakarase/ef86184a90e7d7696dcdfa2cc3c287fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Blender 質感・マテリアル設定実践テクニック 3-03-1.blend | |
import bpy | |
object = bpy.data.objects['export3dcoat'] # シーンに配置済みのオブジェクト | |
slot = object.material_slots[0] # そのマテリアルスロット | |
material = slot.material # に設定されているマテリアル | |
node_tree = material.node_tree # のノードネットワーク | |
nodes = node_tree.nodes # にある各ノード | |
def delete(): | |
"""過去に生成したノードを削除します。""" | |
for node in nodes: | |
if node.label.endswith(' del'): | |
nodes.remove(node) | |
delete() | |
def show(): | |
"""既存のノードの情報を表示します。""" | |
for node in nodes: | |
s = 'node "{}" at {}' | |
s = s.format(node.name, list(node.location)) | |
print(s, type(node)) | |
show() | |
def format_key(key): | |
"""与えられた文字列を加工して必要なら整数にします。""" | |
if key.startswith('_'): # 先頭の下線で整数を表すことにします。 | |
return int(key[1:]) | |
else: | |
key = key.rstrip('_') # 末端の下線は無視することにします。 | |
# 中間の下線で空白を表すことにします。 | |
return key.replace('_', ' ') | |
def create(name, location, **args): | |
"""新しくノードを作ってlocationに配置します。""" | |
node = nodes.new('ShaderNode' + name) | |
node.label = node.name + ' del' # 自動生成したノードだというフラグです。 | |
y_offset = -600 | |
node.location = (location[0], location[1] + y_offset) | |
for key, value in args.items(): | |
node.inputs[format_key(key)].default_value = value | |
return node | |
def link(**args): | |
"""2つのノードを接続します。""" | |
(k_left, v_left), (k_right, v_right) = args.items() | |
k_left, k_right = format_key(k_left), format_key(k_right) | |
node_tree.links.new(v_right.inputs[k_right], v_left.outputs[k_left]) | |
# オブジェクトの原点からのローカル3次元座標空間を利用します。 | |
texture_coordinate = create('TexCoord', (-1031, -66)) | |
# 最も複雑なマスグレイブで解像度に不足のない最小のScaleとします。 | |
musgrave_texture = create('TexMusgrave', (-842, 73), Scale=2, Detail=15, Dimension=0, Lacunarity=1.4) | |
link(Object=texture_coordinate, Vector=musgrave_texture) | |
# マスグレイブの出力値の平均は0なので0.5足して分布を対称にします。 | |
# マスグレイブの分布より小さい[0, 1]範囲をクランプして使います。 | |
# [-0.5, 0.5]を[0, 1]に範囲マッピングするのと同じです。 | |
# add = create('Math', (-646, 82)) | |
# link(Fac=musgrave_texture, Value=add) | |
map_range_0 = create('MapRange', (-646, 82), From_Min=0.5, From_Max=1.5) | |
link(Fac=musgrave_texture, Value=map_range_0) | |
# 第2のマスグレイブは値をずらして別の模様を形成します。 | |
musgrave_texture = create('TexMusgrave', (-649, -101), Scale=2, Detail=15, Dimension=0, Lacunarity=1.5) | |
link(Object=texture_coordinate, Vector=musgrave_texture) | |
# よごれ部分のための混色ですが、今回は同じ色を割り当て、ただ出力します。 | |
# 覆い焼きのAに直接色を割り当てても同じです。 | |
albedo_iron = (0.56, 0.57, 0.58, 1.00) # 0xC5C7C8 | |
mix = create('Mix', (-462, 211), _6=albedo_iron, _7=albedo_iron) | |
mix.data_type = 'RGBA' | |
# link(Value=add, Factor=mix) | |
link(Result=map_range_0, Factor=mix) | |
# 覆い焼きにより、アルベドそのものに微細なムラを与えます。 | |
# MixからのAへの入力を抜いても同じです。 | |
color_dudge = create('Mix', (-284, 189), _0=0.15, _6=albedo_iron) | |
color_dudge.data_type = 'RGBA' | |
color_dudge.blend_type = 'DODGE' | |
link(_2=mix, _6=color_dudge) | |
link(Fac=musgrave_texture, _7=color_dudge) | |
principled_bsdf = nodes['Principled BSDF.001'] | |
link(_2=color_dudge, Base_Color=principled_bsdf) | |
# 第1マスグレイブのクランプ[-0.5, 0.5]を粗さとします。 | |
# ただし粗さ[0, 1]だと大げさなので粗さ[0.2, 0.8]に範囲マッピングします。 | |
map_range = create('MapRange', (-322, -39), To_Min=0.2, To_Max=0.3) | |
# link(Value=add, Value_=map_range) | |
link(Result=map_range_0, Value_=map_range) | |
link(Result=map_range, Roughness=principled_bsdf) | |
print('Done.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment