### Initialize MotionEditor Component Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Setup an interactive motion editor by defining a dataset with analysis modules and attaching the component to a scene entity. ```python from ai4animation import ( AI4Animation, Dataset, MotionEditor, RootModule, MotionModule, ContactModule, GuidanceModule ) class Program: def Start(self): # Create motion editor entity editor_entity = AI4Animation.Scene.AddEntity("Editor") # Create dataset with analysis modules dataset = Dataset( directory="/path/to/motions", modules=[ lambda m: RootModule( m, "Hips", "LeftUpLeg", "RightUpLeg", "LeftShoulder", "RightShoulder", "Neck" ), lambda m: MotionModule(m), lambda m: ContactModule(m, [ ("LeftFoot", 0.1, 0.25), ("RightFoot", 0.1, 0.25), ]), lambda m: GuidanceModule(m), ] ) # Add MotionEditor component bone_names = ["Hips", "Spine", "Head", "LeftArm", "RightArm", "LeftLeg", "RightLeg"] self.editor = editor_entity.AddComponent( MotionEditor, dataset, # Motion dataset "character.glb", # Character model bone_names # Bones to animate ) # Set camera target AI4Animation.Standalone.Camera.SetTarget(editor_entity) def Update(self): # Editor handles its own updates pass AI4Animation(Program(), mode=AI4Animation.Mode.STANDALONE) ``` -------------------------------- ### AI4Animation Application Lifecycle Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt The AI4Animation class serves as the main entry point for the framework, managing the application lifecycle through Start, Update, Draw, and GUI callbacks. ```APIDOC ## AI4Animation Application Lifecycle ### Description Initializes the simulation environment and manages the execution loop. Supports Standalone (with rendering), Headless (server-side), and Manual (custom control) modes. ### Method Class Initialization ### Parameters #### Request Body - **Program** (object) - Required - An instance of a class implementing Start, Update, Draw, and GUI methods. - **mode** (enum) - Required - Execution mode: STANDALONE, HEADLESS, or MANUAL. ### Request Example AI4Animation(Program(), mode=AI4Animation.Mode.STANDALONE) ``` -------------------------------- ### Initialize AdamW and CyclicLRWithRestarts Source: https://github.com/facebookresearch/ai4animationpy/blob/main/ai4animation/AI/Optimizers/AdamWR/README.md Demonstrates setting up the AdamW optimizer and the CyclicLRWithRestarts scheduler within a training loop. ```python batch_size = 32 epoch_size = 1024 model = resnet() optimizer = AdamW(model.parameters(), lr=1e-3, weight_decay=1e-5) scheduler = CyclicLRWithRestarts(optimizer, batch_size, epoch_size, restart_period=5, t_mult=1.2, policy="cosine") for epoch in range(100): scheduler.step() train_for_every_batch(...) ... optimizer.zero_grad() loss.backward() optimizer.step() scheduler.batch_step() validate(...) ``` -------------------------------- ### Initialize and Run AI4Animation Application Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt The `AI4Animation` class is the main entry point. It initializes the scene and manages the update loop. Use different modes like STANDALONE, HEADLESS, or MANUAL for various use cases. ```python from ai4animation import AI4Animation, Scene, Entity, Vector3 class Program: def Start(self): # Called once at initialization self.entity = AI4Animation.Scene.AddEntity("MyEntity") self.entity.SetPosition(Vector3.Create(0, 1, 0)) print("Application started!") def Update(self): # Called every frame for logic updates pass def Draw(self): # Called every frame for rendering (Standalone mode only) AI4Animation.Draw.Sphere(self.entity.GetPosition(), size=0.1) def GUI(self): # Called every frame for UI (Standalone mode only) pass # Run with built-in renderer AI4Animation(Program(), mode=AI4Animation.Mode.STANDALONE) # Run headless for server-side training AI4Animation(Program(), mode=AI4Animation.Mode.HEADLESS) # Manual mode for custom update control app = AI4Animation(Program(), mode=AI4Animation.Mode.MANUAL) for i in range(1000): AI4Animation.Update(deltaTime=1/60) # Manually step the simulation ``` -------------------------------- ### Configure and Use DataSampler Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Initializes a data sampler for motion datasets and demonstrates batching strategies for training loops. ```python # Create sampler sampler = DataSampler( dataset=dataset, framerate=30, # Sampling rate batch_size=32, function=extract_features, start_padding=0.5, # Skip first 0.5s end_padding=0.5 # Skip last 0.5s ) print(f"Total samples: {sampler.SampleCount}") # Get toy sample for debugging toy_sample = sampler.GetToySample() # Training loop with batches from same motion for epoch in range(num_epochs): for batch in sampler.SampleBatchesWithinMotions(epoch, num_epochs): # batch contains extracted features positions = Tensor.ToDevice(batch["positions"]) velocities = Tensor.ToDevice(batch["velocities"]) # ... training step # Alternative: batches across different motions batches = sampler.SampleBatchesAcrossMotions() for batch in batches: # Each sample in batch may be from different motion file pass ``` -------------------------------- ### Create and train an MLP model Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Set up and train a Multi-Layer Perceptron (MLP) model for neural network tasks. This includes defining the model architecture, configuring an optimizer with a cyclic learning rate scheduler, and implementing a training loop with loss logging. ```python import torch from ai4animation import MLP, AdamW, CyclicScheduler, Tensor, Plotting # Create MLP model model = Tensor.ToDevice(MLP.Model( input_dim=64, output_dim=128, hidden_dim=256, dropout=0.1 )) # Setup optimizer with warm restarts optimizer = AdamW(model.parameters(), lr=1e-4, weight_decay=1e-4) scheduler = CyclicScheduler( optimizer=optimizer, batch_size=32, epoch_size=10000, restart_period=10, t_mult=2, policy="cosine", verbose=True ) # Training loop loss_history = Plotting.LossHistory("Training Loss", drawInterval=100, yScale="log") for epoch in range(100): for batch in dataloader: x_batch = Tensor.ToDevice(batch["input"]) y_batch = Tensor.ToDevice(batch["output"]) # Learn with statistics update (first epoch only) predictions, losses = model.learn(x_batch, y_batch, update_stats=(epoch == 0)) optimizer.zero_grad() sum(losses.values()).backward() optimizer.step() scheduler.batch_step() # Log loss for name, value in losses.items(): loss_history.Add((Plotting.ToNumpy(value), name)) scheduler.step() loss_history.Print() # Inference model.eval() with torch.no_grad(): output = model(input_tensor) ``` -------------------------------- ### Batch Convert Motion Files via CLI Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Utilize the command-line interface to convert motion files (GLB, FBX, BVH) in a directory to NPZ format. ```bash # Convert all motion files in a directory convert --input_dir /path/to/motions --output_dir /path/to/output # The converter supports GLB, FBX, and BVH files # Output format is NPZ with positions and quaternions per joint per frame ``` -------------------------------- ### Initialize Tensor Utilities Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Imports the Tensor module for unified array operations across NumPy and PyTorch. ```python from ai4animation.Math import Tensor import torch ``` -------------------------------- ### Batch Convert Motion Directories via CLI Source: https://github.com/facebookresearch/ai4animationpy/blob/main/README.md Use the command line interface to convert all motion files within a specified input directory to the output directory. ```bash convert --input_dir path/to/motions --output_dir path/to/output ``` -------------------------------- ### Initialize and Manage Actor Component Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Set up an Actor component for a skeletal character, mapping motion data to scene entities. Allows retrieval and modification of bone transforms. ```python from ai4animation import AI4Animation, Actor, Vector3, Transform import os class Program: def Start(self): # Create actor entity entity = AI4Animation.Scene.AddEntity("Character") # Add Actor component with model and bone names bone_names = ["Hips", "Spine", "Chest", "Head", "LeftArm", "RightArm"] self.actor = entity.AddComponent( Actor, "character.glb", # Model path bone_names, # Bone names to use True # Show GUI controls ) # Position the character entity.SetPosition(Vector3.Create(0, 0, 0)) def Update(self): # Get/set bone transforms transforms = self.actor.GetTransforms() # All bones positions = self.actor.GetPositions(["LeftHand", "RightHand"]) rotations = self.actor.GetRotations() velocities = self.actor.GetVelocities() # Modify specific bones self.actor.SetPositions(new_positions, bones=["LeftHand"]) self.actor.SetRotations(new_rotations, bones=[0, 1, 2]) # Root transform operations root = self.actor.GetRoot() root_pos = self.actor.GetRootPosition() root_dir = self.actor.GetRootDirection() # Get bone by name hand_bone = self.actor.GetBone("LeftHand") hand_pos = hand_bone.GetPosition() hand_rot = hand_bone.GetRotation() # Bone chain operations chain = Actor.GetChain( self.actor.GetBone("Spine"), self.actor.GetBone("LeftHand") ) # Sync actor transforms to/from scene self.actor.SyncToScene() # Actor -> Scene entities self.actor.SyncFromScene() # Scene entities -> Actor # Restore bone lengths/alignments self.actor.RestoreBoneLengths() self.actor.RestoreBoneAlignments() AI4Animation(Program(), mode=AI4Animation.Mode.STANDALONE) ``` -------------------------------- ### Load and Access Data from GLB/FBX/BVH Files Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Use GLB, FBX, and BVH importers to access mesh, skeleton, and motion data directly from files. Supports custom hierarchy loading for GLB. ```python from ai4animation import GLB, FBX, BVH # GLB Importer - access mesh and skeleton data glb = GLB("character.glb") # Get mesh data skinned_mesh = glb.SkinnedMesh print(f"Vertices: {skinned_mesh.Vertices.shape}") print(f"Triangles: {skinned_mesh.Triangles.shape}") print(f"Skin weights: {skinned_mesh.SkinWeights.shape}") # Get skeleton info joint_names = glb.JointNames joint_parents = glb.JointParents joint_matrices = glb.JointMatrices # Load motion with custom hierarchy motion = glb.LoadMotion( names=["Hips", "Spine", "Head"], # Subset of bones floor="Hips" # Reference bone for floor alignment ) # FBX Importer fbx = FBX("character.fbx") motion_fbx = fbx.LoadMotion() # BVH Importer with scale and corrections bvh = BVH("animation.bvh", scale=0.01) motion_bvh = bvh.LoadMotion() # Debug GLB structure glb.Debug() ``` -------------------------------- ### Import Motion Data in Python Source: https://github.com/facebookresearch/ai4animationpy/blob/main/README.md Load various motion capture formats into the Motion object and export to the internal NPZ format. ```python from ai4animation import Motion motion = Motion.LoadFromGLB("character.glb") motion = Motion.LoadFromFBX("character.fbx") motion = Motion.LoadFromBVH("character.bvh", scale=0.01) motion.SaveToNPZ("character") ``` -------------------------------- ### Manage Motion Datasets Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt The Dataset class handles loading and filtering motion files. Modules can be injected during initialization to process motion data automatically. ```python from ai4animation import Dataset, Motion, RootModule, MotionModule, ContactModule # Create dataset from directory of NPZ files dataset = Dataset( directory="/path/to/motions", modules=[ lambda motion: RootModule( motion, hip="Hips", left_hip="LeftUpLeg", right_hip="RightUpLeg", left_shoulder="LeftShoulder", right_shoulder="RightShoulder", neck="Neck" ), lambda motion: MotionModule(motion), lambda motion: ContactModule( motion, contacts=[ ("LeftFoot", 0.1, 0.25), # (bone_name, radius, height_threshold) ("RightFoot", 0.1, 0.25), ] ), ], max_files=100 # Optional: limit number of files ) # Dataset properties print(f"Total files: {len(dataset)}") # Filter dataset dataset.Filter(id="walk") # Only files containing "walk" dataset.Filter() # Reset filter # Load motion by index motion = dataset.LoadMotion(0) # Access modules on loaded motion root_module = motion.GetModule(RootModule) contact_module = motion.GetModule(ContactModule) ``` -------------------------------- ### Load and Process Motion Data with AI4Animation Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt The `Motion` class loads animation data from GLB, FBX, BVH, and NPZ formats. It allows querying bone transformations, positions, rotations, and velocities. Use `LoadFromGLB`, `LoadFromFBX`, `LoadFromBVH`, `LoadFromNPZ`, `SaveToNPZ`, `GetBonePositions`, `GetBoneRotations`, and `GetBoneVelocities`. ```python from ai4animation import Motion, Hierarchy # Load motion from different formats motion_glb = Motion.LoadFromGLB("character.glb") motion_fbx = Motion.LoadFromFBX("character.fbx") motion_bvh = Motion.LoadFromBVH("character.bvh", scale=0.01) # Load with specific bone subset and floor reference motion = Motion.LoadFromGLB( "character.glb", names=["Hips", "Spine", "LeftArm", "RightArm"], # Only these bones floor="Hips" # Use Hips as floor reference ) # Save to NPZ format for fast loading motion.SaveToNPZ("processed_motion") motion_loaded = Motion.LoadFromNPZ("processed_motion.npz") # Query motion properties print(f"Duration: {motion.TotalTime}s") print(f"Framerate: {motion.Framerate} FPS") print(f"Frames: {motion.NumFrames}") print(f"Joints: {motion.NumJoints}") # Get bone data at specific timestamps timestamps = [0.0, 0.5, 1.0, 1.5] positions = motion.GetBonePositions(timestamps, bone_names_or_indices=["LeftHand", "RightHand"]) rotations = motion.GetBoneRotations(timestamps, bone_names_or_indices=[0, 1, 2]) velocities = motion.GetBoneVelocities(timestamps) # Get mirrored motion data mirrored_positions = motion.GetBonePositions(timestamps, mirrored=True) # Get averaged bone lengths across all frames bone_names, lengths = motion.GetAveragedBoneLengths() # Debug motion info motion.Debug() ``` -------------------------------- ### Create and use an Autoencoder model Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Initialize and utilize an Autoencoder for motion feature compression and reconstruction. The model can be trained to learn latent representations and then used for encoding, decoding, or full reconstruction. ```python import torch from ai4animation import Autoencoder, Tensor # Create Autoencoder model = Tensor.ToDevice(Autoencoder.Model( feature_dim=256, # Input/output dimension hidden_dim=512, # Hidden layer size latent_dim=32, # Latent space dimension dropout=0.1, manifold=None # Optional manifold constraint )) # Training for batch in dataloader: features = Tensor.ToDevice(batch["features"]) # Learn returns predictions and losses preds, losses = model.learn(features, update_stats=(epoch == 0)) # preds["Y"] = reconstruction # preds["Z"] = latent codes # losses["MSE"] = reconstruction loss optimizer.zero_grad() losses["MSE"].backward() optimizer.step() # Inference model.eval() with torch.no_grad(): # Full forward pass reconstruction = model(input_features) # Get latent code and reconstruction reconstruction, latent = model(input_features, return_latent=True) # Encode only latent = model.encode(input_features) # Decode from latent output = model.decode(latent) ``` -------------------------------- ### Motion Capture Import and Processing Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Handles loading animation data from various file formats and provides utilities for querying bone transformations and motion properties. ```APIDOC ## Motion Capture Import ### Description Loads motion data from GLB, FBX, BVH, or NPZ files. Allows filtering by bone names and querying bone positions, rotations, and velocities at specific timestamps. ### Method Motion.LoadFromGLB, Motion.LoadFromFBX, Motion.LoadFromBVH, Motion.LoadFromNPZ ### Parameters #### Request Body - **filepath** (string) - Required - Path to the animation file. - **names** (list) - Optional - List of bone names to include. - **floor** (string) - Optional - Bone name to use as floor reference. - **scale** (float) - Optional - Scaling factor for BVH files. ``` -------------------------------- ### Solve Inverse Kinematics with FABRIK Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Use the FABRIK class to solve bone chain positions with optional pole targets. Ensure the actor is initialized and transforms are reset before calling Solve. ```python from ai4animation import AI4Animation, Actor, FABRIK, Vector3 class Program: def Start(self): entity = AI4Animation.Scene.AddEntity("Character") self.actor = entity.AddComponent(Actor, "character.glb", bone_names) # Create IK chain from shoulder to wrist self.ik_left = FABRIK( source=self.actor.GetBone("LeftShoulder"), target=self.actor.GetBone("LeftWrist") ) self.ik_right = FABRIK( source=self.actor.GetBone("RightShoulder"), target=self.actor.GetBone("RightWrist") ) # Create interactive target self.target = AI4Animation.Scene.AddEntity("IKTarget") self.target.SetPosition(self.actor.GetBone("LeftWrist").GetPosition()) # Store initial pose for reset self.initial_pose = self.actor.GetTransforms() def Update(self): # Reset to initial pose before solving IK self.actor.SetTransforms(self.initial_pose) # Solve IK with position and rotation target self.ik_left.Solve( position=self.target.GetPosition(), rotation=self.target.GetRotation(), max_iterations=10, threshold=0.001 ) # Solve with pole target for elbow direction elbow_pole = Vector3.Create(0, 0, -1) # Elbow points backward self.ik_right.Solve( position=Vector3.Create(1, 1.5, 0), pole_target=elbow_pole, pole_weight=1.0 ) # Sync affected bones to scene self.actor.SyncToScene(self.ik_left.Bones) self.actor.SyncToScene(self.ik_right.Bones) # Access IK chain info first_bone = self.ik_left.FirstBone() last_bone = self.ik_left.LastBone() all_bones = self.ik_left.Bones AI4Animation(Program(), mode=AI4Animation.Mode.STANDALONE) ``` -------------------------------- ### Create and use TimeSeries object Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Initialize a TimeSeries object to manage temporal data. It provides utilities for sampling, calculating time differences, and retrieving data at specific timestamps or simulating future timestamps. ```python from ai4animation import TimeSeries # Create time series from -1s to +1s with 13 samples timeseries = TimeSeries(start=-1.0, end=1.0, samples=13) # Properties print(f"Sample count: {timeseries.SampleCount}") # 13 print(f"Window: {timeseries.Window}") # 2.0 print(f"Delta time: {timeseries.DeltaTime}") # ~0.167 print(f"Max frequency: {timeseries.MaximumFrequency}") # 3.25 Hz # Get timestamps array timestamps = timeseries.Timestamps # [-1.0, -0.833, ..., 1.0] # Get sample at specific timestamp sample = timeseries.GetSample(0.5) print(f"Index: {sample.Index}, Time: {sample.Timestamp}") # Simulate timestamps from current time current_time = 2.5 future_timestamps = timeseries.SimulateTimestamps(current_time) # Returns [1.5, 1.667, ..., 3.5] ``` -------------------------------- ### Control trajectory with user input Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Use the Control class to manage character movement based on user input, specifying position, direction, velocity, and sensitivity parameters. ```python series.Control( position=current_pos, direction=move_direction, velocity=move_velocity, deltaTime=dt, moveSensitivty=10.0, turnSensitivity=10.0 ) ``` -------------------------------- ### Perform Tensor Operations Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Collection of tensor creation, shape manipulation, math, trigonometry, and aggregation methods. ```python # Creation arr = Tensor.Create([1, 2, 3]) zeros = Tensor.Zeros(10, 3) ones = Tensor.Ones(5, 4, 4) eye = Tensor.Eye(4) linspace = Tensor.LinSpace(0, 1, 100) arange = Tensor.Arange(0, 10, 0.1) # Shape operations reshaped = Tensor.Reshape(arr, (3, 1)) squeezed = Tensor.Squeeze(arr, axis=0) unsqueezed = Tensor.Unsqueeze(arr, axis=-1) flattened = Tensor.Flatten(arr) stacked = Tensor.Stack([a, b, c], axis=0) concatenated = Tensor.Concat([a, b], axis=0) # Math operations norm = Tensor.Norm(arr, keepDim=True) normalized = Tensor.Normalize(arr) clamped = Tensor.Clamp(arr, min_val=0, max_val=1) interpolated = Tensor.Interpolate(a, b, weight=0.5) # Trigonometry sin = Tensor.Sin(angles) cos = Tensor.Cos(angles) rad = Tensor.Deg2Rad(degrees) deg = Tensor.Rad2Deg(radians) # Aggregation mean = Tensor.Mean(arr, axis=0) sum_val = Tensor.Sum(arr, axis=-1) gaussian = Tensor.Gaussian(arr, power=2.0, axis=0) # PyTorch device management gpu_tensor = Tensor.ToDevice(torch_tensor) # Move to GPU if available numpy_arr = Tensor.ToNumpy(gpu_tensor) # Convert to NumPy ``` -------------------------------- ### Scene and Entity Management Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Manages the hierarchical scene graph, allowing for the creation of entities, parent-child relationships, and transformation operations. ```APIDOC ## Scene and Entity Management ### Description Provides methods to add entities to the scene, establish parent-child hierarchies for forward kinematics, and perform coordinate transformations. ### Method Scene.AddEntity, Entity.SetPosition, Entity.SetRotation ### Parameters #### Request Body - **name** (string) - Required - Name of the entity. - **position** (Vector3) - Optional - Initial position. - **rotation** (Rotation) - Optional - Initial rotation. - **parent** (Entity) - Optional - Parent entity for hierarchy. ``` -------------------------------- ### Extract Root Trajectory Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Use RootModule to compute root transforms and velocities. It supports both biped and quadruped topologies. ```python from ai4animation import Motion, RootModule, TimeSeries # Load motion and add RootModule motion = Motion.LoadFromNPZ("walk.npz") motion.AddModule(lambda m: RootModule( m, hip="Hips", left_hip="LeftUpLeg", right_hip="RightUpLeg", left_shoulder="LeftShoulder", right_shoulder="RightShoulder", neck="Neck", topology=RootModule.Topology.BIPED # or QUADRUPED )) root = motion.GetModule(RootModule) # Get root transforms at specific timestamps timestamps = [0.0, 0.5, 1.0] transforms = root.GetTransforms(timestamps, mirrored=False) positions = root.GetPositions(timestamps, mirrored=False) rotations = root.GetRotations(timestamps, mirrored=False) velocities = root.GetVelocities(timestamps, mirrored=False) # Get delta transforms (change between frames) delta = root.GetDeltaTransforms(timestamps, mirrored=False) delta_vectors = root.GetDeltaVectors(timestamps) # [dx, rotation_y, dz] # Compute trajectory series for time window timeseries = TimeSeries(start=-1.0, end=1.0, samples=13) series = root.ComputeSeries( timestamp=0.5, mirrored=False, timeseries=timeseries, smoothing=None # Optional smoothing ) # Access series data series_positions = series.Transforms series_velocities = series.Velocities ``` -------------------------------- ### Manage Scene and Entities in AI4Animation Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt The `Scene` class manages entities, which represent objects with transformations. Entities can form a hierarchy for forward kinematics. Use `AddEntity`, `SetPosition`, `SetRotation`, `GetPosition`, `FindChild`, and `GetChain` for scene graph operations. ```python from ai4animation import AI4Animation, Entity, Vector3, Rotation # Create entities with the scene scene = AI4Animation.Scene # Add root entity root = scene.AddEntity("Root", position=Vector3.Create(0, 0, 0)) # Add child entity with parent relationship child = scene.AddEntity( "Child", position=Vector3.Create(1, 0, 0), rotation=Rotation.Euler(0, 45, 0), parent=root ) # Transform operations root.SetPosition(Vector3.Create(0, 2, 0)) root.SetRotation(Rotation.Euler(0, 90, 0)) child_pos = child.GetPosition() # World position affected by parent # Find entities in hierarchy found = root.FindChild("Child") chain = Entity.GetChain(root, child) # Get entity chain from root to child # Print hierarchy for debugging scene.PrintHierarchy() ``` -------------------------------- ### Perform 4x4 Matrix Operations with Transform Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Provides methods for creating, manipulating, and transforming 4x4 homogeneous matrices. ```python from ai4animation.Math import Transform, Rotation, Vector3, Tensor # Create identity transform identity = Transform.Identity() # Single 4x4 matrix batch_identity = Transform.Identity(shape=(10,)) # Batch of 10 # Create from translation and rotation translation = Vector3.Create(1, 2, 3) rotation = Rotation.Euler(0, 45, 0) transform = Transform.TR(translation, rotation) # Create with scale scale = Vector3.Create(1, 1, 1) transform = Transform.TRS(translation, rotation, scale) # Extract components position = Transform.GetPosition(transform) # [x, y, z] rotation = Transform.GetRotation(transform) # 3x3 matrix axis_x = Transform.GetAxisX(transform) # Right vector axis_y = Transform.GetAxisY(transform) # Up vector axis_z = Transform.GetAxisZ(transform) # Forward vector # Set components Transform.SetPosition(transform, Vector3.Create(0, 1, 0)) Transform.SetRotation(transform, Rotation.Euler(0, 90, 0)) # Matrix operations inverse = Transform.Inverse(transform) combined = Transform.Multiply(parent, child) interpolated = Transform.Interpolate(a, b, weight=0.5) # Space transformations world_to_local = Transform.TransformationTo(world_transform, space) local_to_world = Transform.TransformationFrom(local_transform, space) a_to_b = Transform.TransformationFromTo(transform, space_a, space_b) # Mirroring mirrored = Transform.GetMirror(transform, Vector3.Axis.ZPositive) ``` -------------------------------- ### Execute Quaternion Rotation Operations Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Handles quaternion-based rotations, including conversions from Euler angles and angle-axis representations. ```python from ai4animation.Math import Quaternion, Vector3 # Create quaternion identity = Quaternion.Create() # [0, 0, 0, 1] q = Quaternion.Create(0, 0, 0.707, 0.707) # 90 degrees around Z # From Euler angles (degrees) q = Quaternion.Euler(pitch=0, yaw=45, roll=0) q = Quaternion.Euler([0, 45, 0]) # Same as above # From angle-axis q = Quaternion.AngleAxis(angle=90, axis=Vector3.Y) q = Quaternion.RotationX(45) q = Quaternion.RotationY(90) q = Quaternion.RotationZ(30) # To angle-axis angle, axis = Quaternion.ToAngleAxis(q) # Quaternion operations conjugate = Quaternion.Conjugate(q) inverse = Quaternion.Inverse(q) normalized = Quaternion.Normalize(q) # Multiply quaternions combined = Quaternion.Multiply(q1, q2) # Rotate vector by quaternion rotated_vec = Quaternion.Multiply(q, vector) # Convert to/from rotation matrix matrix = Quaternion.ToMatrix(q) # 3x3 rotation matrix q = Quaternion.FromMatrix(matrix) # Rotation from one vector to another q = Quaternion.FromTo(from_vector, to_vector) ``` -------------------------------- ### Perform 3D Vector Operations with Vector3 Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Offers vectorized 3D math operations including interpolation, distance calculations, and space transformations. ```python from ai4animation.Math import Vector3, Tensor # Create vectors v = Vector3.Create(1, 2, 3) v = Vector3.Create([1, 2, 3]) zeros = Vector3.Zero(10) # Batch of 10 zero vectors ones = Vector3.One(10) # Batch of 10 ones # Unit vectors Vector3.X # [1, 0, 0] Vector3.Y # [0, 1, 0] Vector3.Z # [0, 0, 1] # Basic operations length = Vector3.Length(v) normalized = Vector3.Normalize(v) distance = Vector3.Distance(v1, v2) # Vector math dot = Vector3.Dot(v1, v2) cross = Vector3.Cross(v1, v2) # Interpolation lerped = Vector3.Lerp(a, b, t=0.5) slerped = Vector3.Slerp(a, b, t=0.5) # Spherical interpolation lerped_dt = Vector3.LerpDt(a, b, dt=0.016, speed=5.0) # Time-based slerped_dt = Vector3.SlerpDt(a, b, dt=0.016, speed=5.0) # Angles angle_deg = Vector3.SignedAngle(from_vec, to_vec, axis=Vector3.Y) # Space transformations world_pos = Vector3.PositionFrom(local_pos, transform) local_pos = Vector3.PositionTo(world_pos, transform) # Axis enum for mirroring Vector3.Axis.XPositive Vector3.Axis.YPositive Vector3.Axis.ZPositive ``` -------------------------------- ### Define feature extraction for DataSampler Source: https://context7.com/facebookresearch/ai4animationpy/llms.txt Define a function to extract features from motion data, including bone positions, velocities, and root transformations, for use with the DataSampler. This function processes motion data at specified timestamps and returns them as PyTorch tensors. ```python from ai4animation import Dataset, DataSampler, Tensor import torch # Define feature extraction function def extract_features(args): motion, timestamps = args root = motion.GetModule(RootModule) # Extract features at timestamps positions = motion.GetBonePositions(timestamps) velocities = motion.GetBoneVelocities(timestamps) root_transforms = root.GetTransforms(timestamps, mirrored=False) # Return as tensors return { "positions": torch.tensor(positions, dtype=torch.float32), "velocities": torch.tensor(velocities, dtype=torch.float32), "root": torch.tensor(root_transforms, dtype=torch.float32), } # Create dataset dataset = Dataset("/path/to/motions", modules=[...]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.