You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
5.5 KiB
179 lines
5.5 KiB
import oss2 |
|
import os |
|
from tqdm import tqdm |
|
import os |
|
from pathlib import Path |
|
|
|
import os |
|
import argparse |
|
|
|
from dataclasses import dataclass |
|
|
|
from config import oss_config |
|
from config import print_data_dir |
|
from config import url_get_oss_suffix_by_orderId |
|
from config import url_get_info_by_print_batch_id |
|
|
|
from general import is_use_debug_oss |
|
from general import transform_save_bpy |
|
from general import read_models_from_json |
|
from general import is_small_machine |
|
|
|
from compute_print_net import get_oss_client |
|
|
|
from download_print_out import DataTransfer |
|
from download_print_out import download_data_by_json |
|
|
|
import requests |
|
import shutil |
|
|
|
@dataclass |
|
class BatchModelInfo: |
|
order_id: str |
|
pid: str |
|
print_order_id: str |
|
model_size: str |
|
path: str |
|
count: str |
|
|
|
def read_paths_from_batch(batch_id): |
|
|
|
res = requests.get(f"{url_get_info_by_print_batch_id}{batch_id}") |
|
|
|
datas = res.json()["data"] |
|
print("datas=",datas) |
|
|
|
list_print_model_info = [] |
|
for data in datas: |
|
batch_model_info = BatchModelInfo( |
|
order_id=data["order_id"], |
|
pid=data["pid"], |
|
print_order_id=data["print_order_id"], |
|
model_size=data["model_size"], |
|
path=data["path"], |
|
count=data["quantity"] |
|
|
|
) |
|
list_print_model_info.append(batch_model_info) |
|
|
|
return list_print_model_info, datas |
|
|
|
def download_data_by_batch(batch_model_info, workdir, oss_client ): |
|
try: |
|
target_dir = f"{workdir}" |
|
|
|
print("target_dir=", target_dir) |
|
path = batch_model_info.path |
|
download_textures = DataTransfer(target_dir, f"{path}/", oss_client) |
|
|
|
if not download_textures.download_data_rename_batch(batch_model_info): |
|
print("fail download_data_rename_batch") |
|
return False |
|
# 下载后检查目标文件夹是否为空 |
|
if os.path.exists(target_dir) and not os.listdir(target_dir): |
|
shutil.rmtree(target_dir) |
|
print(f"下载后检查发现目标文件夹为空,已删除: {target_dir}") |
|
except Exception as e: |
|
print(f"下载失败: {path}, 错误: {str(e)}") |
|
pass |
|
|
|
return True |
|
|
|
def download_datas_by_batch(batch_id, workdir, oss_config): |
|
oss_client = get_oss_client(oss_config) |
|
|
|
# 读取所有path |
|
list_print_model_info, datas = read_paths_from_batch(batch_id) |
|
print(f"从文件读取了 {len(list_print_model_info)} 个path") |
|
# 批量下载 |
|
for batch_model_info in list_print_model_info: |
|
print(f"开始下载print_model_info: {batch_model_info}") |
|
if not download_data_by_batch(batch_model_info, workdir, oss_client): |
|
return datas, False |
|
|
|
return datas, True |
|
|
|
def download_datas_by_pre_layout(list_print_model_info, workdir, oss_config): |
|
oss_client = get_oss_client(oss_config) |
|
|
|
print(f"从文件读取了 {len(list_print_model_info)} 个path") |
|
# 批量下载 |
|
for batch_model_info in list_print_model_info: |
|
print(f"开始下载print_model_info: {batch_model_info}") |
|
if not download_data_by_batch(batch_model_info, workdir, oss_client): |
|
return False |
|
return True |
|
|
|
def download_datas_by_json(json_name, workdir, oss_config): |
|
oss_client = get_oss_client(oss_config) |
|
|
|
json_path = os.path.join(workdir, f"{json_name}.json") |
|
|
|
list_model_info, json_data = read_models_from_json(json_path) |
|
print(f"从文件读取了 {len(list_model_info)} 个model") |
|
# 批量下载 |
|
for model_info in list_model_info: |
|
print(f"开始下载PID: {model_info}") |
|
download_data_by_json(model_info, workdir, oss_client) |
|
|
|
return json_data |
|
|
|
def upload_result(base_original_obj_dir, oss_config, batch_id): |
|
|
|
oss_client = get_oss_client(oss_config) |
|
|
|
try: |
|
target_dir = f"{base_original_obj_dir}" |
|
|
|
oss_batch_dir = "batchPrint" |
|
print(f"is_use_debug_oss={is_use_debug_oss()}") |
|
if is_use_debug_oss(): |
|
oss_batch_dir = "batchPrint/debug_hsc" |
|
|
|
print(f"target_dir={target_dir}, batch_id={batch_id}") |
|
data_transfer = DataTransfer(f"{target_dir}/{batch_id}.json", f"{oss_batch_dir}/{batch_id}/{batch_id}.json", oss_client) |
|
data_transfer.upload_data() |
|
data_transfer = DataTransfer(f"{target_dir}/{batch_id}.jpg", f"{oss_batch_dir}/{batch_id}/{batch_id}.jpg", oss_client) |
|
data_transfer.upload_data() |
|
|
|
except Exception as e: |
|
print(f"失败: {batch_id}, 错误: {str(e)}") |
|
pass |
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser() |
|
|
|
is_by_batch = False |
|
batch_id = 10118 |
|
if is_by_batch: |
|
# 通过batch_id下载 |
|
|
|
""" |
|
parser.add_argument("--batch_id", type=str, required=True, help="batch_id") |
|
args = parser.parse_args() |
|
batch_id = args.batch_id |
|
""" |
|
|
|
workdir = f"{print_data_dir}{batch_id}" |
|
|
|
is_transform_save = False |
|
datas, succ = download_datas_by_batch(batch_id, workdir, oss_config) |
|
if is_transform_save: |
|
layout_data = datas["layout_data"] |
|
transform_save_bpy(layout_data, workdir) |
|
else: |
|
# 通过Json下载 |
|
|
|
""" |
|
parser.add_argument("--batch_id", type=str, required=True, help="batch_id") |
|
args = parser.parse_args() |
|
batch_id = args.batch_id |
|
""" |
|
|
|
workdir = f"{print_data_dir}{batch_id}" |
|
|
|
json_name = batch_id |
|
|
|
json_data = download_datas_by_json(json_name, workdir, oss_config) |
|
if is_small_machine(json_data): |
|
transform_save_bpy(json_data, workdir)
|
|
|