Added support for Blender 2.80

master
TigerKat 5 years ago
parent 4eb13819c4
commit 0adfd9cc30

@ -2,8 +2,8 @@
bl_info = { bl_info = {
"name": "City of Heroes (.geo)", "name": "City of Heroes (.geo)",
"author": "TigerKat", "author": "TigerKat",
"version": (0, 1, 3), "version": (0, 2, 0),
"blender": (2, 79, 0), "blender": (2, 80, 0),
"location": "File > Import/Export,", "location": "File > Import/Export,",
"description": "City of Heroes (.geo)", "description": "City of Heroes (.geo)",
"tracker_url": "https://git.ourodev.com/tigerkat/geopy/issues", "tracker_url": "https://git.ourodev.com/tigerkat/geopy/issues",
@ -107,15 +107,39 @@ def menu_func_export(self, context):
self.layout.operator(ExportGeoMetric.bl_idname, self.layout.operator(ExportGeoMetric.bl_idname,
text="City of Heroes (Meters) (.geo)") text="City of Heroes (Meters) (.geo)")
def make_annotations(cls):
"""Converts class fields to annotations if running with Blender 2.8"""
if bpy.app.version < (2, 80):
return cls
bl_props = {k: v for k, v in cls.__dict__.items() if isinstance(v, tuple)}
if bl_props:
if '__annotations__' not in cls.__dict__:
setattr(cls, '__annotations__', {})
annotations = cls.__dict__['__annotations__']
for k, v in bl_props.items():
annotations[k] = v
delattr(cls, k)
return cls
classes = (
ImportGeo,
ImportGeoMetric,
ExportGeo,
ExportGeoMetric
)
def register(): def register():
bpy.utils.register_module(__name__) #bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_import.append(menu_func_import) for cls in classes:
bpy.types.INFO_MT_file_export.append(menu_func_export) make_annotations(cls)
bpy.utils.register_class(cls)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister(): def unregister():
bpy.utils.unregister_module(__name__) #bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_import.remove(menu_func_import) for cls in reversed(classes):
bpy.types.INFO_MT_file_export.remove(menu_func_export) bpy.utils.unregister_class(cls)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
if __name__ == "__main__": if __name__ == "__main__":
register() register()

@ -6,41 +6,75 @@ import bpy
from bpy_extras.io_utils import axis_conversion from bpy_extras.io_utils import axis_conversion
def convert_mesh(geo_model, mesh, obj): def convert_mesh(geo_model, mesh, obj):
# Be sure tessface & co are available! if bpy.app.version < (2, 80):
if not mesh.tessfaces and mesh.polygons: # Be sure tessface & co are available!
mesh.calc_tessface() if not mesh.tessfaces and mesh.polygons:
mesh.calc_tessface()
else:
# Be sure tessellated loop trianlges are available!
if not mesh.loop_triangles and mesh.polygons:
mesh.calc_loop_triangles()
mesh_verts = mesh.vertices # save a lookup mesh_verts = mesh.vertices # save a lookup
has_uv = bool(mesh.tessface_uv_textures) if bpy.app.version < (2, 80):
if has_uv: has_uv = bool(mesh.tessface_uv_textures)
active_uv_layer = mesh.tessface_uv_textures.active if has_uv:
if not active_uv_layer: active_uv_layer = mesh.tessface_uv_textures.active
has_uv = False if not active_uv_layer:
else: has_uv = False
active_uv_layer = active_uv_layer.data else:
active_uv_layer = active_uv_layer.data
else:
has_uv = bool(mesh.uv_layers)
if has_uv:
active_uv_layer = mesh.uv_layers.active
if not active_uv_layer:
has_uv = False
else:
active_uv_layer = active_uv_layer.data
geomesh = GeoMesh() geomesh = GeoMesh()
texture_name = "white" texture_name = "white.tga"
print("len(mesh.tessfaces): %s" % len(mesh.tessfaces)) if bpy.app.version < (2, 80):
print("mesh.tessfaces: %s" % repr(mesh.tessfaces)) faces = mesh.tessfaces
for i, f in enumerate(mesh.tessfaces): print("len(mesh.tessfaces): %s" % len(faces))
print("mesh.tessfaces: %s" % repr(faces))
else:
faces = mesh.loop_triangles
for i, f in enumerate(faces):
if has_uv: if has_uv:
uv = active_uv_layer[i] uv = active_uv_layer[i]
texture_image = uv.image if bpy.app.version < (2, 80):
uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4] texture_image = uv.image
if texture_image is None: if texture_image is None:
texture_name = "white" texture_name = "white.tga"
else:
texture_name = texture_image.name
if texture_name == "":
texture_name = bpy.path.display_name_from_filepath(texture_image.filepath)
if texture_name == "":
texture_name = "white.tga"
else:
mat = mesh.materials[f.material_index]
if len(mat.texture_paint_images) <= 0:
texture_name = mat.name
else:
texture_image = mat.texture_paint_images[0]
texture_name = texture_image.name
if texture_name == "":
texture_name = bpy.path.display_name_from_filepath(texture_image.filepath)
if texture_name == "":
texture_name = "white.tga"
if bpy.app.version < (2, 80):
uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4]
else: else:
texture_name = texture_image.name print("uv.uv: %s" % uv.uv)
if texture_name == "": uv = [active_uv_layer[l].uv[:] for l in f.loops]
texture_name = bpy.path.display_name_from_filepath(texture_image.filepath)
if texture_name == "":
texture_name = "white"
else: else:
uv = [(0, 0)] * 4 uv = [(0, 0)] * 4
texture_name = "white" texture_name = "white.tga"
f_verts = f.vertices f_verts = f.vertices
verts = [] verts = []
norms = [] norms = []
@ -55,6 +89,7 @@ def convert_mesh(geo_model, mesh, obj):
group = obj.vertex_groups[weight.group] group = obj.vertex_groups[weight.group]
w = [group.name, weight.weight] w = [group.name, weight.weight]
weights.append(w) weights.append(w)
print("i: %s f_verts: %s uv: %s" % (i, repr(f_verts), repr(uv)))
gv = GeoVertex(v.co, v.normal, uv[i], weights) gv = GeoVertex(v.co, v.normal, uv[i], weights)
geoverts.append(gv) geoverts.append(gv)
geomesh.addFace(geoverts, texture_name) geomesh.addFace(geoverts, texture_name)
@ -90,7 +125,10 @@ def save(operator, context, scale = 1.0, filepath = "", global_matrix = None, us
global_matrix = Matrix() global_matrix = Matrix()
# get the modifiers # get the modifiers
mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW") if bpy.app.version < (2, 80):
mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW")
else:
mesh = ob.to_mesh(preserve_all_data_layers = True)
#translate_matrix = Matrix.Translation(-ob.location) #translate_matrix = Matrix.Translation(-ob.location)
translate_matrix = Matrix() translate_matrix = Matrix()
@ -101,7 +139,10 @@ def save(operator, context, scale = 1.0, filepath = "", global_matrix = None, us
print("obj_scale: %s final_scale: %s" % (obj_scale, obj_scale * scale)) print("obj_scale: %s final_scale: %s" % (obj_scale, obj_scale * scale))
print(obj_scale * scale) print(obj_scale * scale)
scale_matrix = Matrix.Scale(obj_scale * scale, 4) scale_matrix = Matrix.Scale(obj_scale * scale, 4)
mesh.transform(global_matrix * scale_matrix * translate_matrix * axis_rotation) if bpy.app.version < (2, 80):
mesh.transform(global_matrix * scale_matrix * translate_matrix * axis_rotation)
else:
mesh.transform(global_matrix @ scale_matrix @ translate_matrix @ axis_rotation)
mesh.calc_normals() mesh.calc_normals()

@ -1 +1,33 @@
def convert_model(geo_model, mesh, obj):
#Create vertices
#todo: populate coordinates
#todo: populate normals
#todo: create edges
#todo: create faces
#todo: populate uvs
mesh.validate()
mesh.update()
#todo: attempt to load textures/images
def load(operator, context, scale = 1.0, filepath = "", global_matrix = None, use_mesh_modifiers = True):
#load .geo
fh_in = open(filepath, "rb")
geo = Geo()
geo.loadFromFile(fh_in)
fh_in.close()
for geo_model in geo.models:
#create object matching model's name (or next equivilant)
mesh = bpy.data.meshes.new(name = model.name.decode("utf-8"))
#convert model to mesh
convert_mode(geo_model, mesh, None)
#Create object for this mesh
scn = bpy.context.scene
obj = bpy.data.objects.new(ply_name, mesh)
scn.objects.link(obj)
scn.objects.active = obj
obj.select = True
pass
pass

Loading…
Cancel
Save