Blender中使用python脚本

文章详细介绍了如何在Blender中复制网格、添加材质,以及进行点线面分离、面法线翻转,还展示了如何创建指定大小的地形平面并处理PFM格式的纹理数据,包括导入和导出过程。
摘要由CSDN通过智能技术生成

 一.创建形变动画morph_key

import bpy
import math
 
objects = bpy.context.scene.objects
mesh_name = 'p51_propeller'
morph_key_cnt = 13
 
for morph_key_index in range(morph_key_cnt):
    blender_obj = objects[mesh_name].copy()
    blender_obj.data = objects[mesh_name].data.copy()
    blender_obj.data.materials[0] = objects[mesh_name].data.materials[0].copy()
    blender_obj.rotation_euler = objects[mesh_name].rotation_euler.copy()
    
    if morph_key_index == 0:
        blender_obj.name = mesh_name + '_morph_key_base'
    else:
        blender_obj.name = mesh_name + '_morph_key_' + str(morph_key_index)
    blender_obj_rotation = (0, 2 * morph_key_index * math.pi / (morph_key_cnt -1), 0)
    blender_obj.rotation_euler = blender_obj_rotation
    
    bpy.context.collection.objects.link(blender_obj)
 
for obj in objects:
     if obj.type == 'MESH':
        mesh_name_x = obj.name + '_mesh'
        obj.data.name = mesh_name_x
        material_name = obj.name + '_mat'
        obj.data.materials[0].name = material_name

 二.获取点线面并分离为独立物体

import bpy
import math

#polygon format [polygon_index]
polygon_start_index = 5;
 
objects = bpy.context.scene.objects
mesh = objects['Cube'].data
points = mesh.vertices
faces = mesh.polygons
 
vert_coords = []
polygon_faces = []
polygon_face = []
 
for point in points:
    vert_coords.append((point.co.x, point.co.y, point.co.z))
 
for face in faces:
    name = 'polygon_' + str(polygon_start_index)
    mymesh = bpy.data.meshes.new(name)
    myobject = bpy.data.objects.new(name, mymesh)  
    bpy.context.collection.objects.link(myobject)
    
    for vertice_index in face.vertices:
        polygon_face.append(vertice_index)
        
    polygon_faces.append(polygon_face)
    mymesh.from_pydata(vert_coords, [], polygon_faces)
    polygon_start_index = polygon_start_index + 1
    polygon_faces = []
    polygon_face = []

三.翻转面法线

import bpy
import math
 
objects = bpy.context.scene.objects
for index in range(6, 12):
    target_name = 'polygon_' + str(index)
    objects[target_name].select_set(True)
    faces = objects[target_name].data.polygons
    for face in faces:
        face.flip()

四.创建指定长宽的地形平面高度网格

import bpy
import math

#terrain params 
terrain_x_size = 946
terrain_y_size = 938
polygon_size = 5
blender_unit_size = 0.1

#calculate terrain_x_node_count, terrain_y_node_count
terrain_x_node_count = (int)(terrain_x_size / polygon_size)
terrain_y_node_count = (int)(terrain_y_size / polygon_size)

#calculate terrain_min_x terrain_min_y
terrain_min_x = terrain_x_node_count * blender_unit_size / -2
terrain_min_y = terrain_x_node_count * blender_unit_size / -2

#fill terrain verts
terrain_verts = []

for y in range(0, terrain_y_node_count + 1):
    for x in range(0, terrain_x_node_count + 1):
        coord_x = x * blender_unit_size + terrain_min_x
        coord_y = y * blender_unit_size + terrain_min_y
        coord_z = 0
        terrain_verts.append((coord_x, coord_y, coord_z))

#fill terrain faces
terrain_faces = []
for y in range(0, terrain_y_node_count):
    for x in range(0, terrain_x_node_count):
        face_index_1 = y * (terrain_x_node_count + 1) + x
        face_index_2 = face_index_1 + 1
        face_index_3 = face_index_2 + terrain_x_node_count + 1
        face_index_4 = face_index_3 - 1
        terrain_faces.append((face_index_1, face_index_2, face_index_3, face_index_4))

#create terrain plane      
name = 'terrain_plane'
mymesh = bpy.data.meshes.new(name)
myobject = bpy.data.objects.new(name, mymesh)  
bpy.context.collection.objects.link(myobject)
mymesh.from_pydata(terrain_verts, [], terrain_faces)
myobject["terrain_x_size"] = terrain_x_size
myobject["terrain_y_size"] = terrain_y_size
myobject["polygon_size"] = polygon_size
myobject["blender_unit_size"] = blender_unit_size

五.导入PFM格式文件

import bpy
import math
import numpy as np
import struct

#terrain params 
terrain_x_size = 234
terrain_y_size = 233
polygon_size = 3
blender_unit_size = 0.1

#pfm height_map[height][width]
def read_pfm(file_name):
    global terrain_x_node_count
    global terrain_y_node_count
    with open(file_name, "rb") as file:
        header = file.readline().decode('utf-8').rstrip()
        if header != 'Pf':
            raise Exception('Not a pfm file.')
        width, height = map(int, file.readline().decode('utf-8').rstrip().split())
        scale = float(file.readline().decode('utf-8').rstrip())
        data = np.fromfile(file, np.float32)
        shape = (height, width,  1)
        data = np.reshape(data, shape)
        return data

#read pfm
height_map = read_pfm("D:\layer_ground_elevation.pfm")
terrain_x_node_count = len(height_map[0]) - 1
terrain_y_node_count = len(height_map) - 1
terrain_min_x = terrain_x_node_count * blender_unit_size / -2
terrain_min_y = terrain_x_node_count * blender_unit_size / -2

#fill terrain verts
terrain_verts = []

for y in range(0, terrain_y_node_count + 1):
    for x in range(0, terrain_x_node_count + 1):
        coord_x = x * blender_unit_size + terrain_min_x
        coord_y = y * blender_unit_size + terrain_min_y
        coord_z = height_map[y][x] * blender_unit_size
        terrain_verts.append((coord_x, coord_y, coord_z))

#fill terrain faces
terrain_faces = []
for y in range(0, terrain_y_node_count):
    for x in range(0, terrain_x_node_count):
        face_index_1 = y * (terrain_x_node_count + 1) + x
        face_index_2 = face_index_1 + 1
        face_index_3 = face_index_2 + terrain_x_node_count + 1
        face_index_4 = face_index_3 - 1
        terrain_faces.append((face_index_1, face_index_2, face_index_3, face_index_4))

#create terrain plane      
name = 'terrain_plane'
mymesh = bpy.data.meshes.new(name)
myobject = bpy.data.objects.new(name, mymesh)  
bpy.context.collection.objects.link(myobject)
mymesh.from_pydata(terrain_verts, [], terrain_faces)
myobject["terrain_x_size"] = terrain_x_size
myobject["terrain_y_size"] = terrain_y_size
myobject["polygon_size"] = polygon_size
myobject["blender_unit_size"] = blender_unit_size

六.导出PFM格式文件

import bpy
import math
import numpy as np
import struct

#pfm height_map[terrain_y_node_count][terrain_x_node_count]
def write_pfm(file, height_map):
    file = open(file, 'wb')
    terrain_x_node_count = len(height_map[0])
    terrain_y_node_count = len(height_map)
    file.write('Pf\n'.encode('ascii'))
    file.write('%d %d\n'.encode('ascii') % (terrain_x_node_count, terrain_y_node_count))
    file.write('%.3f\n'.encode('ascii') % (-1))
 
    for row in height_map:
        for pixel in row:
            file.write(struct.pack('f', pixel))
    file.close()
        
height_map = []

objects = bpy.context.scene.objects
mesh = objects['terrain_plane'].data
points = mesh.vertices

terrain_x_size = int(objects['terrain_plane']["terrain_x_size"])
terrain_y_size = int(objects['terrain_plane']["terrain_y_size"])
polygon_size = int(objects['terrain_plane']["polygon_size"])
blender_unit_size = objects['terrain_plane']["blender_unit_size"]

#calculate terrain_x_node_count, terrain_y_node_count
terrain_x_node_count = (int)(terrain_x_size / polygon_size)
terrain_y_node_count = (int)(terrain_y_size / polygon_size)


#sort vertex x,y
points_array = []
for vertex in points:
    points_array.append((vertex.co.x, vertex.co.y, vertex.co.z))
points_array.sort(key=lambda x: (x[1], x[0]))

for y in range(0, terrain_y_node_count + 1):
    row = []
    for x in range(0, terrain_x_node_count + 1):
        vertex_index = y * (terrain_x_node_count + 1) + x
        coord_z = points_array[vertex_index][2] / blender_unit_size
        row.append(coord_z)
    height_map.append(row)

write_pfm('D:\layer_ground_elevation.pfm', height_map)

七.导入TRF文件

import bpy

trf_meshes = []

trf_contents = []

trf_import_path = 'D:\pt_ladder.trf'

trf_export_path = 'D:\pt_ladder_morph_keys.trf'

class TrfMesh:
    def __init__(self):
        self.mesh_name = ''
        self.mesh_materials = []
        self.vertex_cnt = 0
        self.vertex_fvf_cnt = 0
        self.morph_key_cnt = 0
        self.bone_cnt = 0
        self.face_cnt = 0
        self.verts = []
        self.vert_fvfs = []
        self.face_vert_fvf_indices = []
        self.face_vert_indices = []
        self.morph_keys = []
        self.bones = []
        self.bone_weights = []
        
class TrfVert:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0
        
class TrfVertFvf:
    def __init__(self):
        self.vert_index = 0
        self.normal_x = 0
        self.normal_y = 0
        self.normal_z = 0
        self.uv_x = 0
        self.uv_y = 0

class TrfFaceVertFvfIndex:
    def __init__(self):
        self.vert_fvf_index_1 = 0
        self.vert_fvf_index_2 = 0
        self.vert_fvf_index_3 = 0
        
class TrfFaceVertIndex:
    def __init__(self):
        self.vert_index_1 = 0
        self.vert_index_2 = 0
        self.vert_index_3 = 0
        
class TrfMorphKey:
    def __init__(self):
        self.morph_key_index = 0
        self.vertex_cnt = 0
        self.verts = []
        self.vertex_fvf_cnt = 0
        self.vert_fvfs = []
        
class TrfBoneWeight:
    def __init__(self):
        self.vertex_fvf_index = 0
        self.bone1_index = 0
        self.bone1_weight = 0
        self.bone2_index = 0
        self.bone2_weight = 0
        


def export_trf_file(file_path):
    trf_contents.append('rfver 4\n')
    trf_contents.append('mesh ' + str(len(trf_meshes)) + '\n') 
    for trf_mesh in trf_meshes:
        write_trf_mesh(trf_mesh)
    
    with open(file_path, 'w', encoding='utf-8') as file:
        file.writelines(trf_contents)
        
def write_trf_mesh(trf_mesh):
    #write mesh header
    space_prefix = '   '
    row_arr = [space_prefix, trf_mesh.mesh_name, '0', trf_mesh.mesh_materials[0], '\n']
    trf_contents.append(' '.join(row_arr))
       
    #write mesh vertex
    space_prefix = space_prefix + '     '
    trf_contents.append(space_prefix + str(trf_mesh.vertex_cnt) + '\n')
    for index in range(0, trf_mesh.vertex_cnt):
        write_trf_mesh_vert(trf_mesh.verts[index])
        
    #write mesh morph keys
    trf_contents.append(space_prefix + str(trf_mesh.morph_key_cnt) + '\n')
    for index in range(0, trf_mesh.morph_key_cnt):
        write_trf_mesh_morph_key(trf_mesh.morph_keys[index])
        
    #write mesh vertex fvf
    trf_contents.append(space_prefix + str(trf_mesh.vertex_fvf_cnt) + '\n')
    for index in range(0, trf_mesh.vertex_fvf_cnt):
        write_trf_mesh_vert_fvf(trf_mesh.vert_fvfs[index])
        
    #write mesh bone
    trf_contents.append(space_prefix + '0' + '\n')
    
    #write mesh face
    trf_contents.append(space_prefix + str(trf_mesh.face_cnt) + '\n')
    for index in range(0, trf_mesh.face_cnt):
        write_trf_mesh_face(trf_mesh.face_vert_fvf_indices[index])
        
    trf_contents.append('end\n')
    
def write_trf_mesh_vert(vert):
    space_prefix = '       '
    row_arr = [space_prefix, format(vert.x, '.6f'), format(vert.y, '.6f'), format(vert.z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_mesh_vert_fvf(vert_fvf):
    space_prefix = '       '
    row_arr = [space_prefix, str(vert_fvf.vert_index), '4294967295', format(vert_fvf.normal_x, '.6f'), format(vert_fvf.normal_y, '.6f'), format(vert_fvf.normal_z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    row_arr = [space_prefix, format(vert_fvf.uv_x, '.6f'), format(vert_fvf.uv_y, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    row_arr = [space_prefix, '0.000000', '0.000000', '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_mesh_face(face):
    space_prefix = '       '
    row_arr = [space_prefix, str(face.vert_fvf_index_1), str(face.vert_fvf_index_2), str(face.vert_fvf_index_3), '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_mesh_morph_key(morph_key):
    space_prefix = '           '
    row_arr = [space_prefix, str(morph_key.morph_key_index), '\n']
    trf_contents.append(' '.join(row_arr))
    
    #morph key verts
    row_arr = [space_prefix, str(morph_key.vertex_cnt), '\n']
    trf_contents.append(' '.join(row_arr))
    for index in range(0, morph_key.vertex_cnt):
        write_trf_morph_key_vert(morph_key.verts[index])
        
    #morph key vert_fvfs
    row_arr = [space_prefix, str(morph_key.vertex_fvf_cnt), '\n']
    trf_contents.append(' '.join(row_arr))
    for index in range(0, morph_key.vertex_fvf_cnt):
        write_trf_morph_key_vert_fvf(morph_key.vert_fvfs[index])
        
def write_trf_morph_key_vert(vert):
    space_prefix = '           '
    row_arr = [space_prefix, format(vert.x, '.6f'), format(vert.y, '.6f'), format(vert.z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_morph_key_vert_fvf(vert_fvf):
    space_prefix = '           '
    row_arr = [space_prefix, format(vert_fvf.normal_x, '.6f'), format(vert_fvf.normal_y, '.6f'), format(vert_fvf.normal_z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    

def import_trf_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return read_trf_file(file)
    
        
def read_trf_file(file):
    trf_header = file.readline()
    trf_meshes_header = file.readline()
    mesh_count = int(trf_meshes_header.split(' ')[1])
    for index in range(0, mesh_count):
        trf_mesh = read_trf_mesh(file)
        trf_meshes.append(trf_mesh)
    return trf_meshes;
        
def read_trf_vert(file):
    trf_vert = TrfVert()
    trf_vert_coords = file.readline().strip().split(' ')
    trf_vert.x = round(float(trf_vert_coords[0]), 6)
    trf_vert.y = round(float(trf_vert_coords[1]), 6)
    trf_vert.z = round(float(trf_vert_coords[2]), 6)
    return trf_vert

def read_trf_morph_key(file):
    trf_morph_key = TrfMorphKey()
    trf_morph_key.morph_key_index = int(file.readline().strip())
    trf_morph_key.vertex_cnt = int(file.readline().strip())
    for index in range(0, trf_morph_key.vertex_cnt):
        trf_vert = read_trf_vert(file)
        trf_morph_key.verts.append(trf_vert)
    trf_morph_key.vertex_fvf_cnt = int(file.readline().strip())
    for index in range(0, trf_morph_key.vertex_fvf_cnt):
        trf_vert_fvf = read_trf_morph_key_vert_fvf(file)
        trf_morph_key.vert_fvfs.append(trf_vert_fvf)
    return trf_morph_key

def read_trf_morph_key_vert_fvf(file):
    trf_vert_fvf = TrfVertFvf()
    trf_vert_fvf_normals = file.readline().strip().split(' ')
    trf_vert_fvf.normal_x = round(float(trf_vert_fvf_normals[0]), 6)
    trf_vert_fvf.normal_y = round(float(trf_vert_fvf_normals[1]), 6)
    trf_vert_fvf.normal_z = round(float(trf_vert_fvf_normals[2]), 6)
    return trf_vert_fvf

def read_trf_mesh_vert_fvf(file):
    trf_vert_fvf = TrfVertFvf()
    trf_vert_fvf_normals = file.readline().strip().split(' ')
    trf_vert_fvf.vert_index = int(trf_vert_fvf_normals[0])
    trf_vert_fvf.normal_x = round(float(trf_vert_fvf_normals[2]), 6)
    trf_vert_fvf.normal_y = round(float(trf_vert_fvf_normals[3]), 6)
    trf_vert_fvf.normal_z = round(float(trf_vert_fvf_normals[4]), 6)
    trf_vert_fvf_uvs = file.readline().strip().split(' ')
    trf_vert_fvf.uv_x = round(float(trf_vert_fvf_uvs[0]), 6)
    trf_vert_fvf.uv_y = round(float(trf_vert_fvf_uvs[1]), 6)
    trf_vert_fvf_unused = file.readline().strip().split(' ')
    return trf_vert_fvf

def read_trf_face_vert_fvf_indices(file):
    trf_face_vert_fvf_index = TrfFaceVertFvfIndex()
    trf_face_vert_fvf_indices = file.readline().strip().split(' ')
    trf_face_vert_fvf_index.vert_fvf_index_1 = int(trf_face_vert_fvf_indices[0])
    trf_face_vert_fvf_index.vert_fvf_index_2 = int(trf_face_vert_fvf_indices[1])
    trf_face_vert_fvf_index.vert_fvf_index_3 = int(trf_face_vert_fvf_indices[2])
    return trf_face_vert_fvf_index
        
def read_trf_mesh(file):
    trf_mesh_header = file.readline().strip()
    #read mesh name&materials
    trf_mesh = TrfMesh()
    trf_mesh.mesh_name = trf_mesh_header.split(' ')[0]

    trf_mesh_materials_cnt = int(trf_mesh_header.split(' ')[1]) + 1
    trf_mesh_materials = []
    for index in range(0, trf_mesh_materials_cnt):
        trf_mesh_materials.append(trf_mesh_header.split(' ')[2 + index])
    trf_mesh.mesh_materials = trf_mesh_materials
    
    #read mesh verts
    trf_mesh.vertex_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.vertex_cnt):
        trf_vert = read_trf_vert(file)
        trf_mesh.verts.append(trf_vert)
        
    #read mesh morph keys
    trf_mesh.morph_key_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.morph_key_cnt):
        trf_morph_key = read_trf_morph_key(file)
        trf_mesh.morph_keys.append(trf_morph_key)
        
    #read mesh fvfs
    trf_mesh.vertex_fvf_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.vertex_fvf_cnt):
        trf_mesh_vert_fvf = read_trf_mesh_vert_fvf(file)
        trf_mesh.vert_fvfs.append(trf_mesh_vert_fvf)
    
    #read mesh bones
    trf_mesh.bone_cnt = int(file.readline().strip())
    if trf_mesh.bone_cnt >0 :
        trf_mesh.bone_index = file.readline().strip()
        trf_mesh.mesh_bone_cnt = int(file.readline().strip())
        for index in range(0, trf_mesh.mesh_bone_cnt):
            trf_mesh.mesh_bone_weight = file.readline().strip()
    
    trf_mesh.face_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.face_cnt):
        trf_face_vert_fvf_index = read_trf_face_vert_fvf_indices(file)
        trf_face_vert_index = TrfFaceVertIndex()
        trf_face_vert_index.vert_index_1 = trf_mesh.vert_fvfs[trf_face_vert_fvf_index.vert_fvf_index_1].vert_index
        trf_face_vert_index.vert_index_2 = trf_mesh.vert_fvfs[trf_face_vert_fvf_index.vert_fvf_index_2].vert_index
        trf_face_vert_index.vert_index_3 = trf_mesh.vert_fvfs[trf_face_vert_fvf_index.vert_fvf_index_3].vert_index
        trf_mesh.face_vert_fvf_indices.append(trf_face_vert_fvf_index)
        trf_mesh.face_vert_indices.append(trf_face_vert_index)
    return trf_mesh


import_trf_file(trf_import_path)

for trf_mesh in trf_meshes:
    print('mesh_name:' + trf_mesh.mesh_name)
    print('mesh_materials:' + trf_mesh.mesh_materials[0])
    print('vertex_count:' + str(trf_mesh.vertex_cnt))
    print('morph_key_count:' + str(trf_mesh.morph_key_cnt))
    print('bone_count:' + str(trf_mesh.bone_cnt))
    print('face_count:' + str(trf_mesh.face_cnt))

print('start building blender object')
for trf_mesh in trf_meshes:
    blender_mesh = bpy.data.meshes.new(trf_mesh.mesh_name)
    for mesh_material in trf_mesh.mesh_materials:
        blender_mesh.materials.append(bpy.data.materials.new(name=mesh_material))
    verts = []
    faces = []
    for trf_vert in trf_mesh.verts:
        verts.append((trf_vert.x, trf_vert.y, trf_vert.z))
    for trf_face_vert_index in trf_mesh.face_vert_indices:
        faces.append((trf_face_vert_index.vert_index_1, trf_face_vert_index.vert_index_2, trf_face_vert_index.vert_index_3))
    blender_mesh.from_pydata(verts, [], faces)
    blender_object = bpy.data.objects.new(trf_mesh.mesh_name, blender_mesh)
    bpy.context.collection.objects.link(blender_object)

八.导出TRF文件

import bpy

trf_meshes = []

trf_contents = []

trf_import_path = 'D:\pt_ladder.trf'

trf_export_path = 'D:\pt_ladder_morph_keys.trf'

model_morph_key_count = 4

class TrfMesh:
    def __init__(self):
        self.mesh_name = ''
        self.mesh_materials = []
        self.vertex_cnt = 0
        self.vertex_fvf_cnt = 0
        self.morph_key_cnt = 0
        self.bone_cnt = 0
        self.face_cnt = 0
        self.verts = []
        self.vert_fvfs = []
        self.face_vert_fvf_indices = []
        self.face_vert_indices = []
        self.morph_keys = []
        self.bones = []
        self.bone_weights = []
        
class TrfVert:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0
        
class TrfVertFvf:
    def __init__(self):
        self.vert_index = 0
        self.normal_x = 0
        self.normal_y = 0
        self.normal_z = 0
        self.uv_x = 0
        self.uv_y = 0

class TrfFaceVertFvfIndex:
    def __init__(self):
        self.vert_fvf_index_1 = 0
        self.vert_fvf_index_2 = 0
        self.vert_fvf_index_3 = 0
        
class TrfFaceVertIndex:
    def __init__(self):
        self.vert_index_1 = 0
        self.vert_index_2 = 0
        self.vert_index_3 = 0
        
class TrfMorphKey:
    def __init__(self):
        self.morph_key_index = 0
        self.vertex_cnt = 0
        self.verts = []
        self.vertex_fvf_cnt = 0
        self.vert_fvfs = []
        
class TrfBoneWeight:
    def __init__(self):
        self.vertex_fvf_index = 0
        self.bone1_index = 0
        self.bone1_weight = 0
        self.bone2_index = 0
        self.bone2_weight = 0
        


def export_trf_file(file_path):
    trf_contents.append('rfver 4\n')
    trf_contents.append('mesh ' + str(len(trf_meshes)) + '\n') 
    for trf_mesh in trf_meshes:
        write_trf_mesh(trf_mesh)
    
    with open(file_path, 'w', encoding='utf-8') as file:
        file.writelines(trf_contents)
        
def write_trf_mesh(trf_mesh):
    #write mesh header
    space_prefix = '   '
    row_arr = [space_prefix, trf_mesh.mesh_name, '0', trf_mesh.mesh_materials[0], '\n']
    trf_contents.append(' '.join(row_arr))
       
    #write mesh vertex
    space_prefix = space_prefix + '     '
    trf_contents.append(space_prefix + str(trf_mesh.vertex_cnt) + '\n')
    for index in range(0, trf_mesh.vertex_cnt):
        write_trf_mesh_vert(trf_mesh.verts[index])
        
    #write mesh morph keys
    trf_contents.append(space_prefix + str(trf_mesh.morph_key_cnt) + '\n')
    for index in range(0, trf_mesh.morph_key_cnt):
        write_trf_mesh_morph_key(trf_mesh.morph_keys[index])
        
    #write mesh vertex fvf
    trf_contents.append(space_prefix + str(trf_mesh.vertex_fvf_cnt) + '\n')
    for index in range(0, trf_mesh.vertex_fvf_cnt):
        write_trf_mesh_vert_fvf(trf_mesh.vert_fvfs[index])
        
    #write mesh bone
    trf_contents.append(space_prefix + '0' + '\n')
    
    #write mesh face
    trf_contents.append(space_prefix + str(trf_mesh.face_cnt) + '\n')
    for index in range(0, trf_mesh.face_cnt):
        write_trf_mesh_face(trf_mesh.face_vert_fvf_indices[index])
        
    trf_contents.append('end')
    
def write_trf_mesh_vert(vert):
    space_prefix = '       '
    row_arr = [space_prefix, format(vert.x, '.6f'), format(vert.y, '.6f'), format(vert.z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_mesh_vert_fvf(vert_fvf):
    space_prefix = '       '
    row_arr = [space_prefix, str(vert_fvf.vert_index), '4294967295', format(vert_fvf.normal_x, '.6f'), format(vert_fvf.normal_y, '.6f'), format(vert_fvf.normal_z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    row_arr = [space_prefix, format(vert_fvf.uv_x, '.6f'), format(vert_fvf.uv_y, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    row_arr = [space_prefix, '0.000000', '0.000000', '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_mesh_face(face):
    space_prefix = '       '
    row_arr = [space_prefix, str(face.vert_fvf_index_1), str(face.vert_fvf_index_2), str(face.vert_fvf_index_3), '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_mesh_morph_key(morph_key):
    space_prefix = '           '
    row_arr = [space_prefix, str(morph_key.morph_key_index), '\n']
    trf_contents.append(' '.join(row_arr))
    
    #morph key verts
    row_arr = [space_prefix, str(morph_key.vertex_cnt), '\n']
    trf_contents.append(' '.join(row_arr))
    for index in range(0, morph_key.vertex_cnt):
        write_trf_morph_key_vert(morph_key.verts[index])
        
    #morph key vert_fvfs
    row_arr = [space_prefix, str(morph_key.vertex_fvf_cnt), '\n']
    trf_contents.append(' '.join(row_arr))
    for index in range(0, morph_key.vertex_fvf_cnt):
        write_trf_morph_key_vert_fvf(morph_key.vert_fvfs[index])
        
def write_trf_morph_key_vert(vert):
    space_prefix = '           '
    row_arr = [space_prefix, format(vert.x, '.6f'), format(vert.y, '.6f'), format(vert.z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    
def write_trf_morph_key_vert_fvf(vert_fvf):
    space_prefix = '           '
    row_arr = [space_prefix, format(vert_fvf.normal_x, '.6f'), format(vert_fvf.normal_y, '.6f'), format(vert_fvf.normal_z, '.6f'), '\n']
    trf_contents.append(' '.join(row_arr))
    

def import_trf_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return read_trf_file(file)
    
        
def read_trf_file(file):
    trf_header = file.readline()
    trf_meshes_header = file.readline()
    mesh_count = int(trf_meshes_header.split(' ')[1])
    for index in range(0, mesh_count):
        trf_mesh = read_trf_mesh(file)
        trf_meshes.append(trf_mesh)
    return trf_meshes;
        
def read_trf_vert(file):
    trf_vert = TrfVert()
    trf_vert_coords = file.readline().strip().split(' ')
    trf_vert.x = round(float(trf_vert_coords[0]), 6)
    trf_vert.y = round(float(trf_vert_coords[1]), 6)
    trf_vert.z = round(float(trf_vert_coords[2]), 6)
    return trf_vert

def read_trf_morph_key(file):
    trf_morph_key = TrfMorphKey()
    trf_morph_key.morph_key_index = int(file.readline().strip())
    trf_morph_key.vertex_cnt = int(file.readline().strip())
    for index in range(0, trf_morph_key.vertex_cnt):
        trf_vert = read_trf_vert(file)
        trf_morph_key.verts.append(trf_vert)
    trf_morph_key.vertex_fvf_cnt = int(file.readline().strip())
    for index in range(0, trf_morph_key.vertex_fvf_cnt):
        trf_vert_fvf = read_trf_morph_key_vert_fvf(file)
        trf_morph_key.vert_fvfs.append(trf_vert_fvf)
    return trf_morph_key

def read_trf_morph_key_vert_fvf(file):
    trf_vert_fvf = TrfVertFvf()
    trf_vert_fvf_normals = file.readline().strip().split(' ')
    trf_vert_fvf.normal_x = round(float(trf_vert_fvf_normals[0]), 6)
    trf_vert_fvf.normal_y = round(float(trf_vert_fvf_normals[1]), 6)
    trf_vert_fvf.normal_z = round(float(trf_vert_fvf_normals[2]), 6)
    return trf_vert_fvf

def read_trf_mesh_vert_fvf(file):
    trf_vert_fvf = TrfVertFvf()
    trf_vert_fvf_normals = file.readline().strip().split(' ')
    trf_vert_fvf.vert_index = int(trf_vert_fvf_normals[0])
    trf_vert_fvf.normal_x = round(float(trf_vert_fvf_normals[2]), 6)
    trf_vert_fvf.normal_y = round(float(trf_vert_fvf_normals[3]), 6)
    trf_vert_fvf.normal_z = round(float(trf_vert_fvf_normals[4]), 6)
    trf_vert_fvf_uvs = file.readline().strip().split(' ')
    trf_vert_fvf.uv_x = round(float(trf_vert_fvf_uvs[0]), 6)
    trf_vert_fvf.uv_y = round(float(trf_vert_fvf_uvs[1]), 6)
    trf_vert_fvf_unused = file.readline().strip().split(' ')
    return trf_vert_fvf

def read_trf_face_vert_fvf_indices(file):
    trf_face_vert_fvf_index = TrfFaceVertFvfIndex()
    trf_face_vert_fvf_indices = file.readline().strip().split(' ')
    trf_face_vert_fvf_index.vert_fvf_index_1 = int(trf_face_vert_fvf_indices[0])
    trf_face_vert_fvf_index.vert_fvf_index_2 = int(trf_face_vert_fvf_indices[1])
    trf_face_vert_fvf_index.vert_fvf_index_3 = int(trf_face_vert_fvf_indices[2])
    return trf_face_vert_fvf_index
        
def read_trf_mesh(file):
    trf_mesh_header = file.readline().strip()
    #read mesh name&materials
    trf_mesh = TrfMesh()
    trf_mesh.mesh_name = trf_mesh_header.split(' ')[0]

    trf_mesh_materials_cnt = int(trf_mesh_header.split(' ')[1]) + 1
    trf_mesh_materials = []
    for index in range(0, trf_mesh_materials_cnt):
        trf_mesh_materials.append(trf_mesh_header.split(' ')[2 + index])
    trf_mesh.mesh_materials = trf_mesh_materials
    
    #read mesh verts
    trf_mesh.vertex_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.vertex_cnt):
        trf_vert = read_trf_vert(file)
        trf_mesh.verts.append(trf_vert)
        
    #read mesh morph keys
    trf_mesh.morph_key_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.morph_key_cnt):
        trf_morph_key = read_trf_morph_key(file)
        trf_mesh.morph_keys.append(trf_morph_key)
        
    #read mesh fvfs
    trf_mesh.vertex_fvf_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.vertex_fvf_cnt):
        trf_mesh_vert_fvf = read_trf_mesh_vert_fvf(file)
        trf_mesh.vert_fvfs.append(trf_mesh_vert_fvf)
    
    #read mesh bones
    trf_mesh.bone_cnt = int(file.readline().strip())
    if trf_mesh.bone_cnt >0 :
        trf_mesh.bone_index = file.readline().strip()
        trf_mesh.mesh_bone_cnt = int(file.readline().strip())
        for index in range(0, trf_mesh.mesh_bone_cnt):
            trf_mesh.mesh_bone_weight = file.readline().strip()
    
    trf_mesh.face_cnt = int(file.readline().strip())
    for index in range(0, trf_mesh.face_cnt):
        trf_face_vert_fvf_index = read_trf_face_vert_fvf_indices(file)
        trf_face_vert_index = TrfFaceVertIndex()
        trf_face_vert_index.vert_index_1 = trf_mesh.vert_fvfs[trf_face_vert_fvf_index.vert_fvf_index_1].vert_index
        trf_face_vert_index.vert_index_2 = trf_mesh.vert_fvfs[trf_face_vert_fvf_index.vert_fvf_index_2].vert_index
        trf_face_vert_index.vert_index_3 = trf_mesh.vert_fvfs[trf_face_vert_fvf_index.vert_fvf_index_3].vert_index
        trf_mesh.face_vert_fvf_indices.append(trf_face_vert_fvf_index)
        trf_mesh.face_vert_indices.append(trf_face_vert_index)
    return trf_mesh


import_trf_file(trf_import_path)

for trf_mesh in trf_meshes:
    print('mesh_name:' + trf_mesh.mesh_name)
    print('mesh_materials:' + trf_mesh.mesh_materials[0])
    print('vertex_count:' + str(trf_mesh.vertex_cnt))
    print('morph_key_count:' + str(trf_mesh.morph_key_cnt))
    print('bone_count:' + str(trf_mesh.bone_cnt))
    print('face_count:' + str(trf_mesh.face_cnt))
    print('________________')
    

#clear origin morph keys
for trf_mesh in trf_meshes:
    trf_mesh.morph_key_count = 0
    trf_mesh.morph_keys = []

#export blender object morph key
for trf_mesh in trf_meshes:
    trf_mesh.morph_key_cnt = model_morph_key_count
    for morph_key_index in range(0, trf_mesh.morph_key_cnt):
        trf_morph_key = TrfMorphKey()
        trf_morph_key.morph_key_index = morph_key_index
        #get blender object
        if morph_key_index > 0:
            blender_mesh_name = trf_mesh.mesh_name + '_morph_key_' + str(morph_key_index)
        else:
            blender_mesh_name = trf_mesh.mesh_name + '_morph_key_' + 'base'
        blender_mesh = bpy.context.scene.objects[blender_mesh_name].data
        #fill vertex info 
        trf_morph_key.vertex_cnt = len(blender_mesh.vertices)
        for vertex in blender_mesh.vertices:
            trf_vert = TrfVert()
            trf_vert.x = round(vertex.co.x, 6)
            trf_vert.y = round(vertex.co.y, 6)
            trf_vert.z = round(vertex.co.z, 6)
            trf_morph_key.verts.append(trf_vert)
        #fill vertex fvf info
        trf_morph_key.vertex_fvf_cnt = trf_mesh.vertex_fvf_cnt
        for index in range(0, trf_morph_key.vertex_fvf_cnt):
            trf_morph_key.vert_fvfs.append(TrfVertFvf())
        for loop_index in range(0, trf_mesh.face_cnt):
            vert_fvf_index_1 = trf_mesh.face_vert_fvf_indices[loop_index].vert_fvf_index_1
            vert_fvf_index_2 = trf_mesh.face_vert_fvf_indices[loop_index].vert_fvf_index_2
            vert_fvf_index_3 = trf_mesh.face_vert_fvf_indices[loop_index].vert_fvf_index_3
            normal_1 = blender_mesh.loops[loop_index * 3].normal
            normal_2 = blender_mesh.loops[loop_index * 3 + 1].normal
            normal_3 = blender_mesh.loops[loop_index * 3 + 2].normal
            trf_morph_key.vert_fvfs[vert_fvf_index_1].normal_x = normal_1.x
            trf_morph_key.vert_fvfs[vert_fvf_index_1].normal_y = normal_1.y
            trf_morph_key.vert_fvfs[vert_fvf_index_1].normal_z = normal_1.z
            trf_morph_key.vert_fvfs[vert_fvf_index_2].normal_x = normal_2.x
            trf_morph_key.vert_fvfs[vert_fvf_index_2].normal_y = normal_2.y
            trf_morph_key.vert_fvfs[vert_fvf_index_2].normal_z = normal_2.z
            trf_morph_key.vert_fvfs[vert_fvf_index_3].normal_x = normal_3.x
            trf_morph_key.vert_fvfs[vert_fvf_index_3].normal_y = normal_3.y
            trf_morph_key.vert_fvfs[vert_fvf_index_3].normal_z = normal_3.z
            
        trf_mesh.morph_keys.append(trf_morph_key)
    

export_trf_file(trf_export_path)
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸王•吕布

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值