Skip to content

Commit

Permalink
Refactor keypoint script and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
aelmiger committed Mar 8, 2024
1 parent e73d3b4 commit c5a08a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
7 changes: 0 additions & 7 deletions syclops/blender/sensor_outputs/keypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ def generate_output(self, parent_class: object):
depsgraph = bpy.context.view_layer.depsgraph
scene = bpy.context.scene
camera = scene.camera
# Camera matrices
modelview_matrix = camera.matrix_world.inverted()
projection_matrix = camera.calc_matrix_camera(
bpy.context.evaluated_depsgraph_get(), x=bpy.context.scene.render.resolution_x, y=bpy.context.scene.render.resolution_y
)
render_scale = scene.render.resolution_percentage / 100
width = int(scene.render.resolution_x * render_scale)
height = int(scene.render.resolution_y * render_scale)


for object_instance in depsgraph.object_instances:
if object_instance.object.type == "MESH":
parent = object_instance.parent
Expand Down Expand Up @@ -101,7 +95,6 @@ def _prepare_output_folder(self, sensor_name):
utility.create_folder(output_folder)
return output_folder


def write_meta_output_file(self, file: Path, sensor_name: str):
"""Write the metadata output to a YAML file."""
output_path = file.parent
Expand Down
23 changes: 17 additions & 6 deletions syclops/utility/keypoint_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,45 @@
import bpy
import mathutils


# Function to get relative position
def get_relative_position(obj, target):
return target.matrix_world.inverted() @ obj.matrix_world.translation


# Function to create empties at keypoint positions relative to the mesh object
def create_empties_from_keypoints(mesh_object):
keypoints = mesh_object["keypoints"]
for key, pos in keypoints.items():
# Calculate the world position from the relative position
world_position = mesh_object.matrix_world @ mathutils.Vector((pos['x'], pos['y'], pos['z']))
world_position = mesh_object.matrix_world @ mathutils.Vector(
(pos["x"], pos["y"], pos["z"])
)
# Create an empty and set its world position
bpy.ops.object.empty_add(location=world_position)
empty = bpy.context.active_object
empty.name = f"Keypoint_{key}"


# Main script
selected_objects = bpy.context.selected_objects
active_object = bpy.context.active_object

# Case 1: Multiple objects selected, last is a mesh
if len(selected_objects) > 1 and active_object.type == 'MESH':
if len(selected_objects) > 1 and active_object.type == "MESH":
selected_objects.remove(active_object)
selected_objects.sort(key=lambda x: x.name)

keypoints = {}

for index, obj in enumerate(selected_objects):
if obj.type == 'EMPTY':
if obj.type == "EMPTY":
relative_pos = get_relative_position(obj, active_object)
keypoints[str(index)] = {'x': relative_pos.x, 'y': relative_pos.y, 'z': relative_pos.z}
keypoints[str(index)] = {
"x": relative_pos.x,
"y": relative_pos.y,
"z": relative_pos.z,
}

if "keypoints" in active_object:
del active_object["keypoints"]
Expand All @@ -43,7 +52,7 @@ def create_empties_from_keypoints(mesh_object):
print("Key points added to", active_object.name)

# Case 2: Single mesh object with keypoints attribute
elif len(selected_objects) == 1 and selected_objects[0].type == 'MESH':
elif len(selected_objects) == 1 and selected_objects[0].type == "MESH":
active_object = selected_objects[0]
if "keypoints" in active_object:
create_empties_from_keypoints(active_object)
Expand All @@ -52,4 +61,6 @@ def create_empties_from_keypoints(mesh_object):
print("No keypoints attribute found in", active_object.name)

else:
print("Please select either multiple objects with a mesh as the active object, or a single mesh with a keypoints attribute.")
print(
"Please select either multiple objects with a mesh as the active object, or a single mesh with a keypoints attribute."
)

0 comments on commit c5a08a0

Please sign in to comment.