Skip to content

Commit

Permalink
Adds new tests and corrects baking directory
Browse files Browse the repository at this point in the history
  • Loading branch information
norgeotloic committed Oct 29, 2018
1 parent acf399f commit a991766
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 20 deletions.
186 changes: 178 additions & 8 deletions scripts/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import sys
import json

################################################################################
# 1 - A class to manage the execution of the different test cases
################################################################################

class TestSequence:

def __init__(self):
Expand Down Expand Up @@ -110,6 +114,10 @@ def report(self):
TESTS = TestSequence()
TESTS.reset()

############################################################################
# 2.0 - Functions used before operators and to check the results
############################################################################

def create_suzanne():
bpy.ops.mesh.primitive_monkey_add()
bpy.ops.object.modifier_add(type='SUBSURF')
Expand All @@ -118,6 +126,12 @@ def create_suzanne():
bpy.context.active_object.select=True
def empty_material_library():
bpy.types.Scene.pbrtextures = {}
def prepare_for_baking():
source = bpy.data.objects["Suzanne"]
bpy.ops.mesh.primitive_monkey_add(calc_uvs=True)
target = bpy.context.active_object
source.select = True

def assert_model_imported():
assert(len(bpy.data.objects) == 1)
def assert_suzanne_remeshed():
Expand All @@ -139,53 +153,97 @@ def assert_suzanne_has_material():
def assert_suzanne_is_unwrapped():
obj = bpy.data.objects["Suzanne"]
assert(len(obj.data.uv_layers)>0)
def assert_orthoview_created():
assert(os.path.exists(os.path.join(os.path.dirname(__file__),"orthoview.png")))
os.remove(os.path.join(os.path.dirname(__file__),"orthoview.png"))
def assert_baked_textures():
alb = os.path.join(os.path.dirname(__file__), "baked_basecolor.png")
nor = os.path.join(os.path.dirname(__file__), "baked_normals.png")
rou = os.path.join(os.path.dirname(__file__), "baked_roughness.png")
met = os.path.join(os.path.dirname(__file__), "baked_metallic.png")
assert(os.path.exists(alb))
assert(os.path.exists(nor))
assert(os.path.exists(rou))
assert(os.path.exists(met))
os.remove(alb)
os.remove(nor)
os.remove(rou)
os.remove(met)
def assert_mesh_file_created():
assert(os.path.exists(os.path.join(os.path.dirname(__file__),"suzanne.mesh")))
os.remove(os.path.join(os.path.dirname(__file__),"suzanne.mesh"))
def assert_cube_read():
assert( len(bpy.data.objects) == 1 )
assert( len(bpy.context.active_object.data.polygons) == 12 )

############################################################################
# 2.1 - Testing the import operators on cubes in different formats
############################################################################

#Import multiple versions of the cube
#Import a .obj
TESTS.add_operator(
name="import_obj",
operator="import_scan",
args = {"filepath": os.path.join(os.path.dirname(__file__), "cube.obj")},
after = assert_model_imported
)

#Import a .ply
TESTS.add_operator(
name="import_ply",
operator="import_scan",
args = {"filepath": os.path.join(os.path.dirname(__file__), "cube.ply")},
after = assert_model_imported
)

#Import a .stl
TESTS.add_operator(
name="import_stl",
operator="import_scan",
args = {"filepath": os.path.join(os.path.dirname(__file__), "cube.stl")},
after = assert_model_imported
)

#Import a .fbx
TESTS.add_operator(
name="import_fbx",
operator="import_scan",
args = {"filepath": os.path.join(os.path.dirname(__file__), "cube.fbx")},
after = assert_model_imported
)

#Import a .dae
TESTS.add_operator(
name="import_dae",
operator="import_scan",
args = {"filepath": os.path.join(os.path.dirname(__file__), "cube.dae")},
after = assert_model_imported
)

#Import a .x3d
TESTS.add_operator(
name="import_x3d",
operator="import_scan",
args = {"filepath": os.path.join(os.path.dirname(__file__), "cube.x3d")},
after = assert_model_imported
)

#Clean the object
############################################################################
# 2.2 - Preprocessing filter on the last imported cube
############################################################################

#Preprocess a cube
TESTS.add_operator(
name="preprocessing",
operator="clean_object",
reset=False
)

#Internal remeshing methods
############################################################################
# 2.3 - Internal remeshing methods
############################################################################

#Remesh suzanne with the simple decimate modifier
TESTS.add_operator(
name="remesh_decimate",
operator="remesh_decimate",
Expand All @@ -194,6 +252,8 @@ def assert_suzanne_is_unwrapped():
reset=True,
args={"limit":300}
)

#Remesh suzanne with the naive quads method
TESTS.add_operator(
name="remesh_quads",
operator="remesh_quads",
Expand All @@ -202,6 +262,8 @@ def assert_suzanne_is_unwrapped():
reset=True,
args={"ratio":0.01, "smooth":2}
)

#Remesh suzanne with an iterative method
TESTS.add_operator(
name="remesh_iterative",
operator="remesh_iterative",
Expand All @@ -211,7 +273,11 @@ def assert_suzanne_is_unwrapped():
args={"limit":500}
)

#External remeshing methods
############################################################################
# 2.4 - External remeshing methods
############################################################################

#Remesh suzanne with mmgs
TESTS.add_operator(
name="remesh_mmgs",
operator="remesh_mmgs",
Expand All @@ -220,6 +286,8 @@ def assert_suzanne_is_unwrapped():
reset=True,
args={"smooth":True, "hausd":0.01}
)

#Remesh suzanne with Instant Meshes
TESTS.add_operator(
name="remesh_instant",
operator="remesh_instant",
Expand All @@ -228,8 +296,10 @@ def assert_suzanne_is_unwrapped():
reset=True,
args={"interactive":False, "method":"faces", "facescount":1000}
)

#Do not run meshlabserver on Travis, as it needs a X server to run
if not "travis" in os.path.abspath(os.getcwd()):
#Remesh suzanne with meshlabserver
TESTS.add_operator(
name="remesh_meshlab",
operator="remesh_meshlab",
Expand All @@ -239,14 +309,20 @@ def assert_suzanne_is_unwrapped():
args={"facescount":1500}
)

#Interact with the material library
############################################################################
# 2.5 - PBR textures library operators
############################################################################

#Read textures from a directory
TESTS.add_operator(
name="create_library",
operator="create_library",
args={"filepath":os.path.dirname(__file__)},
after=assert_pbr_library_non_empty,
reset=True
)

#Import a material which was read previously
TESTS.add_operator(
name="material_from_library",
operator="material_from_library",
Expand All @@ -255,13 +331,17 @@ def assert_suzanne_is_unwrapped():
after=assert_suzanne_has_material,
reset=False,
)

#Save the current library as a .json file for future use
TESTS.add_operator(
name="save_json",
operator="save_json_library",
args={"filepath":os.path.join(os.path.dirname(__file__),"test.json")},
after=assert_json_non_empty,
reset=False
)

#Load a library from a .json file
TESTS.add_operator(
name="load_json",
operator="load_json_library",
Expand All @@ -270,6 +350,8 @@ def assert_suzanne_is_unwrapped():
after=assert_pbr_library_non_empty,
reset=False
)

#Import a material from a library, loaded from a .json file
TESTS.add_operator(
name="material_from_json",
operator="material_from_library",
Expand All @@ -279,36 +361,124 @@ def assert_suzanne_is_unwrapped():
reset=False,
)

#Other material operations
############################################################################
# 2.6 - Other material operators
############################################################################

#Unwrap a model with the basic method
TESTS.add_operator(
name="unwrap",
operator="unwrap",
args={"method":"basic"},
before=create_suzanne,
after=assert_suzanne_is_unwrapped,
)

#Unwrap a model with the smart uv project
TESTS.add_operator(
name="unwrap",
operator="unwrap",
args={"method":"smart"},
before=create_suzanne,
after=assert_suzanne_is_unwrapped,
)

#Unwrap a model with the "smarter" UV project
TESTS.add_operator(
name="unwrap",
operator="unwrap",
args={"method":"smarter"},
before=create_suzanne,
after=assert_suzanne_is_unwrapped,
)

#Add an empty PBR material to suzanne
TESTS.add_operator(
name="empty_material",
operator="create_empty_material",
before=create_suzanne,
after=assert_suzanne_has_material,
)

#Assign an albedo texture
TESTS.add_operator(
name="assign_albedo",
operator="assign_texture",
args={"slot":"albedo", "filepath":os.path.join(os.path.dirname(__file__),"stick_albedo.jpg")},
reset=False,
)

#Assign a normal map
TESTS.add_operator(
name="assign_normals",
operator="assign_texture",
args={"slot":"normal", "filepath":os.path.join(os.path.dirname(__file__),"stick_normals.jpg")},
reset=False,
)

#Baking operations
############################################################################
# 2.7 - Texture baking (mixing everything)
############################################################################

#Remesh suzanne
TESTS.add_operator(
name="bake_textures",
operator="bake_textures",
args={
"filepath":os.path.dirname(__file__),
"resolution": 64,
"imgFormat": "PNG",
"bake_albedo": True,
"bake_geometry": True,
"bake_surface": True,
"bake_roughness": True,
"bake_metallic": True
},
before=prepare_for_baking,
after=assert_baked_textures,
reset=False,
)

############################################################################
# 2.8 - Export operators
############################################################################

#Export an orthoview
#Not available as blender window needs to be open to have an opengl context
"""
TESTS.add_operator(
name="export_orthoview",
operator="export_orthoview",
args={"filepath":os.path.join(os.path.dirname(__file__),"orthoview.png")},
before=create_suzanne,
after=assert_orthoview_created,
)
"""

############################################################################
# 2.9 - Other operators (.mesh file format and future operators)
############################################################################

#Exports suzanne as a .mesh
TESTS.add_operator(
name="export_mesh",
operator="export_mesh",
args={"filepath":os.path.join(os.path.dirname(__file__),"suzanne.mesh")},
before=create_suzanne,
after=assert_mesh_file_created,
)

#Imports suzanne .mesh file
TESTS.add_operator(
name="import_mesh",
operator="import_mesh",
args={"filepath":os.path.join(os.path.dirname(__file__),"cube.mesh")},
after=assert_cube_read
)

#Run tests
############################################################################
# 3 - Run the tests and exit
############################################################################
TESTS.run()
err = TESTS.report()
sys.exit(err)
1 change: 1 addition & 0 deletions src/fn_ortho.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def create_axio_array(a01, a10, a11, a12, a13, a21, M=50):
#Third line
A[3*M+a01.shape[0]+a10.shape[0]:-M,2*M+a10.shape[1]:2*M+a10.shape[1]+a11.shape[1],:] = a21
return A

def array_to_image(arr, path):
img = bpy.data.images.new("tmp", arr.shape[1], arr.shape[0], alpha=1)
img.file_format = "PNG"
Expand Down
8 changes: 4 additions & 4 deletions src/op_BAKING_bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def poll(self, context):

def execute(self, context):
#Get the directory to save the images to
self.directory = os.path.abspath(os.path.dirname(self.properties.filepath))
self.directory = os.path.abspath(os.path.dirname(self.properties.filepath)) if os.path.isfile(self.properties.filepath) else os.path.abspath(self.properties.filepath)

#Find which object is the source and which is the target
source, target = None, None
Expand Down Expand Up @@ -234,9 +234,9 @@ def execute(self, context):
ARGS = "-compose overlay -composite"
output, error, code = fn_soft.convert(GEOM, OUT, ARGS, input2=TMP, executable=context.user_preferences.addons["BakeMyScan"].preferences.convert)
#Remove the old normal images (no blue channel, geometric normals...)
#os.remove(GEOM)
#os.remove(TMP)
#os.remove(NORM)
os.remove(GEOM)
os.remove(TMP)
os.remove(NORM)
else:
os.rename(GEOM, OUT)

Expand Down
Loading

0 comments on commit a991766

Please sign in to comment.