Examples

This page provides examples of how to use py-dem-bones in various scenarios.

Basic Skinning Decomposition

This example demonstrates how to perform basic skinning decomposition on a simple mesh:

Basic Skinning Decomposition Example
 1"""
 2Example of using py-dem-bones for basic skinning decomposition.
 3"""
 4
 5import numpy as np
 6
 7import py_dem_bones as pdb
 8
 9
10def create_cube():
11    """Create a simple cube mesh."""
12    vertices = np.array([
13        [-1, -1, -1],  # 0
14        [1, -1, -1],   # 1
15        [1, 1, -1],    # 2
16        [-1, 1, -1],   # 3
17        [-1, -1, 1],   # 4
18        [1, -1, 1],    # 5
19        [1, 1, 1],     # 6
20        [-1, 1, 1]     # 7
21    ], dtype=np.float64)
22
23    return vertices
24
25
26def create_deformed_cube(scale_y):
27    """Create a deformed cube by stretching it along the y-axis."""
28    vertices = create_cube()
29    deformed = vertices.copy()
30    deformed[:, 1] *= scale_y
31    return deformed
32
33
34def main():
35    """Run the example."""
36    # Create rest pose and animated poses
37    rest_pose = create_cube()
38    animated_poses = np.vstack([
39        create_deformed_cube(1.2),  # Frame 1
40        create_deformed_cube(1.5),  # Frame 2
41        create_deformed_cube(1.8)   # Frame 3
42    ])
43
44    # Create DemBones instance
45    dem_bones = pdb.DemBones()
46
47    # Set parameters
48    dem_bones.nIters = 20
49    dem_bones.nInitIters = 10
50    dem_bones.nTransIters = 5
51    dem_bones.nWeightsIters = 3
52    dem_bones.nnz = 4
53    dem_bones.weightsSmooth = 1e-4
54
55    # Set data
56    dem_bones.nV = 8  # 8 vertices in a cube
57    dem_bones.nB = 2  # 2 bones
58    dem_bones.nF = 3  # 3 frames
59    dem_bones.nS = 1  # 1 subject
60    dem_bones.fStart = np.array([0], dtype=np.int32)
61    dem_bones.subjectID = np.zeros(3, dtype=np.int32)
62    dem_bones.u = rest_pose
63    dem_bones.v = animated_poses
64
65    # Compute skinning decomposition
66    dem_bones.compute()
67
68    # Get results
69    weights = dem_bones.get_weights()
70    transformations = dem_bones.get_transformations()
71
72    print("Skinning weights:")
73    print(weights)
74    print("\nBone transformations:")
75    print(transformations)
76
77
78if __name__ == "__main__":
79    main()

Working with Hierarchical Skeletons

This example demonstrates how to work with hierarchical skeletons:

Hierarchical Skeleton Example
  1"""
  2Example of using py-dem-bones with hierarchical skeletons.
  3"""
  4
  5import numpy as np
  6
  7import py_dem_bones as pdb
  8
  9
 10def create_articulated_mesh():
 11    """Create a simple articulated mesh (two connected boxes)."""
 12    # First box: vertices 0-7
 13    box1 = np.array([
 14        [-2, -1, -1],
 15        [-1, -1, -1],
 16        [-1, 1, -1],
 17        [-2, 1, -1],
 18        [-2, -1, 1],
 19        [-1, -1, 1],
 20        [-1, 1, 1],
 21        [-2, 1, 1]
 22    ], dtype=np.float64)
 23
 24    # Second box: vertices 8-15
 25    box2 = np.array([
 26        [1, -1, -1],
 27        [2, -1, -1],
 28        [2, 1, -1],
 29        [1, 1, -1],
 30        [1, -1, 1],
 31        [2, -1, 1],
 32        [2, 1, 1],
 33        [1, 1, 1]
 34    ], dtype=np.float64)
 35
 36    return np.vstack([box1, box2])
 37
 38
 39def create_deformed_articulated_mesh(angle_deg):
 40    """Create a deformed articulated mesh by rotating the second box."""
 41    vertices = create_articulated_mesh()
 42    angle_rad = np.radians(angle_deg)
 43
 44    # Keep the first box fixed
 45    deformed = vertices.copy()
 46
 47    # Rotate the second box around the y-axis
 48    cos_a = np.cos(angle_rad)
 49    sin_a = np.sin(angle_rad)
 50
 51    for i in range(8, 16):
 52        x, z = vertices[i, 0], vertices[i, 2]
 53        deformed[i, 0] = x * cos_a - z * sin_a
 54        deformed[i, 2] = x * sin_a + z * cos_a
 55
 56    return deformed
 57
 58
 59def main():
 60    """Run the example."""
 61    # Create rest pose and animated poses
 62    rest_pose = create_articulated_mesh()
 63    animated_poses = np.vstack([
 64        create_deformed_articulated_mesh(15),  # Frame 1
 65        create_deformed_articulated_mesh(30),  # Frame 2
 66        create_deformed_articulated_mesh(45)   # Frame 3
 67    ])
 68
 69    # Create DemBonesExt instance
 70    dem_bones_ext = pdb.DemBonesExt()
 71
 72    # Set parameters
 73    dem_bones_ext.nIters = 20
 74    dem_bones_ext.nInitIters = 10
 75    dem_bones_ext.nTransIters = 5
 76    dem_bones_ext.nWeightsIters = 3
 77    dem_bones_ext.nnz = 4
 78    dem_bones_ext.weightsSmooth = 1e-4
 79
 80    # Set data
 81    dem_bones_ext.nV = 16  # 16 vertices in the articulated mesh
 82    dem_bones_ext.nB = 2   # 2 bones
 83    dem_bones_ext.nF = 3   # 3 frames
 84    dem_bones_ext.nS = 1   # 1 subject
 85    dem_bones_ext.fStart = np.array([0], dtype=np.int32)
 86    dem_bones_ext.subjectID = np.zeros(3, dtype=np.int32)
 87    dem_bones_ext.u = rest_pose
 88    dem_bones_ext.v = animated_poses
 89
 90    # Set hierarchical skeleton data
 91    dem_bones_ext.parent = np.array([-1, 0], dtype=np.int32)  # Bone 1 is the child of Bone 0
 92    dem_bones_ext.boneName = ["Box1", "Box2"]
 93    dem_bones_ext.bindUpdate = 1
 94
 95    # Compute skinning decomposition
 96    dem_bones_ext.compute()
 97
 98    # Get results
 99    weights = dem_bones_ext.get_weights()
100    transformations = dem_bones_ext.get_transformations()
101
102    # Compute local rotations and translations
103    dem_bones_ext.computeRTB()
104
105    print("Skinning weights:")
106    print(weights)
107    print("\nBone transformations:")
108    print(transformations)
109
110
111if __name__ == "__main__":
112    main()

DCC Integration Examples

py-dem-bones can be integrated with various Digital Content Creation (DCC) software:

  • Maya Integration: View Example

  • Blender Integration: View Example

  • Houdini Integration: View Example

  • 3ds Max Integration: View Example

  • Unreal Engine Integration: View Example