Skip to content

PCA Blendshape Node for Autodesk Maya

Mauro Lopez edited this page Jan 28, 2025 · 2 revisions

This is a custom Maya node designed to reduce and compress blendshapes using Principal Component Analysis (PCA). It extracts PCA components, weights, and mean values from the shapes of a blendshape node, allowing users to control how many components to preserve via a compression ratio. The node provides efficient storage and manipulation of blendshapes while maintaining a high level of control over the reconstruction quality.

Features

  • PCA Extraction:

    • Decomposes blendshape data into PCA components and weights.
    • Stores PCA components, weights, and mean values in the node.
  • Compression Control:

    • Allows the user to specify a desired compression ratio.
    • Calculates and displays the applied compression percentage.
  • Customizable Reconstruction:

    • Supports dynamic adjustment of the number of PCA components used for reconstruction.
  • UI Integration:

    • Includes a custom UI to interact with the node in Maya.

What is PCA?

Principal Component Analysis (PCA) is a statistical technique used to simplify complex datasets by reducing their dimensionality while preserving as much variance as possible. PCA identifies the directions (called principal components) in which the data varies the most and uses these components to re-express the data.

How PCA Works

  1. Centering the Data: PCA begins by centering the data by subtracting the mean

  2. Covariance Matrix: The covariance matrix of the centered data is computed to measure the relationships between features

  3. Eigen Decomposition: Eigenvalues and eigenvectors are calculated from the covariance matrix

  4. Dimensionality Reduction: The data is projected onto the top eigenvectors to reduce dimensionality

PCA Inference

To use the trained PCA model for inference (e.g., reconstructing or approximating data), the following steps are applied:

  1. Reconstruction $\hat{X} = W C + \mu$
  • $\hat{X}$ : Reconstructed data matrix.
  • $W$ : Matrix of PCA weights.
  • $C$ : Matrix of PCA components.
  • $\mu$ : Mean vector of the original data.

This reconstructs the original data from its principal component representation.

  • PCA Weights: Represents the weights of each principal component for each data point. These weights capture how much each data point contributes to the principal components.
  • PCA Components: Represents the principal components themselves, which are the directions of maximum variance in the data.
  • Mean: Represents the mean of the original data, which is added back to the reconstructed data to shift it back to the original data space.

In essence, this function combines the principal components weighted by their respective contributions to reconstruct an approximation of the original data. Since the components are sorted, we can choose how many components preserve and how much variation discard

  1. After the shapes are reconstructed, we need to apply the Shape weighting: $\hat{X}_{scaled} = S \odot \hat{X}$
  • $\hat{X}_{scaled}$ : Scaled reconstructed data matrix.
  • $S$: Diagonal matrix of shape weights.
  • $\odot$: Element-wise product.
  • $\hat{X}$ : Reconstructed data matrix.

This scales the reconstructed data based on a set of shape weights to activate only the shapes with weight different to zero.

  • shapes Weights: A vector or array containing scaling factors for each shape of the reconstructed data.
  • the Element-wise product allows for adjusting the scale or magnitude of the reconstructed data for each data point independently

In the context of blendshapes, PCA inference allows real-time manipulation of the mesh using a reduced set of PCA components while maintaining accurate reconstructions.


Installation

  1. Clone the repository:
    git clone https://github.com/yourusername/ml-example-nodes.git
  2. Copy the Python files to your Maya scripts directory, or Add Repository to System Path using sys.path.append.
    import sys
    sys.path.append('path/to/your/repo')

Usage

  1. Open Maya and load the script editor.
  2. To open the PCA Blendshape UI execute:
import nodes_ui
nodes_ui.pca_ui()
  1. Select the source blendshape node and target mesh.
  2. Click the "Create PCA Blendshape" button to create the PCA blendshape node.

UI Overview

  1. Source Blendshape:

    • Enter the name of the blendshape node containing the target shapes.
    • Use the "Get Blendshape Node" button to populate automatically.
  2. Target Mesh:

    • Specify the mesh to which the PCA blendshape will be applied.
    • Use the "Get Target Mesh" button to populate automatically.
  3. Create PCA Blendshape:

    • Click to process the blendshape data and create the PCA blendshape node.

Node Attributes

  • shapeWeights: Array of weights for the blendshape shapes.
  • pcaComponents: Stores the PCA components.
  • pcaWeights: Stores the weights for each PCA component.
  • pcaMean: Stores the mean shape used for reconstruction.
  • cumulativeVarianceRatio: Tracks how much variance is explained by each PCA component.
  • pcaCompressionRatio: User-defined ratio to control the level of compression (0.0 to 1.0).
  • compressionPercentage: Outputs the actual compression percentage achieved.

Example Workflow

  1. Create or select a blendshape node with multiple shapes.
  2. Use the UI to specify the source blendshape and target mesh.
  3. Adjust the compression ratio to achieve the desired trade-off between accuracy and data reduction.
  4. Use the PCA blendshape node to deform the mesh dynamically.

License

This project is open-source and licensed under the MIT License.


Contact

If you encounter issues or have suggestions, feel free to create an issue on GitHub or contact me directly on Blue Sky