SuperDex Physics C++ API
Loading...
Searching...
No Matches
mochi_prefab.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Documentation for all public types and functions in this file lives:
18// mochi_physics/mochi_physics_prefab.mochi_gen
19
20#pragma once
21
23
28
29#include <optional>
30#include <string_view>
31
33
34/***********************************************************************************************
35 Prefab Types:
36*/
37
39 std::optional<DynamicString> comment = std::nullopt;
41 std::optional<Real3> gravity = std::nullopt;
42 std::optional<SolverParams> solver = std::nullopt;
43
44 bool operator==(SceneParams const&) const = default;
45};
46
48 bool enable = true;
51
52 bool operator==(ActorContactEntry const&) const = default;
53};
54
56 bool enable = true;
58
59 bool operator==(LayerContactEntry const&) const = default;
60};
61
63 std::optional<DynamicString> comment = std::nullopt;
64 std::optional<DynamicArray<ActorContactEntry>> actorContactAsymmetric{};
65 std::optional<DynamicArray<ActorContactEntry>> actorContactSymmetric{};
66 std::optional<DynamicArray<LayerContactEntry>> layerContactAsymmetric{};
67 std::optional<DynamicArray<LayerContactEntry>> layerContactSymmetric{};
68
69 bool operator==(ContactFilter const&) const = default;
70};
71
78
92
111
115
128
131
132 // TODO[T265138451]: Add support for baked-in transform
133 // Real3 shapeScale = {1_r, 1_r, 1_r};
134 // Quaternion shapeRotation;
135 // Real3 shapeTranslation = {};
136
138 Real3 renderModelScale = {1_r, 1_r, 1_r};
141
142 bool operator==(ArticulatedSkinPrefab const&) const = default;
143};
144
159
172
182
187
192
198
203
208
212
216
220
226
231
236
241
246
251
252/////////////////
253
273
283
284struct ScenePrefab;
285
287 std::optional<DynamicString> comment = std::nullopt;
290 real scale = 1_r;
293
294 // Loaded prefab. Runtime state, not serialized.
296
297 bool operator==(PrefabReference const&) const = default;
298};
299
301 std::optional<DynamicString> comment = std::nullopt;
306 std::optional<SceneParams> scene = std::nullopt;
307 std::optional<DynamicString> sourceFilePath = std::nullopt;
308 std::optional<ContactFilter> contactFilter = std::nullopt;
309 std::optional<DynamicArray<ContactPairParamsOverrideEntry>> contactPairParamsOverrides =
310 std::nullopt;
311
312 bool operator==(ScenePrefab const&) const = default;
313};
314
315/***********************************************************************************************
316 Prefab Serialization:
317*/
318
320 std::string_view prefabPath,
321 std::string_view rootPath,
322 Context* context,
323 Error& error);
324
326 std::string_view json,
327 std::string_view rootPath,
328 Context* context,
329 Error& error);
330
331MOCHI_API void SaveToJsonFile(ScenePrefab const& prefab, std::string_view path, Error& error);
332
334
335/***********************************************************************************************
336 Prefab Instantiation:
337*/
338
346
354
356AddToScene(ScenePrefab const& prefab, Scene* scene, PrefabParams const& params, Error& error);
357
359
361 std::string_view prefabPath,
362 std::string_view rootPath,
363 Scene* scene,
364 PrefabParams const& params,
365 Error& error);
366
368AddToScene(std::string_view prefabPath, std::string_view rootPath, Scene* scene, Error& error);
369
370/***********************************************************************************************
371 Prefab Utilities:
372*/
373
375 std::string_view inputPath,
376 std::string_view rootForRelativePath,
377 std::string_view prefabFilePath);
378
379MOCHI_API ScenePrefab ShallowLoadFromFile(std::string_view path, Error& error);
380
382
383MOCHI_API void LoadNestedPrefabs(ScenePrefab& prefab, std::string_view rootPath, Error& error);
384
385MOCHI_API void
386LoadShapes(ScenePrefab& prefab, std::string_view rootPath, Context* context, Error& error);
387
388MOCHI_API void
389EnsureFullyLoaded(ScenePrefab& prefab, std::string_view rootPath, Context* context, Error& error);
390
392 Scene const* scene,
393 std::string_view exportName,
394 std::string_view outputDir,
395 Error& error);
396
398 Scene const* scene,
399 std::string_view exportName,
400 std::string_view outputDir,
401 Span<ActorHandle const> excludeActors,
402 Error& error);
403
405 Actor const* actor,
406 std::string_view exportName,
407 std::string_view outputDir,
408 Error& error);
409
410} // namespace superdex::prefab
411
Represents a simulated body (actor) in a scene.
Definition mochi_actor.h:49
Main entry point for SuperDex Physics simulation.
A dynamically resizable array with syntax and behavior similar to std::pmr::vector.
Represents a simulation scene.
Definition mochi_scene.h:45
#define MOCHI_API
void LoadNestedPrefabs(ScenePrefab &prefab, std::string_view rootPath, Error &error)
Load nested prefab files recursively.
AddToSceneResult AddToScene(ScenePrefab const &prefab, Scene *scene, PrefabParams const &params, Error &error)
Instantiate a ScenePrefab and add it to a Scene.
DynamicString SaveToJsonString(ScenePrefab const &prefab, Error &error)
Serialize a ScenePrefab to a JSON string.
ScenePrefab ShallowLoadFromJsonString(std::string_view json, Error &error)
Deserialize a single ScenePrefab from a JSON string, without loading any nested files.
DynamicString GetPrefabFullPath(std::string_view inputPath, std::string_view rootForRelativePath, std::string_view prefabFilePath)
Resolve a path referenced inside a prefab to a full path.
void LoadShapes(ScenePrefab &prefab, std::string_view rootPath, Context *context, Error &error)
Load shape files referenced by prefab and its loaded nested prefabs.
void SaveToJsonFile(ScenePrefab const &prefab, std::string_view path, Error &error)
Serialize a ScenePrefab to a JSON file.
void ExportSceneExcluding(Scene const *scene, std::string_view exportName, std::string_view outputDir, Span< ActorHandle const > excludeActors, Error &error)
Export a Scene to a folder containing a prefab file and all generated mesh files, omitting a caller-p...
ScenePrefab ShallowLoadFromFile(std::string_view path, Error &error)
Deserialize a single ScenePrefab from a file, without loading any nested files.
ScenePrefab LoadFromFile(std::string_view prefabPath, std::string_view rootPath, Context *context, Error &error)
Fully load a ScenePrefab from a file, including all nested prefabs and shapes.
ScenePrefab LoadFromJsonString(std::string_view json, std::string_view rootPath, Context *context, Error &error)
Fully load a ScenePrefab from a JSON string, including all nested prefabs and shapes.
void ExportScene(Scene const *scene, std::string_view exportName, std::string_view outputDir, Error &error)
Export a Scene to a folder containing a prefab file and all generated mesh files.
void ExportActor(Actor const *actor, std::string_view exportName, std::string_view outputDir, Error &error)
Export a single actor to a prefab file under outputDir/exportName/<exportName>.mochi_scene.
void EnsureFullyLoaded(ScenePrefab &prefab, std::string_view rootPath, Context *context, Error &error)
Ensure that all nested prefabs and shape files are loaded, skipping any that are already loaded.
NdArray< real, 3 > Real3
Definition nd_array.h:106
ConstraintType
Type of constraint acting on actors' degrees of freedom.
Definition mochi_enums.h:79
ColliderType
Collision detection geometry for an Actor.
@ None
No collision representation.
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > DynamicString
Alias for std::string using a polymorphic superdex::Allocator pointer.
ActorType
Type of actor.
Definition mochi_enums.h:25
Parameters for creating a range constraint on a 3D-rotation articulated joint.
Parameters for creating a target constraint on 3D rotation DoFs of an articulated actor.
Parameters describing a single joint within an articulated actor.
Parameters describing a single rigid link within an articulated actor.
Parameters for creating a range constraint on a single articulated DoF.
Parameters for creating a target constraint on a single articulated DoF.
Optional skinned mesh attached to an articulated actor for collision and rendering.
Optional contact-response parameter replacements for an unordered actor pair.
Parameters for creating a position constraint on a deformable actor node.
Parameters for creating a constraint coupling a node in a deformable actor to a node in another defor...
Parameters for creating a constraint coupling a deformable node to a rigid actor.
Parameters controlling the resolution of grid-based Signed Distance Fields (SDF).
Parameters for creating a joint rotation range constraint between two rigid actors.
Parameters for creating a joint rotation tracking constraint between two rigid actors.
Runtime handle to a loaded nested prefab (the resolved target of a prefab::PrefabReference).
Parameters to create a rigid body actor.
Parameters for creating a position constraint on a rigid actor's pivot point.
Parameters for creating a rotation constraint on a rigid actor's pivot frame.
Parameters for creating a constraint on a rigid actor's pivot point.
Parameters for creating a prismatic joint between two rigid actors.
Parameters for creating a spherical joint between two rigid actors.
Handle to a shape (mesh, sphere, plane, etc.) within a Context.
Parameters to create a soft deformable actor.
Used by ContactFilter to enable or disable contact for a pair of actors.
bool enable
Enable (true) or disable (false) contact for the specified pair of actors.
DynamicArray< DynamicString > actors
Identifies two actors by name or hierarchy path.
bool includeNestedActors
Whether expandable parent actor names should include nested actors.
bool operator==(ActorContactEntry const &) const =default
Lists of actors grouped by type.
DynamicArray< ArticulatedActorPrefab > articulated
Articulated actors.
std::optional< DynamicString > comment
Optional serialized comment.
bool operator==(ActorLists const &) const =default
DynamicArray< RigidActorPrefab > rigid
Rigid actors.
DynamicArray< SoftActorPrefab > soft
Soft actors.
DynamicArray< SoftSkinnedActorPrefab > softSkinned
Soft-skinned actors.
Struct used to return all created actors and constraints from AddToScene.
DynamicArray< Actor * > Filter(ActorType type) const
Return all the newly created actors of a particular type, in order.
DynamicArray< Constraint * > constraints
All newly created constraints.
DynamicArray< Constraint * > Filter(ConstraintType type) const
Return all the newly created constraints of a particular type, in order.
DynamicArray< Actor * > actors
All newly created actors.
Prefab parameters for a Articulated3dRotationRangeConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the articulated actor.
Prefab parameters for a Articulated3dRotationTargetConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the articulated actor.
Prefab parameters for an articulated actor.
real scale
Uniform scale baked into all link and skin shapes when the prefab is instantiated.
bool operator==(ArticulatedActorPrefab const &) const =default
DynamicArray< ArticulatedLinkPrefab > links
Link parameters (one per link).
DynamicString name
Optional actor name.
std::optional< DynamicString > comment
Optional serialized comment.
Real3 translation
Actor translation [x, y, z] relative to the prefab's local frame (i.e., the prefab-from-root translat...
DynamicArray< ArticulatedJointPrefab > joints
Joint parameters (one per joint).
DynamicArray< ArticulatedCycleJointParams > cycles
Cycle joints creating closed kinematic loops.
Quaternion rotation
Actor rotation quaternion [x, y, z, w] relative to the prefab's local frame (i.e.,...
std::optional< DynamicArray< real > > jointVelocities
Optional initial velocity per DoF.
std::optional< ArticulatedSkinPrefab > skin
Optional skinned mesh parameters.
Joint parameters for an articulated actor prefab.
bool operator==(ArticulatedJointPrefab const &) const =default
Link parameters for an articulated actor prefab.
bool operator==(ArticulatedLinkPrefab const &) const =default
DynamicString shapeFile
Path to a simulation model file defining the geometry of the rigid link actor.
Real3 shapeScale
Scale [x, y, z] to bake into the shape file.
Real3 shapeTranslation
Translation [x, y, z] to bake into the shape file.
Quaternion renderModelRotation
Rotation quaternion [x, y, z, w] to apply to the render model.
Quaternion shapeRotation
Rotation quaternion [x, y, z, w] to bake into the shape file.
Real3 renderModelTranslation
Translation [x, y, z] to apply to the render model.
DynamicString renderModelFile
Optional path to a render model file (e.g.
Real3 renderModelScale
Scale [x, y, z] to apply to the render model.
Prefab parameters for a ArticulatedSingleDofRangeConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the articulated actor.
Prefab parameters for a ArticulatedSingleDofTargetConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the articulated actor.
Skin parameters for an articulated actor prefab.
Real3 renderModelScale
Scale [x, y, z] to apply to the render model.
DynamicString renderModelFile
Optional path to a render model file (e.g.
bool operator==(ArticulatedSkinPrefab const &) const =default
Quaternion renderModelRotation
Rotation quaternion [x, y, z, w] to apply to the render model.
Real3 renderModelTranslation
Translation [x, y, z] to apply to the render model.
DynamicString shapeFile
Path to the model file containing a skinned mesh.
Lists of constraints grouped by type.
DynamicArray< RigidPivotToRigidTargetConstraintPrefab > rigidPivotToRigidTarget
Position constraints attaching a rigid actor's pivot point to a rigid target.
DynamicArray< RigidPivotPositionConstraintPrefab > rigidPivotPosition
Position constraints on a rigid actor's pivot point.
bool operator==(ConstraintLists const &) const =default
DynamicArray< RigidPrismaticJointConstraintPrefab > rigidPrismaticJoint
Prismatic joint constraints between two rigid actors.
DynamicArray< Articulated3dRotationTargetConstraintPrefab > articulated3dRotationTarget
3D rotation target constraints on articulated actors.
DynamicArray< JointRotationTrackingConstraintPrefab > jointRotationTracking
Joint rotation tracking constraints.
DynamicArray< ArticulatedSingleDofRangeConstraintPrefab > articulatedSingleDofRange
Single-DoF range constraints on articulated actors.
DynamicArray< JointRotationRangeConstraintPrefab > jointRotationRange
Joint rotation range constraints.
DynamicArray< RigidSphericalJointConstraintPrefab > rigidSphericalJoint
Spherical joint constraints between two rigid actors.
DynamicArray< Articulated3dRotationRangeConstraintPrefab > articulated3dRotationRange
3D rotation range constraints on articulated actors.
DynamicArray< ArticulatedSingleDofTargetConstraintPrefab > articulatedSingleDofTarget
Single-DoF target constraints on articulated actors.
std::optional< DynamicString > comment
Optional serialized comment.
DynamicArray< RigidPivotRotationConstraintPrefab > rigidPivotRotation
Rotation constraints on a rigid actor's pivot frame.
DynamicArray< DeformableNodeToRigidConstraintPrefab > deformableNodeToRigid
Constraints connecting a deformable node to a rigid actor.
DynamicArray< DeformableNodeToDeformableNodeConstraintPrefab > deformableNodeToDeformableNode
Constraints connecting two deformable actor nodes.
DynamicArray< DeformableNodePositionConstraintPrefab > deformableNodePosition
Position constraints on a deformable actor node.
Contact filtering parameters for a prefab.
std::optional< DynamicArray< LayerContactEntry > > layerContactAsymmetric
Enables or disables contact asymmetrically for each ordered pair of layers.
std::optional< DynamicString > comment
Optional serialized comment.
std::optional< DynamicArray< LayerContactEntry > > layerContactSymmetric
Enables or disables contact symmetrically for each pair of layers.
std::optional< DynamicArray< ActorContactEntry > > actorContactAsymmetric
Enables or disables contact asymmetrically for each ordered pair of actors.
bool operator==(ContactFilter const &) const =default
std::optional< DynamicArray< ActorContactEntry > > actorContactSymmetric
Enables or disables contact symmetrically for each pair of actors.
Overrides selected contact parameters for an unordered actor pair.
ContactPairParamsOverride paramsOverride
Partial contact parameter override for the actor pair.
bool operator==(ContactPairParamsOverrideEntry const &) const =default
DynamicArray< DynamicString > actors
Identifies two actors by name or hierarchy path.
Prefab parameters for a DeformableNodePositionConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the deformable actor.
bool operator==(DeformableNodePositionConstraintPrefab const &) const =default
Prefab parameters for a DeformableNodeToDeformableNodeConstraintParams.
DynamicString actorNameA
Name or hierarchy path identifying deformable actor A.
DynamicString actorNameB
Name or hierarchy path identifying deformable actor B.
Prefab parameters for a DeformableNodeToRigidConstraintParams.
DynamicString rigidActorName
Name or hierarchy path identifying the rigid actor.
DynamicString deformableActorName
Name or hierarchy path identifying the deformable actor.
Prefab parameters for a JointRotationRangeConstraintParams.
DynamicString actorNameB
Name or hierarchy path identifying rigid actor B.
DynamicString actorNameA
Name or hierarchy path identifying rigid actor A.
Prefab parameters for a JointRotationTrackingConstraintParams.
DynamicString actorNameB
Name or hierarchy path identifying rigid actor B.
DynamicString actorNameA
Name or hierarchy path identifying rigid actor A.
Used by ContactFilter to enable or disable contact for a pair of layers.
bool enable
Enable (true) or disable (false) contact for the specified pair of contact layer names.
DynamicArray< DynamicString > layers
Identifies two contact layer names.
bool operator==(LayerContactEntry const &) const =default
Prefab parameters for an articulated pose controller.
DynamicArray< PoseTrackingParams > linkRotTracking
Per-link rotation tracking parameters.
DynamicString articulatedActor
Name or hierarchy path of the articulated actor to control.
DynamicArray< PoseTrackingParams > jointTracking
Per-joint pose tracking parameters.
bool operator==(PoseControllerPrefab const &) const =default
std::optional< DynamicString > comment
Optional serialized comment.
DynamicArray< PoseTrackingParams > linkPosTracking
Per-link position tracking parameters.
Parameters for instantiating a ScenePrefab into a Scene.
Real3 translation
Translation (position) of the new prefab in world-space.
Quaternion rotation
Rotation quaternion [x, y, z, w] of the new prefab in world-space.
real scale
Uniform scale of the new prefab in world-space.
bool applySceneSettings
Whether to apply top-level scene settings.
DynamicString name
Optional name prefix for created actors.
Reference to another prefab (scene or actor) for prefab nesting.
real scale
Uniform scale of the nested prefab, relative to the parent prefab.
std::optional< DynamicString > comment
Optional serialized comment.
Real3 translation
Translation [x, y, z] of the nested prefab in the parent's local space.
bool operator==(PrefabReference const &) const =default
DynamicString path
Path to the nested prefab file.
Quaternion rotation
Rotation quaternion [x, y, z, w] of the nested prefab in the parent's local space.
PrefabHandle prefab
Handle to the loaded nested prefab.
DynamicString name
Name of the nested prefab instance.
Prefab parameters for a rigid actor.
Real3 translation
Actor translation [x, y, z] relative to the prefab's local frame (i.e., the prefab-from-local transla...
Real3 renderModelScale
Scale [x, y, z] to apply to the render model.
Quaternion rotation
Actor rotation quaternion [x, y, z, w] relative to the prefab's local frame (i.e.,...
Real3 shapeTranslation
Translation [x, y, z] to bake into the shape file.
Real3 renderModelTranslation
Translation [x, y, z] to apply to the render model.
Quaternion renderModelRotation
Rotation quaternion [x, y, z, w] to apply to the render model.
Real3 scale
Scale [x, y, z] to bake into the shape file.
DynamicString shapeFile
Path to a model file that will be loaded and referenced via ShapeHandle.
std::optional< DynamicString > comment
Optional serialized comment.
DynamicString renderModelFile
Optional path to a render model file (e.g.
Quaternion shapeRotation
Rotation quaternion [x, y, z, w] to bake into the shape file.
Prefab parameters for a RigidPivotPositionConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the rigid actor.
Prefab parameters for a RigidPivotRotationConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the rigid actor.
Prefab parameters for a RigidPivotToRigidTargetConstraintParams.
DynamicString actorName
Name or hierarchy path identifying the rigid actor.
Prefab parameters for a RigidPrismaticJointConstraintParams.
DynamicString actorNameB
Name or hierarchy path identifying rigid actor B.
DynamicString actorNameA
Name or hierarchy path identifying rigid actor A.
Prefab parameters for a RigidSphericalJointConstraintParams.
DynamicString actorNameA
Name or hierarchy path identifying rigid actor A.
DynamicString actorNameB
Name or hierarchy path identifying rigid actor B.
Parameters that are global to the Scene in which this prefab will be instantiated.
std::optional< Real3 > gravity
Optional gravity vector [m/s^2] in world frame.
std::optional< DynamicString > comment
Optional serialized comment.
DynamicString description
Human-readable description of the scene.
bool operator==(SceneParams const &) const =default
std::optional< SolverParams > solver
Optional solver parameters.
Top-level prefab describing a complete or partial physics scene (possibly just one actor).
std::optional< ContactFilter > contactFilter
Contact filter settings for selective contact filtering.
std::optional< SceneParams > scene
Global scene parameters (top-level prefab only).
bool operator==(ScenePrefab const &) const =default
DynamicArray< PrefabReference > prefabs
List of nested prefab references.
DynamicArray< PoseControllerPrefab > controllers
List of pose controllers.
ActorLists actors
Lists of actors by type.
std::optional< DynamicArray< ContactPairParamsOverrideEntry > > contactPairParamsOverrides
Optional actor-pair contact parameter overrides.
ConstraintLists constraints
Lists of constraints by type.
std::optional< DynamicString > comment
Optional serialized comment.
std::optional< DynamicString > sourceFilePath
Path this prefab was loaded from, exactly as passed to the loading function.
Prefab parameters for a soft actor.
DynamicString renderModelFile
Optional path to a render model file (e.g.
ShapeHandle flow
[Experimental] Optional deep flow shape handle loaded from flowFile.
Quaternion shapeRotation
Rotation quaternion [x, y, z, w] to bake into the shape file.
DynamicString shapeFile
Path to a model file that will be loaded and referenced via ShapeHandle.
Real3 translation
Actor translation [x, y, z] relative to the prefab's local frame (i.e., the prefab-from-local transla...
Real3 shapeTranslation
Translation [x, y, z] to bake into the shape file.
Quaternion renderModelRotation
Rotation quaternion [x, y, z, w] to apply to the render model.
bool useRecentering
[Experimental] Enable automatic recentering of the local coordinate system.
Real3 scale
Scale [x, y, z] to bake into the shape file.
GridSdfParams sdf
[Experimental] Parameters used to construct a grid-based Signed Distance Field (SDF) if the shape doe...
Real3 renderModelTranslation
Translation [x, y, z] to apply to the render model.
DynamicString flowFile
[Experimental] Optional path to a deep flow shape handle for collision detection.
ColliderType colliderType
[Experimental] Collision detection geometry.
std::optional< DynamicString > comment
Optional serialized comment.
Quaternion rotation
Actor rotation quaternion [x, y, z, w] relative to the prefab's local frame (i.e.,...
Real3 renderModelScale
Scale [x, y, z] to apply to the render model.
Prefab parameters for a soft-skinned actor.
DynamicArray< SoftActorPrefab > softParams
Parameters for each nested soft actor.
ArticulatedActorPrefab skeletonParams
Articulated actor parameters for the skeleton.
bool operator==(SoftSkinnedActorPrefab const &) const =default
bool enableCollidingLinks
Whether links can collide with each other.
bool hasInertia
Whether inertia is applied.
std::optional< DynamicString > comment
Optional serialized comment.
DynamicArray< DynamicString > softAttachLinks
Local link names to attach each nested soft actor to.
bool hasStress
Whether soft material stress is applied.
bool hasGravity
Whether gravity is applied.