更新下载

This commit is contained in:
dongchangxi committed 2025-12-04 15:39:48 +08:00
1 parent baa883ca7e
commit 827f9847f8
3 files changed
+234 -38

No files matched your search

+63 -8
View File
@@ -1,7 +1,8 @@
import requests,time
import os
import os,platform
import shutil
import json
import shlex
from .oss_redis import ossClient
from .logs import log
from .oss_func import download_file_with_check, checkFileExists
@@ -122,9 +123,9 @@ def downloadJsonAndJpgFileAndMoveToCorrectDir(versionId,currentDir):
print(f"machineInfo={machineInfo['id']}")
dirNewName = ""
if str(machineInfo["machine_type"]) == '1':
dirNewName = os.path.join(currentDir, 'batchPrint', versionId + '_big_No'+str(machineInfo['id']))
else:
dirNewName = os.path.join(currentDir, 'batchPrint', versionId + '_small_No'+str(machineInfo['id']))
else:
dirNewName = os.path.join(currentDir, 'batchPrint', versionId + '_big_No'+str(machineInfo['id']))
#判断目录是否存在,存在就删除
if os.path.exists(dirNewName):
@@ -193,6 +194,7 @@ def getJsonData(dirNewName):
"pid": pid,
"size": size,
"counts": counts,
"file_name": file_name,
}
listData.append(modelInfo)
@@ -232,8 +234,13 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
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)
@@ -246,13 +253,23 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
os.makedirs(dirPath)
#遍历数据
for info in arrDownloadPath:
for v in listData:
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}")
filePath = info["path"]
pid = info["pid"]
orderId = info["order_id"]
printId = info["print_order_id"]
size = info["real_size"]
counts = info["quantity"]
fileName = v["file_name"]
#判断文件是否存在
ossJpgFilePath = f"{filePath}/printId_{printId}Tex1.jpg"
@@ -261,7 +278,7 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
localJpgName = os.path.join(f"{orderId}_{pid}Tex1.jpg")
loaclMtlName = os.path.join(f"{orderId}_{pid}.mtl")
localObjName = os.path.join(f"{orderId}_{pid}_P{printId}_{size}_x{counts}.obj")
localObjName = fileName
arrDownloadFiles = [
{"ossPath": ossJpgFilePath, "localPath": os.path.join(dirPath, localJpgName)},
{"ossPath": f"{filePath}/{pid}.obj", "localPath": os.path.join(dirPath, localObjName)},
@@ -271,6 +288,12 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
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"]):
continue
downloadOk = download_file_with_check(objFiles["ossPath"], objFiles["localPath"])
if not downloadOk:
log(f"下载文件失败, ossPath={objFiles["ossPath"]}, localPath={objFiles["localPath"]}")
@@ -296,9 +319,41 @@ def downloadDataByOssAndTransformSave(dirNewName,isSmallMachine=False):
if not homo_matrix:
log(f"获取homo_matrix失败, dirNewName={dirNewName}, objsLocal={objsLocal}")
return False
transform_save(objFiles["localPath"], homo_matrix)
# transform_save(objFiles["localPath"], homo_matrix)
#通过blender 调用执行 python 文件
blender_bin_path = findBpyModule()
# 获取 small_machine_transform.py 的绝对路径
script_dir = os.path.dirname(os.path.abspath(__file__))
transform_script_path = os.path.join(script_dir, 'small_machine_transform.py')
# 将 homo_matrix 转换为 JSON 字符串,并对路径进行转义
homo_matrix_json = json.dumps(homo_matrix)
# 使用 shlex.quote 来安全地转义路径和参数
transform_script_path_quoted = shlex.quote(transform_script_path)
objsLocal_quoted = shlex.quote(objsLocal)
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}")
timeEnd = time.time()
log(f"转换数据时间{objsLocal}: 耗时{timeEnd - timeBegin}秒")
log(f"转换数据时间: 耗时{timeEnd - timeBegin}秒 - {objsLocal}")
return True
return True
def findBpyModule():
# 返回 Blender 可执行文件路径(macOS)
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'
else:
blender_bin_path = '/Applications/Blender.app/Contents/MacOS/Blender'
return blender_bin_path
@@ -1,7 +1,6 @@
import os,time
import os,time,sys,argparse
import numpy as np
import subprocess
from .logs import log
import json
def findBpyModule():
@@ -40,14 +39,15 @@ def custom_mesh_transform(vertices, transform_matrix):
# }
# },
def transform_save(obj_path,homo_matrix):
import bpy
# 延迟导入 bpy,只在 Blender 环境中可用
try:
blender_bin_path = findBpyModule()
subprocess.run([blender_bin_path, "--background", "--python-expr", "import bpy"])
except ImportError:
log("错误: bpy 模块不可用。此函数只能在 Blender 环境中运行。")
log("请使用 Blender 的 Python 解释器运行此脚本,或通过 Blender 命令行运行。")
raise ImportError("bpy 模块不可用。此函数只能在 Blender 环境中运行。")
# try:
# blender_bin_path = findBpyModule()
# subprocess.run([blender_bin_path, "--background", "--python-expr", "import bpy"])
# except ImportError:
# log("错误: bpy 模块不可用。此函数只能在 Blender 环境中运行。")
# log("请使用 Blender 的 Python 解释器运行此脚本,或通过 Blender 命令行运行。")
# raise ImportError("bpy 模块不可用。此函数只能在 Blender 环境中运行。")
obj_name = obj_path.split("/")[-1]
# 清除场景
@@ -60,26 +60,7 @@ def transform_save(obj_path,homo_matrix):
bpy.context.scene.unit_settings.scale_length = 0.001
bpy.context.scene.unit_settings.mass_unit = 'GRAMS'
# meshes = []
# need_offset = True
# for model in layout_data["models"]:
#transform = model.get('transform', {})
#homo_matrix = transform["homo_matrix"]
reconstructed_matrix = np.array(homo_matrix, dtype=np.float64)
# obj_name = model.get('file_name', '')
# obj_path = os.path.join(original_obj_pid_dir, obj_name)
#mtl_name_temp = obj_name
#separator = "_P"
#index = mtl_name_temp.find(separator)
# if index != -1:
# old_mtl_name = mtl_name_temp[:index]
# else:
# old_mtl_name = mtl_name_temp # 或者你希望的其他处理逻辑,比如直接使用原字符串或报错
# old_mtl_name = f"{old_mtl_name}.mtl"
#old_mtl_path = os.path.join(original_obj_pid_dir, old_mtl_name)
bpy.ops.wm.obj_import(filepath=obj_path)
imported_object = bpy.context.object
@@ -117,4 +98,30 @@ def transform_save(obj_path,homo_matrix):
path_mode='AUTO'
)
#os.remove(old_mtl_path)
#os.remove(old_mtl_path)
if __name__ == "__main__":
script_args = []
try:
separator_index = sys.argv.index("--")
script_args = sys.argv[separator_index + 1:]
except ValueError:
pass
parser = argparse.ArgumentParser()
parser.add_argument("--objPathName", type=str, required=True, help="obj文件路径")
parser.add_argument("--trans", type=str, required=True, help="旋转参数(JSON格式)")
args = parser.parse_args(script_args)
print(f"objPathName={args.objPathName}, trans={args.trans}")
# 解析 JSON 字符串为列表
try:
homo_matrix = json.loads(args.trans)
except json.JSONDecodeError as e:
print(f"错误: 无法解析 trans 参数为 JSON: {e}")
sys.exit(1)
transform_save(args.objPathName, homo_matrix)