Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Skin Swap's behavior for more nuanced swapping #498

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions MCprep_addon/materials/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,19 @@ def set_texture_pack(material: Material, folder: Path, use_extra_passes: bool) -
return 1


def assert_textures_on_materials(image: Image, materials: List[Material]) -> int:
def assert_textures_on_materials(image: Image, materials: List[Material], legacy_skin_swap_behavior: bool=False) -> int:
"""Called for any texture changing, e.g. skin, input a list of material and
an already loaded image datablock."""
# TODO: Add option to search for or ignore/remove extra maps (normal, etc)
count = 0
for mat in materials:
status = set_cycles_texture(image, mat)
status = set_cycles_texture(image, mat, legacy_skin_swap_behavior)
if status:
count += 1
return count


def set_cycles_texture(image: Image, material: Material, extra_passes: bool=False) -> bool:
def set_cycles_texture(image: Image, material: Material, extra_passes: bool=False, legacy_skin_swap_behavior: bool=False) -> bool:
"""
Used by skin swap and assiging missing textures or tex swapping.
Args:
Expand Down Expand Up @@ -425,8 +425,17 @@ def set_cycles_texture(image: Image, material: Material, extra_passes: bool=Fals
else:
node.mute = True
node.hide = True

# Unlike the other names, this one
# is set with the Name option in the
# Blender UI, and thus is mapped to
# node.name and not node itself
elif "MCPREP_SKIN_SWAP" in node.name and node.type == "TEX_IMAGE":
node.image = image
node.mute = False
node.hide = False

elif node.type == "TEX_IMAGE":
elif node.type == "TEX_IMAGE" and legacy_skin_swap_behavior is True:
# assume all unlabeled texture nodes should be the diffuse pass
node["MCPREP_diffuse"] = True # annotate node for future reference
node.image = image
Expand Down
24 changes: 18 additions & 6 deletions MCprep_addon/materials/skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def handler_skins_load(scene):
env.log("Didn't run skin reloading callback", vv_only=True)


def loadSkinFile(self, context: Context, filepath: Path, new_material: bool=False):
def loadSkinFile(self, context: Context, filepath: Path, new_material: bool=False, legacy_skin_swap_behavior: bool=False):
if not os.path.isfile(filepath):
self.report({'ERROR'}, f"Image file not found: {filepath}")
return 1
Expand All @@ -121,7 +121,7 @@ def loadSkinFile(self, context: Context, filepath: Path, new_material: bool=Fals
self.report(
{'WARNING'}, "Skinswap skipped {} linked objects".format(skipped))

status = generate.assert_textures_on_materials(image, mats)
status = generate.assert_textures_on_materials(image, mats, legacy_skin_swap_behavior)
if status is False:
self.report({'ERROR'}, "No image textures found to update")
return 1
Expand Down Expand Up @@ -363,13 +363,17 @@ class MCPREP_OT_swap_skin_from_file(bpy.types.Operator, ImportHelper):
name="New Material",
description="Create a new material instead of overwriting existing one",
default=True)
legacy_skin_swap_behavior: bpy.props.BoolProperty(
name="Use Legacy Skin Swap Behavior",
description="Swap textures in all image nodes that exist on the selected material; will be deprecated in the next release",
default=False)
skipUsage: bpy.props.BoolProperty(default=False, options={'HIDDEN'})

track_function = "skin"
track_param = "file import"
@tracking.report_error
def execute(self, context):
res = loadSkinFile(self, context, self.filepath, self.new_material)
res = loadSkinFile(self, context, self.filepath, self.new_material, self.legacy_skin_swap_behavior)
if res != 0:
return {'CANCELLED'}

Expand All @@ -391,6 +395,10 @@ class MCPREP_OT_apply_skin(bpy.types.Operator):
name="New Material",
description="Create a new material instead of overwriting existing one",
default=True)
legacy_skin_swap_behavior: bpy.props.BoolProperty(
name="Use Legacy Skin Swap Behavior",
description="Swap textures in all image nodes that exist on the selected material; will be deprecated in the next release",
default=False)
skipUsage: bpy.props.BoolProperty(
default=False,
options={'HIDDEN'})
Expand All @@ -399,7 +407,7 @@ class MCPREP_OT_apply_skin(bpy.types.Operator):
track_param = "ui list"
@tracking.report_error
def execute(self, context):
res = loadSkinFile(self, context, self.filepath, self.new_material)
res = loadSkinFile(self, context, self.filepath, self.new_material, self.legacy_skin_swap_behavior)
if res != 0:
return {'CANCELLED'}

Expand Down Expand Up @@ -431,6 +439,10 @@ class MCPREP_OT_apply_username_skin(bpy.types.Operator):
"If an older skin layout (pre Minecraft 1.8) is detected, convert "
"to new format (with clothing layers)"),
default=True)
legacy_skin_swap_behavior: bpy.props.BoolProperty(
name="Use Legacy Skin Swap Behavior",
description="Swap textures in all image nodes that exist on the selected material; will be deprecated in the next release",
default=False)
skipUsage: bpy.props.BoolProperty(default=False, options={'HIDDEN'})

def invoke(self, context, event):
Expand Down Expand Up @@ -463,15 +475,15 @@ def execute(self, context):
return {'CANCELLED'}

# Now load the skin
res = loadSkinFile(self, context, saveloc, self.new_material)
res = loadSkinFile(self, context, saveloc, self.new_material, self.legacy_skin_swap_behavior)
if res != 0:
return {'CANCELLED'}
bpy.ops.mcprep.reload_skins()
return {'FINISHED'}
else:
env.log("Reusing downloaded skin")
ind = skins.index(user_ref)
res = loadSkinFile(self, context, paths[ind], self.new_material)
res = loadSkinFile(self, context, paths[ind], self.new_material, self.legacy_skin_swap_behavior)
if res != 0:
return {'CANCELLED'}
return {'FINISHED'}
Expand Down