fix loading and bm import/export error

This commit is contained in:
yyc12345 2022-04-04 11:30:04 +08:00
parent e264c85a04
commit c40f956771
6 changed files with 42 additions and 40 deletions

View File

@ -19,8 +19,8 @@ class BALLANCE_OT_export_bm(bpy.types.Operator, bpy_extras.io_utils.ExportHelper
)
def execute(self, context):
if (self.export_mode == 'COLLECTION' and context.scene.BallanceBlenderPluginProperty.collection_picker is None) or
(self.export_mode == 'OBJECT' and context.scene.BallanceBlenderPluginProperty.object_picker is None):
if ((self.export_mode == 'COLLECTION' and context.scene.BallanceBlenderPluginProperty.collection_picker is None) or
(self.export_mode == 'OBJECT' and context.scene.BallanceBlenderPluginProperty.object_picker is None)):
UTILS_functions.show_message_box(("No specific target", ), "Lost parameter", 'ERROR')
else:
prefs = bpy.context.preferences.addons[__package__].preferences
@ -47,7 +47,7 @@ class BALLANCE_OT_export_bm(bpy.types.Operator, bpy_extras.io_utils.ExportHelper
def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTarget):
# ============================================ alloc a temp folder
utils_tempFolderObj = tempfile.TemporaryDirectory()
utils_tempFolder = tempFolderObj.name
utils_tempFolder = utils_tempFolderObj.name
utils_tempTextureFolder = os.path.join(utils_tempFolder, "Texture")
os.makedirs(utils_tempTextureFolder)
@ -69,7 +69,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar
# ============================================ export
with open(os.path.join(utils_tempFolder, "index.bm"), "wb") as finfo:
UTILS_file_io.write_uint32(finfo, bm_current_version)
UTILS_file_io.write_uint32(finfo, UTILS_constants.bmfile_currentVersion)
# ====================== export object
meshSet = set()
@ -92,7 +92,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar
object_isComponent = False
else:
# check isComponent normally
object_isComponent = is_component(obj.name)
object_isComponent = UTILS_functions.is_component(obj.name)
# triangle first and then group
if not object_isComponent:
@ -105,7 +105,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar
else:
object_meshIndex = meshList.index(object_blenderMesh)
else:
object_meshIndex = get_component_id(obj.name)
object_meshIndex = UTILS_functions.get_component_id(obj.name)
# get visibility
object_isHidden = not obj.visible_get()
@ -124,7 +124,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar
# write fobject
UTILS_file_io.write_bool(fobject, object_isComponent)
UTILS_file_io.write_bool(fobject, object_isHidden)
UTILS_file_io.write_worldMatrix(fobject, obj.matrix_world)
UTILS_file_io.write_world_matrix(fobject, obj.matrix_world)
UTILS_file_io.write_uint32(fobject, len(object_groupList))
for item in object_groupList:
UTILS_file_io.write_string(fobject, item)
@ -337,7 +337,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar
# blender related functions
def _is_external_texture(name):
if name in config.external_texture_list:
if name in UTILS_constants.bmfile_externalTextureSet:
return True
else:
return False

View File

@ -88,7 +88,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
return
# collect block header data
while len(peek_stream(findex)) != 0:
while len(UTILS_file_io.peek_stream(findex)) != 0:
# read
index_name = UTILS_file_io.read_string(findex)
index_type = UTILS_file_io.read_uint8(findex)
@ -120,7 +120,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
if texture_isExternal:
(texture_target, skip_init) = UTILS_functions.create_instance_with_option(
UTILS_constants.BmfileInfoType.TEXTURE, item.name, opts_texture,
extra_texture_path= texture_filename, extra_texture_path= prefs_externalTexture)
extra_texture_filename= texture_filename, extra_texture_path= prefs_externalTexture)
else:
# not external. copy temp file into blender temp. then use it.
# try copy. if fail, don't need to do more
@ -132,7 +132,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
(texture_target, skip_init) = UTILS_functions.create_instance_with_option(
UTILS_constants.BmfileInfoType.TEXTURE, item.name, opts_texture,
extra_texture_path= texture_filename, extra_texture_path= prefs_tempTextureFolder)
extra_texture_filename= texture_filename, extra_texture_path= prefs_tempTextureFolder)
# setup name and blender data for header
item.blender_data = texture_target
@ -154,7 +154,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
material_texture = UTILS_file_io.read_uint32(fmaterial)
# alloc basic material
(material_target, skip_init) = create_instance_with_option(
(material_target, skip_init) = UTILS_functions.create_instance_with_option(
UTILS_constants.BmfileInfoType.MATERIAL, item.name, opts_material)
item.blender_data = material_target
if skip_init:
@ -178,7 +178,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
fmesh.seek(item.offset, os.SEEK_SET)
# create real mesh
(mesh_target, skip_init) = create_instance_with_option(
(mesh_target, skip_init) = UTILS_functions.create_instance_with_option(
UTILS_constants.BmfileInfoType.MESH, item.name, opts_mesh)
item.blender_data = mesh_target
if skip_init:
@ -215,7 +215,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
if mesh_useMaterial:
mesh_neededMaterial = materialList[mesh_materialIndex].blender_data
if mesh_neededMaterial in mesh_materialSolt:
mesh_blenderMtlIndex = materialSolt.index(mesh_neededMaterial)
mesh_blenderMtlIndex = mesh_materialSolt.index(mesh_neededMaterial)
else:
mesh_blenderMtlIndex = len(mesh_materialSolt)
mesh_materialSolt.append(mesh_neededMaterial)
@ -235,22 +235,22 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
mesh_target.materials.append(mat)
# then, we need add correspond count for vertices
mesh_target.vertices.add(len(vList))
mesh_target.loops.add(len(faceList)*3) # triangle face confirm
mesh_target.polygons.add(len(faceList))
mesh_target.vertices.add(len(mesh_vList))
mesh_target.loops.add(len(mesh_faceList)*3) # triangle face confirm
mesh_target.polygons.add(len(mesh_faceList))
mesh_target.uv_layers.new(do_init=False)
mesh_target.create_normals_split()
# add vertices data
mesh_target.vertices.foreach_set("co", unpack_list(vList))
mesh_target.vertices.foreach_set("co", unpack_list(mesh_vList))
mesh_target.loops.foreach_set("vertex_index", unpack_list(_flat_vertices_index(mesh_faceList)))
mesh_target.loops.foreach_set("normal", unpack_list(_flat_vertices_normal(mesh_faceList, mesh_vnList)))
mesh_target.uv_layers[0].data.foreach_set("uv", unpack_list(_flat_vertices_uv(mesh_faceList, mesh_vtList)))
for i in range(len(faceList)):
for i in range(len(mesh_faceList)):
mesh_target.polygons[i].loop_start = i * 3
mesh_target.polygons[i].loop_total = 3
if faceList[i][9] != -1:
mesh_target.polygons[i].material_index = faceList[i][9]
if mesh_faceList[i][9] != -1:
mesh_target.polygons[i].material_index = mesh_faceList[i][9]
mesh_target.polygons[i].use_smooth = True
@ -285,7 +285,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
object_isComponent = UTILS_file_io.read_bool(fobject)
#object_isForcedNoComponent = UTILS_file_io.read_bool(fobject)
object_isHidden = UTILS_file_io.read_bool(fobject)
object_worldMatrix = UTILS_file_io.read_worldMaterix(fobject)
object_worldMatrix = UTILS_file_io.read_world_materix(fobject)
object_groupListCount = UTILS_file_io.read_uint32(fobject)
object_groupList.clear()
for i in range(object_groupListCount):
@ -299,15 +299,15 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te
object_neededMesh = meshList[object_meshIndex].blender_data
# create real object
(object_target, skip_init) = create_instance_with_option(
(object_target, skip_init) = UTILS_functions.create_instance_with_option(
UTILS_constants.BmfileInfoType.OBJECT, item.name, opts_object,
extraMesh=object_neededMesh)
extra_mesh=object_neededMesh)
if skip_init:
continue
# link to correct collection
if (object_fncgCollection is not None) and (not object_isComponent) and UTILS_functions.is_component(item.name):
# a object should be grouped into fncg should fufill following requirements
# a object should be grouped into fncg should check following requirements
# fncg is not null
# this object is a normal object
# but its name match component format

View File

@ -12,7 +12,7 @@ class BALLANCE_OT_add_components(bpy.types.Operator):
elements_type: bpy.props.EnumProperty(
name="Type",
description="This element type",
items=tuple(map(lambda x: (x, x, ""), UTILS_constants.componentList)),
items=tuple(map(lambda x: (x, x, ""), UTILS_constants.bmfile_componentList)),
)
attentionElements = ["PC_TwoFlames", "PR_Resetpoint"]
@ -37,7 +37,7 @@ class BALLANCE_OT_add_components(bpy.types.Operator):
# create object
loadedMesh = UTILS_functions.load_component(
UTILS_constants.componentList.index(self.elements_type))
UTILS_constants.bmfile_componentList.index(self.elements_type))
obj = bpy.data.objects.new(finalObjectName, loadedMesh)
UTILS_functions.add_into_scene_and_move_to_cursor(obj)

View File

@ -1,5 +1,5 @@
import bpy, bmesh, bpy_extras, mathutils
import struct,shutil
import struct, shutil, os
# writer
@ -26,7 +26,7 @@ def write_bool(fs,boolean):
def write_float(fs,fl):
fs.write(struct.pack("f", fl))
def write_worldMatrix(fs, matt):
def write_world_matrix(fs, matt):
mat = matt.transposed()
fs.write(struct.pack("ffffffffffffffff",
mat[0][0],mat[0][2], mat[0][1], mat[0][3],

View File

@ -1,6 +1,7 @@
import bpy, bmesh, bpy_extras, mathutils
import struct, shutil
import struct, shutil, os
from bpy_extras.io_utils import unpack_list
from bpy_extras.image_utils import load_image
from bpy_extras import io_utils, node_shader_utils
from . import UTILS_file_io, UTILS_constants
@ -177,20 +178,21 @@ def create_instance_with_option(instance_type, instance_name, instance_opt,
temp_is_existed = True
except:
temp_instance = None
temp_is_existed = False
return (temp_instance, temp_is_existed)
def create_instance():
if instType == UTILS_constants.BmfileInfoType.OBJECT:
if instance_type == UTILS_constants.BmfileInfoType.OBJECT:
instance_obj = bpy.data.objects.new(instance_name, extra_mesh)
instance_obj.name = instance_name
elif instType == UTILS_constants.BmfileInfoType.MESH:
#instance_obj.name = instance_name
elif instance_type == UTILS_constants.BmfileInfoType.MESH:
instance_obj = bpy.data.meshes.new(instance_name)
instance_obj.name = instance_name
elif instType == UTILS_constants.BmfileInfoType.MATERIAL:
#instance_obj.name = instance_name
elif instance_type == UTILS_constants.BmfileInfoType.MATERIAL:
instance_obj = bpy.data.materials.new(instance_name)
instance_obj.name = instance_name
#instance_obj.name = instance_name
elif instance_type == UTILS_constants.BmfileInfoType.TEXTURE:
# this command will also check current available texture
# because `get_instance()` only check texture name
@ -206,7 +208,7 @@ def create_instance_with_option(instance_type, instance_name, instance_opt,
# create new instance
# or always create new instance if opts is not string
temp_instance = create_instance()
temp_skip_init = True
temp_skip_init = False
elif instance_opt == 'CURRENT':
# try get instance
(temp_instance, temp_is_existed) = get_instance()

View File

@ -82,7 +82,7 @@ class BALLANCE_MT_AddFloorMenu(bpy.types.Menu):
layout = self.layout
layout.label(text="Basic floor")
for item in UTILS_constants.floor_basicBlock_list:
for item in UTILS_constants.floor_basicBlockList:
cop = layout.operator(
OBJS_add_floors.BALLANCE_OT_add_floors.bl_idname,
text=item, icon_value = UTILS_constants.icons_floorDict[item])