Skip to content

Commit

Permalink
sugiany#49 Expose MMDShaderDev Color and Alpha sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
UuuNyaa committed Jun 6, 2022
1 parent 1333f2e commit afacd99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions mmd_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def load_handler(_dummy):
FnSDEF.clear_cache()
FnSDEF.register_driver_function()

from mmd_tools.core.material import MigrationFnMaterial
MigrationFnMaterial.update_mmd_shader()

def register():
auto_load.register()
properties.register()
Expand Down
26 changes: 24 additions & 2 deletions mmd_tools/core/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import logging
import os
from typing import Optional

import bpy
from mmd_tools.bpyutils import addon_preferences, select_object
from mmd_tools.bpyutils import addon_preferences
from mmd_tools.core.exceptions import MaterialNotFoundError
from mmd_tools.core.shader import _NodeGroupUtils

Expand Down Expand Up @@ -765,7 +766,7 @@ class _Dummy: default_value, is_linked = None, True
node_uv.location = node_shader.location + Vector((-5*210, -2.5*220))
node_uv.node_tree = self.__get_shader_uv()

if not node_shader.outputs['Shader'].is_linked:
if not (node_shader.outputs['Shader'].is_linked or node_shader.outputs['Color'].is_linked or node_shader.outputs['Alpha'].is_linked):
node_output = next((n for n in nodes if isinstance(n, bpy.types.ShaderNodeOutputMaterial) and n.is_active_output), None)
if node_output is None:
node_output = nodes.new('ShaderNodeOutputMaterial')
Expand Down Expand Up @@ -915,10 +916,31 @@ def __get_shader(self):
links.new(node_input.outputs['Sphere Tex'], node_spa.inputs['Color2'])

ng.new_output_socket('Shader', shader_alpha_mix.outputs['Shader'])
ng.new_output_socket('Color', node_sphere.outputs['Color'])
ng.new_output_socket('Alpha', node_alpha.outputs['Value'])

return shader

FnMaterial = _FnMaterialCycles
if bpy.app.version < (2, 80, 0):
FnMaterial = _FnMaterialBI

class MigrationFnMaterial:
@staticmethod
def update_mmd_shader():
mmd_shader_node_tree: Optional[bpy.types.NodeTree] = bpy.data.node_groups.get('MMDShaderDev')
if mmd_shader_node_tree is None:
return

if 'Color' in mmd_shader_node_tree.outputs:
return

ng = _NodeGroupUtils(mmd_shader_node_tree)
shader_diffuse: bpy.types.ShaderNodeBsdfDiffuse = [n for n in mmd_shader_node_tree.nodes if n.type == 'BSDF_DIFFUSE'][0]
node_sphere: bpy.types.ShaderNodeMixRGB = shader_diffuse.inputs['Color'].links[0].from_node
node_output: bpy.types.NodeGroupOutput = ng.node_output
shader_alpha_mix: bpy.types.ShaderNodeMixShader = node_output.inputs['Shader'].links[0].from_node
node_alpha: bpy.types.ShaderNodeMath = shader_alpha_mix.inputs['Fac'].links[0].from_node

ng.new_output_socket('Color', node_sphere.outputs['Color'])
ng.new_output_socket('Alpha', node_alpha.outputs['Value'])

0 comments on commit afacd99

Please sign in to comment.