Created
March 20, 2023 17:38
-
-
Save wakarase/e02f539108981da52367016f993da6fa 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-2.blend ガラスのくすみ・表面劣化 | |
import bpy | |
NOT_MODIFIED = False | |
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 = -700 | |
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]) | |
texture_coordinate = create('TexCoord', (-938, 7)) | |
musgrave_texture = create('TexMusgrave', (-749, 146), Scale=2, Detail=15, Dimension=0, Lacunarity=1.4) | |
link(Object=texture_coordinate, Vector=musgrave_texture) | |
if NOT_MODIFIED: | |
add = create('Math', (-553, 155)) | |
link(Fac=musgrave_texture, Value=add) | |
else: | |
map_range_0 = create('MapRange', (-553, 155), From_Min=-2.5, From_Max=-2.0) | |
link(Fac=musgrave_texture, Value=map_range_0) | |
musgrave_texture = create('TexMusgrave', (-556, -28), Scale=2, Detail=15, Dimension=0, Lacunarity=1.5) | |
link(Object=texture_coordinate, Vector=musgrave_texture) | |
# 本来のガラスの色(0xF0F0F0)に対して、より暗いくすみ(0xBCBCBC)を加えます。 | |
# 色を0xFFFFFFにすると「不自然なまでに透明」になるため0.875以下が推奨されます。 | |
natural_glass_color = (0.875, 0.875, 0.875, 1.000) | |
if NOT_MODIFIED: | |
mix = create('Mix', (-368, 284), _6=natural_glass_color, _7=(0.50, 0.50, 0.50, 1.00)) | |
mix.data_type = 'RGBA' | |
link(Value=add, Factor=mix) | |
else: | |
mix = create('Mix', (-368, 284), _6=natural_glass_color, _7=(0.35, 0.35, 0.35, 1.00)) | |
mix.data_type = 'RGBA' | |
link(Result=map_range_0, Factor=mix) | |
if NOT_MODIFIED: | |
color_dudge = create('Mix', (-191, 262), _0=0.2) | |
else: | |
color_dudge = create('Mix', (-191, 262), _0=0.4) | |
color_dudge.data_type = 'RGBA' | |
color_dudge.blend_type = 'DODGE' | |
link(_2=mix, _6=color_dudge) | |
link(Fac=musgrave_texture, _7=color_dudge) | |
# 伝搬1のPrincipled BSDFです。メタリックは優先されるのでデフォルトの0である必要があります。 | |
principled_bsdf = create('BsdfPrincipled', (16, 307), Base_Color=natural_glass_color, Roughness=0,Transmission=1) | |
link(_2=color_dudge, Base_Color=principled_bsdf) | |
if NOT_MODIFIED: | |
map_range = create('MapRange', (-188, 49), To_Min=0.10, To_Max=0.30) | |
link(Value=add, Value_=map_range) | |
else: | |
map_range = create('MapRange', (-188, 49), To_Min=0.15, To_Max=0.60) | |
link(Result=map_range_0, Value=map_range) | |
link(Result=map_range, Roughness=principled_bsdf) | |
material_output = nodes['Material Output.003'] | |
link(BSDF=principled_bsdf, Surface=material_output) | |
print('Done.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment