Skip to content

Commit

Permalink
Some changes, upgrade materials > replaced materials
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspirata committed Aug 12, 2024
1 parent 67947ff commit ce48ff0
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Mcblend Source/Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

#

Upgrade_Materials_Array = {
Replace_Materials_Array = {

"bricks": "Upgraded Bricks",

Expand Down
2 changes: 0 additions & 2 deletions Mcblend Source/MCB_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,12 @@ def GetConnectedSocketFrom(output, tag, material=None):
if node.type == tag:
output_socket = node.outputs[output]
for link in output_socket.links:
to_node = link.to_node
to_sockets.append(link.to_socket)
return to_sockets

else:
output_socket = tag.outputs[output]
for link in output_socket.links:
to_node = link.to_node
to_sockets.append(link.to_socket)
return to_sockets
except:
Expand Down
21 changes: 12 additions & 9 deletions Mcblend Source/Materials/Materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ..Resource_Packs import *
from ..Utils.Absolute_Solver import Absolute_Solver

# Scan the material for image texture node duplicates > if nothing is connected to the vector then delete else don't touch
# Scan the material for image texture node duplicates > if nothing is connected to the vector input then delete and restore connections else don't touch
def DeleteUselessTextures(material):
texture_nodes = [node for node in material.node_tree.nodes if node.type == "TEX_IMAGE"]
image_to_nodes = {}
Expand Down Expand Up @@ -44,20 +44,22 @@ def get_node_suffix_number(node_name):
material.node_tree.nodes.remove(node)

@ Perf_Time
def upgrade_materials():
def replace_materials():
for selected_object in bpy.context.selected_objects:
if selected_object.material_slots:
for i, material in enumerate(selected_object.data.materials):
if material is not None and material.use_nodes:
for original_material, upgraded_material in Upgrade_Materials_Array.items():
# Maybe replace this with material name format | original_material: upgraded_material
# so, after replacing script will fix the name to | upgraded_material
for original_material, upgraded_material in Replace_Materials_Array.items():
for material_part in material.name.lower().replace("-", ".").split("."):
if original_material == material_part:
if upgraded_material not in bpy.data.materials:
try:
with bpy.data.libraries.load(os.path.join(materials_folder, "Upgraded Materials.blend"), link=False) as (data_from, data_to):
with bpy.data.libraries.load(os.path.join(materials_folder, "Replaced Materials.blend"), link=False) as (data_from, data_to):
data_to.materials = [upgraded_material]
except:
Absolute_Solver('004', "Upgraded Materials", traceback.format_exc())
Absolute_Solver('004', "Replaced Materials", traceback.format_exc())

appended_material = bpy.data.materials.get(upgraded_material)
selected_object.data.materials[i] = appended_material
Expand Down Expand Up @@ -199,7 +201,7 @@ def fix_world():
material_parts = image_texture_node.image.name.lower().replace(".png", "").replace("-", "_").split("_")

# Lazy Biome Color Fix Exclusions
if any(part in material_parts for part in ("grass", "water", "leaves", "stem", "lily", "vine", "fern")) and all(part not in material_parts for part in ("cherry", "side", "azalea", "snow")) or ("redstone" and "dust" in material_parts):
if any(part in material_parts for part in ("grass", "water", "leaves", "stem", "lily", "vine", "fern")) and all(part not in material_parts for part in ("cherry", "side", "azalea", "snow", "mushroom")) or ("redstone" and "dust" in material_parts):
if lbcf_node is None:
if "Lazy Biome Color Fix" not in bpy.data.node_groups:
try:
Expand Down Expand Up @@ -532,7 +534,7 @@ def setproceduralpbr():

if bump_node is None:
bump_node = material.node_tree.nodes.new(type='ShaderNodeBump')
bump_node.location = (PBSDF.location.x - 200, PBSDF.location.y - 100)
bump_node.location = (PBSDF.location.x - 180, PBSDF.location.y - 132)
material.node_tree.links.new(GetConnectedSocketTo("Base Color", PBSDF), bump_node.inputs['Height'])
material.node_tree.links.new(bump_node.outputs['Normal'], PBSDF.inputs['Normal'])

Expand All @@ -547,7 +549,7 @@ def setproceduralpbr():

PNormals = material.node_tree.nodes.new(type='ShaderNodeGroup')
PNormals.node_tree = Current_node_tree
PNormals.location = (PBSDF.location.x - 200, PBSDF.location.y - 132)
PNormals.location = (PBSDF.location.x - 180, PBSDF.location.y - 132)

else:
with bpy.data.libraries.load(nodes_file, link=False) as (data_from, data_to):
Expand All @@ -558,7 +560,7 @@ def setproceduralpbr():
bpy.data.node_groups[f"PNormals"].name = f"PNormals; {material.name}"
PNormals.node_tree = bpy.data.node_groups[f"PNormals; {material.name}"]
Current_node_tree = PNormals.node_tree
PNormals.location = (PBSDF.location.x - 200, PBSDF.location.y - 132)
PNormals.location = (PBSDF.location.x - 180, PBSDF.location.y - 132)

for node in material.node_tree.nodes:
if node.type == "GROUP":
Expand Down Expand Up @@ -712,6 +714,7 @@ def setproceduralpbr():
if (mult_socket := GetConnectedSocketTo("Multiply", node_group)) is not None:
material.node_tree.links.new(mult_socket, PBSDF.inputs["Emission Strength"])
material.node_tree.nodes.remove(node_group)

if Preferences.dev_tools and Preferences.experimental_features:
if PProperties.proughness:
if proughness_node is None:
Expand Down
4 changes: 2 additions & 2 deletions Mcblend Source/Operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ def execute(self, context):
return {'FINISHED'}

class UpgradeMaterialsOperator(Operator):
bl_idname = "materials.upgrade_materials"
bl_idname = "materials.replace_materials"
bl_label = "Upgrade Materials"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
Materials.upgrade_materials()
Materials.replace_materials()
return {'FINISHED'}

class FixMaterialsOperator(Operator):
Expand Down
4 changes: 2 additions & 2 deletions Mcblend Source/UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ def draw(self, context):
row = box.row()
row.label(text="Materials", icon="MATERIAL_DATA")

if os.path.isfile(os.path.join(materials_folder, "Upgraded Materials.blend")):
if os.path.isfile(os.path.join(materials_folder, "Replaced Materials.blend")):
row = box.row()
row.scale_y = Big_Button_Scale
row.operator("materials.upgrade_materials", text="Upgrade Materials")
row.operator("materials.replace_materials", text="Replace Materials")

row = box.row()
row.scale_y = Big_Button_Scale
Expand Down
7 changes: 1 addition & 6 deletions Mcblend Source/Utils/Absolute_Solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@
"Description": "{Data}.blend not found",
},

"005": {
"Error Name": "Internal Function Error",
"Description": "Something is wrong in {Data}",
},

"u006": {
"Error Name": "Color Space Not Found",
"Description": "You have custom color manager that doesn't have {Data}",
},

"007": {
"Error Name": "Create Thing Doesn't Exists in the File",
"Error Name": "Create Thing Doesn't Exist in the File",
"Description": "Create feature uses alredy imported asset to your file, so if you see this message then your file doesn't have {Data} and you should probably use recreate feature instead",
"Mode": "Full"
},
Expand Down
2 changes: 1 addition & 1 deletion Mcblend Source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def InitOnStart():
def load_post_handler(dummy):
InitOnStart()

classes = [McblendPreferences, AbsoluteSolverPanel, RecreateEnvironment, # Special Paneles
classes = [McblendPreferences, AbsoluteSolverPanel, RecreateEnvironment, # Special Panels
WorldProperties, MaterialsProperties, ResourcePackProperties, CreateEnvProperties, PPBRProperties, OptimizationProperties, UtilsProperties, AssetsProperties, # Properties
WorldAndMaterialsPanel, OptimizationPanel, UtilsPanel, AssetPanel, Assets_List_UL_, # Panels
RemoveAttributeOperator, OpenConsoleOperator, FixWorldOperator, SwapTexturesOperator, ResourcePackToggleOperator, MoveResourcePackUp, MoveResourcePackDown, # Operators
Expand Down
Binary file modified Mcblend.blend
Binary file not shown.

0 comments on commit ce48ff0

Please sign in to comment.