Skip to content

Commit

Permalink
Can import shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
UuuNyaa committed Nov 17, 2023
1 parent 8dbf1b9 commit 2cbe178
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
11 changes: 5 additions & 6 deletions mmd_tools/core/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def update_sphere_texture_type(self, obj=None):
if hasattr(texture, 'color_space'):
texture.color_space = 'NONE' if is_sph_add else 'COLOR'
elif hasattr(texture.image, 'colorspace_settings'):
texture.image.colorspace_settings.name = 'Linear' if is_sph_add else 'sRGB'
texture.image.colorspace_settings.name = 'Linear Rec.709' if is_sph_add else 'sRGB'

mat = self.material
nodes, links = mat.node_tree.nodes, mat.node_tree.links
Expand Down Expand Up @@ -725,11 +725,10 @@ def __update_shader_input(self, name, val):
self.__update_shader_nodes()
shader = mat.node_tree.nodes.get('mmd_shader', None)
if shader and name in shader.inputs:
if hasattr(shader, 'node_tree'):
input_socket = shader.node_tree.inputs[name]
if hasattr(input_socket, 'min_value'):
val = min(max(val, input_socket.min_value), input_socket.max_value)
shader.inputs[name].default_value = val
input_socket = shader.node_tree.interface.items_tree[name]
if hasattr(input_socket, 'min_value'):
val = min(max(val, input_socket.min_value), input_socket.max_value)
input_socket.default_value = val

def __update_shader_nodes(self):
mat = self.material
Expand Down
26 changes: 18 additions & 8 deletions mmd_tools/core/shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def new_mix_node(self, blend_type, pos, fac=None, color1=None, color2=None):
return node


SOCKET_TYPE_MAPPING = {
'NodeSocketFloatFactor': 'NodeSocketFloat'
}

SOCKET_SUBTYPE_MAPPING = {
'NodeSocketFloatFactor': 'FACTOR'
}

class _NodeGroupUtils(_NodeTreeUtils):

def __init__(self, shader):
Expand Down Expand Up @@ -75,26 +83,28 @@ def hide_nodes(self, hide_sockets=True):
s.hide = not s.is_linked

def new_input_socket(self, io_name, socket, default_val=None, min_max=None, socket_type=None):
self.__new_io(self.shader.inputs, self.node_input.outputs, io_name, socket, default_val, min_max, socket_type)
self.__new_io('INPUT', self.node_input.outputs, io_name, socket, default_val, min_max, socket_type)

def new_output_socket(self, io_name, socket, default_val=None, min_max=None, socket_type=None):
self.__new_io(self.shader.outputs, self.node_output.inputs, io_name, socket, default_val, min_max, socket_type)
self.__new_io('OUTPUT', self.node_output.inputs, io_name, socket, default_val, min_max, socket_type)

def __new_io(self, shader_io, io_sockets, io_name, socket, default_val=None, min_max=None, socket_type=None):
def __new_io(self, in_out, io_sockets, io_name, socket, default_val=None, min_max=None, socket_type=None):
if io_name not in io_sockets:
idname = socket_type or socket.bl_idname
shader_io.new(type=idname, name=io_name)
interface_socket = self.shader.interface.new_socket(name=io_name, in_out=in_out, socket_type=SOCKET_TYPE_MAPPING.get(idname, idname))
if idname in SOCKET_SUBTYPE_MAPPING:
interface_socket.subtype = SOCKET_SUBTYPE_MAPPING.get(idname, '')
if not min_max:
if idname.endswith('Factor') or io_name.endswith('Alpha'):
shader_io[io_name].min_value, shader_io[io_name].max_value = 0, 1
interface_socket.min_value, interface_socket.max_value = 0, 1
elif idname.endswith('Float') or idname.endswith('Vector'):
shader_io[io_name].min_value, shader_io[io_name].max_value = -10, 10
interface_socket.min_value, interface_socket.max_value = -10, 10
if socket is not None:
self.links.new(io_sockets[io_name], socket)
if default_val is not None:
shader_io[io_name].default_value = default_val
interface_socket.default_value = default_val
if min_max is not None:
shader_io[io_name].min_value, shader_io[io_name].max_value = min_max
interface_socket.min_value, interface_socket.max_value = min_max


class _MaterialMorph:
Expand Down

0 comments on commit 2cbe178

Please sign in to comment.