Skip to content

Commit

Permalink
Fix wrong enum function and more thorough unit testing for animated mats
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDuckCow authored and StandingPadAnimations committed Sep 23, 2023
1 parent 99caad2 commit 4c5405d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
32 changes: 17 additions & 15 deletions MCprep_addon/materials/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ def execute(self, context):

if engine == 'CYCLES' or engine == 'BLENDER_EEVEE':
options = generate.PrepOptions(
passes,
self.useReflections,
self.usePrincipledShader,
self.makeSolid,
generate.PackFormat[self.packFormat.upper()],
self.useEmission,
False # This is for an option set in matprep_cycles
passes,
self.useReflections,
self.usePrincipledShader,
self.makeSolid,
generate.PackFormat[self.packFormat.upper()],
self.useEmission,
False # This is for an option set in matprep_cycles
)
res = generate.matprep_cycles(
mat=mat,
Expand All @@ -270,7 +270,7 @@ def execute(self, context):
sequences.animate_single_material(
mat,
context.scene.render.engine,
export_location="original")
export_location=sequences.ExportLocation.ORIGINAL)

# Sync materials.
if self.syncMaterials is True:
Expand Down Expand Up @@ -389,24 +389,24 @@ class MCPREP_OT_swap_texture_pack(
bl_options = {'REGISTER', 'UNDO'}

filter_glob: bpy.props.StringProperty(
default="",
default="",
options={"HIDDEN"})
use_filter_folder = True
fileselectparams = "use_filter_blender"
filepath: bpy.props.StringProperty(subtype="DIR_PATH")
filter_image: bpy.props.BoolProperty(
default=True,
default=True,
options={"HIDDEN", "SKIP_SAVE"})
filter_folder: bpy.props.BoolProperty(
default=True,
default=True,
options={"HIDDEN", "SKIP_SAVE"})
prepMaterials: bpy.props.BoolProperty(
name="Prep materials",
description="Runs prep materials after texture swap to regenerate materials",
default=False,
)
skipUsage: bpy.props.BoolProperty(
default=False,
default=False,
options={"HIDDEN"})

@classmethod
Expand Down Expand Up @@ -437,7 +437,7 @@ def draw(self, context):
col.prop(self, "syncMaterials")
col.prop(self, "improveUiSettings")
col.prop(self, "combineMaterials")

track_function = "texture_pack"
track_param = None
track_exporter = None
Expand Down Expand Up @@ -484,7 +484,7 @@ def execute(self, context):
sequences.animate_single_material(
mat,
context.scene.render.engine,
export_location="original")
export_location=sequences.ExportLocation.ORIGINAL)
# may be a double call if was animated tex
generate.set_saturation_material(mat)

Expand Down Expand Up @@ -634,7 +634,9 @@ def update_material(self, context, mat):

if self.animateTextures:
sequences.animate_single_material(
mat, context.scene.render.engine, export_location=sequences.ExportLocation["original"])
mat,
context.scene.render.engine,
export_location=sequences.ExportLocation.ORIGINAL)

return success, None

Expand Down
42 changes: 31 additions & 11 deletions test_files/materials_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,20 @@ def test_generate_material_sequence(self):
"""Validates generating an image sequence works ok."""
self._material_sequnece_subtest(operator=False)

def test_load_material_animated(self):
def test_prep_material_animated(self):
"""Validates loading an animated material works ok."""
self._material_sequnece_subtest(operator=True)

def _has_animated_tex_node(self, mat) -> bool:
any_animated = False
for nd in mat.node_tree.nodes:
if nd.type != "TEX_IMAGE":
continue
if nd.image.source == "SEQUENCE":
any_animated = True
break
return any_animated

def _material_sequnece_subtest(self, operator: bool):
"""Validates generating an image sequence works ok."""

Expand All @@ -256,9 +266,23 @@ def _material_sequnece_subtest(self, operator: bool):
if operator is True:
# Add a mesh to operate on
bpy.ops.mesh.primitive_plane_add()

res = bpy.ops.mcprep.load_material(
filepath=tiled_img, animateTextures=False)
self.assertEqual(res, {'FINISHED'})

any_animated = self._has_animated_tex_node(
bpy.context.object.active_material)
self.assertFalse(any_animated, "Should not be initially animated")

res = bpy.ops.mcprep.load_material(
filepath=tiled_img, animateTextures=True)
self.assertEqual(res, {'FINISHED'})
any_animated = self._has_animated_tex_node(
bpy.context.object.active_material)
self._debug_save_file_state()
self.assertTrue(any_animated, "Affirm animated material applied")

else:
res, err = sequences.generate_material_sequence(
source_path=fake_orig_img,
Expand All @@ -267,16 +291,12 @@ def _material_sequnece_subtest(self, operator: bool):
export_location="original",
clear_cache=True)

gen_files = [img for img in os.listdir(result_dir)
if img.endswith(".png")]
self.assertTrue(os.path.isdir(result_dir),
"Output directory does not exist")
shutil.rmtree(result_dir)
self.assertTrue(gen_files, "No images generated")

if operator is True:
self.assertEqual(res, {'FINISHED'})
else:
gen_files = [img for img in os.listdir(result_dir)
if img.endswith(".png")]
self.assertTrue(os.path.isdir(result_dir),
"Output directory does not exist")
shutil.rmtree(result_dir)
self.assertTrue(gen_files, "No images generated")
self.assertIsNone(err, "Generate materials had an error")
self.assertTrue(
res,
Expand Down

0 comments on commit 4c5405d

Please sign in to comment.