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.
 
 
 

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