-
Notifications
You must be signed in to change notification settings - Fork 4
/
export_overte_json.py
169 lines (136 loc) · 6.14 KB
/
export_overte_json.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import bpy
import os
import json
import time
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, FloatProperty
from bpy.types import Operator
from .asset_loader import AssetLoader
from .entities import BaseEntity, ZoneEntity
from .entity_factory import EntityFactory
from .export_params import ExportParams
class ExportOverteJson(Operator, ExportHelper):
"""Exports scene to Overte json world file"""
bl_idname = "export_scene.overte" # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Export"
# ExportHelper mixin class uses this
filename_ext = ".json"
filter_glob: StringProperty(
default="*.json",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
use_material_references: BoolProperty(
name="Enable references in Material Entities",
description="One material will use data of an another material entity",
default=True,
)
use_fst: BoolProperty(
name="Enable FST files",
description="Will create FST files instead of material entities for models",
default=True,
)
lightmap_brightness: FloatProperty(
name="Lightmap brigthness",
description="Adjust the brightness of the materials that are using lightmaps",
default=0,
min=-2.0,
max=2.0
)
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
def append_path_from_object(self, obj, paths):
path_name = obj.name[5:] if len(obj.name) > 4 else obj.name[4:]
if path_name == 'default':
path_name = ''
entity = BaseEntity(obj)
p = entity.get_position()
r = entity.get_rotation()
position = str(p["x"]) + ',' + str(p["y"]) + ',' + str(p["z"])
rotation = str(r["x"]) + ',' + str(r["y"]) + ',' + str(r["z"]) + ',' + str(r["w"])
paths["/" + path_name] = "/" + position + "/" + rotation
def process_object(self, obj, entities, parent):
for child in obj.children:
if child.type != 'MESH' and child.type != 'LIGHT':
continue
entity = EntityFactory.createEntity(child)
if entity:
entity.generate(os.path.dirname(self.filepath))
materials = entity.get_material_entities()
position = entity.get_relative_postion(parent)
rotation = entity.get_relative_rotation(parent)
entity_json = { **entity.export(), **{ "position": position }, **{ "rotation": rotation } }
entity_json["parentID"] = parent.get_uuid()
entities.append(entity_json)
if len(materials) > 0:
for material in materials:
material.generate(os.path.dirname(self.filepath))
material = material.export(entity_json)
entities.append(material)
self.process_object(child, entities, entity)
def process_collection(self, col, entities, zone):
if EntityFactory.matchName(col, "Zone"):
zone = ZoneEntity(col).export()
entities.append(zone)
for obj in col.objects:
if obj.parent or (obj.type != 'MESH' and obj.type != 'LIGHT'):
continue
entity = EntityFactory.createEntity(obj)
if entity:
entity.generate(os.path.dirname(self.filepath))
materials = entity.get_material_entities()
entity_json = entity.export()
if zone:
entity_json["position"]["x"] -= zone["position"]["x"]
entity_json["position"]["y"] -= zone["position"]["y"]
entity_json["position"]["z"] -= zone["position"]["z"]
entity_json["parentID"] = zone["id"]
entities.append(entity_json)
if len(materials) > 0:
for material in materials:
material.generate(os.path.dirname(self.filepath))
material = material.export(entity_json)
entities.append(material)
self.process_object(obj, entities, entity)
for child in col.children:
self.process_collection(child, entities, zone)
def process_paths(self, col, paths):
for obj in col.objects:
if obj.type == 'MESH' and EntityFactory.matchName(obj, "Path"):
self.append_path_from_object(obj, paths)
for child in col.children:
self.process_paths(child, paths)
def write_overte_json(self, context, filepath):
print("running write_overte_json...")
AssetLoader.find_all_models()
ExportParams.current_time = int(time.time() * 1000000)
world = bpy.context.scene.world
ExportParams.domain_url = world.overte.domain_url
ExportParams.world_scale = world.overte.world_scale
ExportParams.models_path = world.overte.models_path
ExportParams.textures_path = world.overte.textures_path
ExportParams.use_material_references = self.use_material_references
ExportParams.use_fst = self.use_fst
ExportParams.lightmap_brightness = self.lightmap_brightness
ExportParams.materials_dict = {}
ExportParams.models_dict = {}
entities = []
self.process_collection(bpy.context.scene.collection, entities, None)
paths = { }
self.process_paths(bpy.context.scene.collection, paths)
data = {
"DataVersion": 0,
"Entities": entities,
"Id": BaseEntity(None).get_uuid(),
"Version": 133
}
if len(paths) > 0:
data["Paths"] = paths
f = open(filepath, 'w', encoding='utf-8')
f.write(json.dumps(data, indent=4))
f.close()
return {'FINISHED'}
def execute(self, context):
return self.write_overte_json(context, self.filepath)