Пример #1
0
    def test_create_materials(self):
        not_a_mesh = Object()
        mesh_with_0_mat = Object()
        mesh_with_1_mat = Object()
        mesh_with_2_mat = Object()

        mesh_with_0_mat.data = Mesh()
        mesh_with_1_mat.data = Mesh()
        mesh_with_2_mat.data = Mesh()

        material = Material()
        material.name = "material"
        material.diffuse_color = [1.0, 1.0, 1.0, 1.0]

        mesh_with_0_mat.data.materials = []
        mesh_with_1_mat.data.materials = [material] * 1
        mesh_with_2_mat.data.materials = [material] * 2

        exporter = Exporter()
        exporter.create_materials(
            [not_a_mesh, mesh_with_0_mat, mesh_with_1_mat, mesh_with_2_mat])

        self.assertEqual(len(exporter.names), 4)
        self.assertEqual(len(exporter.materials), 4)
        self.assertEqual(exporter.materials[0],
                         exporter.get_default_material())
        self.assertEqual(not_a_mesh.data in exporter.materials_by_mesh, False)
        self.assertEqual(len(exporter.materials_by_mesh[mesh_with_0_mat.data]),
                         0)
        self.assertEqual(len(exporter.materials_by_mesh[mesh_with_1_mat.data]),
                         1)
        self.assertEqual(len(exporter.materials_by_mesh[mesh_with_2_mat.data]),
                         2)
Пример #2
0
def create_entity_alphatest_one_sided_node_group(material: Material,
                                                 is_first: bool) -> NodeTree:
    '''
    Creates a node group for entity alphatest one sided material if it
    doesn't exist already, otherwise it returns existing node group.
    '''
    if is_first:
        material.blend_method = 'CLIP'
        material.use_backface_culling = True
    try:
        return bpy.data.node_groups['entity_alphatest_one_sided']
    except:  # pylint: disable=bare-except
        pass
    group, inputs, outputs = _create_node_group_defaults(
        'entity_alphatest_one_sided')

    # In: Color-> Out: Color
    group.links.new(outputs.inputs[0], inputs.outputs[0])
    # In: Alpha -> Math[ADD] -> Math[FLOOR] -> Out: Alpha
    math_node = group.nodes.new('ShaderNodeMath')
    math_node.operation = 'GREATER_THAN'
    math_node.location = (1 * PADDING, -1 * PADDING)
    group.links.new(math_node.inputs[0], inputs.outputs[1])
    group.links.new(outputs.inputs[1], math_node.outputs[0])
    # RGB (black) -> Out: Emission
    rgb_node = group.nodes.new("ShaderNodeRGB")
    rgb_node.outputs['Color'].default_value = (0, 0, 0, 1)  # black
    rgb_node.location = (2 * PADDING, -2 * PADDING)
    group.links.new(outputs.inputs[2], rgb_node.outputs[0])

    return group
Пример #3
0
    def test_create_shapes(self):
        obj = Object()

        mesh = Mesh()
        mesh.name = "mesh"
        obj.data = mesh

        mat_red = Material()
        mat_red.name = "red"
        mat_red.diffuse_color = [1.0, 0.0, 0.0, 1.0]

        mesh.materials = [mat_red]

        vertex = MeshVertex()
        vertex.co = [0.0, 0.0, 0.0]

        mesh.vertices = [vertex] * 3

        uv_loop = MeshUVLoop()
        uv_loop.uv = [0.5, 0.5]

        uv_loop_layer = MeshUVLoopLayer()
        uv_loop_layer.data = [uv_loop] * 3
        mesh.uv_layers = [uv_loop_layer]

        loop = MeshLoop()
        loop.normal = [1.0, 2.0, 3.0]
        mesh.loops = [loop] * 3

        tri_red = MeshLoopTriangle()
        tri_red.vertices = [0, 1, 2]
        tri_red.loops = [0, 1, 2]
        tri_red.material_index = 0

        mesh.loop_triangles = [tri_red]

        exporter = Exporter()
        exporter.create_materials([obj])
        exporter.create_vertex_groups([obj])
        exporter.create_vector_groups([obj])
        exporter.create_st_groups([obj])
        exporter.create_tri_groups([obj])
        exporter.create_parts([obj])
        exporter.create_shapes([obj])

        self.assertEqual(exporter.shapes[0].shape_tri_link,
                         exporter.tri_groups[0].this_tri_group_index)
        self.assertEqual(exporter.shapes[0].shape_mat_link,
                         exporter.get_default_material().this_mat_index)
        self.assertEqual(exporter.shapes[0].shape_part_num, 1)
        self.assertEqual(exporter.shape_by_mesh[mesh], exporter.shapes[0])
Пример #4
0
def create_entity_emissive_alpha_node_group(material: Material,
                                            is_first: bool) -> NodeTree:
    '''
    Creates a node group for entity emissive alpha material if it doesn't exist
    already, otherwise it returns existing node group.
    '''
    if is_first:
        material.blend_method = 'CLIP'
    try:
        return bpy.data.node_groups['entity_emissive_alpha']
    except:  # pylint: disable=bare-except
        pass
    group, inputs, outputs = _create_node_group_defaults(
        'entity_emissive_alpha')

    # In: Color-> Out: Color
    group.links.new(outputs.inputs[0], inputs.outputs[0])
    #  In: Alpha -> MATH[CEIL] -> Out: Alpha
    math_3_node = group.nodes.new('ShaderNodeMath')
    math_3_node.operation = 'CEIL'
    math_3_node.location = (2 * PADDING, -1 * PADDING)
    group.links.new(math_3_node.inputs[0], inputs.outputs[1])
    group.links.new(outputs.inputs[1], math_3_node.outputs[0])
    # In: Color -> ... -> ... -> Vector[MULTIPLY][0] -> Out: Emission
    vector_node = group.nodes.new('ShaderNodeVectorMath')
    vector_node.operation = 'MULTIPLY'
    vector_node.location = (3 * PADDING, -2 * PADDING)
    group.links.new(vector_node.inputs[0], inputs.outputs[0])
    group.links.new(outputs.inputs[2], vector_node.outputs[0])
    # In: Alpha -> Math[MULTIPLY] -> Math[SUBTRACT][1] -> Vector[MULTIPLY][1]
    math_1_node = group.nodes.new('ShaderNodeMath')
    math_1_node.operation = 'MULTIPLY'
    math_1_node.location = (1 * PADDING, -2 * PADDING)
    math_2_node = group.nodes.new('ShaderNodeMath')
    math_2_node.operation = 'SUBTRACT'
    math_2_node.use_clamp = True
    math_2_node.location = (2 * PADDING, -2 * PADDING)
    group.links.new(math_1_node.inputs[0], inputs.outputs[1])
    group.links.new(math_2_node.inputs[1], math_1_node.outputs[0])
    group.links.new(vector_node.inputs[1], math_2_node.outputs[0])

    return group
Пример #5
0
    def test_create_textures(self):
        image = Image()
        image.filepath = "//test/image/path.png"
        image.library = "//test/image/library"

        node = ShaderNodeTexImage()
        node.image = image

        material = Material()
        material.name = "material"
        material.diffuse_color = [1.0, 1.0, 1.0, 1.0]
        material.use_nodes = True
        material.node_tree = NodeTree()
        material.node_tree.nodes = [node]

        mesh = Mesh()
        mesh.materials = [material]

        obj = Object()
        obj.data = mesh

        bpy.path.abspath = Mock(return_value=image.filepath)
        bpy.path.display_name_from_filepath = Mock(return_value="texture")

        reader = Mock()
        reader.read = Mock(return_value=[12, 34, [[0x05, 0x06], [0x07, 0x08]]])
        png.Reader = Mock(return_value=reader)

        exporter = Exporter()
        exporter.create_textures([obj])

        bpy.path.abspath.assert_called_once_with(
            "//test/image/path.png", library="//test/image/library")
        bpy.path.display_name_from_filepath.assert_called_once_with(
            "//test/image/path.png")
        png.Reader.assert_called_once_with("//test/image/path.png")
        reader.read.assert_called_once()

        self.assertEqual(exporter.names[0].node_name, "texture.tex")
        self.assertEqual(exporter.tex_images[0].tex_img_data, [5, 6, 7, 8])
        self.assertEqual(exporter.textures[0].tex_img_width, 12)
        self.assertEqual(exporter.textures[0].tex_img_height, 34)
Пример #6
0
    def test_create_objects(self):
        obj = Object()
        obj.name = "obj"

        mesh = Mesh()
        mesh.name = "mesh"

        obj.data = mesh
        obj.location = [1.0, 2.0, 3.0]
        obj.rotation_euler = [4.0, 5.0, 6.0]
        obj.scale = [7.0, 8.0, 9.0]

        mat_red = Material()
        mat_red.name = "red"
        mat_red.diffuse_color = [1.0, 0.0, 0.0, 1.0]

        mesh.materials = [mat_red]

        vertex = MeshVertex()
        vertex.co = [0.0, 0.0, 0.0]

        mesh.vertices = [vertex] * 3

        uv_loop = MeshUVLoop()
        uv_loop.uv = [0.5, 0.5]

        uv_loop_layer = MeshUVLoopLayer()
        uv_loop_layer.data = [uv_loop] * 3
        mesh.uv_layers = [uv_loop_layer]

        loop = MeshLoop()
        loop.normal = [1.0, 2.0, 3.0]
        mesh.loops = [loop] * 3

        tri_red = MeshLoopTriangle()
        tri_red.vertices = [0, 1, 2]
        tri_red.loops = [0, 1, 2]
        tri_red.material_index = 0

        mesh.loop_triangles = [tri_red]

        exporter = Exporter()
        exporter.create_materials([obj])
        exporter.create_vertex_groups([obj])
        exporter.create_vector_groups([obj])
        exporter.create_st_groups([obj])
        exporter.create_tri_groups([obj])
        exporter.create_parts([obj])
        exporter.create_shapes([obj])
        exporter.create_anim_groups([obj])
        exporter.create_objects([obj])

        self.assertEqual(exporter.objs[0].obj_shape_link,
                         exporter.shapes[0].this_shape_index)
        self.assertEqual(exporter.objs[0].obj_mat_link,
                         exporter.materials[0].this_mat_index)
        self.assertEqual(exporter.objs[0].obj_anim_link,
                         exporter.anim_groups[0].this_anim_group_index)
        self.assertEqual(exporter.objs[0].obj_shape_link,
                         exporter.shape_by_mesh[mesh].this_shape_index)
        self.assertEqual(exporter.objs[0].obj_mat_link,
                         exporter.get_default_material().this_mat_index)
        self.assertEqual(
            exporter.objs[0].obj_anim_link,
            exporter.anim_group_by_mesh[mesh].this_anim_group_index)
Пример #7
0
    def test_create_parts(self):
        obj = Object()

        mesh = Mesh()
        mesh.name = "mesh"
        obj.data = mesh

        mat_red = Material()
        mat_red.name = "red"
        mat_red.diffuse_color = [1.0, 0.0, 0.0, 1.0]

        mat_green = Material()
        mat_green.name = "green"
        mat_green.diffuse_color = [0.0, 1.0, 0.0, 1.0]

        mat_blue = Material()
        mat_blue.name = "blue"
        mat_blue.diffuse_color = [0.0, 0.0, 1.0, 1.0]

        mesh.materials = [mat_red, mat_green, mat_blue]

        vertex = MeshVertex()
        vertex.co = [0.0, 0.0, 0.0]

        mesh.vertices = [vertex] * 9

        uv_loop = MeshUVLoop()
        uv_loop.uv = [0.5, 0.5]

        uv_loop_layer = MeshUVLoopLayer()
        uv_loop_layer.data = [uv_loop] * 3
        mesh.uv_layers = [uv_loop_layer]

        loop = MeshLoop()
        loop.normal = [1.0, 2.0, 3.0]
        mesh.loops = [loop] * 3

        tri_red = MeshLoopTriangle()
        tri_red.vertices = [0, 1, 2]
        tri_red.loops = [0, 1, 2]
        tri_red.material_index = 0

        tri_green = MeshLoopTriangle()
        tri_green.vertices = [3, 4, 5]
        tri_green.loops = [0, 1, 2]
        tri_green.material_index = 1

        tri_blue = MeshLoopTriangle()
        tri_blue.vertices = [6, 7, 8]
        tri_blue.loops = [0, 1, 2]
        tri_blue.material_index = 2

        mesh.loop_triangles = [tri_red, tri_green, tri_blue]

        exporter = Exporter()
        exporter.create_materials([obj])
        exporter.create_vertex_groups([obj])
        exporter.create_vector_groups([obj])
        exporter.create_st_groups([obj])
        exporter.create_tri_groups([obj])
        exporter.create_parts([obj])

        self.assertEqual(exporter.parts[0].mat_index,
                         exporter.materials[1].this_mat_index)
        self.assertEqual(exporter.parts[1].mat_index,
                         exporter.materials[2].this_mat_index)
        self.assertEqual(exporter.parts[2].mat_index,
                         exporter.materials[3].this_mat_index)
        self.assertEqual(exporter.parts[0].tri_indices,
                         [exporter.tri_groups[0].tris[0].this_tri_index])
        self.assertEqual(exporter.parts[1].tri_indices,
                         [exporter.tri_groups[0].tris[1].this_tri_index])
        self.assertEqual(exporter.parts[2].tri_indices,
                         [exporter.tri_groups[0].tris[2].this_tri_index])
        self.assertEqual(exporter.parts_by_mesh[mesh], exporter.parts)