Browse Source

新增导出克重计算

master
dongchangxi 11 months ago
parent
commit
c45d3c27ec
  1. 163
      tools/cal_weight_excel.py

163
tools/cal_weight_excel.py

@ -0,0 +1,163 @@ @@ -0,0 +1,163 @@
import os, sys, time, bpy, bmesh, shutil,requests,json
import platform
if platform.system() == 'Windows':
sys.path.append('e:\\libs\\')
else:
sys.path.append('/data/deploy/make3d/make2/libs/')
import config, libs, libs_db
import pandas as pd
def bmesh_copy_from_object(obj, transform=True, triangulate=True, apply_modifiers=False):
"""Returns a transformed, triangulated copy of the mesh"""
assert obj.type == 'MESH'
if apply_modifiers and obj.modifiers:
import bpy
depsgraph = bpy.context.evaluated_depsgraph_get()
obj_eval = obj.evaluated_get(depsgraph)
me = obj_eval.to_mesh()
bm = bmesh.new()
bm.from_mesh(me)
obj_eval.to_mesh_clear()
else:
me = obj.data
if obj.mode == 'EDIT':
bm_orig = bmesh.from_edit_mesh(me)
bm = bm_orig.copy()
else:
bm = bmesh.new()
bm.from_mesh(me)
if transform:
matrix = obj.matrix_world.copy()
if not matrix.is_identity:
bm.transform(matrix)
matrix.translation.zero()
if not matrix.is_identity:
bm.normal_update()
if triangulate:
bmesh.ops.triangulate(bm, faces=bm.faces)
return bm
def reload_obj(pid, action):
obj_filename = os.path.join(config.workdir, action, pid, f'{pid}.obj')
bpy.ops.wm.read_homefile()
bpy.ops.object.delete(use_global=False, confirm=False)
bpy.ops.import_scene.obj(filepath=obj_filename)
bpy.context.scene.unit_settings.scale_length = 0.001
bpy.context.scene.unit_settings.length_unit = 'CENTIMETERS'
bpy.context.scene.unit_settings.mass_unit = 'GRAMS'
obj = bpy.context.selected_objects[0]
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
return obj
def cal_weight(obj, size):
# 统一缩放到9cm标准尺寸
scale = size / obj.dimensions.z
obj.scale = (scale, scale, scale)
bpy.ops.object.transform_apply(scale=True)
# bpy.ops.wm.save_as_mainfile(filepath=os.path.join(config.workdir, action, pid, f'{pid}-{size/10}cm.blend'))
model_info = {}
bm = bmesh_copy_from_object(obj)
model_info['volume'] = round(bm.calc_volume() / 1000)
model_info['weight'] = round(model_info['volume'] * 1.226)
print(f'{size/10}cm:体积 {model_info["volume"]}cm³, 克重 {model_info["weight"]}g')
return model_info["weight"]
def main(action, pid, sizes,orderId=0):
libs.down_obj_from_oss(config.workdir, pid, action)
obj = reload_obj(pid, action)
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
print(f'模型{pid}的体积与克重估算信息:')
arrData = {}
excel_file = 'weights.xlsx' # 替换为你的文件路径
df = pd.read_excel(excel_file)
new_data = {
'pid': [pid], # 假设新的 pid 是 '002'
'6cm': [0], # 新的 6cm 数据
'7cm': [0], # 新的 7cm 数据
'8cm': [0], # 新的 8cm 数据
}
for size in sizes:
size = float(size)
weight = cal_weight(obj, size)
size = str(size/10)+"cm"
arrData[size] = str(weight)+"g"
if size+"cm" in df.columns:
new_data[size+"cm"] = [weight]
# 更新数据
# 将新数据转换为 DataFrame
new_df = pd.DataFrame(new_data)
# 将新数据附加到原始 DataFrame 中
df = pd.concat([df, new_df], ignore_index=True)
# 保存更新后的 Excel 文件
df.to_excel(excel_file, index=False)
#请求接口进行更新数据
arrParams = {"pid":pid,"order_id":orderId}
if action == "auto":
arrParams["auto_weight"] = json.dumps(arrData)
if action == "print":
arrParams["print_weight"] = json.dumps(arrData)
#发起请求
print(f"请求的参数-{arrParams}")
url = "https://mp.api.suwa3d.com/api/physical/addPhysicalWeight"
res = requests.post(url,data=arrParams)
print('res:', res.text)
#获取真实身高
def get_real_height(pid):
get_real_height_url = 'https://mp.api.suwa3d.com/api/physical/infoByPid'
print(f'pid: {pid}')
res = requests.get(get_real_height_url, params={'pid': pid})
res = json.loads(res.text)
if res['code'] == -1:
print(f'Error: {res["message"]}, return default height 160')
return 0
height = res['data']['height']
if height == 0:
print(f'Error: height=0, return default height 160')
return 0
print(f'height: {height}')
return height
if __name__ == '__main__':
pids = [235777,235759,235272,235022,234683,234678,233973,233634,233618,233589,233588,233106,232673,231611,231604,231394,231306,231061,230812,230528,230523,230513,230329,230328,230327,230325,230272,230268,230259,229967,229935,229634,229380,229309,229308,229202,228698,228693,227372,227357,227295,227103,227026,226503,226502,226280,226137,225927,225438,225077,225059,224944,224898,224894,224493,224237,224229,224136,224131,223139,223137,222902,222897,222636,222516,222507,222488,222467,222278,222251,222248,222232,222196,221876,221679,221672,220781,220748,218310,217964,217050,216319,216311,215959,215488,215477,215466,214955,214951,214729,214726,212891,212169,211726,209906,208801,208798,208765,208706,208702,208640,208634,207864,207724,207722,207510,207363,204787,204700,203993,203657,203373,203340,203320,203243,203239,203235,203223,202784,202362,202351,200744,200205,200202,200152,200081]
for pid in pids:
#获取真实身高
realHeight = get_real_height(pid)
if realHeight == 0:
continue
sizes = []
#根据真实身高计算不同的比例的高度
arrBiLie = [1/4,1/6.1/8,1/10,1/12,1/16,1/18,1/24,1/32,1/43,1/64]
for biLie in arrBiLie:
if biLie == 1/43:
if realHeight < 140:
continue
if biLie == 1/64:
if realHeight < 160:
continue
tempHeight = realHeight * biLie
#取整
tempHeight = round(tempHeight)
#拼接字符串
tempHeight = str(tempHeight)+"_"+str(biLie)
sizes.append(tempHeight)
print(sizes)
#main("print", pid, sizes)
Loading…
Cancel
Save