from ctypes import util import os, oss2, time, redis, requests, shutil, sys, subprocess, json, platform from PIL import Image, ImageDraw, ImageFont from retrying import retry import atexit,platform import white_purification_v4,white_purification if platform.system() == 'Windows': sys.path.append('e:\\libs\\') import common import config def find_blender_bin_path(): if platform.system() == 'Linux': return 'blender' 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) @retry(stop_max_attempt_number=10, wait_fixed=3000) def down_obj_fromoss(pid,order_id): # print_type:// 打印状态 1:正常打印 2:重打 3:加打,4: 样品 print('开始下载obj文件...' , pid) #请求接口 获取 对应的 oss 地址 tempUrl = f'https://mp.api.suwa3d.com/api/order/getOssSuffixByOrderId?order_id={order_id}' print(tempUrl) #发起请求 res = requests.get(tempUrl) ossPath = "" if res.json()['code'] == 1000: ossPath = res.json()['data'] ossPath = ossPath.replace("\\","/") print("AAAAAAAAA",ossPath) path = os.path.join(workdir, 'print', pid,order_id) if not os.path.exists(path): os.makedirs(path) # 根据前缀获取文件列表 prefix = f'objs/print/{pid}/' if ossPath != "": prefix = os.path.join("objs","print",pid,ossPath) if "base/" in ossPath: prefix = os.path.join("objs","print",pid,ossPath) prefix = prefix.replace("\\","/") print(f'prefix:{prefix}') filelist = oss2.ObjectIteratorV2(oss_client, prefix=prefix) if len(list(filelist)) == 0: filelist = oss2.ObjectIteratorV2(oss_client, prefix=f"objs/print/{pid}") else: filelist = oss2.ObjectIteratorV2(oss_client, prefix=prefix) find = False for file in filelist: filename = file.key.split('/')[-1] if filename == '': continue if filename.endswith(f'.obj') and find == True: continue if filename.endswith(f'{pid}.obj'): find = True localfile = os.path.join(path, filename) if "process" in file.key or "human" in file.key: continue res = oss_client.get_object_to_file(file.key, localfile) print(f'下载文件:{file.key},状态:{res.status}') if not find and print_type == 3: for file in os.listdir(path): if file.endswith('.obj'): print('找到其他obj文件,采用这个文件来生成需要的尺寸', file) shutil.copy(os.path.join(path, file), os.path.join(path, f'{pid}.obj')) find = True break if not find: print('找不到obj文件,异常退出') sys.exit(1) return ossPath # print('下载完成后静默10秒,等待文件写入完成') # time.sleep(10) def getPidFromOrderId(orderId): getPidFromOrderId_url = 'https://mp.api.suwa3d.com/api/printOrder/info' print(f'{getPidFromOrderId_url}?id={orderId}') res = requests.get(f'{getPidFromOrderId_url}?id={orderId}') resCode = res.json()['code'] if int(resCode) != 1000: return -1 print(res.text) return res.json()['data'] def team_check(r): # try: # if r.llen('model:IndependentRepairTeamcheckGLBQueue') == 0: # time.sleep(5) # return # except Exception as e: # print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), 'redis连接异常,5秒后重试') # print(e) # time.sleep(5) # r = create_redis_connection() # return # repair_id = r.lpop('model:IndependentRepairTeamcheckGLBQueue') # if repair_id is None: return # repair_id = repair_id.decode('utf-8') repair_id = "66682" res = requests.get(f'{getRepairInfo_url}?id={repair_id}') print(f'getRepairInfo_url:{getRepairInfo_url}?id={repair_id}') print(res.text) pid = res.json()['data']['pid'] orderId = res.json()['data']['order_id'] pid = str(pid) orderId = str(orderId) #print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), f'pid:{pid} repair_id:{repair_id} 生成团队审核模型 start model:IndependentRepairTeamcheckGLBQueue', ) ossPath = down_obj_fromoss(pid,orderId) obj_filename = f'{pid}.obj' glb_filename = f'{orderId}.glb' print('开始转换obj文件为glb文件...') os.system(f'gltfpack -c -i {os.path.join(workdir, "print", pid,orderId, obj_filename)} -o {os.path.join(workdir, "print", pid,orderId, glb_filename)} -vt 16 -vc 16') #处理封面图 #执行获取obj缩略图 print("执行获取obj全身缩略图脚本1111111111") os.system(f'python d:\\make2\\tools\pic_for_obj\image_rander_small.py -pid {pid} -i D://print/{pid}/{orderId} -o D://print/{pid}/{orderId}') print("判断是否存在封面图") #判断文件是否存在,存在则上传到oss, 更新数据库内容 if os.path.exists(f'D://print/{pid}/{orderId}/{pid}_pic.png'): #获取拍照订单的信息,从中得到拍照订单的信息 uuid = get_p3d_info(str(pid)) if uuid != 0 and uuid != None and uuid != "": print("uuid",uuid) config.oss_bucket_3d_view.put_object_from_file(f'{uuid}/3d_view.png', f'D://print/{pid}/{orderId}/{pid}_pic.png') oss_client.put_object_from_file(f'glbs/print/order_id/{glb_filename}', os.path.join(workdir, "print", pid,orderId, glb_filename)) #对文件进行白色提纯处理 imagePath = os.path.join(workdir, 'print', pid,orderId,pid+"Tex1.jpg") print("开始处理白色提纯") #white_purification.white_purification_utils(imagePath) os.system(f'python D:\\make2\\apps\white_purification_v4.py -i {imagePath}') print("贴图文件白色提纯完成",imagePath) #提纯完重新上传提纯图片 ossImagePath = os.path.join("objs/print", pid,ossPath,"texture","process_"+pid+"Tex1.jpg") oss_client.put_object_from_file(f"objs/print/{pid}/{ossPath}/texture/process_{pid}Tex1.jpg",imagePath) print("白色提纯上传图片路径-",ossImagePath,imagePath) print(f'{update_repair_status_url}?id={repair_id}') res = requests.get(f'{update_repair_status_url}?id={repair_id}') print('更新团队审核状态:', res.text) shutil.rmtree(os.path.join(workdir, 'print', pid,orderId), ignore_errors=True) print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), f'pid:{pid} 生成团队审核模型 end') def get_p3d_info(pid): url = "https://mp.api.suwa3d.com/api/customerP3dLog/info?id="+pid res = requests.get(url) res = res.json() if res["code"] == 1000: return res["data"]["guid"] else: return 0 def create_redis_connection(): """创建 Redis 连接,若连接失败则重试""" while True: try: r = redis.Redis(host='106.14.158.208', password='kcV2000', port=6379, db=6) # 尝试进行一次操作,检查连接是否有效 r.ping() # ping 操作是一个简单的连接测试 print("Redis连接成功!") return r except ConnectionError: print("Redis连接失败,正在重试...") time.sleep(5) def main(r): print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), '模型生成程序 start') while True: try: team_check(r) except Exception as e: print('出现异常:', e) time.sleep(5) r = create_redis_connection() continue if __name__ == '__main__': atexit.register(common.notify,"R13-修模师审核通过,生成打印任务程序停止") AccessKeyId = 'LTAI5tSReWm8hz7dSYxxth8f' AccessKeySecret = '8ywTDF9upPAtvgXtLKALY2iMYHIxdS' Endpoint = 'oss-cn-shanghai.aliyuncs.com' Bucket = 'suwa3d-securedata' oss_client = oss2.Bucket(oss2.Auth(AccessKeyId, AccessKeySecret), Endpoint, Bucket) update_check_url = 'https://mp.api.suwa3d.com/api/customerP3dLog/updateStatusToWaitingPlatformCheckingStatus' update_team_check_url = 'https://mp.api.suwa3d.com/api/customerP3dLog/updateStatusToWaitingTeamCheckingStatus' update_status_printstatus_url = 'https://mp.api.suwa3d.com/api/customerP3dLog/updateBuildPrintModelStatus' update_makeprintobj_status_url = 'https://mp.api.suwa3d.com/api/printOrder/updateMakePrintObjSucceed' getRepairInfo_url = 'https://repair.api.suwa3d.com/api/modelRepairOrder/teamCheckGLBInfo' update_repair_status_url = 'https://repair.api.suwa3d.com/api/modelRepairOrder/updateStatusToWaitingTeamCheckingStatus' if platform.system() == 'Windows': workdir = 'D:\\' else: workdir = '/data/datasets/' blenderbin = find_blender_bin_path() r = create_redis_connection() main(r)