|
|
|
|
@ -3,6 +3,7 @@ import os,platform
@@ -3,6 +3,7 @@ import os,platform
|
|
|
|
|
import shutil |
|
|
|
|
import json |
|
|
|
|
import shlex |
|
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed |
|
|
|
|
from .oss_redis import ossClient |
|
|
|
|
from .logs import log |
|
|
|
|
from .oss_func import download_file_with_check, checkFileExists |
|
|
|
|
@ -226,42 +227,29 @@ def getHomoMatrixByFileName(dirNewName,fileName):
@@ -226,42 +227,29 @@ def getHomoMatrixByFileName(dirNewName,fileName):
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#json文件进行下载对应的数据 和转换数据,传递目录路径 |
|
|
|
|
def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False): |
|
|
|
|
listData = getJsonData(dirNewName) |
|
|
|
|
if not listData: |
|
|
|
|
log(f"获取数据失败, dirNewName={dirNewName}") |
|
|
|
|
return False |
|
|
|
|
#遍历数据 |
|
|
|
|
arrPrintId = [] |
|
|
|
|
arrPrintDataInfo = [] |
|
|
|
|
for modelInfo in listData: |
|
|
|
|
arrPrintId.append(modelInfo.get('printId')) |
|
|
|
|
arrPrintDataInfo.append({ |
|
|
|
|
"printId": modelInfo.get('printId'), |
|
|
|
|
"file_name": modelInfo.get('file_name'), |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
#调用接口获取下载的路径 |
|
|
|
|
arrDownloadPath = getDownloadDirByPrintId(arrPrintId) |
|
|
|
|
if not arrDownloadPath: |
|
|
|
|
log(f"获取下载路径失败, arrPrintId={arrPrintId}") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
dirPath = os.path.join(dirNewName, 'data') |
|
|
|
|
if not os.path.exists(dirPath): |
|
|
|
|
os.makedirs(dirPath) |
|
|
|
|
|
|
|
|
|
#遍历数据 |
|
|
|
|
for v in listData: |
|
|
|
|
#处理单个数据项:下载文件、修改关联路径、转换数据(如果是小机台) |
|
|
|
|
def _process_single_item(v, arrDownloadPath, dirNewName, dirPath, isSmallMachine): |
|
|
|
|
""" |
|
|
|
|
处理单个数据项的函数,用于多线程处理 |
|
|
|
|
参数: |
|
|
|
|
v: 单个数据项 |
|
|
|
|
arrDownloadPath: 下载路径数组 |
|
|
|
|
dirNewName: 目录名称 |
|
|
|
|
dirPath: 数据目录路径 |
|
|
|
|
isSmallMachine: 是否是小机台 |
|
|
|
|
返回: |
|
|
|
|
(success: bool, error_msg: str) |
|
|
|
|
""" |
|
|
|
|
try: |
|
|
|
|
# 查找匹配的下载路径信息 |
|
|
|
|
info = {} |
|
|
|
|
for tempv in arrDownloadPath: |
|
|
|
|
#print(f"tempv={tempv['print_order_id']} == {v['printId']}") |
|
|
|
|
if str(tempv["print_order_id"]) == str(v["printId"]): |
|
|
|
|
info = tempv |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
#print(f"info={info}") |
|
|
|
|
if not info: |
|
|
|
|
return False, f"未找到匹配的下载路径信息, printId={v['printId']}" |
|
|
|
|
|
|
|
|
|
filePath = info["path"] |
|
|
|
|
pid = info["pid"] |
|
|
|
|
@ -284,11 +272,11 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
@@ -284,11 +272,11 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
|
|
|
|
|
{"ossPath": f"{filePath}/{pid}.obj", "localPath": os.path.join(dirPath, localObjName)}, |
|
|
|
|
{"ossPath": f"{filePath}/{pid}.mtl", "localPath": os.path.join(dirPath, loaclMtlName)}, |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
#遍历下载文件 |
|
|
|
|
beginTime = time.time() |
|
|
|
|
objsLocal = "" |
|
|
|
|
for objFiles in arrDownloadFiles: |
|
|
|
|
if "F" in fileName: |
|
|
|
|
#判断 mtl 和 jpg 文件是否存在,存在就不在下载了 |
|
|
|
|
if "mtl" in objFiles["localPath"] or "jpg" in objFiles["localPath"]: |
|
|
|
|
if os.path.exists(objFiles["localPath"]): |
|
|
|
|
@ -296,8 +284,9 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
@@ -296,8 +284,9 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
|
|
|
|
|
|
|
|
|
|
downloadOk = download_file_with_check(objFiles["ossPath"], objFiles["localPath"]) |
|
|
|
|
if not downloadOk: |
|
|
|
|
log(f"下载文件失败, ossPath={objFiles["ossPath"]}, localPath={objFiles["localPath"]}") |
|
|
|
|
return False |
|
|
|
|
error_msg = f"下载文件失败, ossPath={objFiles['ossPath']}, localPath={objFiles['localPath']}" |
|
|
|
|
log(error_msg) |
|
|
|
|
return False, error_msg |
|
|
|
|
|
|
|
|
|
#下载成功之后要修改文件之间的关联路径 |
|
|
|
|
if objFiles["localPath"].endswith(".obj"): |
|
|
|
|
@ -308,18 +297,16 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
@@ -308,18 +297,16 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
|
|
|
|
|
changeMtlFile(objFiles["localPath"], f"{orderId}_{pid}Tex1.jpg") |
|
|
|
|
|
|
|
|
|
endTime = time.time() |
|
|
|
|
log(f"下载文件和修改文件之间的关联路径 : 耗时{endTime - beginTime}秒") |
|
|
|
|
log(f"下载文件和修改文件之间的关联路径 : 耗时{endTime - beginTime}秒 - {fileName}") |
|
|
|
|
|
|
|
|
|
#如果是小机台,则要转换数据 |
|
|
|
|
if not isSmallMachine: |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
if isSmallMachine: |
|
|
|
|
timeBegin = time.time() |
|
|
|
|
homo_matrix = getHomoMatrixByFileName(dirNewName, localObjName) |
|
|
|
|
if not homo_matrix: |
|
|
|
|
log(f"获取homo_matrix失败, dirNewName={dirNewName}, objsLocal={objsLocal}") |
|
|
|
|
return False |
|
|
|
|
# transform_save(objFiles["localPath"], homo_matrix) |
|
|
|
|
error_msg = f"获取homo_matrix失败, dirNewName={dirNewName}, objsLocal={objsLocal}" |
|
|
|
|
log(error_msg) |
|
|
|
|
return False, error_msg |
|
|
|
|
|
|
|
|
|
#通过blender 调用执行 python 文件 |
|
|
|
|
blender_bin_path = findBpyModule() |
|
|
|
|
@ -334,13 +321,85 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
@@ -334,13 +321,85 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
|
|
|
|
|
homo_matrix_quoted = shlex.quote(homo_matrix_json) |
|
|
|
|
error = os.system(f"{blender_bin_path} -b -P {transform_script_path_quoted} -- --objPathName={objsLocal_quoted} --trans={homo_matrix_quoted}") |
|
|
|
|
if error != 0: |
|
|
|
|
log(f"调用blender 执行 python 文件失败, error={error}") |
|
|
|
|
return False |
|
|
|
|
log(f"调用blender 执行 python 文件成功, error={error}") |
|
|
|
|
error_msg = f"调用blender 执行 python 文件失败, error={error}, fileName={fileName}" |
|
|
|
|
log(error_msg) |
|
|
|
|
return False, error_msg |
|
|
|
|
log(f"调用blender 执行 python 文件成功, error={error}, fileName={fileName}") |
|
|
|
|
|
|
|
|
|
timeEnd = time.time() |
|
|
|
|
log(f"转换数据时间: 耗时{timeEnd - timeBegin}秒 - {objsLocal}") |
|
|
|
|
|
|
|
|
|
return True, "" |
|
|
|
|
except Exception as e: |
|
|
|
|
error_msg = f"处理数据项时发生异常, fileName={v.get('file_name', 'unknown')}, error={str(e)}" |
|
|
|
|
log(error_msg) |
|
|
|
|
return False, error_msg |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#json文件进行下载对应的数据 和转换数据,传递目录路径 |
|
|
|
|
def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False, max_workers=10): |
|
|
|
|
listData = getJsonData(dirNewName) |
|
|
|
|
if not listData: |
|
|
|
|
log(f"获取数据失败, dirNewName={dirNewName}") |
|
|
|
|
return False |
|
|
|
|
#遍历数据 |
|
|
|
|
arrPrintId = [] |
|
|
|
|
arrPrintDataInfo = [] |
|
|
|
|
for modelInfo in listData: |
|
|
|
|
arrPrintId.append(modelInfo.get('printId')) |
|
|
|
|
arrPrintDataInfo.append({ |
|
|
|
|
"printId": modelInfo.get('printId'), |
|
|
|
|
"file_name": modelInfo.get('file_name'), |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
#调用接口获取下载的路径 |
|
|
|
|
arrDownloadPath = getDownloadDirByPrintId(arrPrintId) |
|
|
|
|
if not arrDownloadPath: |
|
|
|
|
log(f"获取下载路径失败, arrPrintId={arrPrintId}") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
dirPath = os.path.join(dirNewName, 'data') |
|
|
|
|
if not os.path.exists(dirPath): |
|
|
|
|
os.makedirs(dirPath) |
|
|
|
|
|
|
|
|
|
#使用多线程并发处理数据 |
|
|
|
|
log(f"开始多线程处理数据, 共{len(listData)}个项目, 线程数={max_workers}") |
|
|
|
|
beginTime = time.time() |
|
|
|
|
|
|
|
|
|
# 使用线程池并发处理 |
|
|
|
|
with ThreadPoolExecutor(max_workers=max_workers) as executor: |
|
|
|
|
# 提交所有任务 |
|
|
|
|
future_to_item = { |
|
|
|
|
executor.submit(_process_single_item, v, arrDownloadPath, dirNewName, dirPath, isSmallMachine): v |
|
|
|
|
for v in listData |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
# 收集结果 |
|
|
|
|
success_count = 0 |
|
|
|
|
fail_count = 0 |
|
|
|
|
for future in as_completed(future_to_item): |
|
|
|
|
v = future_to_item[future] |
|
|
|
|
try: |
|
|
|
|
success, error_msg = future.result() |
|
|
|
|
if success: |
|
|
|
|
success_count += 1 |
|
|
|
|
log(f"处理成功: {v.get('file_name', 'unknown')} ({success_count}/{len(listData)})") |
|
|
|
|
else: |
|
|
|
|
fail_count += 1 |
|
|
|
|
log(f"处理失败: {v.get('file_name', 'unknown')}, 错误: {error_msg} ({fail_count}/{len(listData)})") |
|
|
|
|
# 如果任何一个任务失败,记录错误但继续处理其他任务 |
|
|
|
|
except Exception as e: |
|
|
|
|
fail_count += 1 |
|
|
|
|
error_msg = f"处理数据项时发生异常: {str(e)}" |
|
|
|
|
log(f"处理异常: {v.get('file_name', 'unknown')}, 错误: {error_msg} ({fail_count}/{len(listData)})") |
|
|
|
|
|
|
|
|
|
endTime = time.time() |
|
|
|
|
log(f"多线程处理完成, 总耗时{endTime - beginTime}秒, 成功:{success_count}, 失败:{fail_count}, 总计:{len(listData)}") |
|
|
|
|
|
|
|
|
|
# 如果有任何失败,返回 False |
|
|
|
|
if fail_count > 0: |
|
|
|
|
log(f"部分任务处理失败,共{fail_count}个失败") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
@ -350,7 +409,7 @@ def findBpyModule():
@@ -350,7 +409,7 @@ def findBpyModule():
|
|
|
|
|
blender_bin_path = '/Applications/Blender.app/Contents/MacOS/Blender' |
|
|
|
|
# 判断当前是 windows 还是 macOS |
|
|
|
|
if platform.system() == 'Windows': |
|
|
|
|
blender_bin_path = 'C:\\Program Files\\Blender Foundation\\Blender 4.4\\blender.exe' |
|
|
|
|
blender_bin_path = 'C:\\Program Files\\Blender Foundation\\Blender 5.0\\blender.exe' |
|
|
|
|
else: |
|
|
|
|
blender_bin_path = '/Applications/Blender.app/Contents/MacOS/Blender' |
|
|
|
|
|
|
|
|
|
|