Skip to content

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

TypeScript

Merge branch 'main' into blender_stuff

jonmatthisc70652cbacac
diff --cc freemocap/fmc_blender.py
index 030fd924,eae0c2fe..1f7732da
--- a/freemocap/fmc_blender.py
+++ b/freemocap/fmc_blender.py
@@@ -4,9 -4,8 +4,10 @@@ from mathutils import Vecto
import math
import sys
from pathlib import Path
+import csv
+from math import degrees
+
# get arguments
argv = sys.argv
argv = argv[argv.index("--") + 1:]
@@@ -68,13 -57,8 +69,14 @@@ master_collection = bpy.data.collection
for index, col in enumerate(markers_list):
# parse string float value into floats, create Vector, set position to Vector
- if not math.isnan(col[0]):
- coord = Vector(((float(col[0])*0.001), (float(col[2])*0.001), (float(col[1]))* -0.001))
+ if math.isnan(col[0]):
+ col[0] = np.nan
+ if math.isnan(col[1]):
+ col[1] = np.nan
+ if math.isnan(col[2]):
+ col[2] = np.nan
+ coord = Vector(((float(col[0])*0.001), (float(col[2])*0.001), (float(col[1]))* -0.001))
++
#empties
bpy.ops.object.add(type='EMPTY', location=coord)
@@@ -172,13 -150,14 +174,16 @@@ def add_child_bone(bone_name, empty_chi
#Create a new bone
new_bone = armature_data.data.edit_bones.new(bone_name)
#Set bone's size
- new_bone.head = (0,0,0)
- new_bone.tail = (0,0.1,0)
++
+ new_bone.head = (np.nan,np.nan,np.nan)
+ new_bone.tail = (np.nan,np.nan,np.nan)
+ #Set bone's location to wheel
- new_bone.matrix = empty2.matrix_world
+ new_bone.matrix = empty_parent.matrix_world
++
#set location of bone head
- new_bone.head = empty1.location
+ new_bone.head = empty_child.location
#set location of bone tail
- new_bone.tail = empty2.location
+ new_bone.tail = empty_parent.location
return new_bone
#Create armature object
@@@ -355,18 -331,20 +360,20 @@@ def my_handler(scene)
#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])*0.001), (float(col[2])*0.001), (float(col[1]))* -0.001))
- if len(order_of_markers) > 0:
- empty = order_of_markers[current_marker]
- empty.location = coord
- current_marker += 1
- for index in range(len(virtual_markers)):
- update_virtual_marker(index)
-
++
+ if math.isnan(col[0]) and math.isnan(col[1]) and math.isnan(col[2]):
+ #marker was not tracked, pass
+ pass
+ else:
+ #Flip y and z coordinates, and negate y to account for different coordinate system
+ coord = Vector(((float(col[0])*0.001), (float(col[2])*0.001), (float(col[1]))* -0.001))
+ if len(order_of_markers) > 0:
+ empty = order_of_markers[current_marker]
+ empty.location = coord
+ for index in range(len(virtual_markers)):
+ update_virtual_marker(index)
+ current_marker += 1
++
#keyframe bones
#Goes through each bone
@@@ -730,77 -558,21 +737,78 @@@ if col
for obj in col.objects:
if "Armature" in obj.name:
obj.select_set(True)
- if obj.name == "Cube":
+ if obj.name == "Cube" or obj.name == "Light" or obj.name == "Camera":
bpy.data.objects.remove(obj)
+
+for e in empties:
+ e.select_set(True)
#save blender file
-blend_file_save_path = sesh_path + "skeletal_animation_session.blend"
-print('saving .blend file to - ' + blend_file_save_path)
+bpy.ops.wm.save_as_mainfile(filepath= sesh_path + sesh_ID + '.blend')
-bpy.ops.wm.save_as_mainfile(filepath= sesh_path + "skeletal_animation_session.blend")
+
#export FBX
-bpy.ops.export_scene.fbx(filepath=sesh_path + "skeletal_animation_session.fbx", path_mode='RELATIVE', bake_anim=True, use_selection=True, object_types={'MESH', 'ARMATURE'}, 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)
+bpy.ops.export_scene.fbx(filepath= sesh_path + sesh_ID + '.fbx', path_mode='RELATIVE', bake_anim=True, use_selection=True, object_types={'MESH', 'ARMATURE', 'EMPTY'}, 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=sesh_path + "skeletal_animation_session.gltf", export_format='GLTF_EMBEDDED', export_selected=True, ui_tab='ANIMATION')
+bpy.ops.export_scene.gltf(filepath=sesh_path + sesh_ID + '.gltf', export_format='GLTF_EMBEDDED', use_selection=True, ui_tab='ANIMATION')
#export USD
-bpy.ops.wm.usd_export(filepath=sesh_path + 'skeletal_animation_session.usdc', selected_objects_only=True, export_animation=True)
+bpy.ops.wm.usd_export(filepath=sesh_path + sesh_ID + '.usdc', selected_objects_only=True, export_animation=True)
+
+#--------------------------------------------------------------------------------------------
+#Create a panel to display local bone angles in relation to parent bone
+class LOCAL_PT_BoneAnglesPanel(bpy.types.Panel):
+ bl_label = "Bone Angles Local"
+ bl_space_type = 'VIEW_3D'
+ bl_region_type = 'UI'
+
+ def draw(self, context):
+ arm = get_armature()
+
+ row = self.layout.row()
+ row.label(text='Bone name')
+ row.label(text='Degrees X')
+ row.label(text='Degrees Y')
+ row.label(text='Degrees Z')
+
+ for bone in arm.pose.bones:
+ row = self.layout.row()
+ row.label(text=bone.name)
+ if bone.parent:
+ Original_rot = bone.matrix_basis.copy()
+
+ a = bone.matrix.copy()
+ b = bone.parent.matrix.copy()
+
+ q1 = a.to_quaternion()
+
+ q2 = b.to_quaternion()
+
+ difQuat = q1.rotation_difference(q2)
+
+ difEuler = difQuat.to_euler()
+ mat = difEuler
+
+ else:
+ a = bone.matrix.copy()
+ q1 = a.to_quaternion()
+ mat = q1.to_euler()
+
+ x = mat.x
+ y = mat.y
+ z = mat.z
+
+ if x < 0.0001:
+ x = 0.0
+ if y < 0.0001:
+ y = 0.0
+ if z < 0.0001:
+ z = 0.0
+ row.label(text='X: {:.3}'.format(degrees(x)))
+ row.label(text='Y: {:.3}'.format(degrees(y)))
+ row.label(text='Z: {:.3}'.format(degrees(z)))
+
+
+bpy.utils.register_class(LOCAL_PT_BoneAnglesPanel)
diff --cc freemocap/fmc_runme.py
index 1a391f56,835b81cc..b17b07c5
--- a/freemocap/fmc_runme.py
+++ b/freemocap/fmc_runme.py
@@@ -263,22 -278,26 +278,27 @@@ def RunMe(sessionID=None
print('Skipping 2d point tracking')
-
+ # %% Stage 5 - Use Blender to create output data files
if stage <=5:
- if useBlender == True:
- thisStage=5
- console.rule(style="color({})".format(thisStage))
- console.rule('Exporting Files...'.upper(), style="color({})".format(thisStage))
- console.rule('Hijacking Blender\'s file format converters to export FreeMoCap data as various file format (.blend, .usd, .gltf, .fbx)', style="color({})".format(thisStage))
- console.rule(style="color({})".format(thisStage))
- time.sleep(pauseBetweenStages)
+ try:
+ if useBlender == True:
+ thisStage=5
+ console.rule(style="color({})".format(thisStage))
+ console.rule('Exporting Files...'.upper(), style="color({})".format(thisStage))
+ console.rule('Hijacking Blender\'s file format converters to export FreeMoCap data as various file format (.blend, .usd, .gltf, .fbx)', style="color({})".format(thisStage))
+ console.rule(style="color({})".format(thisStage))
+
- #blenderPath = Path('C:\Program Files\Blender Foundation\Blender 2.93')
- #os.chdir(blenderExePath)
- output = subprocess.run([str(blenderPath), "--background", "--python", "fmc_blender.py", "--", str(sesh.dataArrayPath/'mediaPipeSkel_3d.npy'), str(sesh.dataArrayPath), sesh.sessionID], capture_output=True, text=True, check=True)
- print(output)
+
- # %% Stage Five - Make Animation
+ #blenderPath = Path('C:\Program Files\Blender Foundation\Blender 2.93')
+ #os.chdir(blenderExePath)
- output = subprocess.run([str(blenderPath), "--background", "--python", str(subprocessPath), "--", str(sesh.dataArrayPath/'mediaPipeSkel_3d_smoothed.npy'), str(sesh.dataArrayPath)], capture_output=True, text=True, check=True)
++ output = subprocess.run([str(blenderPath), "--background", "--python", "fmc_blender.py", "--", str(sesh.dataArrayPath/'mediaPipeSkel_3d.npy'), str(sesh.dataArrayPath), sesh.sessionID], capture_output=True, text=True, check=True)
+ print(output)
+ except:
+ console.print_exception()
+
+
+ # %% Stage 6 - Make Animation
if stage <= 6:
thisStage=6
console.rule(style="color({})".format(thisStage))