class Resource:
- def __init__(self, name):
+ def __init__(self, name, keyword):
self.name = name
+ self.keyword = keyword
self.statements = []
self.references = []
refs += [e for e in r.collect_references() if e not in refs]
refs += [e for e in self.references if e not in refs]
return refs
+
+ def write_to_file(self, fn):
+ with open(fn, "w") as out_file:
+ for s in self.statements:
+ s.write_to_file(out_file)
+
+ def write_collection(self, fn, *, exclude_self=False, filter=None):
+ items = self.collect_references()
+ if not exclude_self:
+ items.append(self)
+ with open(fn, "w") as out_file:
+ for i in items:
+ if filter and not filter(i):
+ continue
+ st = Statement(i.keyword, i.name)
+ st.sub = i.statements
+ st.write_to_file(out_file)
base, ext = os.path.splitext(base)
if self.collection:
- from .datafile import Statement
- with open(os.path.join(path, base+".mdc"), "w") as out_file:
- for n in sorted(resources.keys()):
- r = resources[n]
- st = Statement("animation", r.name)
- st.sub = r.statements
- st.write_to_file(out_file)
+ from .datafile import Resource
+ dummy = Resource("dummy", "dummy")
+ dummy.references = list(sorted(resources.values(), key=lambda r: r.name))
+ dummy.write_collection(os.path.join(path, base+".mdc"), exclude_self=True)
else:
for r in resources.values():
- with open(os.path.join(path, r.name), w) as out_file:
- for s in r.statements:
- s.write_to_file(out_file)
+ r.write_to_file(os.path.join(path, r.name))
else:
anim_data = context.active_object.animation_data
if not anim_data:
resource = self.export_animation(context, anim_data.action)
- with open(out_fn, "w") as out_file:
- for s in resource.statements:
- s.write_to_file(out_file)
+ resource.write_to_file(out_fn)
def export_animation(self, context, action):
from .animation import create_animation_from_action
anim = create_animation_from_action(context, action, looping_threshold=self.looping_threshold)
from .datafile import Resource, Statement
- resource = Resource(action.name+".anim")
+ resource = Resource(action.name+".anim", "animation")
components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
coords = "xyz"
resource = self.export_camera(obj)
- with open(out_fn, "w") as out_file:
- for s in resource.statements:
- s.write_to_file(out_file)
+ resource.write_to_file(out_fn)
def export_camera(self, obj):
if obj.type!='CAMERA':
raise Exception("Object is not a camera")
from .datafile import Resource, Statement
- resource = Resource(obj.name+".camera")
+ resource = Resource(obj.name+".camera", "camera")
position = obj.matrix_world*mathutils.Vector((0, 0, 0))
resource.statements.append(Statement("position", position[0], position[1], position[2]))
def create_technique_resource(material, resources, single_file):
from .datafile import Resource, Statement
- tech_res = Resource(material.name+".tech")
+ tech_res = Resource(material.name+".tech", "technique")
mat_res = resources[material.name+".mat"]
def export_material(self, material, *, resources):
from .datafile import Resource, Statement
- mat_res = Resource(material.name+".mat")
+ mat_res = Resource(material.name+".mat", "material")
st = Statement("pbr")
st.sub.append(self.create_property_statement(mat_res, material.base_color, "base_color", resources))
from .datafile import Resource, Statement, Token
base_color_name = material_map.name+"_base_color.tex2d"
if base_color_name not in resources:
- base_color_res = Resource(base_color_name)
+ base_color_res = Resource(base_color_name, "texture2d")
base_color_res.statements.append(Statement("min_filter", Token('NEAREST')))
base_color_res.statements.append(Statement("mag_filter", Token('NEAREST')))
mat_name = material_map.name+".mat"
if mat_name not in resources:
- mat_res = Resource(mat_name)
+ mat_res = Resource(mat_name, "material")
st = Statement("pbr")
st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
mat_res.statements.append(st)
progress.push_task("", 0.0, 0.95)
resource = self.export_mesh(context, obj, progress)
- with open(out_fn, "w") as out_file:
- for s in resource.statements:
- s.write_to_file(out_file)
+ resource.write_to_file(out_fn)
def export_mesh(self, context, mesh_or_obj, progress):
from .mesh import Mesh, create_mesh_from_object
progress.pop_task()
from .datafile import Resource, Statement, Token
- resource = Resource(mesh.name+".mesh")
+ resource = Resource(mesh.name+".mesh", "mesh")
statements = resource.statements
st = Statement("vertices", Token("NORMAL3"))
numbers[ext] = n+1
for r in refs:
- with open(os.path.join(path, r.name), "w") as out_file:
- for s in r.statements:
- s.write_to_file(out_file)
-
- with open(out_fn, "w") as out_file:
- for s in obj_res.statements:
- s.write_to_file(out_file)
+ r.write_to_file(os.path.join(path, r.name))
+ obj_res.write_to_file(out_fn)
def export_object_resources(self, context, obj, resources, progress, material_maps=None):
if material_maps is None:
lods = self.collect_object_lods(obj)
from .datafile import Resource, Statement
- obj_res = Resource(obj.name+".object")
+ obj_res = Resource(obj.name+".object", "object")
statements = obj_res.statements
center, radius = self.compute_bounding_sphere(obj)
def export_stub_technique(self):
from .datafile import Resource, Statement
- tech_res = Resource("stub.tech")
+ tech_res = Resource("stub.tech", "technique")
pass_st = Statement("pass", "")
tech_res.statements.append(pass_st)
mat_st = Statement("material")
refs = scene_res.collect_references()
if self.resource_collection:
- from .datafile import Statement
- keywords = { ".mat": "material",
- ".mesh": "mesh",
- ".object": "object",
- ".tech": "technique",
- ".tex2d": "texture2d" }
- with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
- for r in refs:
- if self.skip_existing and os.path.exists(os.path.join(path, r.name)):
- continue
-
- st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
- st.sub = r.statements
- st.write_to_file(res_out)
+ filter = None
+ if self.skip_existing:
+ filter = lambda r: not os.path.exists(os.path.join(path, r.name))
+ scene_res.write_collection(os.path.join(path, base+"_resources.mdc"), exclude=self=True, filter)
else:
res_dir = os.path.join(path, base+"_resources")
if not os.path.exists(res_dir):
os.makedirs(res_dir)
for r in refs:
- with open(os.path.join(res_dir, r.name), "w") as res_out:
- for s in r.statements:
- s.write_to_file(res_out)
+ r.write_to_file(os.path.join(res_dir, r.name))
- with open(out_fn, "w") as out_file:
- for s in scene_res.statements:
- s.write_to_file(out_file)
+ scene_res.write_to_file(out_fn)
def export_scene_resources(self, context, objs, resources, progress):
from .export_object import ObjectExporter
def export_scene(self, context, objs, progress, *, prototypes, resources):
from .datafile import Resource, Statement
- scene_res = Resource("scene.scene")
+ scene_res = Resource("scene.scene", "scene")
for o in objs:
obj_res = resources[prototypes[o.name].name+".object"]
def export_texture(self, tex_node, usage='RGB'):
image = tex_node.image
from .datafile import Resource, Statement, Token
- tex_res = Resource(image.name+".tex2d")
+ tex_res = Resource(image.name+".tex2d", "texture2d")
use_interpolation = tex_node.interpolation!='Closest'
if use_interpolation: