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.
 
 
 

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