Skip to content

Commit

Permalink
got a lot more tests working again
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarcontar committed Sep 17, 2020
1 parent 62966fc commit 8c38ec8
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 113 deletions.
4 changes: 2 additions & 2 deletions io_mesh_w3d/common/shading/vertex_material_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def create(node_tree, vert_mat, shader):
instance.inputs['Specular'].default_value = vert_mat.vm_info.specular.to_vector_rgba()
instance.inputs['Emissive'].default_value = vert_mat.vm_info.emissive.to_vector_rgba()
instance.inputs['Shininess'].default_value = vert_mat.vm_info.shininess
instance.inputs['Opacity'].default_value = vert_mat.vm_info.shininess
instance.inputs['Translucency'].default_value = vert_mat.vm_info.shininess
instance.inputs['Opacity'].default_value = vert_mat.vm_info.opacity
instance.inputs['Translucency'].default_value = vert_mat.vm_info.translucency

instance.inputs['DepthCompare'].default_value = str(shader.depth_compare)
instance.inputs['DepthMaskWrite'].default_value = str(shader.depth_mask)
Expand Down
2 changes: 1 addition & 1 deletion io_mesh_w3d/common/structs/mesh_structs/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def write(self, io_stream):


class Texture:
def __init__(self, id='', file='', texture_info=None):
def __init__(self, id='', file='', texture_info=TextureInfo()):
self.id = id
self.file = file
self.texture_info = texture_info
Expand Down
36 changes: 21 additions & 15 deletions io_mesh_w3d/common/utils/material_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# <pep8 compliant>
# Written by Stephan Vedder and Michael Schnabel

import os

from mathutils import Vector
from io_mesh_w3d.common.shading.vertex_material_group import *
from io_mesh_w3d.common.structs.mesh_structs.texture import *
Expand Down Expand Up @@ -38,12 +40,12 @@ def retrieve_materials(context, mesh_struct, b_mesh, mesh, used_textures):
# -> only export shader materials so convert VertexMaterial to DefaultW3D

if shader_node.node_tree.name == 'VertexMaterial':
vert_mat, shader, tex_name, uv_layer_name = retrieve_vertex_material(context, material, shader_node)
vert_mat, shader, tex_name, tex_path, uv_layer_name = retrieve_vertex_material(context, material, shader_node)
if not tex_name in used_textures:
used_textures.append(tex_name)
mesh_struct.vert_materials = [vert_mat]
mesh_struct.shaders = [shader]
mesh_struct.textures = [Texture(file=tex_name)]
mesh_struct.textures = [Texture(id=tex_name, file=tex_path if tex_path != '' else tex_name)]

tx_stage = TextureStage(
tx_ids=[[0]],
Expand Down Expand Up @@ -118,11 +120,11 @@ def retrieve_vertex_material(context, material, shader_node):
post_detail_color_func=get_value(context, node_tree, shader_node.inputs['PostDetailColorFunc'], int),
post_detail_alpha_func=get_value(context, node_tree, shader_node.inputs['PostDetailAlphaFunc'], int))

texture, uv_layer_name = get_texture_value(context, node_tree, shader_node.inputs['DiffuseTexture'])
texture, texture_path, uv_layer_name = get_texture_value(context, node_tree, shader_node.inputs['DiffuseTexture'])

shader.texturing = 1 if texture is not None else 0

return vert_mat, shader, texture, uv_layer_name
return vert_mat, shader, texture, texture_path, uv_layer_name


def retrieve_shader_material(context, shader_node):
Expand All @@ -134,28 +136,31 @@ def retrieve_shader_material(context, shader_node):
shader_mat.header.technique_index = get_value(context, node_tree, shader_node.inputs['Technique'], int)

for input_socket in shader_node.inputs:
if isinstance(input_socket, NodeSocketTexture):
if input_socket.bl_idname == 'NodeSocketTexture':
prop_type = STRING_PROPERTY
value, _ = get_texture_value(context, node_tree, input_socket)
value, _, _ = get_texture_value(context, node_tree, input_socket)
tex_name = value
elif isinstance(input_socket, NodeSocketTextureAlpha):
elif input_socket.bl_idname == 'NodeSocketTextureAlpha':
continue
elif isinstance(input_socket, NodeSocketFloat):
elif input_socket.bl_idname == 'NodeSocketFloat':
prop_type = FLOAT_PROPERTY
value = get_value(context, node_tree, input_socket, float)
elif isinstance(input_socket, NodeSocketVector2):
elif input_socket.bl_idname == 'NodeSocketVector2':
prop_type = VEC2_PROPERTY
value = get_vec2_value(context, node_tree, input_socket)
elif isinstance(input_socket, NodeSocketVector):
elif input_socket.bl_idname == 'NodeSocketVector':
prop_type = VEC3_PROPERTY
value = get_vec_value(context, node_tree, input_socket)
elif isinstance(input_socket, (NodeSocketVector4, NodeSocketColor)):
elif input_socket.bl_idname == 'NodeSocketVector4':
prop_type = VEC4_PROPERTY
value = get_color_value(context, node_tree, input_socket)
elif input_socket.bl_idname == 'NodeSocketColor':
prop_type = VEC4_PROPERTY
value = get_color_value(context, node_tree, input_socket)
elif isinstance(input_socket, NodeSocketInt):
elif input_socket.bl_idname == 'NodeSocketInt':
prop_type = LONG_PROPERTY
value = get_value(context, node_tree, input_socket, int)
elif isinstance(input_socket, NodeSocketBool):
elif input_socket.bl_idname == 'NodeSocketBool':
prop_type = BOOL_PROPERTY
value = get_value(context, node_tree, input_socket, bool)
else:
Expand Down Expand Up @@ -205,10 +210,11 @@ def get_texture_value(context, node_tree, socket):

for link in socket.links:
if link.from_node.bl_idname == node_type:
return link.from_node.image.name, get_uv_layer_name(context, node_tree, link.from_node.inputs['Vector'])
image = link.from_node.image
return image.name, os.path.basename(image.filepath), get_uv_layer_name(context, node_tree, link.from_node.inputs['Vector'])

context.error('Node ' + link.from_node.bl_idname + ' connected to ' + socket.name + ' in ' + node_tree.name + ' is not of type ' + type)
return None, None
return None, None, None


def get_value(context, node_tree, socket, cast):
Expand Down
2 changes: 1 addition & 1 deletion io_mesh_w3d/w3d/structs/mesh_structs/prelit.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def size(self, include_head=True):
return size

def write(self, io_stream):
write_chunk_head(self.type, io_stream,
write_chunk_head(self.prelit_type, io_stream,
self.size(False), has_sub_chunks=True)

self.mat_info.write(io_stream)
Expand Down
23 changes: 21 additions & 2 deletions tests/common/cases/structs/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,49 @@ def test_unknown_chunk_skip(self):
Animation.read(self, io_stream, subchunk_end)
report_func.assert_called_with('unknown chunk_type in io_stream: 0x0')

def test_validate(self):
def test_is_valid_if_w3d(self):
ani = get_animation()
self.file_format = 'W3D'
self.assertTrue(ani.validate(self))

def test_is_valid_if_w3x(self):
ani = get_animation()
self.file_format = 'W3X'
self.assertTrue(ani.validate(self))

def test_is_invalid_if_w3d_and_too_long_name(self):
ani = get_animation()
ani.header.name = 'tooooolonganiname'
self.file_format = 'W3D'
self.assertFalse(ani.validate(self))

def test_is_valid_if_w3x_and_too_long_name(self):
ani = get_animation()
ani.header.name = 'tooooolonganiname'
self.file_format = 'W3X'
self.assertTrue(ani.validate(self))

def test_is_invalid_if_w3d_and_too_long_hierarchy_name(self):
ani = get_animation()
ani.header.hierarchy_name = 'tooooolonganiname'
ani.header.hierarchy_name = 'tooooolonghieraname'
self.file_format = 'W3D'
self.assertFalse(ani.validate(self))

def test_is_valid_if_w3x_and_too_long_hierarchy_name(self):
ani = get_animation()
ani.header.name = 'tooooolonghieraname'
self.file_format = 'W3X'
self.assertTrue(ani.validate(self))

def test_is_invalid_if_w3d_and_no_channels(self):
ani = get_animation()
ani.channels = []
self.file_format = 'W3D'
self.assertFalse(ani.validate(self))

def test_is_invalid_if_w3x_and_no_channels(self):
ani = get_animation()
ani.channels = []
self.file_format = 'W3X'
self.assertFalse(ani.validate(self))

Expand Down
44 changes: 1 addition & 43 deletions tests/common/cases/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,6 @@ def test_shader_material_roundtrip(self):
actual = retrieve_materials(self, material, principled)
compare_shader_materials(self, mesh.shader_materials[0], actual)

# is that really a valid scenario? might only be a single shader material per mesh
def test_duplicate_shader_material_roundtrip(self):
mesh = get_mesh(shader_mat=True)
mesh.shader_materials = [get_shader_material(), get_shader_material()]

materials = []
for mat in mesh.shader_materials:
material = create_shader_material(self, mat, 'uv_layer')
materials.append(material)

self.assertEqual(1, len(bpy.data.materials))
self.assertTrue('meshName.NormalMapped.fx' in bpy.data.materials)

for i, expected in enumerate(mesh.shader_materials):
principled = node_shader_utils.PrincipledBSDFWrapper(materials[i], is_readonly=True)
actual = retrieve_shader_material(self, materials[i], principled)
compare_shader_materials(self, expected, actual)

def test_shader_material_w3x_roundtrip(self):
mesh = get_mesh(shader_mat=True)
mesh.shader_materials = [get_shader_material()]
Expand All @@ -80,7 +62,7 @@ def test_shader_material_w3x_roundtrip(self):
for source in mesh.shader_materials:
material = create_shader_material(self, source, 'uv_layer')
principled = node_shader_utils.PrincipledBSDFWrapper(material, is_readonly=True)
actual = retrieve_shader_material(self, material, principled, w3x=True)
actual = retrieve_shader_material(self, material, principled)
compare_shader_materials(self, source, actual)

def test_shader_material_w3x_rgb_colors_roundtrip(self):
Expand All @@ -99,30 +81,6 @@ def test_shader_material_w3x_rgb_colors_roundtrip(self):

compare_shader_materials(self, source, actual)

def test_shader_material_type_name_fallback(self):
mesh = get_mesh(shader_mat=True)

for source in mesh.shader_materials:
source.header.type_name = 'LoremIpsum'
source.properties = []

material = create_shader_material(self, source, 'uv_layer')
actual = retrieve_shader_material(self, material, principled)
source.header.type_name = 'DefaultW3D.fx'
compare_shader_materials(self, source, actual)

# also not a valid scenario anymore?
def test_shader_material_type_name_upgrade_to_normal_mapped(self):
mesh = get_mesh(shader_mat=True)

for source in mesh.shader_materials:
source.header.type_name = 'LoremIpsum'

material = create_shader_material(self, source, 'uv_layer')
actual = retrieve_shader_material(self, material, principled)
source.header.type_name = 'NormalMapped.fx'
compare_shader_materials(self, source, actual)

def test_shader_roundtrip(self):
mesh = get_mesh()

Expand Down
3 changes: 1 addition & 2 deletions tests/common/cases/utils/test_mesh_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ def test_used_texture_file_ending_is_correct(self):
meshes, _ = retrieve_meshes(self, None, None, 'container_name')

mesh = meshes[0]
self.assertEqual(mesh.textures[0].file, 'texture.dds')
self.assertEqual(mesh.textures[1].file, 'texture.dds')
self.assertEqual('texture.dds', mesh.textures[0].file)

def test_retrieve_meshes_with_meshes_in_edit_mode(self):
mesh = get_mesh()
Expand Down
5 changes: 1 addition & 4 deletions tests/common/cases/utils/test_mesh_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,11 @@ def test_mesh_import_vertex_colors_are_imported_correctly(self):

mesh = bpy.data.objects[mesh_name].data

self.assertEqual(6, len(mesh.vertex_colors))
self.assertEqual(3, len(mesh.vertex_colors))

self.assertEqual('DCG_0', mesh.vertex_colors[0].name)
self.assertEqual('DIG_0', mesh.vertex_colors[1].name)
self.assertEqual('SCG_0', mesh.vertex_colors[2].name)
self.assertEqual('DCG_1', mesh.vertex_colors[3].name)
self.assertEqual('DIG_1', mesh.vertex_colors[4].name)
self.assertEqual('SCG_1', mesh.vertex_colors[5].name)

def test_mesh_import_tx_stage_has_no_tx_coords(self):
mesh_name = 'mesh'
Expand Down
Loading

0 comments on commit 8c38ec8

Please sign in to comment.