import os, sys, datetime, oss2, requests, time import subprocess, shlex, shutil def find_blender_bin_path(): base_path = 'C:\\Program Files\\Blender Foundation\\' if os.path.exists(base_path): for dir in os.listdir(base_path): if dir.startswith('Blender'): blender_bin_path = base_path + dir + '\\blender.exe' return f'"{blender_bin_path}"' else: print('未找到blender安装目录') exit(1) def diff_time(start_time, end_time): d = end_time - start_time m = d.microseconds return str(d.seconds // 60) + ":" + str(d.seconds % 60) + ":" + str(m)[:-3] def delete_lines_in_file(filename, count): with open(filename, 'r') as f: lines = f.readlines() lines = lines[count:] with open(filename, 'w') as f: f.writelines(lines) def fix_mtl_file(pid): mtl_file = os.path.join(input_path, 'output', pid + '.mtl') with open(mtl_file, 'r') as f: lines = f.readlines() lines = [line.replace('_u0_v0_diffuse.jpg', '.jpg') for line in lines] with open(mtl_file, 'w') as f: f.writelines(lines) os.rename(os.path.join(input_path, 'output', pid + '_u0_v0_diffuse.jpg'), os.path.join(input_path, 'output', pid + '.jpg')) if __name__ == '__main__': AccessKeyId = 'LTAI5tSReWm8hz7dSYxxth8f' AccessKeySecret = '8ywTDF9upPAtvgXtLKALY2iMYHIxdS' Endpoint = 'oss-cn-shanghai.aliyuncs.com' Bucket = 'suwa3d-securedata' oss_client = oss2.Bucket(oss2.Auth(AccessKeyId, AccessKeySecret), Endpoint, Bucket) update_status_modelsuccess_url = 'https://repair.api.suwa3d.com/api/modelRepairOrder/toModelMakeSucceed' workdir = 'd:\\' start = datetime.datetime.now() if len(sys.argv) == 2: pid = sys.argv[1] else: print('usage: python rc_export.py [pid]') exit(1) input_path = os.path.join(workdir, pid) output_path = input_path + '\\output\\' ImagesGeometry = os.path.join(input_path, "photo1") ImagesTexture = os.path.join(input_path, "photo2") rcbin = '"C:\\Program Files\\Capturing Reality\\RealityCapture\\RealityCapture.exe"' blenderbin = find_blender_bin_path() config_path = 'D:\\apps\\config\\' shutil.rmtree(output_path, ignore_errors=True) os.system('mkdir ' + output_path) cmd = rcbin + \ ' -delegateTo ' + pid + \ ' -exportSelectedModel "' + output_path + pid + '.obj" "' + config_path + 'ModelExportParams.xml" ' + \ ' -quit' print(cmd) cmd = shlex.split(cmd) subprocess.run(cmd) time.sleep(20) fix_mtl_file(pid) delete_lines_in_file(os.path.join(output_path, pid + '.mtl'), 6) delete_lines_in_file(os.path.join(output_path, pid + '.obj'), 6) os.system(f'{blenderbin} -b -P d:\\apps\\blender\\autofix.py -- {pid}') os.system(f'gltfpack -c -i {os.path.join(output_path, pid + ".obj")} -o {os.path.join(output_path, pid + ".glb")}') print(f'开始上传{pid}模型文件...') oss_client.put_object_from_file(f'objs/auto/{pid}/{pid}.obj.rcInfo', os.path.join(input_path, 'output', pid + '.obj.rcInfo')) oss_client.put_object_from_file(f'objs/auto/{pid}/{pid}.obj', os.path.join(input_path, 'output', pid + '.obj')) oss_client.put_object_from_file(f'objs/auto/{pid}/{pid}.mtl', os.path.join(input_path, 'output', pid + '.mtl')) oss_client.put_object_from_file(f'objs/auto/{pid}/{pid}.jpg', os.path.join(input_path, 'output', pid + '.jpg')) oss_client.put_object_from_file(f'glbs/auto/{pid}.glb', os.path.join(input_path, 'output', pid + '.glb')) print(f'{update_status_modelsuccess_url}?id={pid}') res = requests.post(update_status_modelsuccess_url, data={'id': pid}) print('上传完成更新建模成功状态:', res.text) end = datetime.datetime.now() print('Time: ' + diff_time(start, end)) shutil.rmtree(input_path, ignore_errors=True)