import os, sys, time, argparse, requests, json, re, oss2 import bpy, bmesh import open3d as o3d import numpy as np # import cupy as cp from mathutils import Matrix import math import logging import matplotlib.pyplot as plt def get_auto_obj_bake_vertex_ply(obj_filepath, save_ply_path): start_time = time.time() print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: Getting ply vertex from {obj_filepath}') bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.delete(use_global=False, confirm=False) bpy.ops.wm.obj_import(filepath=obj_filepath) obj_original = [obj for obj in bpy.data.objects if obj.type == 'MESH'][0] bpy.context.view_layer.objects.active = obj_original obj_original.select_set(True) pid = obj_original.name obj_num_vertices = len(obj_original.data.vertices) # print(f"模型 {pid} 顶点数量:", len(obj_original.data.vertices)) # 选择新分离出来的网格对象,并将该对象设为进活动对象 bpy.ops.object.select_all(action='DESELECT') bpy.context.view_layer.objects.active = obj_original obj_original.select_set(True) # 添加颜色属性 bpy.ops.geometry.color_attribute_add() # 节点的创建: nodes = bpy.context.active_object.active_material.node_tree.nodes links = bpy.context.active_object.active_material.node_tree.links tex_coord = nodes.new("ShaderNodeTexCoord") Mapping = nodes.new('ShaderNodeMapping') vertex_color = nodes.new("ShaderNodeVertexColor") tex_coord.location = (-800, 600) Mapping.location = (-500, 200) vertex_color.location = (-200, 600) # 节点之间的连接: # 纹理坐标的UV 连接 映射的矢量;映射的矢量 连接 纹理贴图的矢量;原理化BSDF删除;纹理贴图的矢量 连接 表(曲)面。 links.new(tex_coord.outputs[2], nodes['Mapping'].inputs[0]) links.new(Mapping.outputs[0], nodes['Image Texture'].inputs[0]) principled_bsdf = nodes.get("Principled BSDF") nodes.remove(principled_bsdf) links.new(nodes['Image Texture'].outputs[0], nodes['Material Output'].inputs[0]) # 4.渲染烘焙相关操作 bpy.context.scene.render.engine = 'CYCLES' bpy.context.scene.cycles.device = 'GPU' bpy.context.scene.cycles.preview_samples = 1 bpy.context.scene.cycles.samples = 1 bpy.context.scene.cycles.bake_type = 'EMIT' bpy.context.scene.render.bake.use_selected_to_active = False bpy.context.scene.render.bake.target = 'VERTEX_COLORS' bpy.ops.object.bake(type='EMIT') # 开始 Bake # 连接节点 Color Attribute 和 节点 Material Output links.new(nodes['Color Attribute'].outputs[0], nodes['Material Output'].inputs[0]) print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: Bake EMIT time: ', time.time() - start_time) start_time = time.time() # 5.输出模型 # bpy.ops.wm.save_as_mainfile(filepath='/data/datasets/plys/121614.bend') bpy.ops.wm.ply_export(filepath=save_ply_path, apply_modifiers=True, export_selected_objects=True, export_uv=False, export_normals=False, export_colors='SRGB', export_triangulated_mesh=False, ascii_format=True) bpy.data.objects.remove(obj_original, do_unlink=True) print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: Save ply time: ', time.time() - start_time) return obj_num_vertices if __name__ == '__main__': # workdir = '/data/datasets/suwa_cut/data/auto/' # output = '/data/datasets/plys/auto/' workdir = 'G:\obj_fix\print' output = 'G:\obj_fix\ply\whole_obj_bake_vertex_high_ply' directory = f'G:\obj_fix/print/right' #遍历获取 G:\obj_fix\print\right 目录的文件,根据文件名获取 pid for file_name in os.listdir(directory): # 获取文件的完整路径 file_path = os.path.join(directory, file_name) # 检查是否是文件 if os.path.isfile(file_path): # arrFileName = file_name.split('_') pid = arrFileName[0] if int(pid) == 0: continue print(f"File: {file_name}, PID: {pid}") #判断文件是否存在 file_path = os.path.join(output, f'{pid}.ply') if os.path.exists(file_path) and os.path.isfile(file_path): print("文件已存在,跳过循环!!!") continue obj_filepath = os.path.join(workdir, f'{pid}/{pid}.obj') save_ply_path = os.path.join(output, f'{pid}.ply') if not os.path.exists(obj_filepath): print(f"{obj_filepath} 文件不存在,跳过循环!!!") continue obj_num_vertices = get_auto_obj_bake_vertex_ply(obj_filepath, save_ply_path) if obj_num_vertices == 0: print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: 模型 {pid} 程序getHeadCount()无法找到模型人数!总共运行时间: ', time.time() - start_time0) logging.info(f'模型 {pid} 顶点数量: {obj_num_vertices},程序getHeadCount()无法找到模型人数!,程序总共运行时间: {time.time() - start_time0:.2f} 秒') continue # # 从文件名中提取 pid # pid = extract_pid(file_name) # if pid: # print(f"File: {file_name}, PID: {pid}") # else: # print(f"Unable to extract PID from file: {file_name}") # return # listdir_pids = os.listdir(workdir) # for pid in listdir_pids: # file_path = os.path.join(output, f'{pid}/{pid}.ply') # if os.path.exists(file_path) and os.path.isfile(file_path): # print("文件已存在,跳过循环!!!") # continue # print(pid) # logging.basicConfig(filename=f'/data/datasets/plys/auto_vertex_time.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # start_time0 = time.time() # print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: 模型 {pid} 开始运行时间: ', start_time0) # # 判断输出目录是否存在 # if not os.path.exists(os.path.join(output, f'{pid}/')): # os.makedirs(os.path.join(output, f'{pid}/')) # obj_filepath = os.path.join(workdir, f'{pid}/{pid}_cpd.obj') # save_ply_path = os.path.join(output, f'{pid}/{pid}.ply') # if not os.path.exists(obj_filepath): # print(f"{obj_filepath} 文件不存在,跳过循环!!!") # continue # obj_num_vertices = get_auto_obj_bake_vertex_ply(obj_filepath, save_ply_path) # if obj_num_vertices == 0: # print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: 模型 {pid} 程序getHeadCount()无法找到模型人数!总共运行时间: ', time.time() - start_time0) # logging.info(f'模型 {pid} 顶点数量: {obj_num_vertices},程序getHeadCount()无法找到模型人数!,程序总共运行时间: {time.time() - start_time0:.2f} 秒') # continue # print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: 模型 {pid} 顶点数量: {obj_num_vertices} 程序总共运行时间: ', time.time() - start_time0) # logging.info(f'模型 {pid} 顶点数量: {obj_num_vertices},程序总共运行时间: {time.time() - start_time0:.2f} 秒') # # 退出 Blender 并释放资源 # bpy.ops.wm.quit_blender()