Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

Aaron/remove legacy blender code (#572)

Aaron Cherianff1a69f62b6e
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/export_to_blender.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/export_to_blender.py
index 44546cfd..22d89f01 100644
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/export_to_blender.py
+++ b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/export_to_blender.py
@@ -5,15 +5,6 @@ from typing import Union
from freemocap.core_processes.export_data.blender_stuff.export_to_blender.methods.ajc_addon.run_ajc_addon_main import (
run_ajc_blender_addon_subprocess,
)
-from freemocap.core_processes.export_data.blender_stuff.export_to_blender.methods.legacy.run_alpha_megascript import (
- run_alpha_megascript,
-)
-from freemocap.core_processes.export_data.blender_stuff.export_to_blender.methods.legacy.run_cgtinker_method import (
- run_cgtinker_method,
-)
-from freemocap.core_processes.export_data.blender_stuff.export_to_blender.methods.legacy.run_megascript_take2 import (
- run_blender_megascript_take2,
-)
from freemocap.core_processes.export_data.blender_stuff.get_best_guess_of_blender_path import (
get_best_guess_of_blender_path,
)
@@ -38,25 +29,6 @@ def export_to_blender(
blender_exe_path=blender_exe_path,
)
- elif method == "megascript_take2":
- run_blender_megascript_take2(
- recording_folder_path=recording_folder_path,
- blender_file_path=blender_file_path,
- blender_exe_path=blender_exe_path,
- )
- elif method == "alpha_megascript":
- run_alpha_megascript(
- recording_folder_path=recording_folder_path,
- blender_file_path=blender_file_path,
- blender_exe_path=blender_exe_path,
- )
- elif method == "cgtinker":
- run_cgtinker_method(
- recording_folder_path=recording_folder_path,
- blender_file_path=blender_file_path,
- blender_exe_path=blender_exe_path,
- )
-
logger.info("Done with Blender Export :D")
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/__init__.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/__init__.py
deleted file mode 100644
index e69de29b..00000000
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/alpha_freemocap_blender_megascript.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/alpha_freemocap_blender_megascript.py
deleted file mode 100644
index 4ecb7dc3..00000000
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/alpha_freemocap_blender_megascript.py
+++ /dev/null
@@ -1,1687 +0,0 @@
-import sys
-from pathlib import Path
-
-import bpy
-import numpy as np
-
-print(" - Starting (alpha) blender megascript - ")
-
-#######################################################################
-##% Activate necessary addons
-bpy.ops.preferences.addon_enable(module="io_import_images_as_planes")
-bpy.ops.preferences.addon_enable(module="rigify")
-
-
-#######################################################################
-
-
-def get_video_paths(path_to_video_folder: Path) -> list:
- """Search the folder for 'mp4' files (case insensitive) and return them as a list"""
-
- list_of_video_paths = list(Path(path_to_video_folder).glob("*.mp4")) + list(
- Path(path_to_video_folder).glob("*.MP4")
- )
- unique_list_of_video_paths = get_unique_list(list_of_video_paths)
-
- return unique_list_of_video_paths
-
-
-def get_unique_list(list: list) -> list:
- """Return a list of the unique elements from input list"""
- unique_list = []
- [unique_list.append(clip) for clip in list if clip not in unique_list]
-
- return unique_list
-
-
-try:
- ##% Session path
- # #get session path as command line argument
- argv = sys.argv
- argv = argv[argv.index("--") + 1 :]
- session_path = argv[0]
-
- blend_file_save_path = argv[1]
- if len(argv) > 2:
- good_clean_frame_number = int(argv[2])
- else:
- good_clean_frame_number = 0
-except:
- print("we appear to be running from the Blender Scripting tab! Manually enter your `session_path` at line 23")
- session_path = Path(
- r"C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data\sesh_2022-05-07_17_15_05_pupil_wobble_juggle_0"
- )
- good_clean_frame_number = 3341
-
-print(f"Using session path - {str(session_path)}")
-session_path = Path(session_path)
-
-path_to_mediapipe_npy = (
- session_path / "output_data" / "raw_data" / "mediapipe3dData_numFrames_numTrackedPoints_spatialXYZ.npy"
-)
-path_to_mediapipe_3d_reproj = (
- session_path / "output_data" / "raw_data" / "mediapipe3dData_numFrames_numTrackedPoints_reprojectionError.npy"
-)
-
-print(f"Loading mediapipe data from {path_to_mediapipe_npy}")
-#######################################################################
-# %% load mediapipe data
-# %%
-print("loading data")
-# %%
-print(f"Loading mediapipe data from {path_to_mediapipe_npy}")
-mediapipe_skel_fr_mar_dim = np.load(str(path_to_mediapipe_npy))
-print("mediapipe_skel_fr_mar_dim.shape", mediapipe_skel_fr_mar_dim.shape)
-mediapipe_skel_fr_mar_dim = mediapipe_skel_fr_mar_dim / 1000 # convert to meters
-
-print(f"Loading mediapipe reprojection error data from {path_to_mediapipe_3d_reproj}")
-mediapipe_reprojection_error_fr_mar = np.load(str(path_to_mediapipe_3d_reproj))
-print(f"mediapipe_reprojection_error_fr_mar.shape = {mediapipe_reprojection_error_fr_mar.shape}")
-
-body_marker_range = np.arange(0, 33)
-right_hand_marker_range = np.arange(33, 33 + 21)
-left_hand_marker_range = np.arange(33 + 21, 33 + 21 + 21)
-face_marker_range = np.arange(left_hand_marker_range[-1], mediapipe_skel_fr_mar_dim.shape[1])
-
-body_skel_fr_mar_dim = mediapipe_skel_fr_mar_dim[
- :,
- body_marker_range,
- :,
-]
-right_hand_fr_mar_dim = mediapipe_skel_fr_mar_dim[
- :,
- right_hand_marker_range,
- :,
-]
-left_hand_fr_mar_dim = mediapipe_skel_fr_mar_dim[
- :,
- left_hand_marker_range,
- :,
-]
-face_fr_mar_dim = mediapipe_skel_fr_mar_dim[
- :,
- face_marker_range,
- :,
-]
-
-# %% Session Specific stuff
-number_of_frames = mediapipe_skel_fr_mar_dim.shape[0]
-start_frame = 1
-end_frame = number_of_frames
-
-bpy.context.scene.frame_start = start_frame
-bpy.context.scene.frame_end = end_frame
-
-########################
-## Find Good Clean Frame with some fiddly nonsense
-if good_clean_frame_number == 0:
- frame_nan_counts = []
- frame_mean_reproj_error = []
-
- for this_frame in range(mediapipe_skel_fr_mar_dim.shape[0]):
- frame_nan_counts.append(np.sum(np.isnan(mediapipe_skel_fr_mar_dim[this_frame, :, 0])))
- frame_mean_reproj_error.append(np.nanmean(mediapipe_reprojection_error_fr_mar[this_frame, :]))
-
- nan_times_vis = np.array(frame_nan_counts) * np.array(frame_mean_reproj_error)
- num_frames = len(frame_nan_counts)
- # nan_times_vis[0:int(num_frames/5)] = np.nanmax(nan_times_vis)
- # nan_times_vis[-int(num_frames/5):-1] = np.nanmax(nan_times_vis)
-
- good_clean_frame_number = np.nanargmin(
- nan_times_vis
- ) # the frame with the fewest nans (i.e. hopefully a frame where all tracked points are visible)
-
-print(f"----good_clean_frame_number: {good_clean_frame_number}----")
-
-#######################################################################
-# %% Mediapipe tracked point names
-mediapipe_tracked_point_names = [
- "nose",
- "left_eye_inner",
- "left_eye",
- "left_eye_outer",
- "right_eye_inner",
- "right_eye",
- "right_eye_outer",
- "left_ear",
- "right_ear",
- "mouth_left",
- "mouth_right",
- "left_shoulder",
- "right_shoulder",
- "left_elbow",
- "right_elbow",
- "left_wrist",
- "right_wrist",
- "left_pinky",
- "right_pinky",
- "left_index",
- "right_index",
- "left_thumb",
- "right_thumb",
- "left_hip",
- "right_hip",
- "left_knee",
- "right_knee",
- "left_ankle",
- "right_ankle",
- "left_heel",
- "right_heel",
- "left_foot_index",
- "right_foot_index",
- "right_hand_wrist",
- "right_hand_thumb_cmc",
- "right_hand_thumb_mcp",
- "right_hand_thumb_ip",
- "right_hand_thumb_tip",
- "right_hand_index_finger_mcp",
- "right_hand_index_finger_pip",
- "right_hand_index_finger_dip",
- "right_hand_index_finger_tip",
- "right_hand_middle_finger_mcp",
- "right_hand_middle_finger_pip",
- "right_hand_middle_finger_dip",
- "right_hand_middle_finger_tip",
- "right_hand_ring_finger_mcp",
- "right_hand_ring_finger_pip",
- "right_hand_ring_finger_dip",
- "right_hand_ring_finger_tip",
- "right_hand_pinky_finger_mcp",
- "right_hand_pinky_finger_pip",
- "right_hand_pinky_finger_dip",
- "right_hand_pinky_finger_tip",
- "left_hand_wrist",
- "left_hand_thumb_cmc",
- "left_hand_thumb_mcp",
- "left_hand_thumb_ip",
- "left_hand_thumb_tip",
- "left_hand_index_finger_mcp",
- "left_hand_index_finger_pip",
- "left_hand_index_finger_dip",
- "left_hand_index_finger_tip",
- "left_hand_middle_finger_mcp",
- "left_hand_middle_finger_pip",
- "left_hand_middle_finger_dip",
- "left_hand_middle_finger_tip",
- "left_hand_ring_finger_mcp",
- "left_hand_ring_finger_pip",
- "left_hand_ring_finger_dip",
- "left_hand_ring_finger_tip",
- "left_hand_pinky_finger_mcp",
- "left_hand_pinky_finger_pip",
- "left_hand_pinky_finger_dip",
- "left_hand_pinky_finger_tip",
-]
-
-#####################################################################
-## Blender Stuff
-#####################################################################
-
-try:
- #####################################################################
- ###%% clear scene
- print("clearing scene")
- try:
- bpy.ops.object.mode_set(mode="OBJECT")
- except:
- pass
-
- try:
- bpy.ops.object.select_all(action="SELECT") # select all objects
- bpy.ops.object.delete(use_global=True) # delete all objects from all scenes
- except:
- pass
-
- #######################################################################
- # %% create world origin
-
- bpy.ops.object.empty_add(type="ARROWS")
- world_origin_axes = bpy.context.active_object
- world_origin_axes.name = "world_origin" # will stay at origin
- # inspect(world_origin_axes, methods=True)
-
- bpy.ops.object.empty_add(type="ARROWS")
- freemocap_origin_axes = bpy.context.active_object
- freemocap_origin_axes.name = (
- "freemocap_origin_axes" # will translate to put skelly on ground symmetric-ish about origin
- )
-
- #######################################################################
- # %% load empties
- print("loading {} empties on {} frames".format(len(mediapipe_tracked_point_names), number_of_frames))
-
- empty_size = 0.01
-
- for this_point_index, this_point_name in enumerate(mediapipe_tracked_point_names):
- print(f"loading {this_point_name}...")
- bpy.ops.object.empty_add(type="SPHERE")
- this_empty = bpy.context.active_object
- this_empty.name = this_point_name
-
- if "face" in this_point_name:
- this_empty.scale = [empty_size / 4] * 3
- if "hand" in this_point_name:
- this_empty.scale = [empty_size / 2] * 3
- else:
- this_empty.scale = [empty_size] * 3
-
- this_empty.parent = freemocap_origin_axes
-
- this_point_fr_xyz = mediapipe_skel_fr_mar_dim[:, this_point_index, :]
-
- for frame_num in range(end_frame):
- this_empty.location = [
- this_point_fr_xyz[frame_num, 0],
- this_point_fr_xyz[frame_num, 1],
- this_point_fr_xyz[frame_num, 2],
- ]
- bpy.context.view_layer.update()
-
- this_empty.keyframe_insert(data_path="location", frame=frame_num)
-
- #######################################################################
- # %% create virtual markers
- left_ear_index = 7
- right_ear_index = 8
- #######################################################################
- print("creating virtual markers")
- print("head_center - midway between left and right ears")
- hips_xyz = (mediapipe_skel_fr_mar_dim[:, left_ear_index, :] + mediapipe_skel_fr_mar_dim[:, right_ear_index, :]) / 2
- bpy.ops.object.empty_add(type="SPHERE")
- this_empty = bpy.context.active_object
- this_empty.name = "head_center"
- this_empty.scale = [empty_size] * 3
- this_empty.parent = freemocap_origin_axes
- mediapipe_tracked_point_names.append(this_empty.name)
-
- for frame_num in range(end_frame):
- this_empty.location = [
- hips_xyz[frame_num, 0],
- hips_xyz[frame_num, 1],
- hips_xyz[frame_num, 2],
- ]
- this_empty.keyframe_insert(data_path="location", frame=frame_num)
-
- print("neck_center - midway between left and right shoulders")
- left_shoulder_index = 11
- right_shoulder_index = 12
- neck_xyz = (
- mediapipe_skel_fr_mar_dim[:, left_shoulder_index, :] + mediapipe_skel_fr_mar_dim[:, right_shoulder_index, :]
- ) / 2
- bpy.ops.object.empty_add(type="PLAIN_AXES")
- this_empty = bpy.context.active_object
- this_empty.name = "neck_center"
- this_empty.scale = [empty_size] * 3
- this_empty.parent = freemocap_origin_axes
- mediapipe_tracked_point_names.append(this_empty.name)
-
- for frame_num in range(end_frame):
- this_empty.location = [
- neck_xyz[frame_num, 0],
- neck_xyz[frame_num, 1],
- neck_xyz[frame_num, 2],
- ]
- this_empty.keyframe_insert(data_path="location", frame=frame_num)
-
- print("hip_center - midway between left and right hips")
- left_hip_index = 23
- right_hip_index = 24
- hips_xyz = (mediapipe_skel_fr_mar_dim[:, left_hip_index, :] + mediapipe_skel_fr_mar_dim[:, right_hip_index, :]) / 2
- bpy.ops.object.empty_add(type="PLAIN_AXES")
- this_empty = bpy.context.active_object
- this_empty.name = "hip_center"
- this_empty.scale = [empty_size] * 3
- this_empty.parent = freemocap_origin_axes
- mediapipe_tracked_point_names.append(this_empty.name)
- rig_constraint_dict_of_dicts = {
- "spine": {
- "COPY_LOCATION": "hip_center",
- "STRETCH_TO": "chest_center",
- },
- "spine.001": {
- "STRETCH_TO": "neck_center",
- },
- "spine.002": {
- "STRETCH_TO": "head_center",
- },
- "spine.003": {
- "STRETCH_TO": "nose",
- },
- "pelvis.L": {
- "STRETCH_TO": "left_hip",
- },
- "thigh.L": {
- "COPY_LOCATION": "left_hip",
- "STRETCH_TO": "left_knee",
- },
- "shin.L": {
- "STRETCH_TO": "left_ankle",
- },
- "foot.L": {
- "STRETCH_TO": "left_foot_index",
- },
- "heel.02.L": {
- "STRETCH_TO": "left_ankle",
- },
- "shoulder.L": {
- "COPY_LOCATION": "neck_center",
- "STRETCH_TO": "left_shoulder",
- },
- "upper_arm.L": {
- "STRETCH_TO": "left_elbow",
- },
- "forearm.L": {
- "STRETCH_TO": "left_wrist",
- },
- "hand.L": {
- "STRETCH_TO": "left_index",
- },
- "pelvis.R": {
- "STRETCH_TO": "right_hip",
- },
- "thigh.R": {
- "COPY_LOCATION": "right_hip",
- "STRETCH_TO": "right_knee",
- },
- "shin.R": {
- "STRETCH_TO": "right_ankle",
- },
- "foot.R": {
- "STRETCH_TO": "right_foot_index",
- },
- "heel.02.R": {
- "STRETCH_TO": "right_ankle",
- },
- "shoulder.R": {
- "COPY_LOCATION": "neck_center",
- "STRETCH_TO": "right_shoulder",
- },
- "upper_arm.R": {
- "STRETCH_TO": "right_elbow",
- },
- "forearm.R": {
- "STRETCH_TO": "right_wrist",
- },
- "hand.R": {
- "STRETCH_TO": "right_index",
- },
- "thumb.01.R": {
- "COPY_LOCATION": "right_wrist",
- "STRETCH_TO": "right_hand_thumb_mcp",
- },
- "thumb.02.R": {
- "STRETCH_TO": "right_hand_thumb_ip",
- },
- "thumb.02.R": {
- "STRETCH_TO": "right_hand_thumb_tip",
- },
- "palm.01.R": {
- "COPY_LOCATION": "right_wrist",
- "STRETCH_TO": "right_hand_index_finger_mcp",
- },
- "f_index.01.R": {
- "STRETCH_TO": "right_hand_index_finger_pip",
- },
- "f_index.02.R": {
- "STRETCH_TO": "right_hand_index_finger_dip",
- },
- "f_index.03.R": {
- "STRETCH_TO": "right_hand_index_finger_tip",
- },
- "palm.02.R": {
- "COPY_LOCATION": "right_wrist",
- "STRETCH_TO": "right_hand_middle_finger_mcp",
- },
- "f_middle.01.R": {
- "STRETCH_TO": "right_hand_middle_finger_pip",
- },
- "f_middle.02.R": {
- "STRETCH_TO": "right_hand_middle_finger_dip",
- },
- "f_middle.03.R": {
- "STRETCH_TO": "right_hand_middle_finger_tip",
- },
- "palm.03.R": {
- "COPY_LOCATION": "right_wrist",
- "STRETCH_TO": "right_hand_ring_finger_mcp",
- },
- "f_ring.01.R": {
- "STRETCH_TO": "right_hand_ring_finger_pip",
- },
- "f_ring.02.R": {
- "STRETCH_TO": "right_hand_ring_finger_dip",
- },
- "f_ring.03.R": {
- "STRETCH_TO": "right_hand_ring_finger_tip",
- },
- "palm.04.R": {
- "COPY_LOCATION": "right_wrist",
- "STRETCH_TO": "right_hand_pinky_finger_mcp",
- },
- "f_pinky.01.R": {
- "STRETCH_TO": "right_hand_pinky_finger_pip",
- },
- "f_pinky.02.R": {
- "STRETCH_TO": "right_hand_pinky_finger_dip",
- },
- "f_pinky.03.R": {
- "STRETCH_TO": "right_hand_pinky_finger_tip",
- },
- "thumb.01.L": {
- "COPY_LOCATION": "left_wrist",
- "STRETCH_TO": "left_hand_thumb_mcp",
- },
- "thumb.02.L": {
- "STRETCH_TO": "left_hand_thumb_ip",
- },
- "thumb.03.L": {
- "STRETCH_TO": "left_hand_thumb_tip",
- },
- "palm.01.L": {
- "COPY_LOCATION": "left_wrist",
- "STRETCH_TO": "left_hand_index_finger_mcp",
- },
- "f_index.01.L": {
- "STRETCH_TO": "left_hand_index_finger_pip",
- },
- "f_index.02.L": {
- "STRETCH_TO": "left_hand_index_finger_dip",
- },
- "f_index.03.L": {
- "STRETCH_TO": "left_hand_index_finger_tip",
- },
- "palm.02.L": {
- "COPY_LOCATION": "left_wrist",
- "STRETCH_TO": "left_hand_middle_finger_mcp",
- },
- "f_middle.01.L": {
- "STRETCH_TO": "left_hand_middle_finger_pip",
- },
- "f_middle.02.L": {
- "STRETCH_TO": "left_hand_middle_finger_dip",
- },
- "f_middle.03.L": {
- "STRETCH_TO": "left_hand_middle_finger_tip",
- },
- "palm.03.L": {
- "COPY_LOCATION": "left_wrist",
- "STRETCH_TO": "left_hand_ring_finger_mcp",
- },
- "f_ring.01.L": {
- "STRETCH_TO": "left_hand_ring_finger_pip",
- },
- "f_ring.02.L": {
- "STRETCH_TO": "left_hand_ring_finger_dip",
- },
- "f_ring.03.L": {
- "STRETCH_TO": "left_hand_ring_finger_tip",
- },
- "palm.04.L": {
- "COPY_LOCATION": "left_wrist",
- "STRETCH_TO": "left_hand_pinky_finger_mcp",
- },
- "f_pinky.01.L": {
- "STRETCH_TO": "left_hand_pinky_finger_pip",
- },
- "f_pinky.02.L": {
- "STRETCH_TO": "left_hand_pinky_finger_dip",
- },
- "f_pinky.03.L": {
- "STRETCH_TO": "left_hand_pinky_finger_tip",
- },
- }
-
- # loop through dictionary applying constraints
- try:
- bpy.ops.object.mode_set(mode="POSE")
- except:
- pass
-
- for frame_num in range(end_frame):
- this_empty.location = [
- hips_xyz[frame_num, 0],
- hips_xyz[frame_num, 1],
- hips_xyz[frame_num, 2],
- ]
- this_empty.keyframe_insert(data_path="location", frame=frame_num)
-
- print("chest_center - mean of R/L shoulders and R/L hips")
- chest_xyz = (neck_xyz + hips_xyz) / 2
- bpy.ops.object.empty_add(type="PLAIN_AXES")
- this_empty = bpy.context.active_object
- this_empty.name = "chest_center"
- this_empty.scale = [empty_size] * 3
- this_empty.parent = freemocap_origin_axes
- mediapipe_tracked_point_names.append(this_empty.name)
-
- for frame_num in range(end_frame):
- this_empty.location = [
- chest_xyz[frame_num, 0],
- chest_xyz[frame_num, 1],
- chest_xyz[frame_num, 2],
- ]
- this_empty.keyframe_insert(data_path="location", frame=frame_num)
-
- print("Done loading empties :D")
-
- ######################################################################
- ## Fit Rigify metarig armatures to empties
-
- bpy.context.scene.frame_set(good_clean_frame_number)
-
- # each bone name has to match one in the Blender Rigify Human Metarig, or it will be ignored
- metarig_bone_empty_pairs_dict_of_dicts = {
- "nose.001": {
- "head": "head_center",
- "tail": "nose",
- },
- "nose.L.001": {
- "head": "nose",
- "tail": "left_eye_inner",
- },
- "eye.L": {
- "head": "left_eye_inner",
- "tail": "left_eye",
- },
- "lid.B.L": {
- "head": "left_eye",
- "tail": "left_eye_outer",
- },
- "ear.L": {
- "head": "left_eye_outer",
- "tail": "left_ear",
- },
- "nose.R.001": {
- "head": "nose",
- "tail": "right_eye_inner",
- },
- "eye.R": {
- "head": "right_eye_inner",
- "tail": "right_eye",
- },
- "lid.B.R": {
- "head": "right_eye",
- "tail": "right_eye_outer",
- },
- "ear.R": {
- "head": "right_eye_outer",
- "tail": "right_ear",
- },
- "lip.T.R": {
- "head": "nose",
- "tail": "mouth_right",
- },
- "lip.B.R": {
- "head": "mouth_right",
- "tail": "mouth_left",
- },
- "lip.T.L": {
- "head": "nose",
- "tail": "mouth_left",
- },
- "hand.L": {
- "head": "left_wrist",
- "tail": "left_index",
- },
- "forearm.L": {
- "head": "left_elbow",
- "tail": "left_wrist",
- },
- "upper_arm.L": {
- "head": "left_shoulder",
- "tail": "left_elbow",
- },
- "shoulder.L": {
- "head": "neck_center",
- "tail": "left_shoulder",
- },
- "hand.R": {
- "head": "right_wrist",
- "tail": "right_index",
- },
- "forearm.R": {
- "head": "right_elbow",
- "tail": "right_wrist",
- },
- "upper_arm.R": {
- "head": "right_shoulder",
- "tail": "right_elbow",
- },
- "shoulder.R": {
- "head": "neck_center",
- "tail": "right_shoulder",
- },
- "thigh.L": {
- "head": "left_hip",
- "tail": "left_knee",
- },
- "shin.L": {
- "head": "left_knee",
- "tail": "left_ankle",
- },
- "heel.02.L": {
- "head": "left_heel",
- "tail": "left_ankle",
- },
- "foot.L": {
- "head": "left_ankle",
- "tail": "left_foot_index",
- },
- "toe.L": {
- "head": "left_foot_index",
- "tail": "left_heel",
- },
- "thigh.R": {
- "head": "right_hip",
- "tail": "right_knee",
- },
- "shin.R": {
- "head": "right_knee",
- "tail": "right_ankle",
- },
- "heel.02.R": {
- "head": "right_heel",
- "tail": "right_ankle",
- },
- "foot.R": {
- "head": "right_ankle",
- "tail": "right_foot_index",
- },
- "toe.R": {
- "head": "right_foot_index",
- "tail": "right_heel",
- },
- "pelvis.L": {
- "head": "hip_center",
- "tail": "left_hip",
- },
- "pelvis.R": {
- "head": "hip_center",
- "tail": "right_hip",
- },
- "spine": {
- "head": "hip_center",
- "tail": "chest_center",
- },
- "spine.001": {
- "head": "chest_center",
- "tail": "neck_center",
- },
- "spine.002": {
- "head": "neck_center",
- "tail": "head_center",
- },
- "spine.003": {
- "head": "head_center",
- "tail": "nose",
- },
- "palm.01.R": {
- "head": "right_wrist",
- "tail": "right_hand_index_finger_mcp",
- },
- "palm.02.R": {
- "head": "right_wrist",
- "tail": "right_hand_middle_finger_mcp",
- },
- "palm.03.R": {
- "head": "right_wrist",
- "tail": "right_hand_ring_finger_mcp",
- },
- "palm.04.R": {
- "head": "right_wrist",
- "tail": "right_hand_pinky_finger_mcp",
- },
- "thumb.01.R": {
- "head": "right_wrist",
- "tail": "right_hand_thumb_mcp",
- },
- "thumb.02.R": {
- "head": "right_hand_thumb_mcp",
- "tail": "right_hand_thumb_ip",
- },
- "thumb.03.R": {
- "head": "right_hand_thumb_ip",
- "tail": "right_hand_thumb_tip",
- },
- "f_index.01.R": {
- "head": "right_hand_index_finger_mcp",
- "tail": "right_hand_index_finger_pip",
- },
- "f_index.02.R": {
- "head": "right_hand_index_finger_pip",
- "tail": "right_hand_index_finger_dip",
- },
- "f_index.03.R": {
- "head": "right_hand_index_finger_dip",
- "tail": "right_hand_index_finger_tip",
- },
- "f_middle.01.R": {
- "head": "right_hand_middle_finger_mcp",
- "tail": "right_hand_middle_finger_pip",
- },
- "f_middle.02.R": {
- "head": "right_hand_middle_finger_pip",
- "tail": "right_hand_middle_finger_dip",
- },
- "f_middle.03.R": {
- "head": "right_hand_middle_finger_dip",
- "tail": "right_hand_middle_finger_tip",
- },
- "f_ring.01.R": {
- "head": "right_hand_ring_finger_mcp",
- "tail": "right_hand_ring_finger_pip",
- },
- "f_ring.02.R": {
- "head": "right_hand_ring_finger_pip",
- "tail": "right_hand_ring_finger_dip",
- },
- "f_ring.03.R": {
- "head": "right_hand_ring_finger_dip",
- "tail": "right_hand_ring_finger_tip",
- },
- "f_pinky.01.R": {
- "head": "right_hand_pinky_finger_mcp",
- "tail": "right_hand_pinky_finger_pip",
- },
- "f_pinky.02.R": {
- "head": "right_hand_pinky_finger_pip",
- "tail": "right_hand_pinky_finger_dip",
- },
- "f_pinky.03.R": {
- "head": "right_hand_pinky_finger_dip",
- "tail": "right_hand_pinky_finger_tip",
- },
- "thumb.01.L": {
- "head": "left_wrist",
- "tail": "left_hand_thumb_mcp",
- },
- "thumb.02.L": {
- "head": "left_hand_thumb_mcp",
- "tail": "left_hand_thumb_ip",
- },
- "thumb.03.L": {
- "head": "left_hand_thumb_ip",
- "tail": "left_hand_thumb_tip",
- },
- "palm.01.L": {
- "head": "left_wrist",
- "tail": "left_hand_index_finger_mcp",
- },
- "palm.02.L": {
- "head": "left_wrist",
- "tail": "left_hand_middle_finger_mcp",
- },
- "palm.03.L": {
- "head": "left_wrist",
- "tail": "left_hand_ring_finger_mcp",
- },
- "palm.04.L": {
- "head": "left_wrist",
- "tail": "left_hand_pinky_finger_mcp",
- },
- "f_index.01.L": {
- "head": "left_hand_index_finger_mcp",
- "tail": "left_hand_index_finger_pip",
- },
- "f_index.02.L": {
- "head": "left_hand_index_finger_pip",
- "tail": "left_hand_index_finger_dip",
- },
- "f_index.03.L": {
- "head": "left_hand_index_finger_dip",
- "tail": "left_hand_index_finger_tip",
- },
- "f_middle.01.L": {
- "head": "left_hand_middle_finger_mcp",
- "tail": "left_hand_middle_finger_pip",
- },
- "f_middle.02.L": {
- "head": "left_hand_middle_finger_pip",
- "tail": "left_hand_middle_finger_dip",
- },
- "f_middle.03.L": {
- "head": "left_hand_middle_finger_dip",
- "tail": "left_hand_middle_finger_tip",
- },
- "f_ring.01.L": {
- "head": "left_hand_ring_finger_mcp",
- "tail": "left_hand_ring_finger_pip",
- },
- "f_ring.02.L": {
- "head": "left_hand_ring_finger_pip",
- "tail": "left_hand_ring_finger_dip",
- },
- "f_ring.03.L": {
- "head": "left_hand_ring_finger_dip",
- "tail": "left_hand_ring_finger_tip",
- },
- "f_pinky.01.L": {
- "head": "left_hand_pinky_finger_mcp",
- "tail": "left_hand_pinky_finger_pip",
- },
- "f_pinky.02.L": {
- "head": "left_hand_pinky_finger_pip",
- "tail": "left_hand_pinky_finger_dip",
- },
- "f_pinky.03.L": {
- "head": "left_hand_pinky_finger_dip",
- "tail": "left_hand_pinky_finger_tip",
- },
- }
-
- # %% create metarig
- bpy.ops.object.armature_human_metarig_add()
- this_metarig = bpy.context.active_object
-
- # %% loop through dictionary - set head and tail of each bone's to the relevant empty
- # bpy.ops.object.posemode_toggle()
- try:
- bpy.ops.object.mode_set(mode="EDIT")
- except:
- pass
-
- for this_bone in list(this_metarig.data.bones):
- this_bone_name = this_bone.name
- print(f"--{this_bone_name}--")
-
- this_edit_bone = this_metarig.data.edit_bones[
- this_bone_name
- ] # note this is using EDIT_bones, not regular bones b/c blender is a wild wacky place
-
- if this_bone_name in metarig_bone_empty_pairs_dict_of_dicts:
- this_bone_dict = metarig_bone_empty_pairs_dict_of_dicts[this_bone_name]
-
- this_head_empty_name = this_bone_dict["head"]
- this_tail_empty_name = this_bone_dict["tail"]
- print(
- "setting (bone:head->tail){}:{}->{})".format(
- this_bone_name, this_head_empty_name, this_tail_empty_name
- )
- )
-
- if this_tail_empty_name is not None:
- print(
- f"setting bone: {this_bone_name}: head: {bpy.data.objects[this_head_empty_name].location}, tail: {bpy.data.objects[this_tail_empty_name].location}"
- )
- this_edit_bone.head = bpy.data.objects[this_head_empty_name].location
- this_edit_bone.tail = bpy.data.objects[this_tail_empty_name].location
- else: # remove bones with no tail (and/or head) until we figure out a better plan
- print(f"removing bone: {this_bone_name}")
- this_metarig.data.edit_bones.remove(this_edit_bone)
- else:
- print(f"removing bone: {this_bone_name}")
- this_metarig.data.edit_bones.remove(this_edit_bone)
-
- try:
- bpy.ops.object.mode_set(mode="OBJECT")
- except:
- pass
-
- #######################################################
- #### Damped track bones to empties
- bpy.ops.object.mode_set(mode="OBJECT")
- armature_name = "metarig"
- this_armature = bpy.data.objects[armature_name]
-
- rig_constraint_dict_of_dicts = {
- "nose.001": {
- "COPY_LOCATION": "head_center",
- "DAMPED_TRACK": "nose",
- },
- "nose.L.001": {
- "DAMPED_TRACK": "left_eye_inner",
- },
- "eye.L": {
- "DAMPED_TRACK": "left_eye",
- },
- "lid.B.L": {
- "DAMPED_TRACK": "left_eye_outer",
- },
- "ear.L": {
- "DAMPED_TRACK": "left_ear",
- },
- "nose.R.001": {
- "DAMPED_TRACK": "right_eye_inner",
- },
- "eye.R": {
- "DAMPED_TRACK": "right_eye",
- },
- "lid.B.R": {
- "DAMPED_TRACK": "right_eye_outer",
- },
- "ear.R": {
- "DAMPED_TRACK": "right_ear",
- },
- "lip.T.R": {
- "DAMPED_TRACK": "mouth_right",
- },
- "lip.B.R": {
- "DAMPED_TRACK": "mouth_left",
- },
- "lip.T.L": {
- "DAMPED_TRACK": "nose",
- },
- "spine": {
- "COPY_LOCATION": "hip_center",
- "DAMPED_TRACK": "chest_center",
- },
- "spine.001": {
- "DAMPED_TRACK": "neck_center",
- },
- "spine.002": {
- "DAMPED_TRACK": "head_center",
- },
- "spine.003": {
- "DAMPED_TRACK": "nose",
- },
- "pelvis.L": {
- "DAMPED_TRACK": "left_hip",
- },
- "thigh.L": {
- "COPY_LOCATION": "left_hip",
- "DAMPED_TRACK": "left_knee",
- },
- "shin.L": {
- "DAMPED_TRACK": "left_ankle",
- },
- "foot.L": {
- "DAMPED_TRACK": "left_foot_index",
- },
- "heel.02.L": {
- "DAMPED_TRACK": "left_ankle",
- },
- "shoulder.L": {
- "COPY_LOCATION": "neck_center",
- "DAMPED_TRACK": "left_shoulder",
- },
- "upper_arm.L": {
- "DAMPED_TRACK": "left_elbow",
- },
- "forearm.L": {
- "DAMPED_TRACK": "left_wrist",
- },
- "hand.L": {
- "DAMPED_TRACK": "left_index",
- },
- "pelvis.R": {
- "DAMPED_TRACK": "right_hip",
- },
- "thigh.R": {
- "COPY_LOCATION": "right_hip",
- "DAMPED_TRACK": "right_knee",
- },
- "shin.R": {
- "DAMPED_TRACK": "right_ankle",
- },
- "foot.R": {
- "DAMPED_TRACK": "right_foot_index",
- },
- "heel.02.R": {
- "DAMPED_TRACK": "right_ankle",
- },
- "shoulder.R": {
- "COPY_LOCATION": "neck_center",
- "DAMPED_TRACK": "right_shoulder",
- },
- "upper_arm.R": {
- "DAMPED_TRACK": "right_elbow",
- },
- "forearm.R": {
- "DAMPED_TRACK": "right_wrist",
- },
- "hand.R": {
- "DAMPED_TRACK": "right_index",
- },
- "thumb.01.R": {
- "COPY_LOCATION": "right_wrist",
- "DAMPED_TRACK": "right_hand_thumb_mcp",
- },
- "thumb.02.R": {
- "DAMPED_TRACK": "right_hand_thumb_ip",
- },
- "thumb.02.R": {
- "DAMPED_TRACK": "right_hand_thumb_tip",
- },
- "palm.01.R": {
- "COPY_LOCATION": "right_wrist",
- "DAMPED_TRACK": "right_hand_index_finger_mcp",
- },
- "f_index.01.R": {
- "DAMPED_TRACK": "right_hand_index_finger_pip",
- },
- "f_index.02.R": {
- "DAMPED_TRACK": "right_hand_index_finger_dip",
- },
- "f_index.03.R": {
- "DAMPED_TRACK": "right_hand_index_finger_tip",
- },
- "palm.02.R": {
- "COPY_LOCATION": "right_wrist",
- "DAMPED_TRACK": "right_hand_middle_finger_mcp",
- },
- "f_middle.01.R": {
- "DAMPED_TRACK": "right_hand_middle_finger_pip",
- },
- "f_middle.02.R": {
- "DAMPED_TRACK": "right_hand_middle_finger_dip",
- },
- "f_middle.03.R": {
- "DAMPED_TRACK": "right_hand_middle_finger_tip",
- },
- "palm.03.R": {
- "COPY_LOCATION": "right_wrist",
- "DAMPED_TRACK": "right_hand_ring_finger_mcp",
- },
- "f_ring.01.R": {
- "DAMPED_TRACK": "right_hand_ring_finger_pip",
- },
- "f_ring.02.R": {
- "DAMPED_TRACK": "right_hand_ring_finger_dip",
- },
- "f_ring.03.R": {
- "DAMPED_TRACK": "right_hand_ring_finger_tip",
- },
- "palm.04.R": {
- "COPY_LOCATION": "right_wrist",
- "DAMPED_TRACK": "right_hand_pinky_finger_mcp",
- },
- "f_pinky.01.R": {
- "DAMPED_TRACK": "right_hand_pinky_finger_pip",
- },
- "f_pinky.02.R": {
- "DAMPED_TRACK": "right_hand_pinky_finger_dip",
- },
- "f_pinky.03.R": {
- "DAMPED_TRACK": "right_hand_pinky_finger_tip",
- },
- "thumb.01.L": {
- "COPY_LOCATION": "left_wrist",
- "DAMPED_TRACK": "left_hand_thumb_mcp",
- },
- "thumb.02.L": {
- "DAMPED_TRACK": "left_hand_thumb_ip",
- },
- "thumb.03.L": {
- "DAMPED_TRACK": "left_hand_thumb_tip",
- },
- "palm.01.L": {
- "COPY_LOCATION": "left_wrist",
- "DAMPED_TRACK": "left_hand_index_finger_mcp",
- },
- "f_index.01.L": {
- "DAMPED_TRACK": "left_hand_index_finger_pip",
- },
- "f_index.02.L": {
- "DAMPED_TRACK": "left_hand_index_finger_dip",
- },
- "f_index.03.L": {
- "DAMPED_TRACK": "left_hand_index_finger_tip",
- },
- "palm.02.L": {
- "COPY_LOCATION": "left_wrist",
- "DAMPED_TRACK": "left_hand_middle_finger_mcp",
- },
- "f_middle.01.L": {
- "DAMPED_TRACK": "left_hand_middle_finger_pip",
- },
- "f_middle.02.L": {
- "DAMPED_TRACK": "left_hand_middle_finger_dip",
- },
- "f_middle.03.L": {
- "DAMPED_TRACK": "left_hand_middle_finger_tip",
- },
- "palm.03.L": {
- "COPY_LOCATION": "left_wrist",
- "DAMPED_TRACK": "left_hand_ring_finger_mcp",
- },
- "f_ring.01.L": {
- "DAMPED_TRACK": "left_hand_ring_finger_pip",
- },
- "f_ring.02.L": {
- "DAMPED_TRACK": "left_hand_ring_finger_dip",
- },
- "f_ring.03.L": {
- "DAMPED_TRACK": "left_hand_ring_finger_tip",
- },
- "palm.04.L": {
- "COPY_LOCATION": "left_wrist",
- "DAMPED_TRACK": "left_hand_pinky_finger_mcp",
- },
- "f_pinky.01.L": {
- "DAMPED_TRACK": "left_hand_pinky_finger_pip",
- },
- "f_pinky.02.L": {
- "DAMPED_TRACK": "left_hand_pinky_finger_dip",
- },
- "f_pinky.03.L": {
- "DAMPED_TRACK": "left_hand_pinky_finger_tip",
- },
- }
-
- # loop through dictionary applying constraints
- try:
- bpy.ops.object.mode_set(mode="POSE")
- except:
- pass
-
- for this_bone_name, this_bone_dict in rig_constraint_dict_of_dicts.items():
- print(f"---Setting constraints for bone:{this_bone_name}---")
-
- for (
- this_constraint_name,
- this_constraint_target_empty_name,
- ) in this_bone_dict.items():
- print(f"constraint: {this_constraint_name} with target:{this_constraint_target_empty_name}")
- print("grab bone")
- this_bone = this_armature.pose.bones[this_bone_name]
- print("apply bone")
- this_constraint = this_bone.constraints.new(type=this_constraint_name)
- this_constraint.name = this_constraint_name
- this_constraint.target = bpy.data.objects[
- this_constraint_target_empty_name
- ] # point constraint at relevant empty object
-
- ##############
- ## create extra bones
-
- extra_bone_empty_pairs_dict_of_dicts = {
- "hand.L": {
- "head": "left_wrist",
- "tail": "left_index",
- },
- "forearm.L": {
- "head": "left_elbow",
- "tail": "left_wrist",
- },
- }
-
- ###################################################################
- #### Create Stick Figure Mesh
-
- stick_figure_mesh_edge_pairs_dict_of_dicts = {
- "nose.001": {
- "head": "head_center",
- "tail": "nose",
- },
- "nose.L.001": {
- "head": "nose",
- "tail": "left_eye_inner",
- },
- "eye.L": {
- "head": "left_eye_inner",
- "tail": "left_eye",
- },
- "lid.B.L": {
- "head": "left_eye",
- "tail": "left_eye_outer",
- },
- "ear.L": {
- "head": "left_eye_outer",
- "tail": "left_ear",
- },
- "nose.R.001": {
- "head": "nose",
- "tail": "right_eye_inner",
- },
- "eye.R": {
- "head": "right_eye_inner",
- "tail": "right_eye",
- },
- "lid.B.R": {
- "head": "right_eye",
- "tail": "right_eye_outer",
- },
- "ear.R": {
- "head": "right_eye_outer",
- "tail": "right_ear",
- },
- "lip.T.R": {
- "head": "nose",
- "tail": "mouth_right",
- },
- "lip.B.R": {
- "head": "mouth_right",
- "tail": "mouth_left",
- },
- "lip.T.L": {
- "head": "nose",
- "tail": "mouth_left",
- },
- "forearm.L": {
- "head": "left_elbow",
- "tail": "left_wrist",
- },
- "upper_arm.L": {
- "head": "left_shoulder",
- "tail": "left_elbow",
- },
- "shoulder.L": {
- "head": "neck_center",
- "tail": "left_shoulder",
- },
- "forearm.R": {
- "head": "right_elbow",
- "tail": "right_wrist",
- },
- "upper_arm.R": {
- "head": "right_shoulder",
- "tail": "right_elbow",
- },
- "shoulder.R": {
- "head": "neck_center",
- "tail": "right_shoulder",
- },
- "thigh.L": {
- "head": "left_hip",
- "tail": "left_knee",
- },
- "shin.L": {
- "head": "left_knee",
- "tail": "left_ankle",
- },
- "heel.02.L": {
- "head": "left_heel",
- "tail": "left_ankle",
- },
- "foot.L": {
- "head": "left_ankle",
- "tail": "left_foot_index",
- },
- "toe.L": {
- "head": "left_foot_index",
- "tail": "left_heel",
- },
- "thigh.R": {
- "head": "right_hip",
- "tail": "right_knee",
- },
- "shin.R": {
- "head": "right_knee",
- "tail": "right_ankle",
- },
- "heel.02.R": {
- "head": "right_heel",
- "tail": "right_ankle",
- },
- "foot.R": {
- "head": "right_ankle",
- "tail": "right_foot_index",
- },
- "toe.R": {
- "head": "right_foot_index",
- "tail": "right_heel",
- },
- "pelvis.L": {
- "head": "hip_center",
- "tail": "left_hip",
- },
- "pelvis.R": {
- "head": "hip_center",
- "tail": "right_hip",
- },
- "spine": {
- "head": "hip_center",
- "tail": "chest_center",
- },
- "spine.001": {
- "head": "chest_center",
- "tail": "neck_center",
- },
- "spine.002": {
- "head": "neck_center",
- "tail": "head_center",
- },
- "spine.003": {
- "head": "head_center",
- "tail": "nose",
- },
- "palm.01.R": {
- "head": "right_wrist",
- "tail": "right_hand_index_finger_mcp",
- },
- "palm.02.R": {
- "head": "right_wrist",
- "tail": "right_hand_middle_finger_mcp",
- },
- "palm.03.R": {
- "head": "right_wrist",
- "tail": "right_hand_ring_finger_mcp",
- },
- "palm.04.R": {
- "head": "right_wrist",
- "tail": "right_hand_pinky_finger_mcp",
- },
- "thumb.01.R": {
- "head": "right_wrist",
- "tail": "right_hand_thumb_cmc",
- },
- "thumb.02.R": {
- "head": "right_hand_thumb_cmc",
- "tail": "right_hand_thumb_mcp",
- },
- "thumb.03.R": {
- "head": "right_hand_thumb_mcp",
- "tail": "right_hand_thumb_ip",
- },
- "thumb.04.R": {
- "head": "right_hand_thumb_ip",
- "tail": "right_hand_thumb_tip",
- },
- "f_index.01.R": {
- "head": "right_hand_index_finger_mcp",
- "tail": "right_hand_index_finger_pip",
- },
- "f_index.02.R": {
- "head": "right_hand_index_finger_pip",
- "tail": "right_hand_index_finger_dip",
- },
- "f_index.03.R": {
- "head": "right_hand_index_finger_dip",
- "tail": "right_hand_index_finger_tip",
- },
- "f_middle.01.R": {
- "head": "right_hand_middle_finger_mcp",
- "tail": "right_hand_middle_finger_pip",
- },
- "f_middle.02.R": {
- "head": "right_hand_middle_finger_pip",
- "tail": "right_hand_middle_finger_dip",
- },
- "f_middle.03.R": {
- "head": "right_hand_middle_finger_dip",
- "tail": "right_hand_middle_finger_tip",
- },
- "f_ring.01.R": {
- "head": "right_hand_ring_finger_mcp",
- "tail": "right_hand_ring_finger_pip",
- },
- "f_ring.02.R": {
- "head": "right_hand_ring_finger_pip",
- "tail": "right_hand_ring_finger_dip",
- },
- "f_ring.03.R": {
- "head": "right_hand_ring_finger_dip",
- "tail": "right_hand_ring_finger_tip",
- },
- "f_pinky.01.R": {
- "head": "right_hand_pinky_finger_mcp",
- "tail": "right_hand_pinky_finger_pip",
- },
- "f_pinky.02.R": {
- "head": "right_hand_pinky_finger_pip",
- "tail": "right_hand_pinky_finger_dip",
- },
- "f_pinky.03.R": {
- "head": "right_hand_pinky_finger_dip",
- "tail": "right_hand_pinky_finger_tip",
- },
- "thumb.01.L": {
- "head": "left_wrist",
- "tail": "left_hand_thumb_cmc",
- },
- "thumb.02.L": {
- "head": "left_hand_thumb_cmc",
- "tail": "left_hand_thumb_mcp",
- },
- "thumb.03.L": {
- "head": "left_hand_thumb_mcp",
- "tail": "left_hand_thumb_ip",
- },
- "thumb.04.L": {
- "head": "left_hand_thumb_ip",
- "tail": "left_hand_thumb_tip",
- },
- "palm.01.L": {
- "head": "left_wrist",
- "tail": "left_hand_index_finger_mcp",
- },
- "palm.02.L": {
- "head": "left_wrist",
- "tail": "left_hand_middle_finger_mcp",
- },
- "palm.03.L": {
- "head": "left_wrist",
- "tail": "left_hand_ring_finger_mcp",
- },
- "palm.04.L": {
- "head": "left_wrist",
- "tail": "left_hand_pinky_finger_mcp",
- },
- "f_index.01.L": {
- "head": "left_hand_index_finger_mcp",
- "tail": "left_hand_index_finger_pip",
- },
- "f_index.02.L": {
- "head": "left_hand_index_finger_pip",
- "tail": "left_hand_index_finger_dip",
- },
- "f_index.03.L": {
- "head": "left_hand_index_finger_dip",
- "tail": "left_hand_index_finger_tip",
- },
- "f_middle.01.L": {
- "head": "left_hand_middle_finger_mcp",
- "tail": "left_hand_middle_finger_pip",
- },
- "f_middle.02.L": {
- "head": "left_hand_middle_finger_pip",
- "tail": "left_hand_middle_finger_dip",
- },
- "f_middle.03.L": {
- "head": "left_hand_middle_finger_dip",
- "tail": "left_hand_middle_finger_tip",
- },
- "f_ring.01.L": {
- "head": "left_hand_ring_finger_mcp",
- "tail": "left_hand_ring_finger_pip",
- },
- "f_ring.02.L": {
- "head": "left_hand_ring_finger_pip",
- "tail": "left_hand_ring_finger_dip",
- },
- "f_ring.03.L": {
- "head": "left_hand_ring_finger_dip",
- "tail": "left_hand_ring_finger_tip",
- },
- "f_pinky.01.L": {
- "head": "left_hand_pinky_finger_mcp",
- "tail": "left_hand_pinky_finger_pip",
- },
- "f_pinky.02.L": {
- "head": "left_hand_pinky_finger_pip",
- "tail": "left_hand_pinky_finger_dip",
- },
- "f_pinky.03.L": {
- "head": "left_hand_pinky_finger_dip",
- "tail": "left_hand_pinky_finger_tip",
- },
- }
-
- mediapipe_virtual_markers_weights_dict = {
- "head_center": {"right_ear": 0.5, "left_ear": 0.5},
- "neck_center": {"right_shoulder": 0.5, "left_shoulder": 0.5},
- "hip_center": {"right_hip": 0.5, "left_hip": 0.5},
- "chest_center": {
- "right_shoulder": 0.25,
- "left_shoulder": 0.25,
- "right_hip": 0.25,
- "left_hip": 0.25,
- },
- }
-
- print("loading data as stick figure mesh \o/")
- #######################################################################
-
- # %% create virtual marker points (redundant from above, I think ? )
-
- current_frame = bpy.data.scenes[0].frame_current
- virtual_point_index_dict = {}
- for this_virtual_marker_dict in mediapipe_virtual_markers_weights_dict.items():
- this_virtual_marker_name = this_virtual_marker_dict[0]
- these_weighted_points = []
- for this_weighted_point_dict in this_virtual_marker_dict[1].items():
- this_point_name = this_weighted_point_dict[0]
-
- this_weight = this_weighted_point_dict[1]
- these_weighted_points.append(
- mediapipe_skel_fr_mar_dim[:, mediapipe_tracked_point_names.index(this_point_name), :] * this_weight
- )
- this_virtual_point_fr_xyz = np.sum(these_weighted_points, axis=0)
- this_virtual_point_fr_mar_xyz = np.expand_dims(this_virtual_point_fr_xyz, axis=1)
- mediapipe_skel_fr_mar_dim = np.append(mediapipe_skel_fr_mar_dim, this_virtual_point_fr_mar_xyz, axis=1)
- virtual_point_index_dict[this_virtual_marker_name] = mediapipe_skel_fr_mar_dim.shape[1] - 1
- print(
- f"Virtual marker: {this_virtual_marker_name} created at marker index {mediapipe_skel_fr_mar_dim.shape[1] - 1}"
- )
-
- # %% define edges
-
- edges = []
- # regular data
- for this_bone_dict in stick_figure_mesh_edge_pairs_dict_of_dicts.items():
- edge_name = this_bone_dict[0]
- head_name = this_bone_dict[1]["head"]
- tail_name = this_bone_dict[1]["tail"]
-
- try:
- head_index = mediapipe_tracked_point_names.index(head_name)
- tail_index = mediapipe_tracked_point_names.index(tail_name)
-
- if (
- "center" in head_name
- ): # virtual markers are acting weird so we're brute forcing it :-/ this will fail if there is a virtual marker without 'center' in the name....
- head_index = virtual_point_index_dict[head_name]
- if "center" in tail_name:
- tail_index = virtual_point_index_dict[tail_name]
-
- this_edge = (head_index, tail_index)
-
- edges.append(this_edge)
- print(
- f"edge created for bone: ({this_bone_dict[0]} : {head_name}-{tail_name}), indicies({this_edge[0]},{this_edge[1]})"
- )
- except:
- print(f"edge FAILED for for bone:{this_bone_dict[0]} : {head_name}-{tail_name}")
- pass
-
- print("edges created!")
- # #
- # # ##############################
- # # # %% Create mesh
- # # try:
- # # bpy.ops.object.mode_set(mode='OBJECT')
- # # except:
- # # pass
- # #
- # # print('loading verticies')
- # # vertices = mediapipe_skel_fr_mar_dim[current_frame, :,
- # # :] # don't plot face dottos until we know what to do with them
- # # print('done loading verticies')
- # # # edges defined above
- # # faces = []
- # # stick_figure_mesh = bpy.data.meshes.new('stick_figure_mesh')
- # # print('a')
- # # stick_figure_mesh.from_pydata(vertices, edges, faces)
- # # print('b')
- # # stick_figure_mesh.update()
- # # print('c')
- # # # make object from mesh
- # # stick_figure_mesh_object = bpy.data.objects.new('stick_figure_mesh', stick_figure_mesh)
- # # print('d')
- # # # make collection
- # # mesh_collection = bpy.data.collections.new('mesh_collection')
- # # print('e')
- # # bpy.context.scene.collection.children.link(mesh_collection)
- # # print('f')
- # # # add object to scene collection
- # # mesh_collection.objects.link(stick_figure_mesh_object)
- # # print('g')
- # #
- # # bpy.context.view_layer.objects.active = bpy.data.objects['stick_figure_mesh']
- # # print('h')
- #
- # bpy.context.object.mode
- # try:
- # bpy.ops.object.editmode_toggle()
- # except:
- # pass
- # print('i')
- #
- # #### skin that mesh!
- # print('skinning mesh \o/')
- # bpy.data.objects['stick_figure_mesh'].modifiers.new('stick_figure_mesh_skin', 'SKIN')
- # print('resizing mesh')
- # skin_radius = .025
- # bpy.ops.transform.skin_resize(value=(skin_radius, skin_radius, skin_radius))
- #
- # ########
- # # # parent mesh to armature
- # print('parenting mesh to armarture with automatic weights...')
- # bpy.ops.object.mode_set(mode='OBJECT')
- # bpy.data.objects['stick_figure_mesh'].parent = this_metarig
- # bpy.context.view_layer.objects.active = this_metarig
- # bpy.context.active_object.select_set(True)
- # bpy.data.objects['stick_figure_mesh'].select_set(True)
- # bpy.ops.object.parent_set(type='ARMATURE_AUTO')
-
- #####################
- ## Load nSynched Videos
- try:
- print("loading videos as planes...")
- annotated_videos_path = session_path / "annotated_videos"
-
- if annotated_videos_path.is_dir():
- vidFolderPath = annotated_videos_path
- else:
- vidFolderPath = session_path / "synchronized_videos"
-
- world_origin = bpy.data.objects["world_origin"]
-
- number_of_videos = len(list(get_video_paths(vidFolderPath)))
-
- vid_location_scale = 1
-
- for (
- vid_number,
- thisVidPath,
- ) in enumerate(get_video_paths(vidFolderPath)):
- print(thisVidPath)
- # use 'images as planes' add on to load in the video files as planes
- bpy.ops.import_image.to_plane(
- files=[{"name": thisVidPath.name}],
- directory=str(thisVidPath.parent),
- shader="EMISSION",
- )
- this_vid_as_plane = bpy.context.active_object
- this_vid_as_plane.name = "vid_" + str(vid_number)
-
- vid_x = (vid_number - number_of_videos / 2) * vid_location_scale
-
- this_vid_as_plane.location = [
- vid_x,
- vid_location_scale,
- vid_location_scale,
- ]
- this_vid_as_plane.rotation_euler = [np.pi / 2, 0, 0]
- this_vid_as_plane.scale = [vid_location_scale * 1.5] * 3
- this_vid_as_plane.parent = world_origin
- # create a light
- # bpy.ops.object.light_add(type='POINT', radius=1, align='WORLD')
- except:
- print(
- 'Failed to load videos to Blender scene - You might need to install the "images as planes" addon to this version of Blender'
- )
-except Exception as e:
- print(e)
- print(
- "Something broke in the Blender Megascript - Maybe it failed to find a good clean frame and then that borked something downstream?"
- )
-
-# set shading type to 'Material Preview' so you can see the videos
-# bpy.context.space_data.shading.type = "MATERIAL"
-
-# save .blend file
-sessionID = session_path.stem
-
-print(f"Saving Blender output file to: {str(blend_file_save_path)}")
-bpy.ops.wm.save_as_mainfile(filepath=str(blend_file_save_path))
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/cgtinker_blendarmocap_load.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/cgtinker_blendarmocap_load.py
deleted file mode 100644
index d2a2be12..00000000
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/cgtinker_blendarmocap_load.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import logging
-import sys
-
-from BlendArMocap.src.cgt_freemocap.fm_subprocess_cmd_receiver import import_freemocap_session
-
-logger = logging.getLogger(__name__)
-
-argv = sys.argv
-argv = argv[argv.index("--") + 1 :]
-recording_path = argv[0]
-blender_save_path = argv[1]
-bind_to_rig_bool = bool(argv[2])
-load_synchronized_videos_bool = bool(argv[3])
-timeout = bool(argv[4])
-load_raw_bool = bool(argv[5])
-load_quick_bool = bool(argv[6])
-
-logger.info(
- f"Invoking CGTinker's BlendArMocap based export method with arguments -"
- f"\nrecording_path: {recording_path}, "
- f"\nblender_save_path: {blender_save_path}, "
- f"\nbind_to_rig_bool: {bind_to_rig_bool}, "
- f"\nload_synchronized_videos_bool: {load_synchronized_videos_bool}, "
- f"\ntimeout: {timeout}, "
- f"\nload_raw_bool: {load_raw_bool}, "
- f"\nload_quick_bool: {load_quick_bool}"
-)
-
-import_freemocap_session(
- session_directory=recording_path,
- bind_to_rig=bind_to_rig_bool,
- load_synch_videos=load_synchronized_videos_bool,
- timeout=timeout,
- load_raw=load_raw_bool,
- load_quick=load_quick_bool,
-)
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/freemocap_blender_megascript_take2.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/freemocap_blender_megascript_take2.py
deleted file mode 100644
index 77d67d86..00000000
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/freemocap_blender_megascript_take2.py
+++ /dev/null
@@ -1,1240 +0,0 @@
-import json
-import logging
-import sys
-from copy import copy
-from pathlib import Path
-from typing import List, Union, Dict, Tuple
-
-import addon_utils
-import bpy
-import numpy as np
-
-print("Running script to create Blender file from freemocap session data: " + __file__)
-
-logger = logging.getLogger(__name__)
-
-
-###############################################################
-### IF YOU ARE RUNNING THIS SCRIPT FROM WITHIN BLENDER -
-### CHANGE THE PATH TO THE SESSION FOLDER IN THE `if __name__ == "__main__":` BLOCK AT THE **BOTTOM** OF THIS FILE
-### the variable is named `recording_path_input`
-###############################################################
-
-
-def main(
- recording_path: Union[str, Path],
- blender_file_save_path: Union[str, Path],
- mediapipe_empty_names: Dict[str, List[str]],
- create_rig: bool = True,
-):
- ###%% clear the scene - Scorch the earth \o/
- print("Clearing scene...")
- try:
- bpy.ops.object.mode_set(mode="OBJECT")
- except:
- pass
- try:
- bpy.ops.object.select_all(action="SELECT") # select all objects
- bpy.ops.object.delete(use_global=True) # delete all objects from all scenes
- except:
- pass
-
- print("Loading data....")
-
- # paths and whatnot
- try:
- # load mediapipe skeleton data
- path_to_data_arrays_folder = recording_path / "output_data"
- path_to_body_npy = path_to_data_arrays_folder / "mediapipe_body_3d_xyz.npy"
- path_to_right_hand_npy = path_to_data_arrays_folder / "mediapipe_right_hand_3d_xyz.npy"
- path_to_left_hand_npy = path_to_data_arrays_folder / "mediapipe_left_hand_3d_xyz.npy"
- path_to_face_npy = path_to_data_arrays_folder / "mediapipe_face_3d_xyz.npy"
-
- path_to_center_of_mass_npy = path_to_data_arrays_folder / "center_of_mass" / "total_body_center_of_mass_xyz.npy"
- path_to_body_segment_centers_npy = (
- path_to_data_arrays_folder / "center_of_mass" / "segmentCOM_frame_joint_xyz.npy"
- )
-
- mediapipe_body_fr_mar_xyz = np.load(str(path_to_body_npy)) / 1000 # load and convert to meters
- mediapipe_right_hand_fr_mar_xyz = np.load(str(path_to_right_hand_npy)) / 1000
- mediapipe_left_hand_fr_mar_xyz = np.load(str(path_to_left_hand_npy)) / 1000
- mediapipe_face_fr_mar_xyz = np.load(str(path_to_face_npy)) / 1000
-
- center_of_mass_fr_xyz = np.load(str(path_to_center_of_mass_npy)) / 1000
- body_segment_centers_fr_mar_xyz = np.load(str(path_to_body_segment_centers_npy)) / 1000
-
- # load reprojection error
- path_to_reprojection_error_npy = (
- path_to_data_arrays_folder / "raw_data" / "mediapipe3dData_numFrames_numTrackedPoints_reprojectionError.npy"
- )
- mediapipe_reprojection_error_fr_mar = np.load(str(path_to_reprojection_error_npy))
-
- # load skeleton segment lengths
- path_to_segment_length_json = path_to_data_arrays_folder / "mediapipe_skeleton_segment_lengths.json"
- print(f"loading skeleton segment lengths from `json` at - {str(path_to_segment_length_json)}")
- f = open(path_to_segment_length_json)
- skeleton_segment_lengths_dict = json.load(f)
-
- except Exception as e:
- print("Failed to load freemocap data")
- raise e
-
- number_of_frames = mediapipe_body_fr_mar_xyz.shape[0]
- print(f"mediapipe_body_fr_mar_dim.shape: {mediapipe_body_fr_mar_xyz.shape}")
- print(f"mediapipe_reprojection_error_fr_mar.shape: {mediapipe_reprojection_error_fr_mar.shape}")
-
- #########################
- ### Create Origin Axes
-
- bpy.ops.object.empty_add(type="SPHERE", scale=(3.0, 3.0, 3.0))
- world_origin_axes = bpy.context.editable_objects[-1]
- world_origin_axes.name = "world_origin" # will stay at origin
-
- bpy.ops.object.empty_add(type="ARROWS")
- freemocap_origin_axes = bpy.context.editable_objects[-1]
- freemocap_origin_axes.name = (
- "freemocap_origin_axes" # will translate to put skelly on ground symmetric-ish about origin
- )
-
- ##############################
- # %% Set start and end frames
-
- start_frame = 0
- end_frame = number_of_frames
-
- bpy.context.scene.frame_start = start_frame
- bpy.context.scene.frame_end = end_frame
-
- ############################
- ### Load mocap data as empty markers
-
- print(
- "_________________________\n"
- "__________________________\n"
- "Loading freemocap trajectory data as empty markers..."
- "_________________________\n"
- )
-
- mediapipe_body_trajectory_names = [empty_name for empty_name in mediapipe_empty_names["body"]]
-
- mediapipe_right_hand_trajectory_names = [f"right_hand_{empty_name}" for empty_name in mediapipe_empty_names["hand"]]
- mediapipe_left_hand_trajectory_names = [f"left_hand_{empty_name}" for empty_name in mediapipe_empty_names["hand"]]
- # mediapipe_face_trajectory_names = [f"face_{number}:{empty_name}" for number, empty_name in
- # face_contour_marker_indices]
-
- try: # load as much of the data as you can, but if there's an error keep going
- # create empty markers for body
- body_empty_scale = 0.03
- for marker_number in range(mediapipe_body_fr_mar_xyz.shape[1]):
- trajectory_name = mediapipe_body_trajectory_names[marker_number]
- trajectory_fr_xyz = mediapipe_body_fr_mar_xyz[:, marker_number, :]
- create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz=trajectory_fr_xyz,
- trajectory_name=trajectory_name,
- parent_origin=freemocap_origin_axes,
- empty_scale=body_empty_scale,
- empty_type="SPHERE",
- )
-
- # create empty markers for right hand
- hand_empty_scale = body_empty_scale * 0.5
- for marker_number in range(mediapipe_right_hand_fr_mar_xyz.shape[1]):
- trajectory_name = mediapipe_right_hand_trajectory_names[marker_number]
- trajectory_fr_xyz = mediapipe_right_hand_fr_mar_xyz[:, marker_number, :]
- create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz=trajectory_fr_xyz,
- trajectory_name=trajectory_name,
- parent_origin=freemocap_origin_axes,
- empty_scale=hand_empty_scale,
- empty_type="PLAIN_AXES",
- )
-
- # create empty markers for left hand
- for marker_number in range(mediapipe_left_hand_fr_mar_xyz.shape[1]):
- trajectory_name = mediapipe_left_hand_trajectory_names[marker_number]
- trajectory_fr_xyz = mediapipe_left_hand_fr_mar_xyz[:, marker_number, :]
- create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz=trajectory_fr_xyz,
- trajectory_name=trajectory_name,
- parent_origin=freemocap_origin_axes,
- empty_scale=hand_empty_scale,
- empty_type="PLAIN_AXES",
- )
-
- # create empty markers for body segment centers
- body_segment_center_empty_scale = body_empty_scale * 0.7
- for marker_number in range(body_segment_centers_fr_mar_xyz.shape[1]):
- trajectory_name = BODY_SEGMENT_NAMES[marker_number]
- trajectory_fr_xyz = body_segment_centers_fr_mar_xyz[:, marker_number, :]
- create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz=trajectory_fr_xyz,
- trajectory_name=trajectory_name,
- parent_origin=freemocap_origin_axes,
- empty_scale=body_segment_center_empty_scale,
- empty_type="PLAIN_AXES",
- )
-
- # create empty markers for full body center of mass
- full_body_center_of_mass_empty_scale = body_empty_scale * 1.5
- create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz=center_of_mass_fr_xyz,
- trajectory_name=FULL_BODY_CENTER_OF_MASS_NAME,
- parent_origin=freemocap_origin_axes,
- empty_scale=full_body_center_of_mass_empty_scale,
- empty_type="SPHERE",
- )
-
- # # create empty markers for face
- # face_empty_scale = 0.005
- # for marker_number in face_contour_marker_indices:
- #
- # if marker_number < len(mediapipe_face_trajectory_names):
- # trajectory_name = mediapipe_face_trajectory_names[marker_number]
- # else:
- # trajectory_name = f"face_{marker_number}"
- #
- # trajectory_fr_xyz = mediapipe_face_fr_mar_xyz[:, marker_number, :]
- # create_keyframed_empty_from_3d_trajectory_data(
- # trajectory_fr_xyz=trajectory_fr_xyz,
- # trajectory_name=trajectory_name,
- # parent_origin=freemocap_origin_axes,
- # empty_scale=face_empty_scale,
- # empty_type="PLAIN_AXES",
- # )
- except Exception as e:
- print(f"Error loading empty markers: {e}!")
-
- #######################################################################
- # %% create virtual markers
- print("_________________________\n" "-------------------------\n" "Creating virtual markers...")
-
- # verify that the virtual marker definition dictionary is valid
- test_virtual_marker_definitions(mediapipe_virtual_marker_definitions_dict)
-
- virtual_marker_names = list(mediapipe_virtual_marker_definitions_dict.keys())
- for virtual_marker_name in virtual_marker_names:
- component_trajectory_names = mediapipe_virtual_marker_definitions_dict[virtual_marker_name]["marker_names"]
- trajectory_weights = mediapipe_virtual_marker_definitions_dict[virtual_marker_name]["marker_weights"]
-
- print(
- f"Calculating virtual marker trajectory: {virtual_marker_name} \n"
- f"Component trajectories: {component_trajectory_names} \n"
- f" weights: {trajectory_weights}\n"
- )
-
- virtual_marker_xyz = calculate_virtual_marker_trajectory(
- trajectory_3d_frame_marker_xyz=mediapipe_body_fr_mar_xyz,
- all_trajectory_names=mediapipe_body_trajectory_names,
- component_trajectory_names=component_trajectory_names,
- trajectory_weights=trajectory_weights,
- )
- create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz=virtual_marker_xyz,
- trajectory_name=virtual_marker_name,
- parent_origin=freemocap_origin_axes,
- empty_scale=body_empty_scale * 3,
- empty_type="PLAIN_AXES",
- )
-
- print(f"Adding virtual marker names to body trajectory names - {virtual_marker_names}")
- mediapipe_body_trajectory_names.extend(virtual_marker_names)
- print("Done creating virtual markers")
-
- #######################################################################
- # put sphere meshes on empty markers for visualization
- try: # try to create sphere meshes on empty markers, if it fails, print the error and continue
- # body
- put_sphere_meshes_on_empties(
- empty_names_list=mediapipe_body_trajectory_names,
- parent_object=freemocap_origin_axes,
- sphere_scale=body_empty_scale * 0.8,
- marker_type="body",
- color="#33FF66",
- )
-
- # create_stick_meshes_between_emtpies(
- # body_frame_marker_xyz=mediapipe_body_fr_mar_xyz,
- # empty_names=mediapipe_body_trajectory_names,
- # body_connections=body_connections,
- # parent_object=freemocap_origin_axes,
- # )
-
- # right hand
- put_sphere_meshes_on_empties(
- empty_names_list=mediapipe_right_hand_trajectory_names,
- parent_object=freemocap_origin_axes,
- sphere_scale=hand_empty_scale * 0.8,
- marker_type="right_hand",
- color="#FF0000",
- )
-
- put_sphere_meshes_on_empties(
- empty_names_list=mediapipe_left_hand_trajectory_names,
- parent_object=freemocap_origin_axes,
- sphere_scale=hand_empty_scale * 0.8,
- marker_type="left_hand",
- color="#0000FF",
- )
-
- # # face
- # put_sphere_meshes_on_empties(
- # empty_names_list=mediapipe_face_trajectory_names,
- # parent_object=freemocap_origin_axes,
- # sphere_scale=face_empty_scale,
- # )
-
- put_sphere_meshes_on_empties(
- empty_names_list=BODY_SEGMENT_NAMES,
- parent_object=freemocap_origin_axes,
- sphere_scale=body_segment_center_empty_scale * 0.8,
- marker_type="body_segment_center",
- color="#CCCC00",
- )
-
- put_sphere_meshes_on_empties(
- empty_names_list=[FULL_BODY_CENTER_OF_MASS_NAME],
- parent_object=freemocap_origin_axes,
- sphere_scale=body_empty_scale * 1.25,
- marker_type="center_of_mass",
- color="#FF00FF",
- emission_strength=30,
- )
- except Exception as e:
- print(f"Error creating sphere meshes on empties: {e}")
-
- ###########################
- ### Make stick figure
- # creating_stick_figure_mesh_from_bone_dictionary(trajectory_frame_marker_xyz=mediapipe_body_fr_mar_xyz,
- # body_connections=body_connections,)
- ############################
- ### Load videos into scene
- if Path(recording_path / "annotated_videos").is_dir():
- videos_path = Path(recording_path / "annotated_videos")
- elif Path(recording_path / "synchronized_videos").is_dir():
- videos_path = Path(recording_path / "synchronized_videos")
- else:
- print("Did not find an `annotated_videos` or `synchronized_videos` folder in the recording path")
- videos_path = None
-
- if videos_path is not None:
- try:
- addon_utils.enable("io_import_images_as_planes")
- except Exception as e:
- print("Error enabling `io_import_images_as_planes` addon: ")
- print(e)
- try:
- add_videos_to_scene(videos_path=videos_path, parent_object=world_origin_axes)
- except Exception as e:
- print("Error adding videos to scene: ")
- print(e)
-
- if create_rig:
- try:
- #######################################################################
- ### Create meta-rig
- print("Saving blender file with raw motion capture data before moving on to rigging")
- bpy.ops.object.mode_set(mode="OBJECT")
- bpy.ops.wm.save_as_mainfile(filepath=str(blender_file_save_path))
-
- print(
- "____________________________________________________________________________\n",
- "Done loading in motion capture data - Now lets use it to drive an armature!\n"
- "____________________________________________________________________________\n",
- )
-
- #######################################################################
- ##% Activate necessary addons
- addon_utils.enable("rigify")
-
- ######################
- ### Create Rigify human meta-rig
- print("Creating `rigify human meta-rig`")
- bpy.ops.object.armature_human_metarig_add()
- human_metarig = bpy.context.editable_objects[-1]
- human_metarig.data.display_type = "WIRE"
-
- ##################
- ### Scale armature
-
- bpy.ops.object.mode_set(mode="EDIT")
-
- for (
- segment_name,
- rigify_bones_list,
- ) in rigify_bone_to_skeleton_segment_name_correspondance.items():
- for rigify_bone_name in rigify_bones_list:
- median_segment_length = skeleton_segment_lengths_dict[segment_name]["median"]
- median_segment_length *= 0.001 # scale to meters
-
- segment_length = median_segment_length / len(
- rigify_bones_list
- ) # divide by number of bones in segment (for now, eventually will want to set weights
- print(f"setting {rigify_bone_name} length to: {segment_length:.3f} m")
- human_metarig.data.edit_bones[rigify_bone_name].length = segment_length
-
- delete_bones = [
- "pelvis.L",
- "pelvis.R",
- "breast.L",
- "breast.R",
- "heel.02.L",
- "heel.02.R",
- "toe.L",
- "toe.R",
- ]
-
- for bone in delete_bones:
- human_metarig.data.edit_bones.remove(human_metarig.data.edit_bones[bone])
-
- extra_scale_bones = {"spine.005": 0.2}
-
- for bone, scale in extra_scale_bones.items():
- human_metarig.data.edit_bones[bone].length *= scale
-
- ####
- #### Constrain bones to empties
- ####
-
- # loop through dictionary applying constraints
- bpy.ops.object.mode_set(mode="POSE")
-
- for bone_name, bone_constraint_dict in rig_constraint_dict_of_dicts.items():
- print(f"---Setting constraints for bone:{bone_name}---")
- apply_constraints_to_bone(
- bone_name=bone_name,
- bone_constraint_dict=bone_constraint_dict,
- armature_rig=human_metarig,
- )
-
- print(
- "____________________________________________________________________________\n",
- "Done constraining armature bones to follow keyframed empties!",
- "____________________________________________________________________________",
- )
-
- except Exception as e:
- print("Error creating rig!! (You might need to manually activate the `rigify` addon in Blender)")
- print(e)
-
- for window in bpy.context.window_manager.windows:
- for area in window.screen.areas: # iterate through areas in current screen
- if area.type == "VIEW_3D":
- for space in area.spaces: # iterate through spaces in current VIEW_3D area
- if space.type == "VIEW_3D": # check if space is a 3D view
- space.shading.type = "MATERIAL"
-
- # save .blend file
- bpy.ops.object.mode_set(mode="OBJECT")
-
- bpy.ops.wm.save_as_mainfile(filepath=str(blender_file_save_path))
- print(f"Saved .blend file to: {blender_file_save_path}")
-
- # print("try to hide the shameful metarig and then save again lol")
- # human_metarig.hide_set = True
- bpy.ops.wm.save_as_mainfile(filepath=str(blender_file_save_path))
-
- print(
- "____________________________________________________________________________\n",
- "____________________________________________________________________________\n",
- "Done creating Blender scene!\n",
- f"Saved .blend file to: {blender_file_save_path}\n",
- "You can now open the `.blend` file in Blender and play the animation!\n",
- "____________________________________________________________________________\n",
- "____________________________________________________________________________",
- )
-
-
-def add_videos_to_scene(videos_path: Union[Path, str], parent_object: bpy.types.Object, vid_location_scale: float = 2):
- print("loading videos as planes...")
-
- number_of_videos = len(list(get_video_paths(videos_path)))
- print(f"Found {number_of_videos} videos in {videos_path}")
- for (
- video_number,
- video_path,
- ) in enumerate(get_video_paths(videos_path)):
- print(f"Adding video: {video_path.name} to scene")
-
- bpy.ops.import_image.to_plane(
- files=[{"name": video_path.name}],
- directory=str(video_path.parent),
- shader="EMISSION",
- )
- print(f"Added video: {video_path.name} to scene")
- video_as_plane = bpy.context.editable_objects[-1]
- print(f"video_as_plane: {video_as_plane}")
- video_as_plane.name = "video_" + str(video_number)
- print(f"video_as_plane.name: {video_as_plane.name}")
- buffer = 1.1
- vid_x = (video_number * buffer - np.mean(np.arange(0, number_of_videos))) * vid_location_scale
-
- video_as_plane.location = [
- vid_x,
- vid_location_scale,
- 1,
- ]
- video_as_plane.rotation_euler = [np.pi / 2, 0, 0]
- video_as_plane.scale = [1] * 3
- video_as_plane.parent = parent_object
-
-
-def create_keyframed_empty_from_3d_trajectory_data(
- trajectory_fr_xyz: np.ndarray,
- trajectory_name: str,
- parent_origin: bpy.types.Object = None,
- empty_scale: float = 0.1,
- empty_type: str = "PLAIN_AXES",
-):
- """
- Create a key framed empty from 3d trajectory data
- """
- print(f"Creating keyframed empty from: {trajectory_name}...")
- bpy.ops.object.empty_add(type=empty_type)
- empty_object = bpy.context.editable_objects[-1]
- empty_object.name = trajectory_name
-
- empty_object.scale = [empty_scale] * 3
-
- empty_object.parent = parent_origin
-
- for frame_number in range(trajectory_fr_xyz.shape[0]):
- empty_object.location = [
- trajectory_fr_xyz[frame_number, 0],
- trajectory_fr_xyz[frame_number, 1],
- trajectory_fr_xyz[frame_number, 2],
- ]
-
- empty_object.keyframe_insert(data_path="location", frame=frame_number)
-
-
-def create_material(
- name: str = "Generic",
- color: Union[str, Tuple, List] = "#00FFFF",
- emission_strength: float = 1.0,
-):
- """
- Create a material with the given name and color, with a strong emission.
-
- :param name: The name of the material.
- :param color: The color of the material, in hex format.
- :return: The created material.
- """
- material = bpy.data.materials.new(name=name)
- material.use_nodes = True
- nodes = material.node_tree.nodes
- links = material.node_tree.links
-
- # Clear all nodes to start clean
- nodes.clear()
-
- # Create necessary nodes
- output = nodes.new(type="ShaderNodeOutputMaterial")
- emission = nodes.new(type="ShaderNodeEmission")
- bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
- mix = nodes.new(type="ShaderNodeMixShader")
-
- # Set the locations
- output.location = (300, 0)
- mix.location = (100, 0)
- bsdf.location = (-100, 50)
- emission.location = (-100, -50)
-
- # Set node links
- links.new(output.inputs[0], mix.outputs[0])
- links.new(mix.inputs[1], bsdf.outputs[0])
- links.new(mix.inputs[2], emission.outputs[0])
-
- # Convert color from hex to RGB
- if isinstance(color, str) and color.startswith("#"):
- color_rgb = [int(color[i : i + 2], 16) / 255 for i in (1, 3, 5)] # skips the "#" and separates R, G, and B
- elif isinstance(color, list) or isinstance(color, tuple):
- color_rgb = color
-
- # Set the colors
- bsdf.inputs[0].default_value = (*color_rgb, 1)
- emission.inputs[0].default_value = (*color_rgb, 1) # RGB + Alpha
-
- # Set the strength of the emission
- emission.inputs[1].default_value = emission_strength # The higher this value, the more the material will glow
-
- return material
-
-
-def put_sphere_meshes_on_empties(
- empty_names_list: list,
- parent_object: bpy.types.Object,
- sphere_scale: float = 0.02,
- material: Union[bpy.types.Material, str] = None,
- color: str = "#00FFFF",
- marker_type: str = "generic",
- emission_strength: float = 1.0,
-):
- """
- Put uv sphere meshes on the empties in `empty_names_list` with a scale of `sphere_scale`.
-
- :param empty_names_list: List of empty object names.
- :param parent_object: The object to be set as parent for the created spheres.
- :param sphere_scale: The scale of the sphere.
- :param material: The material to be applied to the spheres, can be a bpy.types.Material object or a color string in hex format.
- """
- # If material is None, create a default material
- if material is None:
- material = create_material(
- color=color,
- name=marker_type,
- emission_strength=emission_strength,
- )
-
- # Ensure the provided material is a bpy.types.Material object
- assert isinstance(material, bpy.types.Material), "Material must be a bpy.types.Material object or a color string"
-
- for empty_name in empty_names_list:
- bpy.ops.mesh.primitive_uv_sphere_add(segments=8, ring_count=8, scale=(sphere_scale, sphere_scale, sphere_scale))
- sphere = bpy.context.editable_objects[-1]
- sphere.name = f"{empty_name}_sphere"
- sphere.parent = parent_object
- sphere.data.materials.append(material)
-
- constraint = sphere.constraints.new(type="COPY_LOCATION")
- constraint.target = bpy.data.objects[empty_name]
-
-
-def create_stick_meshes_between_emtpies(
- body_frame_marker_xyz: np.ndarray,
- empty_names: List[str],
- body_connections: Dict[int, int],
- parent_object: bpy.types.Object,
-):
- """make cylinder meshes between the empties as defined in `body_connections`"""
- for origin_marker_index, target_marker_index in body_connections.items():
- origin_marker_name = empty_names[origin_marker_index]
- target_marker_name = empty_names[target_marker_index]
-
- stick = create_stick_mesh()
- stick = bpy.context.editable_objects[-1]
- stick.name = f"{origin_marker_name}_to_{target_marker_name}"
- stick.parent = parent_object
-
- constraint = stick.constraints.new(type="COPY_LOCATION")
- constraint.target = bpy.data.objects[origin_marker_name]
-
- constraint = stick.constraints.new(type="DAMPED_TRACK")
- constraint.target = bpy.data.objects[target_marker_name]
- constraint.track_axis = "TRACK_Z"
-
- for frame_number in range(body_frame_marker_xyz.shape[0]):
- distance_between_markers = np.linalg.norm(
- body_frame_marker_xyz[frame_number, origin_marker_index, :]
- - body_frame_marker_xyz[frame_number, target_marker_index, :]
- )
- stick.scale = [1, 1, distance_between_markers]
- stick.keyframe_insert(data_path="scale", frame=frame_number)
-
-
-def create_stick_mesh(
- vertices: int = 12,
- radius: float = 0.02,
- depth: float = 1.0,
- location=(0, 0, 0),
- rotation=(0, 0, 0),
- scale=(1, 1, 1),
- end_fill_type="TRIFAN",
- enter_editmode=False,
- align="WORLD",
-):
- bpy.ops.mesh.primitive_cylinder_add(
- radius=radius,
- depth=depth,
- vertices=vertices,
- location=location,
- rotation=rotation,
- scale=scale,
- end_fill_type=end_fill_type,
- enter_editmode=enter_editmode,
- align=align,
- )
- # give 3dcursor new coordinates
- bpy.context.scene.cursor.location = (0.0, 0.0, -0.5)
-
- # set the origin on the current object to the 3dcursor location
- bpy.ops.object.origin_set(type="ORIGIN_CURSOR")
- return bpy.context.editable_objects[-1]
-
-
-def apply_constraints_to_bone(bone_name: str, bone_constraint_dict: dict, armature_rig):
- """
- Apply constraints to a bone (that exists in `armature_rig` based on a dictionary of constraints
- """
- for (
- constraint_name,
- constrain_parameters_dict,
- ) in bone_constraint_dict.items():
- constraint_name = constraint_name.split(".")[
- 0
- ] # for duplicated constraints, named `[constraint_name].001`, etc
- print(f"constraint: {constraint_name} with parameters:{constrain_parameters_dict}")
-
- bone = armature_rig.pose.bones[bone_name]
- print(f"bone: {bone.name}")
-
- constraint = bone.constraints.new(type=constraint_name)
- print(f"constraint: {constraint.name}")
-
- # generic constraint settings
- if "target" in constrain_parameters_dict:
- if constrain_parameters_dict["target"] == "armature":
- constraint.target = armature_rig
- constraint.subtarget = constrain_parameters_dict["subtarget"]
- print(f"constraint.target: {constraint.target.name}")
- print(f"constraint.subtarget: {constraint.target.name}")
- else:
- constraint.target = bpy.data.objects[
- constrain_parameters_dict["target"]
- ] # point constraint at relevant empty object
- print(f"constraint.target: {constraint.target.name}")
-
- if "influence" in constrain_parameters_dict:
- constraint.influence = constrain_parameters_dict["influence"]
- print(f"constraint.influence: {constraint.influence}")
-
- # constraint-specific settings
- if constraint_name == "IK":
- constraint.chain_count = constrain_parameters_dict["chain_length"]
- print(f"constraint.chain_count: {constraint.chain_count}")
-
- if constraint_name == "LOCKED_TRACK":
- constraint.track_axis = constrain_parameters_dict["track_axis"]
- constraint.lock_axis = constrain_parameters_dict["lock_axis"]
- print(f"constraint.track_axis: {constraint.track_axis}")
- print(f"constraint.lock_axis: {constraint.lock_axis}")
-
- if constraint_name == "DAMPED_TRACK":
- if "track_axis" in constrain_parameters_dict:
- constraint.track_axis = constrain_parameters_dict["track_axis"]
- print(f"constraint.track_axis: {constraint.track_axis}")
-
-
-def test_virtual_marker_definitions(virtual_marker_definitions_dict: dict):
- """
- Validate the virtual marker definitions dictionary to ensure that there are the same number of marker names and weights, and that the weights sum to 1
- """
-
- for (
- virtual_marker_name,
- virtual_marker_definition,
- ) in virtual_marker_definitions_dict.items():
- assert len(virtual_marker_definition["marker_names"]) == len(
- virtual_marker_definition["marker_weights"]
- ), f"marker_names and marker_weights must be the same length for virtual marker {virtual_marker_name}"
- assert (
- sum(virtual_marker_definition["marker_weights"]) == 1
- ), f"marker_weights must sum to 1 for virtual marker {virtual_marker_name}"
-
-
-def calculate_virtual_marker_trajectory(
- trajectory_3d_frame_marker_xyz: np.ndarray,
- all_trajectory_names: list,
- component_trajectory_names: List,
- trajectory_weights: List,
-) -> np.ndarray:
- """
- Create a virtual marker from a set of component markers. A 'Virtual Marker' is a 'fake' marker created by combining the data from 'real' (measured) marker/trajectory data
- `trajectory_3d_frame_marker_xyz`: all trajectory data in a numpy array with shape [frame, marker, xyz]
- `all_trajectory_names`: list of all trajectory names
- `component_trajectory_names`: the trajectories we'll use to make this virtual marker
- `trajectory_weights`: the weights we'll use to combine the compoenent trajectories into the virtual maker
- """
-
- # double check that the weights scale to one, otherwise this function will return screwy results
- assert np.sum(trajectory_weights) == 1, "Error - Trajectory_weights must sum to 1!"
- assert len(component_trajectory_names) == len(
- trajectory_weights
- ), "Error - component_trajectory_names and trajectory_weights must be the same length!"
- virtual_marker_xyz = np.zeros((trajectory_3d_frame_marker_xyz.shape[0], 3))
-
- for trajectory_name, weight in zip(component_trajectory_names, trajectory_weights):
- # pull out the trajectory data for this component trajectory and scale by it's `weight`
- component_trajectory_xyz = copy(
- trajectory_3d_frame_marker_xyz[:, all_trajectory_names.index(trajectory_name), :] * weight
- )
-
- # add it to the virtual marker
- virtual_marker_xyz += component_trajectory_xyz
-
- return virtual_marker_xyz
-
-
-def creating_stick_figure_mesh_from_bone_dictionary(
- trajectory_frame_marker_xyz: np.ndarray, body_connections: Dict[int, int]
-):
- print("creating_stick_figure_mesh_from_bone_dictionary")
-
- assert (
- len(trajectory_frame_marker_xyz.shape) == 3
- ), "trajectory_frame_marker_xyz must have three axes - [frame, marker, xyz] (e.g. skeleton data from a single frame"
- mesh = bpy.data.meshes.new("stick_figure_mesh")
-
- init_zero_vertices = np.zeros((trajectory_frame_marker_xyz.shape[1], 3))
-
- edges = []
- for head_index, tail_index in body_connections.items():
- edges.append((head_index, tail_index))
-
- mesh.from_pydata(init_zero_vertices, edges, [])
- mesh.validate(verbose=True)
- mesh.update()
-
- skin_radius = 0.025
- print(f"Skinning stick figure mesh with radius {skin_radius}...")
- bpy.data.objects["stick_figure_mesh"].modifiers.new("stick_figure_mesh_skin", "SKIN")
- bpy.ops.transform.skin_resize(value=(skin_radius, skin_radius, skin_radius))
-
- for frame_number in range(trajectory_frame_marker_xyz.shape[0]):
- for vertex in mesh.vertices:
- vertex.co = trajectory_frame_marker_xyz[frame_number, vertex.index, :]
- vertex.keyframe_insert(data_path="co", frame=frame_number) # keyframe the vertex location
- mesh.update()
-
-
-def get_video_paths(path_to_video_folder: Path) -> list:
- """Search the folder for 'mp4' files (case insensitive) and return them as a list"""
-
- list_of_video_paths = list(Path(path_to_video_folder).glob("*.mp4")) + list(
- Path(path_to_video_folder).glob("*.MP4")
- )
- unique_list_of_video_paths = get_unique_list(list_of_video_paths)
-
- return unique_list_of_video_paths
-
-
-def get_unique_list(list: list) -> list:
- """Return a list of the unique elements from input list"""
- unique_list = []
- [unique_list.append(clip) for clip in list if clip not in unique_list]
-
- return unique_list
-
-
-mediapipe_empty_names = {
- # generated by: freemocap/core_processes/detecting_things_in_2d_images/mediapipe_stuff/mediapipe_skeleton_names_and_connections.py
- "body": [
- "nose",
- "left_eye_inner",
- "left_eye",
- "left_eye_outer",
- "right_eye_inner",
- "right_eye",
- "right_eye_outer",
- "left_ear",
- "right_ear",
- "mouth_left",
- "mouth_right",
- "left_shoulder",
- "right_shoulder",
- "left_elbow",
- "right_elbow",
- "left_wrist",
- "right_wrist",
- "left_pinky",
- "right_pinky",
- "left_index",
- "right_index",
- "left_thumb",
- "right_thumb",
- "left_hip",
- "right_hip",
- "left_knee",
- "right_knee",
- "left_ankle",
- "right_ankle",
- "left_heel",
- "right_heel",
- "left_foot_index",
- "right_foot_index",
- ],
- "hand": [
- "wrist",
- "thumb_cmc",
- "thumb_mcp",
- "thumb_ip",
- "thumb_tip",
- "index_finger_mcp",
- "index_finger_pip",
- "index_finger_dip",
- "index_finger_tip",
- "middle_finger_mcp",
- "middle_finger_pip",
- "middle_finger_dip",
- "middle_finger_tip",
- "ring_finger_mcp",
- "ring_finger_pip",
- "ring_finger_dip",
- "ring_finger_tip",
- "pinky_mcp",
- "pinky_pip",
- "pinky_dip",
- "pinky_tip",
- ],
- "face": ["right_eye", "left_eye", "nose_tip", "mouth_center", "right_ear_tragion", "left_ear_tragion"],
-}
-
-# NOTE - numbers refer to the index of the name in the relevant list above
-body_connections = {
- 15: 19,
- 16: 18,
- 18: 20,
- 3: 7,
- 14: 16,
- 23: 24,
- 28: 32,
- 11: 12,
- 27: 29,
- 6: 8,
- 24: 26,
- 4: 5,
- 5: 6,
- 29: 31,
- 12: 14,
- 0: 4,
- 9: 10,
- 1: 2,
- 30: 32,
- 25: 27,
- 26: 28,
- 17: 19,
- 2: 3,
- 13: 15,
-}
-
-hand_connections = {
- 3: 4,
- 0: 1,
- 17: 18,
- 13: 17,
- 18: 19,
- 5: 9,
- 14: 15,
- 9: 13,
- 1: 2,
- 10: 11,
- 19: 20,
- 6: 7,
- 15: 16,
- 2: 3,
- 11: 12,
- 7: 8,
-}
-# boy howdy mediapipe did not make it easy to get this info...
-face_contour_marker_indices = [
- 0,
- 7,
- 10,
- 13,
- 14,
- 17,
- 21,
- 33,
- 37,
- 39,
- 40,
- 46,
- 52,
- 53,
- 54,
- 55,
- 58,
- 61,
- 63,
- 65,
- 66,
- 67,
- 70,
- 78,
- 80,
- 81,
- 82,
- 84,
- 87,
- 88,
- 91,
- 93,
- 95,
- 103,
- 105,
- 107,
- 109,
- 127,
- 132,
- 133,
- 136,
- 144,
- 145,
- 146,
- 148,
- 149,
- 150,
- 152,
- 153,
- 154,
- 155,
- 157,
- 158,
- 159,
- 160,
- 161,
- 162,
- 163,
- 172,
- 173,
- 176,
- 178,
- 181,
- 185,
- 191,
- 234,
- 246,
- 249,
- 251,
- 263,
- 267,
- 269,
- 270,
- 276,
- 282,
- 283,
- 284,
- 285,
- 288,
- 291,
- 293,
- 295,
- 296,
- 297,
- 300,
- 308,
- 310,
- 311,
- 312,
- 314,
- 317,
- 318,
- 321,
- 323,
- 324,
- 332,
- 334,
- 336,
- 338,
- 356,
- 361,
- 362,
- 365,
- 373,
- 374,
- 375,
- 377,
- 378,
- 379,
- 380,
- 381,
- 382,
- 384,
- 385,
- 386,
- 387,
- 388,
- 389,
- 390,
- 397,
- 398,
- 400,
- 402,
- 405,
- 409,
- 415,
- 454,
- 466,
-]
-
-mediapipe_virtual_marker_definitions_dict = {
- "head_center": {
- "marker_names": ["left_ear", "right_ear"],
- "marker_weights": [0.5, 0.5],
- },
- "neck_center": {
- "marker_names": ["left_shoulder", "right_shoulder"],
- "marker_weights": [0.5, 0.5],
- },
- "trunk_center": {
- "marker_names": ["left_shoulder", "right_shoulder", "left_hip", "right_hip"],
- "marker_weights": [0.25, 0.25, 0.25, 0.25],
- },
- "hips_center": {
- "marker_names": ["left_hip", "right_hip"],
- "marker_weights": [0.5, 0.5],
- },
-}
-
-# Skeleton segments names and the rigify bone names that they correspond to
-rigify_bone_to_skeleton_segment_name_correspondance = {
- "lower_spine": ["spine", "spine.001"],
- "upper_spine": ["spine.002", "spine.003"],
- "head": ["spine.004", "spine.005", "spine.006"],
- "left_hand": ["hand.L"],
- "left_forearm": ["forearm.L"],
- "left_upper_arm": ["upper_arm.L"],
- "right_hand": ["hand.R"],
- "right_forearm": ["forearm.R"],
- "right_upper_arm": ["upper_arm.R"],
- "left_thigh": ["thigh.L"],
- "left_calf": ["shin.L"],
- "left_foot": ["foot.L"],
- "right_thigh": ["thigh.R"],
- "right_calf": ["shin.R"],
- "right_foot": ["foot.R"],
-}
-
-# A dictionary where the `keys` are rigify bone names and the `values` are bone constraints and their parameters.
-# NOTE - the `targets` must correspond to the `mediapipe_body_names` list above (or one of the virtural markers we'll make later)
-rig_constraint_dict_of_dicts = {
- "spine": {
- "COPY_LOCATION": {"target": "hips_center"},
- },
- "spine.003": {
- "IK": {"target": "neck_center", "chain_length": 4},
- },
- "spine.004": {
- "COPY_LOCATION": {"target": "neck_center"},
- },
- "spine.006": {
- "IK": {"target": "head_center", "chain_length": 2},
- },
- "face": {
- "COPY_LOCATION": {"target": "armature", "subtarget": "spine.006"},
- "DAMPED_TRACK": {
- "target": "head_center",
- },
- "LOCKED_TRACK": {
- "target": "nose",
- "track_axis": "TRACK_Z",
- "lock_axis": "LOCK_Y",
- "influence": 1.0,
- },
- "LOCKED_TRACK.001": {
- "target": "left_ear",
- "track_axis": "TRACK_Z",
- "lock_axis": "LOCK_Z",
- "influence": 1.0,
- },
- },
- "shoulder.R": {
- "COPY_LOCATION": {"target": "neck_center"},
- "DAMPED_TRACK": {"target": "right_shoulder"},
- },
- "upper_arm.R": {
- "DAMPED_TRACK": {"target": "right_elbow"},
- },
- "forearm.R": {
- "DAMPED_TRACK": {"target": "right_wrist"},
- },
- "hand.R": {
- "DAMPED_TRACK": {"target": "right_index"},
- },
- "shoulder.L": {
- "COPY_LOCATION": {"target": "neck_center"},
- "DAMPED_TRACK": {"target": "left_shoulder"},
- },
- "upper_arm.L": {
- "DAMPED_TRACK": {"target": "left_elbow"},
- },
- "forearm.L": {
- "DAMPED_TRACK": {"target": "left_wrist"},
- },
- "hand.L": {
- "DAMPED_TRACK": {"target": "left_index"},
- },
- "thigh.R": {
- "COPY_LOCATION": {"target": "right_hip"},
- "DAMPED_TRACK": {"target": "right_knee"},
- },
- "shin.R": {
- "DAMPED_TRACK": {"target": "right_ankle"},
- },
- "foot.R": {
- "DAMPED_TRACK": {"target": "right_foot_index"},
- },
- "thigh.L": {
- "COPY_LOCATION": {"target": "left_hip"},
- "DAMPED_TRACK": {"target": "left_knee"},
- },
- "shin.L": {
- "DAMPED_TRACK": {"target": "left_ankle"},
- },
- "foot.L": {
- "DAMPED_TRACK": {"target": "left_foot_index"},
- },
-}
-
-BODY_SEGMENT_NAMES = [
- "head",
- "trunk",
- "right_upper_arm",
- "left_upper_arm",
- "right_forearm",
- "left_forearm",
- "right_hand",
- "left_hand",
- "right_thigh",
- "left_thigh",
- "right_shin",
- "left_shin",
- "right_foot",
- "left_foot",
-]
-
-FULL_BODY_CENTER_OF_MASS_NAME = (
- "_full_body_center_of_mass" # leading underscore to make it easier to find in the outliner
-)
-
-print(f"__name__: {__name__}")
-
-if __name__ == "__main__" or __name__ == "<run_path>":
- # this is the part that actually runs the script
- argv = sys.argv
- create_rig_input = True
-
- print(f"Running script to create Blender file from freemocap session data from script {__file__}")
- try:
- # Session path
- # get session path as command line argument
- print(f"Received command line arguments: {argv}")
- argv = argv[argv.index("--") + 1 :]
-
- recording_path_input = Path(argv[0])
- blender_file_save_path_input = Path(argv[1])
- create_rig_input = bool(argv[2])
- except Exception as e:
- print(f"There was a problem loading command line arguments: {e}. Using hardcoded values instead.")
-
- if len(argv) == 0:
- ####
- # Change the path below (`recording_path_input`) to the path to your recording session folder
- ####
- recording_path_input = Path(
- r"C:\Users\jonma\freemocap_data\recording_sessions\session_2023-02-15_08_46_43_wud\recording_08_47_25_gmt-5_wud"
- )
- blender_file_save_path_input = Path(recording_path_input) / f"{Path(recording_path_input).name}_.blend"
- create_rig = True
-
- print(
- f"recording_path_input: {recording_path_input}, \n"
- f"blender_file_save_path_input: {blender_file_save_path_input}, \n"
- f"create_rig_input: {create_rig_input}"
- )
-
- main(
- recording_path=recording_path_input,
- blender_file_save_path=blender_file_save_path_input,
- mediapipe_empty_names=mediapipe_empty_names,
- create_rig=create_rig_input,
- )
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_alpha_megascript.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_alpha_megascript.py
deleted file mode 100644
index 32f7cb26..00000000
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_alpha_megascript.py
+++ /dev/null
@@ -1,57 +0,0 @@
-import logging
-import subprocess
-from pathlib import Path
-from typing import Union
-
-logger = logging.getLogger(__name__)
-
-
-def run_alpha_megascript(
- recording_folder_path: Union[str, Path],
- blender_file_path: Union[str, Path],
- blender_exe_path: Union[str, Path],
- good_clean_frame_number: int = 0,
-):
- path_to_this_py_file = Path(__file__).parent.resolve()
-
- freemocap_blender_megascript_path = (
- path_to_this_py_file / "blender_bpy_export_scripts" / "alpha_freemocap_blender_megascript.py"
- )
-
- # freemocap_blender_megascript_path = (
- # path_to_this_py_file / "freemocap_blender_megascript_take2.py"
- # )
-
- try:
- blender_exe_path = Path(blender_exe_path)
- if not blender_exe_path.exists():
- raise FileNotFoundError(f"Could not find the blender executable at {blender_exe_path}")
- except Exception as e:
- logger.error(e)
- return
-
- command_list = [
- str(blender_exe_path),
- "--background",
- "--python",
- str(freemocap_blender_megascript_path),
- "--",
- str(recording_folder_path),
- str(blender_file_path),
- str(good_clean_frame_number),
- ]
-
- logger.info(f"Starting `blender` sub-process with this command: \n {command_list}")
-
- blender_process = subprocess.Popen(command_list, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- while True:
- output = blender_process.stdout.readline()
- if blender_process.poll() is not None:
- break
- if output:
- logger.debug(output.strip().decode())
-
- if blender_process.returncode == 0:
- logger.error("Blender returned an error:")
- logger.error(blender_process.stderr.read().decode())
- logger.debug("Done with Blender Alpha Megascript")
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_cgtinker_method.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_cgtinker_method.py
deleted file mode 100644
index ce54e882..00000000
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_cgtinker_method.py
+++ /dev/null
@@ -1,58 +0,0 @@
-import logging
-import subprocess
-from pathlib import Path
-from typing import Union
-
-logger = logging.getLogger(__name__)
-
-
-def run_cgtinker_method(
- recording_folder_path: Union[str, Path],
- blender_file_path: Union[str, Path],
- blender_exe_path: Union[str, Path],
-):
- path_to_this_py_file = Path(__file__).parent.resolve()
-
- blender_script_path = path_to_this_py_file / "cgtinker_blendarmocap_load.py"
-
- # freemocap_blender_megascript_path = (
- # path_to_this_py_file / "freemocap_blender_megascript_take2.py"
- # )
-
- try:
- blender_exe_path = Path(blender_exe_path)
- if not blender_exe_path.exists():
- raise FileNotFoundError(f"Could not find the blender executable at {blender_exe_path}")
- except Exception as e:
- logger.error(e)
- return
-
- command_list = [
- str(blender_exe_path),
- "--background",
- "--python",
- str(blender_script_path),
- "--",
- str(recording_folder_path),
- str(blender_file_path),
- "1", # bind to rig
- "1", # load synchronized_videos
- "9999", # timeout
- "0", # load raw data
- "1,", # load quick
- ]
-
- logger.info(f"Starting `blender` sub-process with this command: \n {command_list}")
-
- blender_process = subprocess.Popen(command_list, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- while True:
- output = blender_process.stdout.readline()
- if blender_process.poll() is not None:
- break
- if output:
- print(output.strip().decode())
-
- if blender_process.returncode == 0:
- logger.error("Blender returned an error:")
- logger.error(blender_process.stderr.read().decode())
- logger.debug("Done with cgtinker Blender Export")
diff --git a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_megascript_take2.py b/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_megascript_take2.py
deleted file mode 100644
index 7fa143a4..00000000
--- a/freemocap/core_processes/export_data/blender_stuff/export_to_blender/methods/legacy/run_megascript_take2.py
+++ /dev/null
@@ -1,68 +0,0 @@
-import logging
-import subprocess
-from pathlib import Path
-from typing import Union
-
-# blender_exe_path = (
-# r"C:\Users\jonma\Blender Foundation\stable\blender-3.1.0-windows-x64\blender.exe"
-# )
-# blender_exe_path = r"C:\Users\jonma\Blender Foundation\Blender 3.1\blender.exe"
-from freemocap.core_processes.detecting_things_in_2d_images.mediapipe_stuff.data_models.mediapipe_skeleton_names_and_connections import (
- mediapipe_names_and_connections_dict,
-)
-from freemocap.system.paths_and_filenames.file_and_folder_names import OUTPUT_DATA_FOLDER_NAME
-from freemocap.utilities.save_dictionary_to_json import save_dictionary_to_json
-
-logger = logging.getLogger(__name__)
-
-
-def run_blender_megascript_take2(
- recording_folder_path: Union[str, Path],
- blender_file_path: Union[str, Path],
- blender_exe_path: Union[str, Path],
-):
- path_to_this_py_file = Path(__file__).parent.resolve()
-
- freemocap_blender_megascript_path = path_to_this_py_file / "freemocap_blender_megascript_take2.py"
-
- if not (
- Path(recording_folder_path) / OUTPUT_DATA_FOLDER_NAME / "mediapipe_names_and_connections_dict.json"
- ).is_file():
- save_dictionary_to_json(
- save_path=str(Path(recording_folder_path) / OUTPUT_DATA_FOLDER_NAME),
- file_name="mediapipe_names_and_connections_dict.json",
- dictionary=mediapipe_names_and_connections_dict,
- )
-
- try:
- blender_exe_path = Path(blender_exe_path)
- if not blender_exe_path.exists():
- raise FileNotFoundError(f"Could not find the blender executable at {blender_exe_path}")
- except Exception as e:
- logger.error(e)
- return
-
- command_list = [
- str(blender_exe_path),
- "--background",
- "--python",
- str(freemocap_blender_megascript_path),
- "--",
- str(recording_folder_path),
- str(blender_file_path),
- ]
-
- logger.info(f"Starting `blender` sub-process with this command: \n {command_list}")
-
- blender_process = subprocess.Popen(command_list, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- while True:
- output = blender_process.stdout.readline()
- if blender_process.poll() is not None:
- break
- if output:
- logger.debug(output.strip().decode())
-
- if blender_process.returncode == 0:
- logger.error("Blender returned an error:")
- logger.error(blender_process.stderr.read().decode())
- logger.debug("Done with Blender Megascript Take 2")
diff --git a/freemocap/gui/qt/main_window/freemocap_main_window.py b/freemocap/gui/qt/main_window/freemocap_main_window.py
index d8d624db..f403b948 100644
--- a/freemocap/gui/qt/main_window/freemocap_main_window.py
+++ b/freemocap/gui/qt/main_window/freemocap_main_window.py
@@ -301,12 +301,10 @@ class MainWindow(QMainWindow):
logger.error("Blender executable path is None!")
return
- self._visualization_control_panel.get_user_selected_method_string()
self._export_to_blender_thread_worker = ExportToBlenderThreadWorker(
recording_path=recording_path,
- blender_file_path=get_blender_file_path(recording_path),
- blender_executable_path=self._visualization_control_panel.blender_executable_path,
- blender_method=self._visualization_control_panel.get_user_selected_method_string(),
+ blender_file_path=Path(get_blender_file_path(recording_path)),
+ blender_executable_path=Path(self._visualization_control_panel.blender_executable_path),
kill_thread_event=self._kill_thread_event,
)
self._export_to_blender_thread_worker.start()
diff --git a/freemocap/gui/qt/widgets/control_panel/export_data_control_panel.py b/freemocap/gui/qt/widgets/control_panel/export_data_control_panel.py
index f77251c7..2180ff0f 100644
--- a/freemocap/gui/qt/widgets/control_panel/export_data_control_panel.py
+++ b/freemocap/gui/qt/widgets/control_panel/export_data_control_panel.py
@@ -2,12 +2,9 @@ import logging
from pathlib import Path
from typing import Union
-from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
- QGroupBox,
QVBoxLayout,
QPushButton,
- QRadioButton,
QWidget,
QCheckBox,
QLabel,
@@ -30,35 +27,12 @@ class VisualizationControlPanel(QWidget):
self._generate_jupyter_notebook = QPushButton("Generate Jupyter Notebook")
self._layout.addWidget(self._generate_jupyter_notebook)
- groupbox = QGroupBox("Export to Blender", parent=self)
- self._layout.addWidget(groupbox)
- vbox = QVBoxLayout()
- vbox.setContentsMargins(0, 0, 0, 0)
- vbox.setAlignment(Qt.AlignmentFlag.AlignTop)
- groupbox.setLayout(vbox)
-
self._export_to_blender_button = QPushButton("Export to Blender")
self.export_to_blender_button.setObjectName("export_to_blender_button")
- groupbox.layout().addWidget(self._export_to_blender_button)
+ self._layout.addWidget(self._export_to_blender_button)
self._open_in_blender_automatically_checkbox = QCheckBox("Open in Blender automatically")
self._open_in_blender_automatically_checkbox.setChecked(True)
- # groupbox.layout().addWidget(self._open_in_blender_automatically_checkbox)
-
- self._use_ajc27_method_radio_button = QRadioButton("Use `@ajc27_blender_addon` method (Recommended)")
- self._use_ajc27_method_radio_button.setChecked(True)
- groupbox.layout().addWidget(self._use_ajc27_method_radio_button)
-
- self._use_alpha_megascript_take2_method_radio_button = QRadioButton(
- "Use `alpha_megascript_take2` method (Legacy)"
- )
- groupbox.layout().addWidget(self._use_alpha_megascript_take2_method_radio_button)
- #
- # self._use_alpha_megascript_method_radio_button = QRadioButton("Use `alpha_megascript` method (Deprecated)")
- # groupbox.layout().addWidget(self._use_alpha_megascript_method_radio_button)
- #
- # self._use_cgtinker_method_radio_button = QRadioButton("Use `@cgtinker` method (Work in progress)")
- # groupbox.layout().addWidget(self._use_cgtinker_method_radio_button)
self._layout.addWidget(QLabel("Blender Executable Path:"))
@@ -92,25 +66,6 @@ class VisualizationControlPanel(QWidget):
"""
return self._blender_executable_path
- def get_user_selected_method_string(self):
- """
- Returns the string that represents the method the user selected to generate the Blender scene.
-
- Returns:
- str: String representing the method the user selected to generate the Blender scene.
- """
- if self._use_ajc27_method_radio_button.isChecked():
- return "ajc27_blender_addon"
-
- if self._use_alpha_megascript_take2_method_radio_button.isChecked():
- return "megascript_take2"
-
- # if self._use_alpha_megascript_method_radio_button.isChecked():
- # return "alpha_megascript"
- #
- # if self._use_cgtinker_method_radio_button.isChecked():
- # return "cgtinker"
-
def _handle_blender_executable_button_clicked(self):
# from this tutorial - https://www.youtube.com/watch?v=gg5TepTc2Jg&t=649s
path_selection = QFileDialog.getOpenFileName(
diff --git a/freemocap/gui/qt/workers/export_to_blender_thread_worker.py b/freemocap/gui/qt/workers/export_to_blender_thread_worker.py
index af208fd6..3f04ca0b 100644
--- a/freemocap/gui/qt/workers/export_to_blender_thread_worker.py
+++ b/freemocap/gui/qt/workers/export_to_blender_thread_worker.py
@@ -18,7 +18,6 @@ class ExportToBlenderThreadWorker(QThread):
recording_path: Path,
blender_file_path: Path,
blender_executable_path: Path,
- blender_method: str,
kill_thread_event: threading.Event,
):
super().__init__()
@@ -27,7 +26,6 @@ class ExportToBlenderThreadWorker(QThread):
self.recording_path = recording_path
self.blender_file_path = blender_file_path
self.blender_executable_path = blender_executable_path
- self.blender_method = blender_method
self._work_done = False
@@ -46,7 +44,6 @@ class ExportToBlenderThreadWorker(QThread):
recording_folder_path=self.recording_path,
blender_file_path=self.blender_file_path,
blender_exe_path=self.blender_executable_path,
- method=self.blender_method,
)
self.success.emit(True)
logger.debug("Blender Export Complete")