less copy protection, more size visualization
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.
 
 
 

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