Skip to content

Commit

Permalink
Procedural Roughness and Specular
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspirata committed Jun 22, 2024
1 parent 4a769f0 commit 5538e0a
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Addon Build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Add warning to __init__.py
run: |
BUILD_DATE=$(TZ='Asia/Novosibirsk' date +"%d.%m.%Y %H:%M:%S")
sed -i "21 a\ \"warning\": \"This is Experimental Branch Build ($BUILD_DATE UTC+7)\"," "Mcblend Source/__init__.py"
sed -i "21 a\ \"warning\": \"This is Experimental Branch Build ($BUILD_DATE)\"," "Mcblend Source/__init__.py"
- name: Prepare Blender Addon
Expand Down
2 changes: 1 addition & 1 deletion Mcblend Source/Data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Properties
import bpy
import os
import json
import zipfile
import traceback
import time
from bpy.props import (IntProperty, BoolProperty, FloatProperty, EnumProperty, StringProperty, PointerProperty)
from bpy.types import PropertyGroup

Expand Down
11 changes: 10 additions & 1 deletion Mcblend Source/MCB_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ def GetConnectedSocketFrom(output, tag, material=None):
return to_sockets
except:
Absolute_Solver("005", __name__, traceback.format_exc())


def RemoveLinksFrom(sockets):
try:
for socket in sockets:
for link in socket.links:
socket.node.id_data.links.remove(link)
except:
for link in socket.links:
socket.node.id_data.links.remove(link)

def GetConnectedSocketTo(input, tag, material=None):
try:
if material is not None:
Expand Down
Binary file modified Mcblend Source/Materials/Materials.blend
Binary file not shown.
102 changes: 69 additions & 33 deletions Mcblend Source/Materials/Materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def get_node_suffix_number(node_name):

def EmissionMode(PBSDF, material):

Preferences = bpy.context.preferences.addons[__package__].preferences
# LoL it works
Preferences = bpy.context.preferences.addons[__package__.replace(".Materials", "")].preferences

if Preferences.emissiondetection == 'Automatic & Manual' and (PBSDF.inputs["Emission Strength"].default_value != 0 or MaterialIn(Emissive_Materials, material)):
return 1
Expand All @@ -75,7 +76,7 @@ def upgrade_materials():
if selected_object.material_slots:
slot += 1
for i, material in enumerate(selected_object.data.materials):
if material is not None:
if material is not None and material.use_nodes:
for original_material, upgraded_material in Upgrade_Materials_Array.items():
for material_part in material.name.lower().replace("-", ".").split("."):
if original_material == material_part:
Expand Down Expand Up @@ -103,7 +104,7 @@ def fix_world():
if selected_object.material_slots:
for material in selected_object.data.materials:
slot += 1
if material is not None:
if material is not None and material.use_nodes:
PBSDF = None
image_texture_node = None
lbcf_node = None
Expand Down Expand Up @@ -399,7 +400,7 @@ def fix_materials():
if selected_object.material_slots:
for material in selected_object.data.materials:
slot += 1
if material is not None:
if material is not None and material.use_nodes:
image_texture_node = None
PBSDF = None

Expand Down Expand Up @@ -429,8 +430,7 @@ def find_texture_users(texture):
Texture_users = []
for obj in bpy.data.objects:
if obj.type == 'MESH':
for slot in obj.material_slots:
material = slot.material
for material in selected_object.data.materials:
if material and material.use_nodes:
for node in material.node_tree.nodes:
if node.type == 'TEX_IMAGE' and node.image == texture:
Expand Down Expand Up @@ -511,14 +511,14 @@ def animate_texture(texture_node, image_texture, new_image_texture_path, ITextur
material.node_tree.nodes.remove(Texture_Animator)

if ITexture_Animator is None:
if f"Animated; {image_texture.replace('.png', '')}" in bpy.data.node_groups:
TAnimator_exists = True
Current_node_tree = bpy.data.node_groups[f"Animated; {image_texture.replace('.png', '')}"]


ITexture_Animator = material.node_tree.nodes.new(type='ShaderNodeGroup')
ITexture_Animator.location = texture_node.location

if TAnimator_exists == False:

if f"Animated; {image_texture.replace('.png', '')}" in bpy.data.node_groups:
Current_node_tree = bpy.data.node_groups[f"Animated; {image_texture.replace('.png', '')}"]
ITexture_Animator.node_tree = Current_node_tree
else:
if "Texture Animator" not in bpy.data.node_groups:
with bpy.data.libraries.load(materials_file_path, link=False) as (data_from, data_to):
data_to.node_groups = ["Texture Animator"]
Expand All @@ -531,8 +531,6 @@ def animate_texture(texture_node, image_texture, new_image_texture_path, ITextur
for node in ITexture_Animator.node_tree.nodes:
if node.type == "TEX_IMAGE":
node.image = bpy.data.images[image_texture]
else:
ITexture_Animator.node_tree = Current_node_tree

if texture_node != None:
for socket in GetConnectedSocketFrom("Color", texture_node):
Expand Down Expand Up @@ -607,7 +605,7 @@ def animate_texture(texture_node, image_texture, new_image_texture_path, ITextur
if selected_object.material_slots:
for material in selected_object.data.materials:
slot += 1
if material is not None:
if material is not None and material.use_nodes:
PBSDF = None
image_texture_node = None
image_path = None
Expand Down Expand Up @@ -824,23 +822,17 @@ def animate_texture(texture_node, image_texture, new_image_texture_path, ITextur
if r_props.roughness:
material.node_tree.links.new(LabPBR_s.outputs["Roughness"], PBSDF.inputs["Roughness"])
else:
for link in material.node_tree.links:
if link.to_socket == PBSDF.inputs["Roughness"]:
material.node_tree.links.remove(link)
RemoveLinksFrom(LabPBR_s.outputs["Roughness"])

if r_props.metallic:
material.node_tree.links.new(LabPBR_s.outputs["Reflectance (Metallic)"], PBSDF.inputs["Metallic"])
else:
for link in material.node_tree.links:
if link.to_socket == PBSDF.inputs["Metallic"]:
material.node_tree.links.remove(link)
RemoveLinksFrom(LabPBR_s.outputs["Reflectance (Metallic)"])

if r_props.specular:
material.node_tree.links.new(LabPBR_s.outputs["Porosity (Specular)"], PBSDF.inputs["Specular IOR Level"])
else:
for link in material.node_tree.links:
if link.to_socket == PBSDF.inputs[PBSDF_compability("Specular IOR Level")]:
material.node_tree.links.remove(link)
RemoveLinksFrom(LabPBR_s.outputs["Porosity (Specular)"])

if r_props.sss:
material.node_tree.links.new(LabPBR_s.outputs["SSS"], PBSDF.inputs[PBSDF_compability("Subsurface Weight")])
Expand All @@ -849,9 +841,7 @@ def animate_texture(texture_node, image_texture, new_image_texture_path, ITextur

PBSDF.subsurface_method = 'BURLEY'
else:
for link in material.node_tree.links:
if link.to_socket == PBSDF.inputs[PBSDF_compability("Subsurface Weight")]:
material.node_tree.links.remove(link)
RemoveLinksFrom(LabPBR_s.outputs["SSS"])

if r_props.emission:
try:
Expand All @@ -861,9 +851,12 @@ def animate_texture(texture_node, image_texture, new_image_texture_path, ITextur
material.node_tree.links.new(ITexture_Animator.outputs["Color"], PBSDF.inputs[PBSDF_compability("Emission Color")])
except:
pass
else:


material.node_tree.links.new(LabPBR_s.outputs["Emission Strength"], PBSDF.inputs["Emission Strength"])
else:
RemoveLinksFrom(LabPBR_s.outputs["Emission Strength"])
RemoveLinksFrom(PBSDF.inputs[PBSDF_compability("Emission Color")])


animate_texture(specular_texture_node, specular_image_name, new_specular_image_path, STexture_Animator, Current_node_tree, image_path)
break
Expand Down Expand Up @@ -939,7 +932,7 @@ def swap_textures(folder_path):
if selected_object.material_slots:
for material in selected_object.data.materials:
slot += 1
if material is not None:
if material is not None and material.use_nodes:
for node in material.node_tree.nodes:
if node.type == "TEX_IMAGE" and node.image is not None:
new_image_path = find_image(node.image.name, folder_path)
Expand All @@ -962,10 +955,11 @@ def setproceduralpbr():
if selected_object.material_slots:
for material in selected_object.data.materials:
slot += 1
if material is not None:
image_texture_node = None
if material is not None and material.use_nodes:
PBSDF = None
bump_node = None
proughness_node = None
pspecular_node = None
PNormals = None
ITexture_Animator = None
Texture_Animator = None
Expand Down Expand Up @@ -994,6 +988,13 @@ def setproceduralpbr():

if "Texture Animator" in node.node_tree.name:
Texture_Animator = node

if node.type == "MAP_RANGE":
if "Procedural Roughness Node" in node.name:
proughness_node = node

if "Procedural Specular Node" in node.name:
pspecular_node = node

if PBSDF is not None:
# Use Normals
Expand Down Expand Up @@ -1186,14 +1187,49 @@ def setproceduralpbr():
pass

node_group.location = (PBSDF.location.x - 200, PBSDF.location.y - 250)
material.node_tree.links.new(GetConnectedSocketTo("Base Color", PBSDF), node_group.inputs[PBSDF_compability("Emission Color")])
material.node_tree.links.new(GetConnectedSocketTo("Base Color", PBSDF), node_group.inputs["Emission Color"])
material.node_tree.links.new(node_group.outputs["Emission Strength"], PBSDF.inputs["Emission Strength"])

if PProperties.make_better_emission == False and PProperties.animate_textures == False:
for node in material.node_tree.nodes:
if node.type == 'GROUP':
if BATGroup in node.node_tree.name:
material.node_tree.nodes.remove(node)

if PProperties.proughness:
if proughness_node == None:
proughness_node = material.node_tree.nodes.new(type='ShaderNodeMapRange')
proughness_node.name = "Procedural Roughness Node"
proughness_node.hide = True

proughness_node.inputs["From Max"].default_value = 0.0
proughness_node.inputs["From Min"].default_value = 1.0
proughness_node.inputs["To Max"].default_value = PBSDF.inputs["Roughness"].default_value
proughness_node.inputs["To Min"].default_value = PBSDF.inputs["Roughness"].default_value * PProperties.pr_dif

material.node_tree.links.new(GetConnectedSocketTo("Base Color", PBSDF), proughness_node.inputs["Value"])
material.node_tree.links.new(proughness_node.outputs[0], PBSDF.inputs["Roughness"])

elif PProperties.revert_proughness and proughness_node != None:
material.node_tree.nodes.remove(proughness_node)

if PProperties.pspecular:
if pspecular_node == None:
pspecular_node = material.node_tree.nodes.new(type='ShaderNodeMapRange')
pspecular_node.name = "Procedural Specular Node"
pspecular_node.hide = True

pspecular_node.inputs["From Max"].default_value = 1.0
pspecular_node.inputs["From Min"].default_value = 0.0
pspecular_node.inputs["To Max"].default_value = PBSDF.inputs[PBSDF_compability("Specular IOR Level")].default_value
pspecular_node.inputs["To Min"].default_value = PBSDF.inputs[PBSDF_compability("Specular IOR Level")].default_value * PProperties.ps_dif

material.node_tree.links.new(GetConnectedSocketTo("Base Color", PBSDF), pspecular_node.inputs["Value"])
material.node_tree.links.new(pspecular_node.outputs[0], PBSDF.inputs[PBSDF_compability("Specular IOR Level")])

elif PProperties.revert_pspecular and pspecular_node != None:
material.node_tree.nodes.remove(pspecular_node)

else:
Absolute_Solver("m002", slot)

Expand Down
60 changes: 56 additions & 4 deletions Mcblend Source/Properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,24 +230,76 @@ class PPBRProperties(PropertyGroup):
description=""
)

make_better_emission: BoolProperty(
name="Make Better Emission",
pspecular: BoolProperty(
name="Procedural Specular",
default=True,
description=""
)

animate_textures: BoolProperty(
name="Animate textures",
revert_pspecular: BoolProperty(
name="Revert",
default=True,
description=""
)

pspecular_settings: BoolProperty(
name="Procedural Specular Settings",
default=False,
description=""
)

ps_dif: FloatProperty(
name="Difference",
default=0.0,
min=0.0,
max=1.0,
description="Value 1 will be Ignored"
)

proughness: BoolProperty(
name="Procedural Roughness",
default=True,
description=""
)

revert_proughness: BoolProperty(
name="Revert",
default=True,
description=""
)

proughness_settings: BoolProperty(
name="Procedural Roughness Settings",
default=False,
description=""
)

pr_dif: FloatProperty(
name="Difference",
default=0.6,
min=0.0,
max=1.0,
description="Value 1 will be Ignored"
)

advanced_settings: BoolProperty(
name="Advanced Settings",
default=False,
description=""
)

make_better_emission: BoolProperty(
name="Make Better Emission",
default=True,
description=""
)

animate_textures: BoolProperty(
name="Animate textures",
default=False,
description=""
)

change_bsdf: BoolProperty(
name="Change BSDF Settings",
default=True,
Expand Down
Loading

0 comments on commit 5538e0a

Please sign in to comment.