Free Motion Capture for Everyone πβ¨
TypeScript
fmc_blender addition
aaroncheriande86a6a91d06
diff --git a/freemocap/fmc_blender.py b/freemocap/fmc_blender.pynew file mode 100644index 00000000..1c6fd968--- /dev/null+++ b/freemocap/fmc_blender.py@@ -0,0 +1,119 @@+import numpy as np, bpy+from mathutils import Vector+import math+from pathlib import Path+import sys++#the path of the npy file+argv = sys.argv+argv = argv[argv.index("--") + 1:]+input_npy = argv[0]++#3D array holding [[[x, y, z], [x, y, z]], [[x, y, z], [x, y, z]]]+arr = np.load(input_npy)++#the first frame+markers_list = arr[0]++#a list containing icosphere objects+order_of_icospheres = []++#frame rate+bpy.context.scene.render.fps = 30++#-----------------------------------------------------------------------------------++# set project unit+bpy.context.scene.unit_settings.length_unit = 'MILLIMETERS'+#iterate through arr and create an empty object at that location for each element+for col in markers_list:+ # parse string float value into floats, create Vector, set position to Vector+ if math.isnan(col[0]):+ col[0] = 0.0+ if math.isnan(col[1]):+ col[1] = 0.0+ if math.isnan(col[2]):+ col[2] = 0.0+ coord = Vector(((float(col[0])), (float(col[2])), (float(col[1]))* -1.0))+ #add icosphere+ bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, radius=0.01, enter_editmode=False, location=coord)+ sphere = bpy.context.active_object+ sphere.name = "Sphere"+ order_of_icospheres.append(sphere)+ #link empty to scene+ bpy.context.scene.collection.objects.link( sphere )+ #set location+ sphere.location = coord+#-----------------------------------------------------------------------------------+# Animate!+#find number of frames in animation+num_frames = len(arr)++#change start frame of animation+bpy.context.scene.frame_start = 1+#change end frame of animation+bpy.context.scene.frame_end = num_frames - 1++#create a new handler to change empty positions every frame+def my_handler(scene):+ bpy.ops.object.mode_set(mode='OBJECT')+ #keep track of current_marker+ current_marker = 0+ #find the current frame number+ frame = scene.frame_current+ #get the list of marker points from the current frame+ markers_list = arr[frame]+ #iterate through list of markers in this frame+ for col in markers_list:+ frame = scene.frame_current+ if math.isnan(col[0]):+ col[0] = 0.0+ if math.isnan(col[1]):+ col[1] = 0.0+ if math.isnan(col[2]):+ col[2] = 0.0+ coord = Vector(((float(col[0])), (float(col[2])), (float(col[1]))* -1.0))+ sphere = order_of_icospheres[current_marker]+ sphere.location = coord+ sphere.keyframe_insert(data_path='location',frame=scene.frame_current)+ current_marker += 1++#-----------------------------------------------------------------------------------+#function to register custom handler+bpy.app.handlers.frame_change_post.clear()+def register():+ bpy.app.handlers.frame_change_post.append(my_handler)++def unregister():+ bpy.app.handlers.frame_change_post.remove(my_handler)++bpy.ops.object.mode_set(mode='OBJECT')++register()++bpy.ops.object.mode_set(mode='OBJECT')++scene = bpy.context.scene++#set keyframes for whole animation+for frame in range(scene.frame_start, scene.frame_end):+ scene.frame_set(frame)++#set export path+export_path = str(Path(__file__).parent.resolve().parent.resolve() / "/")++#Select objects to export+col = bpy.data.collections.get("Collection")+if col:+ for obj in col.objects:+ if "Sphere" in obj.name:+ obj.select_set(True)++#save blender file+bpy.ops.wm.save_as_mainfile(filepath= export_path + "blender_data.blend")++#export FBX+bpy.ops.export_scene.fbx(filepath=export_path + "exported_animation_from_script.fbx", path_mode='RELATIVE', bake_anim=True, use_selection=True, object_types={'MESH'}, use_mesh_modifiers = False, add_leaf_bones = False, axis_forward = '-X', axis_up = 'Z', bake_anim_use_all_bones = False, bake_anim_use_nla_strips = False, bake_anim_use_all_actions = False, bake_anim_force_startend_keying = False)++#export GLTF+bpy.ops.export_scene.gltf(filepath=export_path + "exported_animation_from_script.gltf", export_format='GLTF_EMBEDDED', export_selected=True, ui_tab='ANIMATION', export_nla_strips=False)diff --git a/freemocap/fmc_runme.py b/freemocap/fmc_runme.pyindex 786c40c2..d7e7d805 100644--- a/freemocap/fmc_runme.py+++ b/freemocap/fmc_runme.py@@ -1,9 +1,10 @@-from freemocap.fmc_startup import startup+from freemocap.fmc_startup import startup, startupGUIfrom freemocap.webcam import camera_settings, timesyncfrom pathlib import Pathimport os+import subprocessfrom aniposelib.boards import CharucoBoard@@ -51,7 +52,8 @@ def RunMe(sessionID=None,reconstructionConfidenceThreshold = .7,charucoSquareSize = 36,#mm - ~the size of the squares when printed on 8.5x11" paper based on parameters in ReadMe.mdcalVideoFrameLength = -1,- startFrame = 0+ startFrame = 0,+ useBlender = False):"""Starts the freemocap pipeline based on either user-input values, or default values. Creates a new session class instance (called sesh)@@ -90,7 +92,8 @@ def RunMe(sessionID=None,print('Running ' + str(sesh.sessionID) + ' from ' + str(sesh.dataFolderPath))-+ if useBlender == True:+ blenderPath = startupGUI.RunChooseBlenderPathGUI(session)board = CharucoBoard(7, 5,#square_length=1, # here, in mm but any unit works (JSM NOTE - just using '1' so resulting units will be in 'charuco squarelenghts`)#marker_length=.8,@@ -196,7 +199,9 @@ def RunMe(sessionID=None,else:print('Skipping Skeleton Plotting')-+ if useBlender == True:+ output = subprocess.run([str(blenderPath), "--background", "--python", "fmc_blender.py", "--", str(sesh.dataArrayPath/'mediaPipeSkel_3d.npy')], capture_output=True, text=True, check=True)+ print(output)diff --git a/freemocap/fmc_startup/startupGUI.py b/freemocap/fmc_startup/startupGUI.pyindex 959d3221..9395244a 100644--- a/freemocap/fmc_startup/startupGUI.py+++ b/freemocap/fmc_startup/startupGUI.py@@ -103,6 +103,58 @@ class DLCConfigPathGUI:+class setBlenderPathGUI:+ def __init__(self, master,session):+ self.master = master+ self.session = session++ chooseText = "Choose your Blender executable"+ chooseLabel = Label(master, text=chooseText)+ chooseLabel.pack(side="top")++ self.currentPath = Path.cwd()+ text = 'Current file path: ' + str(self.currentPath)+ self.currentPathText = tk.StringVar()+ self.currentPathText.set(text)+ currentPathLabel = Label(master,textvariable = self.currentPathText)+ currentPathLabel.pack()++ changepath_button = Button(master,text = 'Change Folder Path',command = self.openFileDialog)+ changepath_button.pack(side = 'left')++ proceed_button = Button(text="Proceed", command=self.proceed)+ stop_button = Button(text="Quit", command=self.stop)+ proceed_button.pack()+ stop_button.pack()++++ def openFileDialog(self):+ self.currentPath = filedialog.askopenfilename(+ title='Open your Blender executable',+ initialdir= Path.cwd())+ self.currentPathText.set('Current file path: ' + self.currentPath)+++ def stop(self):+ self.master.destroy()+++ def proceed(self):+ self.continueToRecording = True+ self.dataPath = self.currentPath+ self.master.destroy()+++def RunChooseBlenderPathGUI(session):+ root = tk.Tk()+ chosenBlenderPath = setBlenderPathGUI(root, session)+ root.mainloop()++ return chosenBlenderPath.dataPath+++def RunChooseDataPathGUI(session):root = tk.Tk()diff --git a/runme_FreeMoCap.py b/runme_FreeMoCap.pyindex 53da9f66..84f332f9 100644--- a/runme_FreeMoCap.py+++ b/runme_FreeMoCap.py@@ -3,5 +3,5 @@ import freemocap as fmc#fmc.RunMe(useOpenPose = False, useMediaPipe=True, stage = 5, sessionID ='sesh_2021-09-13_14_34_12', recordVid=False)-fmc.RunMe()+fmc.RunMe(useBlender=True)