less copy protection, more size visualization
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

132 行
3.7 KiB

  1. import bpy
  2. from mathutils import Vector, Euler
  3. from math import pi, sqrt
  4. import json
  5. import os
  6. import pathlib
  7. VIEW_DATA = {
  8. "Front": [0, 1, 2, "Front"],
  9. "Angled": [0.5, 1, 2, "Angled"],
  10. "Side": [1, 1, 2, "Side"],
  11. "Back Angled": [1.5, 1, 2, "Back Angled"],
  12. "Back": [2, 1, 2, "Back"],
  13. "Top": [0, 0, 1, "Top"],
  14. "Bottom": [0, 2, 1, "Bottom"]
  15. }
  16. def get_bounds(objects):
  17. xl = []
  18. yl = []
  19. zl = []
  20. for obj in objects:
  21. if not obj.hide_render:
  22. for bounds in obj.bound_box:
  23. v = obj.matrix_world @ Vector(bounds)
  24. xl += [v[0] for c in obj.bound_box]
  25. yl += [v[1] for c in obj.bound_box]
  26. zl += [v[2] for c in obj.bound_box]
  27. return (
  28. Vector([min(xl), min(yl), min(zl)]),
  29. Vector([max(xl), max(yl), max(zl)])
  30. )
  31. path_info = pathlib.Path(bpy.data.filepath).parent.joinpath("macrovision-directory.txt")
  32. config_path = pathlib.Path(open(path_info).read().strip())
  33. json_path = config_path.joinpath("config.json")
  34. config = json.load(open(json_path.resolve(), encoding="utf-8"))
  35. parent_workdir = config["work-directory"]
  36. c = bpy.data.objects["cam"]
  37. c.data.type = "ORTHO"
  38. bpy.data.scenes["Scene"].render.resolution_x = 2000
  39. bpy.data.scenes["Scene"].render.resolution_y = 2000
  40. bpy.data.scenes["Scene"].render.film_transparent = True
  41. bpy.data.scenes["Scene"].view_settings.view_transform = "Raw"
  42. bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[1].default_value = 0
  43. mv = bpy.data.collections["Macrovision"]
  44. collections = mv.children
  45. all_data = {}
  46. all_data["name"] = mv["MVName"]
  47. all_data["kind"] = mv["MVKind"]
  48. all_data["forms"] = []
  49. default_views = []
  50. for view in mv["MVViews"].split(","):
  51. default_views.append(VIEW_DATA[view])
  52. workdir = pathlib.Path(parent_workdir).joinpath(all_data["name"])
  53. os.makedirs(workdir, exist_ok=True)
  54. for coll in collections:
  55. coll.hide_render = True
  56. for coll in collections:
  57. coll.hide_render = False
  58. bpy.ops.object.select_all(action='DESELECT')
  59. for obj in coll.objects:
  60. obj.select_set(True)
  61. data = {}
  62. data["name"] = coll.name
  63. data["views"] = []
  64. bound_min, bound_max = get_bounds(coll.objects)
  65. dimensions = bound_max - bound_min
  66. size = max(dimensions)
  67. global_bbox_center = 0.5 * (bound_min + bound_max)
  68. view_list = []
  69. if "Views" in coll:
  70. for view in coll["Views"].split(","):
  71. view_list.append(VIEW_DATA[view])
  72. else:
  73. view_list = default_views
  74. for angles in view_list:
  75. c.location = global_bbox_center
  76. c.rotation_euler = Euler([angles[1] * pi / 2, 0, angles[0] * pi / 2])
  77. print(list(bound_min) + list(bound_max))
  78. _, c.data.ortho_scale = c.camera_fit_coords(bpy.context.evaluated_depsgraph_get(), list(bound_min) + list(bound_max))
  79. c.location = Vector([c.location[0], c.location[1], c.location[2]])
  80. c.data.ortho_scale *= 1.2
  81. rot = c.rotation_euler.to_matrix()
  82. rot.invert()
  83. c.location += Vector([0, 0, size * 2]) @ rot
  84. c.data.clip_start = size / 4
  85. c.data.clip_end = size * 8
  86. data["views"].append({
  87. "name": angles[3],
  88. "height": dimensions[angles[2]]
  89. })
  90. if "Volume" in coll:
  91. data["views"][-1]["volume"] = coll["Volume"]
  92. if "Mass" in coll:
  93. data["views"][-1]["mass"] = coll["Mass"]
  94. filename = f"{coll.name}-{angles[3]}.png"
  95. bpy.context.scene.render.filepath = workdir.joinpath(filename).resolve().__str__()
  96. bpy.ops.render.render(write_still = True)
  97. all_data["forms"].append(data)
  98. coll.hide_render = True
  99. with open(workdir.joinpath("data.json"), "w") as file:
  100. json.dump(all_data, file)