Skip to content

Commit

Permalink
Add option to change blend mode if alpha channel is added for Blender…
Browse files Browse the repository at this point in the history
… 2.80 to Blender 4.1 (#183)
  • Loading branch information
ucupumar committed Oct 9, 2024
1 parent ff35e61 commit 0075fd3
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion Root.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,16 @@ class YNewYPaintChannel(bpy.types.Operator):
description = 'Set socket strength to one',
default=True)

blend_method : EnumProperty(
name='Blend Method',
description = 'Blend method for transparent material',
items = (
('CLIP', 'Alpha Clip', ''),
('HASHED', 'Alpha Hashed', ''),
('BLEND', 'Alpha Blend', '')),
default='HASHED'
)

@classmethod
def poll(cls, context):
return get_active_ypaint_node()
Expand Down Expand Up @@ -1152,13 +1162,27 @@ def check(self, context):
return True

def draw(self, context):

mat = get_active_material()

# Detect principled alpha
is_alpha_channel = False
if is_bl_newer_than(2, 80) and not is_bl_newer_than(4, 2):
item = self.input_coll.get(self.connect_to)
if item:
target_node = mat.node_tree.nodes.get(item.node_name)
if target_node.type == 'BSDF_PRINCIPLED' and item.input_name == 'Alpha':
is_alpha_channel = True

row = split_layout(self.layout, 0.4)

col = row.column(align=False)
col.label(text='Name:')
col.label(text='Connect To:')
if self.type != 'NORMAL':
col.label(text='Color Space:')
if is_alpha_channel:
col.label(text='Blend Method:')
if self.type != 'NORMAL': col.label(text='')

col = row.column(align=False)
Expand All @@ -1167,13 +1191,14 @@ def draw(self, context):
#lib.custom_icons[channel_socket_custom_icon_names[self.type]].icon_id)
if self.type != 'NORMAL':
col.prop(self, "colorspace", text='')
if is_alpha_channel:
col.prop(self, 'blend_method', text='')
if self.type != 'NORMAL': col.prop(self, 'use_clamp')

# Blender 4.0 and above has some default weight and strength set to 0.0
if is_bl_newer_than(4):
item = self.input_coll.get(self.connect_to)
if item:
mat = get_active_material()
target_node = mat.node_tree.nodes.get(item.node_name)
if target_node.type == 'BSDF_PRINCIPLED' and item.input_name in {'Emission Color', 'Subsurface Scale'}:
col.prop(self, "set_strength_to_one")
Expand Down Expand Up @@ -1217,6 +1242,7 @@ def execute(self, context):
inp = None
strength_inp = None
input_name = ''
set_blend_method = False
if item:
target_node = mat.node_tree.nodes.get(item.node_name)
input_name = item.input_name
Expand All @@ -1230,6 +1256,11 @@ def execute(self, context):
elif item.input_name == 'Subsurface Scale':
strength_inp = target_node.inputs.get('Subsurface Weight')

if (is_bl_newer_than(2, 80) and not is_bl_newer_than(4, 2) and
target_node.type == 'BSDF_PRINCIPLED' and item.input_name == 'Alpha'
):
set_blend_method = True

# Set input default value
if inp and self.type != 'NORMAL':
set_input_default_value(node, channel, inp.default_value)
Expand All @@ -1247,6 +1278,10 @@ def execute(self, context):
if channel.use_clamp != self.use_clamp:
channel.use_clamp = self.use_clamp

# Set blend method
if set_blend_method:
mat.blend_method = self.blend_method

# Change active channel
last_index = len(yp.channels)-1
group_tree.yp.active_channel_index = last_index
Expand Down

0 comments on commit 0075fd3

Please sign in to comment.