|
| 1 | +import bpy |
| 2 | +import math |
| 3 | +from mathutils import Vector |
| 4 | +from typing import Union |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +def save_obj(target_obj_file_path: Union[Path, str], additional_objs_to_save=None, simplification_ratio=None): |
| 9 | + """ |
| 10 | + save the object and returns a mesh duplicate version of it |
| 11 | + """ |
| 12 | + obj = select_shape() |
| 13 | + refresh_obj_in_viewport(obj) |
| 14 | + dup_obj = copy(obj) |
| 15 | + # set active |
| 16 | + bpy.ops.object.select_all(action='DESELECT') |
| 17 | + dup_obj.select_set(True) |
| 18 | + bpy.context.view_layer.objects.active = dup_obj |
| 19 | + # apply the modifier to turn the geometry node to a mesh |
| 20 | + bpy.ops.object.modifier_apply(modifier="GeometryNodes") |
| 21 | + if simplification_ratio and simplification_ratio < 1.0: |
| 22 | + bpy.ops.object.modifier_add(type='DECIMATE') |
| 23 | + dup_obj.modifiers["Decimate"].decimate_type = 'COLLAPSE' |
| 24 | + dup_obj.modifiers["Decimate"].ratio = simplification_ratio |
| 25 | + bpy.ops.object.modifier_apply(modifier="Decimate") |
| 26 | + assert dup_obj.type == 'MESH' |
| 27 | + bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) |
| 28 | + # set origin to center of bounding box |
| 29 | + bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS') |
| 30 | + dup_obj.location.x = dup_obj.location.y = dup_obj.location.z = 0 |
| 31 | + normalize_scale(dup_obj) |
| 32 | + if additional_objs_to_save: |
| 33 | + for additional_obj in additional_objs_to_save: |
| 34 | + additional_obj.select_set(True) |
| 35 | + # save |
| 36 | + bpy.ops.export_scene.obj(filepath=str(target_obj_file_path), use_selection=True, use_materials=False, use_triangles=True) |
| 37 | + return dup_obj |
| 38 | + |
| 39 | + |
| 40 | +def get_geometric_nodes_modifier(obj): |
| 41 | + # loop through all modifiers of the given object |
| 42 | + gnodes_mod = None |
| 43 | + for modifier in obj.modifiers: |
| 44 | + # check if current modifier is the geometry nodes modifier |
| 45 | + if modifier.type == "NODES": |
| 46 | + gnodes_mod = modifier |
| 47 | + break |
| 48 | + return gnodes_mod |
| 49 | + |
| 50 | + |
| 51 | +def normalize_scale(obj): |
| 52 | + obj.select_set(True) |
| 53 | + bpy.context.view_layer.objects.active = obj |
| 54 | + bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) |
| 55 | + # set origin to the center of the bounding box |
| 56 | + bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS') |
| 57 | + |
| 58 | + obj.location.x = 0 |
| 59 | + obj.location.y = 0 |
| 60 | + obj.location.z = 0 |
| 61 | + |
| 62 | + bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) |
| 63 | + max_vert_dist = math.sqrt(max([v.co.dot(v.co) for v in obj.data.vertices])) |
| 64 | + |
| 65 | + for v in obj.data.vertices: |
| 66 | + v.co /= max_vert_dist |
| 67 | + |
| 68 | + bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) |
| 69 | + |
| 70 | + # verify that the shape is normalized |
| 71 | + # max_vert_dist = math.sqrt(max([v.co.dot(v.co) for v in obj.data.vertices])) |
| 72 | + # assert abs(max_vert_dist - 1.0) < 0.01 |
| 73 | + |
| 74 | + |
| 75 | +def setup_lights(): |
| 76 | + """ |
| 77 | + setup lights for rendering |
| 78 | + used for visualization of 3D objects as images |
| 79 | + """ |
| 80 | + scene = bpy.context.scene |
| 81 | + # light 1 |
| 82 | + light_data_1 = bpy.data.lights.new(name="light_data_1", type='POINT') |
| 83 | + light_data_1.energy = 300 |
| 84 | + light_object_1 = bpy.data.objects.new(name="Light_1", object_data=light_data_1) |
| 85 | + light_object_1.location = Vector((10, -10, 10)) |
| 86 | + scene.collection.objects.link(light_object_1) |
| 87 | + # light 2 |
| 88 | + light_data_2 = bpy.data.lights.new(name="light_data_2", type='POINT') |
| 89 | + light_data_2.energy = 300 |
| 90 | + light_object_2 = bpy.data.objects.new(name="Light_2", object_data=light_data_2) |
| 91 | + light_object_2.location = Vector((-10, -10, 10)) |
| 92 | + scene.collection.objects.link(light_object_2) |
| 93 | + # light 3 |
| 94 | + light_data_3 = bpy.data.lights.new(name="light_data_3", type='POINT') |
| 95 | + light_data_3.energy = 300 |
| 96 | + light_object_3 = bpy.data.objects.new(name="Light_3", object_data=light_data_3) |
| 97 | + light_object_3.location = Vector((10, 0, 10)) |
| 98 | + scene.collection.objects.link(light_object_3) |
| 99 | + |
| 100 | + |
| 101 | +def look_at(obj_camera, point): |
| 102 | + """ |
| 103 | + orient the given camera with a fixed position to loot at a given point in space |
| 104 | + """ |
| 105 | + loc_camera = obj_camera.matrix_world.to_translation() |
| 106 | + direction = point - loc_camera |
| 107 | + # point the cameras '-Z' and use its 'Y' as up |
| 108 | + rot_quat = direction.to_track_quat('-Z', 'Y') |
| 109 | + obj_camera.rotation_euler = rot_quat.to_euler() |
| 110 | + |
| 111 | + |
| 112 | +def clean_scene(start_with_strings=["Camera", "procedural", "Light"]): |
| 113 | + """ |
| 114 | + delete all object of which the name's prefix is matching any of the given strings |
| 115 | + """ |
| 116 | + scene = bpy.context.scene |
| 117 | + bpy.ops.object.select_all(action='DESELECT') |
| 118 | + for obj in scene.objects: |
| 119 | + if any([obj.name.startswith(starts_with_string) for starts_with_string in start_with_strings]): |
| 120 | + # select the object |
| 121 | + if obj.visible_get(): |
| 122 | + obj.select_set(True) |
| 123 | + bpy.ops.object.delete() |
| 124 | + |
| 125 | + |
| 126 | +def del_obj(obj): |
| 127 | + bpy.ops.object.select_all(action='DESELECT') |
| 128 | + obj.select_set(True) |
| 129 | + bpy.ops.object.delete() |
| 130 | + |
| 131 | + |
| 132 | +def refresh_obj_in_viewport(obj): |
| 133 | + # the following two line cause the object to update according to the new geometric nodes input |
| 134 | + obj.show_bounds = not obj.show_bounds |
| 135 | + obj.show_bounds = not obj.show_bounds |
| 136 | + |
| 137 | + |
| 138 | +def select_objs(*objs): |
| 139 | + bpy.ops.object.select_all(action='DESELECT') |
| 140 | + for i, obj in enumerate(objs): |
| 141 | + if i == 0: |
| 142 | + bpy.context.view_layer.objects.active = obj |
| 143 | + obj.select_set(True) |
| 144 | + |
| 145 | + |
| 146 | +def select_obj(obj): |
| 147 | + select_objs(obj) |
| 148 | + |
| 149 | + |
| 150 | +def select_shape(): |
| 151 | + """ |
| 152 | + select the procedural shape in the blend file |
| 153 | + note that in all our domains, the procedural shape is named "procedural shape" within the blend file |
| 154 | + """ |
| 155 | + obj = bpy.data.objects["procedural shape"] |
| 156 | + select_obj(obj) |
| 157 | + return obj |
| 158 | + |
| 159 | + |
| 160 | +def copy(obj): |
| 161 | + dup_obj = obj.copy() |
| 162 | + dup_obj.data = obj.data.copy() |
| 163 | + dup_obj.animation_data_clear() |
| 164 | + bpy.context.collection.objects.link(dup_obj) |
| 165 | + return dup_obj |
0 commit comments