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.
92 lines
3.1 KiB
92 lines
3.1 KiB
import os, sys, time, bpy, bmesh, shutil |
|
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 |
|
|
|
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') |
|
|
|
def main(action, pid, sizes): |
|
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}的体积与克重估算信息:') |
|
for size in sizes: |
|
size = float(size) |
|
cal_weight(obj, size) |
|
|
|
if __name__ == '__main__': |
|
sizes = (90, 120, 150, 180) |
|
|
|
if len(sys.argv) == 3: |
|
action = sys.argv[1] |
|
pids = sys.argv[2].split(',') |
|
for pid in pids: |
|
main(action, pid, sizes) |
|
elif len(sys.argv) == 4: |
|
action = sys.argv[1] |
|
pids = sys.argv[2].split(',') |
|
sizes = sys.argv[3].split(',') |
|
for pid in pids: |
|
main(action, pid, sizes) |
|
print('Usage: python cal_weight.py <pids>') |