-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: Adjust dicing rate and fix displacement parameter handling * fix: Increase precision of keypoint location rounding * refactor: Simplify keypoint handling and improve naming conventions in keypoint_script * feat: Extend bounding box parsing to include rotation data in viewer_utils * refactor: Rename 'translation' to 'location' in base_schema.yaml for clarity * bump: Update version to 1.3.10 in pyproject.toml
- Loading branch information
Showing
6 changed files
with
38 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,45 @@ | ||
""" | ||
Script to be used inside of Blender to add keypoint information to 3d objects | ||
""" | ||
|
||
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"]) | ||
) | ||
# Create an empty and set its world position | ||
world_position = mesh_object.matrix_world @ mathutils.Vector((pos["x"], pos["y"], pos["z"])) | ||
bpy.ops.object.empty_add(location=world_position) | ||
empty = bpy.context.active_object | ||
empty.name = f"Keypoint_{key}" | ||
|
||
empty.name = f"Keypoint_{mesh_object.name}_{key}" | ||
|
||
def add_keypoints_to_mesh(mesh_object, empty_objects): | ||
mesh_object["keypoints"] = {} | ||
for index, empty in enumerate(empty_objects): | ||
relative_pos = get_relative_position(empty, mesh_object) | ||
mesh_object["keypoints"][str(index)] = { | ||
"x": relative_pos.x, | ||
"y": relative_pos.y, | ||
"z": relative_pos.z, | ||
} | ||
|
||
# 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": | ||
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": | ||
relative_pos = get_relative_position(obj, active_object) | ||
keypoints[str(index)] = { | ||
"x": relative_pos.x, | ||
"y": relative_pos.y, | ||
"z": relative_pos.z, | ||
} | ||
|
||
if "keypoints" in active_object: | ||
del active_object["keypoints"] | ||
|
||
active_object["keypoints"] = keypoints | ||
print("Key points added to", active_object.name) | ||
|
||
# Case 2: Single mesh object with keypoints attribute | ||
mesh_objects = [obj for obj in selected_objects if obj.type == "MESH"] | ||
empty_objects = [obj for obj in selected_objects if obj.type == "EMPTY"] | ||
|
||
if mesh_objects and empty_objects: | ||
# Sort empty objects by name | ||
empty_objects.sort(key=lambda x: x.name) | ||
|
||
for mesh in mesh_objects: | ||
add_keypoints_to_mesh(mesh, empty_objects) | ||
print(f"Key points added to {mesh.name}") | ||
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) | ||
print("Empties created from key points in", active_object.name) | ||
print(f"Empties created from key points in {active_object.name}") | ||
else: | ||
print("No keypoints attribute found in", active_object.name) | ||
|
||
print(f"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 mesh objects and at least one empty, or a single mesh with a keypoints attribute.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters