use with syntax to save file and fix no uv bug
This commit is contained in:
parent
40756b0178
commit
41d3c5f3b7
@ -64,7 +64,11 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
zipObj.extractall(tempFolder)
|
zipObj.extractall(tempFolder)
|
||||||
|
|
||||||
# index.bm
|
# index.bm
|
||||||
findex = open(os.path.join(tempFolder, "index.bm"), "rb")
|
objectList = []
|
||||||
|
meshList = []
|
||||||
|
materialList = []
|
||||||
|
textureList = []
|
||||||
|
with open(os.path.join(tempFolder, "index.bm"), "rb") as findex:
|
||||||
# judge version first
|
# judge version first
|
||||||
gotten_version = read_uint32(findex)
|
gotten_version = read_uint32(findex)
|
||||||
if (gotten_version != bm_current_version):
|
if (gotten_version != bm_current_version):
|
||||||
@ -72,10 +76,7 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
findex.close()
|
findex.close()
|
||||||
tempFolderObj.cleanup()
|
tempFolderObj.cleanup()
|
||||||
return
|
return
|
||||||
objectList = []
|
|
||||||
meshList = []
|
|
||||||
materialList = []
|
|
||||||
textureList = []
|
|
||||||
while len(peek_stream(findex)) != 0:
|
while len(peek_stream(findex)) != 0:
|
||||||
index_name = read_string(findex)
|
index_name = read_string(findex)
|
||||||
index_type = read_uint8(findex)
|
index_type = read_uint8(findex)
|
||||||
@ -91,10 +92,10 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
textureList.append(blockCache)
|
textureList.append(blockCache)
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
findex.close()
|
|
||||||
|
|
||||||
# texture.bm
|
# texture.bm
|
||||||
ftexture = open(os.path.join(tempFolder, "texture.bm"), "rb")
|
with open(os.path.join(tempFolder, "texture.bm"), "rb") as ftexture:
|
||||||
for item in textureList:
|
for item in textureList:
|
||||||
ftexture.seek(item.offset, os.SEEK_SET)
|
ftexture.seek(item.offset, os.SEEK_SET)
|
||||||
texture_filename = read_string(ftexture)
|
texture_filename = read_string(ftexture)
|
||||||
@ -113,10 +114,8 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
item.blenderData = txur
|
item.blenderData = txur
|
||||||
txur.name = item.name
|
txur.name = item.name
|
||||||
|
|
||||||
ftexture.close()
|
|
||||||
|
|
||||||
# material.bm
|
# material.bm
|
||||||
fmaterial = open(os.path.join(tempFolder, "material.bm"), "rb")
|
with open(os.path.join(tempFolder, "material.bm"), "rb") as fmaterial:
|
||||||
for item in materialList:
|
for item in materialList:
|
||||||
fmaterial.seek(item.offset, os.SEEK_SET)
|
fmaterial.seek(item.offset, os.SEEK_SET)
|
||||||
|
|
||||||
@ -158,10 +157,9 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
|
|
||||||
item.blenderData = m
|
item.blenderData = m
|
||||||
|
|
||||||
fmaterial.close()
|
|
||||||
|
|
||||||
# mesh.bm
|
# mesh.bm
|
||||||
fmesh = open(os.path.join(tempFolder, "mesh.bm"), "rb")
|
with open(os.path.join(tempFolder, "mesh.bm"), "rb") as fmesh:
|
||||||
vList=[]
|
vList=[]
|
||||||
vtList=[]
|
vtList=[]
|
||||||
vnList=[]
|
vnList=[]
|
||||||
@ -249,10 +247,9 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
# add into item using
|
# add into item using
|
||||||
item.blenderData = mesh
|
item.blenderData = mesh
|
||||||
|
|
||||||
fmesh.close()
|
|
||||||
|
|
||||||
# object
|
# object
|
||||||
fobject = open(os.path.join(tempFolder, "object.bm"), "rb")
|
with open(os.path.join(tempFolder, "object.bm"), "rb") as fobject:
|
||||||
|
|
||||||
# we need get needed collection first
|
# we need get needed collection first
|
||||||
view_layer = context.view_layer
|
view_layer = context.view_layer
|
||||||
@ -292,7 +289,6 @@ def import_bm(context,filepath,externalTexture,blenderTempFolder):
|
|||||||
obj.matrix_world = object_worldMatrix
|
obj.matrix_world = object_worldMatrix
|
||||||
obj.hide_set(object_isHidden)
|
obj.hide_set(object_isHidden)
|
||||||
|
|
||||||
fobject.close()
|
|
||||||
view_layer.update()
|
view_layer.update()
|
||||||
|
|
||||||
tempFolderObj.cleanup()
|
tempFolderObj.cleanup()
|
||||||
@ -320,14 +316,14 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
forcedCollection = None
|
forcedCollection = None
|
||||||
|
|
||||||
# ============================================ export
|
# ============================================ export
|
||||||
finfo = open(os.path.join(tempFolder, "index.bm"), "wb")
|
with open(os.path.join(tempFolder, "index.bm"), "wb") as finfo:
|
||||||
write_uint32(finfo, bm_current_version)
|
write_uint32(finfo, bm_current_version)
|
||||||
|
|
||||||
# ====================== export object
|
# ====================== export object
|
||||||
fobject = open(os.path.join(tempFolder, "object.bm"), "wb")
|
|
||||||
meshSet = set()
|
meshSet = set()
|
||||||
meshList = []
|
meshList = []
|
||||||
meshCount = 0
|
meshCount = 0
|
||||||
|
with open(os.path.join(tempFolder, "object.bm"), "wb") as fobject:
|
||||||
for obj in objectList:
|
for obj in objectList:
|
||||||
# only export mesh object
|
# only export mesh object
|
||||||
if obj.type != 'MESH':
|
if obj.type != 'MESH':
|
||||||
@ -375,12 +371,10 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
write_worldMatrix(fobject, obj.matrix_world)
|
write_worldMatrix(fobject, obj.matrix_world)
|
||||||
write_uint32(fobject, meshId)
|
write_uint32(fobject, meshId)
|
||||||
|
|
||||||
fobject.close()
|
|
||||||
|
|
||||||
# ====================== export mesh
|
# ====================== export mesh
|
||||||
fmesh = open(os.path.join(tempFolder, "mesh.bm"), "wb")
|
|
||||||
materialSet = set()
|
materialSet = set()
|
||||||
materialList = []
|
materialList = []
|
||||||
|
with open(os.path.join(tempFolder, "mesh.bm"), "wb") as fmesh:
|
||||||
for mesh in meshList:
|
for mesh in meshList:
|
||||||
mesh.calc_normals_split()
|
mesh.calc_normals_split()
|
||||||
|
|
||||||
@ -399,8 +393,9 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
|
|
||||||
# uv
|
# uv
|
||||||
face_index_pairs = [(face, index) for index, face in enumerate(mesh.polygons)]
|
face_index_pairs = [(face, index) for index, face in enumerate(mesh.polygons)]
|
||||||
uv_layer = mesh.uv_layers.active.data[:]
|
|
||||||
write_uint32(fmesh, len(face_index_pairs) * 3)
|
write_uint32(fmesh, len(face_index_pairs) * 3)
|
||||||
|
if mesh.uv_layers.active is not None:
|
||||||
|
uv_layer = mesh.uv_layers.active.data[:]
|
||||||
for f, f_index in face_index_pairs:
|
for f, f_index in face_index_pairs:
|
||||||
# it should be triangle face, otherwise throw a error
|
# it should be triangle face, otherwise throw a error
|
||||||
if (f.loop_total != 3):
|
if (f.loop_total != 3):
|
||||||
@ -410,6 +405,10 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
uv = uv_layer[loop_index].uv
|
uv = uv_layer[loop_index].uv
|
||||||
# reverse v
|
# reverse v
|
||||||
write_2vector(fmesh, uv[0], -uv[1])
|
write_2vector(fmesh, uv[0], -uv[1])
|
||||||
|
else:
|
||||||
|
# no uv data. write garbage
|
||||||
|
for i in range(len(face_index_pairs) * 3):
|
||||||
|
write_2vector(fmesh, 0.0, 0.0)
|
||||||
|
|
||||||
# normals
|
# normals
|
||||||
write_uint32(fmesh, len(face_index_pairs) * 3)
|
write_uint32(fmesh, len(face_index_pairs) * 3)
|
||||||
@ -463,13 +462,11 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
|
|
||||||
mesh.free_normals_split()
|
mesh.free_normals_split()
|
||||||
|
|
||||||
fmesh.close()
|
|
||||||
|
|
||||||
# ====================== export material
|
# ====================== export material
|
||||||
fmaterial = open(os.path.join(tempFolder, "material.bm"), "wb")
|
|
||||||
textureSet = set()
|
textureSet = set()
|
||||||
textureList = []
|
textureList = []
|
||||||
textureCount = 0
|
textureCount = 0
|
||||||
|
with open(os.path.join(tempFolder, "material.bm"), "wb") as fmaterial:
|
||||||
for material in materialList:
|
for material in materialList:
|
||||||
# write finfo first
|
# write finfo first
|
||||||
write_string(finfo, material.name)
|
write_string(finfo, material.name)
|
||||||
@ -540,13 +537,11 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
write_bool(fmaterial, material_useTexture)
|
write_bool(fmaterial, material_useTexture)
|
||||||
write_uint32(fmaterial, material_texture)
|
write_uint32(fmaterial, material_texture)
|
||||||
|
|
||||||
fmaterial.close()
|
|
||||||
|
|
||||||
# ====================== export texture
|
# ====================== export texture
|
||||||
ftexture = open(os.path.join(tempFolder, "texture.bm"), "wb")
|
|
||||||
source_dir = os.path.dirname(bpy.data.filepath)
|
source_dir = os.path.dirname(bpy.data.filepath)
|
||||||
existed_texture = set()
|
existed_texture = set()
|
||||||
|
with open(os.path.join(tempFolder, "texture.bm"), "wb") as ftexture:
|
||||||
for texture in textureList:
|
for texture in textureList:
|
||||||
# write finfo first
|
# write finfo first
|
||||||
write_string(finfo, texture.name)
|
write_string(finfo, texture.name)
|
||||||
@ -567,10 +562,6 @@ def export_bm(context,filepath,export_mode, export_target):
|
|||||||
shutil.copy(texture_filepath, os.path.join(tempTextureFolder, filename))
|
shutil.copy(texture_filepath, os.path.join(tempTextureFolder, filename))
|
||||||
existed_texture.add(filename)
|
existed_texture.add(filename)
|
||||||
|
|
||||||
ftexture.close()
|
|
||||||
|
|
||||||
# close info fs
|
|
||||||
finfo.close()
|
|
||||||
|
|
||||||
# ============================================ save zip and clean up folder
|
# ============================================ save zip and clean up folder
|
||||||
if os.path.isfile(filepath):
|
if os.path.isfile(filepath):
|
||||||
|
Loading…
Reference in New Issue
Block a user