Skip to content

Commit

Permalink
feat: mtoon texture link helper
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed Sep 15, 2024
1 parent 8042268 commit bf50d0a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
Binary file modified src/io_scene_vrm/common/mtoon1.blend
Binary file not shown.
97 changes: 51 additions & 46 deletions src/io_scene_vrm/editor/mtoon1/property_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,32 +574,66 @@ def link_nodes(

select_in_node = node_socket_target.create_node_selector(material)
in_socket_name = node_socket_target.get_in_socket_name()
if any(
1
for link in material.node_tree.links
if isinstance(link.from_node, out_node_type)
and link.from_node.name == out_node_name
and link.from_socket
and link.from_socket.name == out_node_socket_name
and select_in_node(link.to_node)
and link.to_socket
and link.to_socket.name == in_socket_name
):
return

disconnecting_link = next(
# 既につながっている場合は何もしない
connection_check_node = next(
(
link
link.from_node
for link in material.node_tree.links
if select_in_node(link.to_node)
and link.to_socket
and link.to_socket.name == in_socket_name
),
None,
)
if disconnecting_link:
material.node_tree.links.remove(disconnecting_link)
while connection_check_node:
if (
isinstance(connection_check_node, out_node_type)
and connection_check_node.name == out_node_name
):
return
if isinstance(connection_check_node, NodeReroute):
connection_check_inputs = connection_check_node.inputs
if len(connection_check_inputs) == 0:
break
connection_check_links = connection_check_inputs[0].links
if len(connection_check_links) == 0:
break
connection_check_link = connection_check_links[0]
connection_check_node = connection_check_link.from_node
continue
break

# 関係ないノードとつながっている場合はリンクを切断
cls.unlink_nodes(material, node_socket_target)

# 出力ノードとソケットを探す
out_node = material.node_tree.nodes.get(out_node_name)
if not isinstance(out_node, out_node_type):
logger.error("No output node: %s", out_node_name)
return
out_socket = out_node.outputs.get(out_node_socket_name)
if not out_socket:
logger.error("No output node socket: %s", out_node_socket_name)
return
traversing_sockets = [out_socket]
while traversing_sockets:
traversing_socket = traversing_sockets.pop()
for connection_check_link in traversing_socket.links:
connection_check_node = connection_check_link.to_node
if not isinstance(connection_check_node, NodeReroute):
continue
if connection_check_node.label != in_socket_name:
traversing_sockets.extend(connection_check_node.outputs)
continue
traversing_outputs = connection_check_node.outputs
if len(traversing_outputs) == 0:
continue
out_socket = traversing_outputs[0]
out_node = connection_check_node
break

# 入力ノードとソケットを探す
in_node = next(
(n for n in material.node_tree.nodes if select_in_node(n)),
None,
Expand All @@ -613,25 +647,12 @@ def link_nodes(
logger.error("No input socket: %s", in_socket_name)
return

out_node = material.node_tree.nodes.get(out_node_name)
if not isinstance(out_node, out_node_type):
logger.error("No output node: %s", out_node_name)
return

out_socket = out_node.outputs.get(out_node_socket_name)
if not out_socket:
logger.error("No output node socket: %s", out_node_socket_name)
return

material.node_tree.links.new(in_socket, out_socket)

@classmethod
def unlink_nodes(
cls,
material: Material,
out_node_name: str,
out_node_type: type[Node],
out_node_socket_name: str,
node_socket_target: NodeSocketTarget,
) -> None:
while True:
Expand All @@ -645,11 +666,7 @@ def unlink_nodes(
(
link
for link in material.node_tree.links
if isinstance(link.from_node, out_node_type)
and link.from_node.name == out_node_name
and link.from_socket
and link.from_socket.name == out_node_socket_name
and select_in_node(link.to_node)
if select_in_node(link.to_node)
and link.to_socket
and link.to_socket.name == in_socket_name
),
Expand Down Expand Up @@ -682,9 +699,6 @@ def link_or_unlink_nodes(
else:
cls.unlink_nodes(
material,
out_node_name,
out_node_type,
out_node_socket_name,
node_socket_target,
)

Expand Down Expand Up @@ -3202,9 +3216,6 @@ def update_alpha_nodes(material: Material, alpha_mode_value: int) -> None:
if alpha_mode_value != mtoon1.ALPHA_MODE_MASK.value:
TextureTraceablePropertyGroup.unlink_nodes(
material,
tex_image_node_name,
ShaderNodeTexImage,
TEX_IMAGE_ALPHA_OUTPUT_KEY,
StaticNodeSocketTarget(
in_node_name=ALPHA_CLIP_INPUT_NODE_NAME,
in_node_type=ShaderNodeMath,
Expand All @@ -3213,9 +3224,6 @@ def update_alpha_nodes(material: Material, alpha_mode_value: int) -> None:
)
TextureTraceablePropertyGroup.unlink_nodes(
material,
ALPHA_CLIP_OUTPUT_NODE_NAME,
ShaderNodeMath,
ALPHA_CLIP_OUTPUT_NODE_SOCKET_NAME,
PrincipledBsdfNodeSocketTarget(
in_socket_name=PRINCIPLED_BSDF_ALPHA_INPUT_KEY
),
Expand All @@ -3226,9 +3234,6 @@ def update_alpha_nodes(material: Material, alpha_mode_value: int) -> None:
):
TextureTraceablePropertyGroup.unlink_nodes(
material,
tex_image_node_name,
ShaderNodeTexImage,
TEX_IMAGE_ALPHA_OUTPUT_KEY,
PrincipledBsdfNodeSocketTarget(
in_socket_name=PRINCIPLED_BSDF_ALPHA_INPUT_KEY
),
Expand Down

0 comments on commit bf50d0a

Please sign in to comment.