summaryrefslogtreecommitdiffstats
path: root/tools/blender-export.py
blob: fbaca57b84d98f749093a5d6beaa2c3e7fcca28e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# Script for exporting Blender models (meshes) to Colobot model files
#  (text format)
#
# Copyright (C) 2012, PPC (Polish Portal of Colobot)
#

import bpy
import struct
import array


class ExportColobot(bpy.types.Operator):
    """Exporter to Colobot text format"""
    bl_idname = "export.colobot"
    bl_label = "Export to Colobot"

    # Version of format
    FORMAT_VERSION = 1

    # TODO: set the following in some user-friendly way
    #  or better, read them from custom, per-polygon data
    # For now, you must make any changes here, as appropriate

    # Render state
    STATE = 0
    # Min & max LOD
    MIN = 0.0
    MAX = 0.0
    # Variable tex2
    VAR_TEX2 = False

    filepath = bpy.props.StringProperty(subtype="FILE_PATH")

    @classmethod
    def poll(cls, context):
        return context.object is not None

    def execute(self, context):
        self.write(context.object, context.scene)
        return {'FINISHED'}

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

    def write(self, object, scene):
        if (object.type != 'MESH'):
            self.report({'ERROR'}, 'Only mesh objects can be exported!')
            return

        for poly in object.data.polygons:
            if (poly.loop_total > 3):
                self.report({'ERROR'}, 'Cannot export polygons with > 3 vertices!')
                return

        file = open(self.filepath, 'w')


        file.write('# Colobot text model\n')
        file.write('\n')

        triangleCount = len(object.data.polygons) * 3

        file.write('### HEAD\n')
        file.write('version ' + str(ExportColobot.FORMAT_VERSION) + '\n')
        file.write('total_triangles ' + str(triangleCount) + '\n')
        file.write('\n')
        file.write('### TRIANGLES\n')


        for poly in object.data.polygons:

            i = 0
            for loop_index in poly.loop_indices:
                v = object.data.vertices[object.data.loops[loop_index].vertex_index]

                i = i + 1

                file.write('p' + str(i))
                file.write(' c ' + str(v.co[0]) + ' ' + str(v.co[1]) + ' ' + str(v.co[2]))
                file.write(' n ' + str(v.normal[0]) + ' ' + str(v.normal[1]) + ' ' + str(v.normal[2]))

                uv1 = array.array('f', [0.0, 0.0])
                uv2 = array.array('f', [0.0, 0.0])

                if (len(object.data.uv_layers) >= 1):
                    uv1 = object.data.uv_layers[0].data[loop_index].uv
                if (len(object.data.uv_layers) >= 2):
                    uv2 = object.data.uv_layers[1].data[loop_index].uv

                file.write(' t1 ' + str(uv1[0]) + ' ' + str(uv1[1]))
                file.write(' t2 ' + str(uv2[0]) + ' ' + str(uv2[1]))
                file.write('\n')

            mat = object.data.materials[poly.material_index]

            file.write('mat')
            file.write(' dif ' + str(mat.diffuse_color[0]) + ' ' + str(mat.diffuse_color[1]) + ' ' + str(mat.diffuse_color[2]))
            amb = scene.world.ambient_color * mat.ambient
            file.write(' amb ' + str(amb[0]) + ' ' + str(amb[1]) + ' ' + str(amb[2]))
            file.write(' spc ' + str(mat.specular_color[0]) + ' ' + str(mat.specular_color[1]) + ' ' + str(mat.specular_color[2]))
            file.write('\n')

            tex1 = ''
            tex2 = ''

            if (len(object.data.uv_textures) >= 1):
                tex1 = bpy.path.basename(object.data.uv_textures[0].data[0].image.filepath)
            if (len(object.data.uv_textures) >= 2):
                tex2 = bpy.path.basename(object.data.uv_textures[1].data[0].image.filepath)

            file.write('tex1 ' + tex1 + '\n')
            file.write('tex2 ' + tex2 + '\n')
            file.write('var_tex2 ' + 'Y' if ExportColobot.VAR_TEX2 else 'N' + '\n')
            file.write('min ' + str(ExportColobot.MIN) + '\n')
            file.write('max ' + str(ExportColobot.MAX) + '\n')
            file.write('state ' + str(ExportColobot.STATE) + '\n')
            file.write('\n')

        file.close()
        self.report({'INFO'}, 'Export OK')


# For menu item
def menu_func(self, context):
    self.layout.operator_context = 'INVOKE_DEFAULT'
    self.layout.operator(ExportColobot.bl_idname, text="Colobot (Text Format)")

# Register and add to the file selector
bpy.utils.register_class(ExportColobot)
bpy.types.INFO_MT_file_export.append(menu_func)