Karana.KUtils.Prefab#

Prefab base class and associated classes/functions.

Attributes#

Classes#

Config

Base class for Prefab Config.

Context

Base class for Prefab Context.

Params

Base class for Prefab Params.

PrefabDS

DataStruct for a Prefab.

Prefab

The Prefab class holds a prefab for a simulation.

Module Contents#

class Karana.KUtils.Prefab.Config(/, **data: Any)[source]#

Bases: Karana.KUtils.DataStruct.DataStruct, Karana.KUtils.DataStruct.NestedBaseMixin

Base class for Prefab Config.

class Karana.KUtils.Prefab.Context(/, **data: Any)[source]#

Bases: Karana.KUtils.DataStruct.DataStruct, Karana.KUtils.DataStruct.NestedBaseMixin

Base class for Prefab Context.

class Karana.KUtils.Prefab.Params(/, **data: Any)[source]#

Bases: Karana.KUtils.DataStruct.DataStruct, Karana.KUtils.DataStruct.NestedBaseMixin

Base class for Prefab Params.

Karana.KUtils.Prefab.ConfigType#
Karana.KUtils.Prefab.ContextType#
Karana.KUtils.Prefab.ParamsType#
class Karana.KUtils.Prefab.PrefabDS(/, **data: Any)[source]#

Bases: Karana.KUtils.DataStruct.DataStruct, Karana.KUtils.DataStruct.NestedBaseMixin, Karana.KUtils.DataStruct.IdMixin, Generic[ConfigType, ContextType, ParamsType]

DataStruct for a Prefab.

Classes derived from Prefab may derive from this and override toDS/fromDS as desired. However, this should work for most Prefabs as is.

Parameters:
  • name (str) – Name of the instance.

  • config (SerializeAsAny[Config]) – Config for the Prefab.

  • context (SerializeAsAny[Context]) – Context for the Prefab.

  • params (SerializeAsAny[Params]) – Params for the Prefab.

  • parent (str) – Name of parent for this prefab relative to the first prefab that was not added by an addChildPrefab method.

  • children (list[SerializeAsAny[DataStruct]]) – Children Prefabs of this Prefab.

  • class_name (PyClassDS) – The class that this DataStruct represents. Used to create a new instance of the class from this DataStruct.

name: str#
config: pydantic.SerializeAsAny[ConfigType]#
context: pydantic.SerializeAsAny[ContextType]#
params: pydantic.SerializeAsAny[ParamsType]#
parent: str#
children_raw: list[pydantic.SerializeAsAny[Karana.KUtils.DataStruct.DataStruct]] = None#
class_name: Karana.KUtils.DataStruct.PyClassDS#
property children: list[PrefabDS[Any, Any, Any]]#

Get the children Prefabs.

Returns:

The children Prefabs.

Return type:

list[PrefabDS[Any, Any, Any]]

toPrefab(parent_or_top: Prefab[Any, Any, Any] | None, context: ContextType | None = None) Prefab[Any, Any, Any][source]#

Convert this PrefabDS to an instance of the associated Prefab.

This will also create instances of any children Prefabs if applicable.

Parameters:
  • parent_or_top (Prefab[Any, Any, Any] | None) – The parent prefab or top_prefab. First, we will try to find the parent string from the PrefabDS on this value. If it exists, we will use that as the parent. Otherwise, we will use this as the parent directly. If None, then we will assume the Prefab being created is the top-level Prefab.

  • context (ContextType | None = None) – Optional context to override what was deserialized. If None, then use what was deserialized.

Returns:

An instance of the associated Prefab.

Return type:

Prefab[Any, Any, Any]

class Karana.KUtils.Prefab.Prefab(name: str, config: ConfigType, context: ContextType, params: ParamsType, parent_prefab: Prefab[Any, Any, Any] | None = None)[source]#

Bases: Karana.Core.Base, Generic[ConfigType, ContextType, ParamsType]

The Prefab class holds a prefab for a simulation.

Prefabs typically consist of one or more of the following: * Bodies * Models * Other prefabs

config#

The configuration for the prefab.

Type:

ConfigType

context#

The context for the prefab.

Type:

ContextType

params#

The parameters for the Prefab.

Type:

ParamsType

config: ConfigType#
context: ContextType#
params: ParamsType#
property parent: Prefab[Any, Any, Any] | None#

Returns the parent prefab of this prefab.

Returns:

The parent prefab of this prefab if it exists.

Return type:

“Prefab[Any, Any, Any] | None”

runBeforeInitialSetParent()[source]#

Add any logic/commands that should run before the parent Prefab is set to this method.

This method runs during construction just before the parent Prefab is set.

checkParentCompatible(parent: Prefab[Any, Any, Any] | None) bool[source]#

Check whether the parent Prefab this Prefab is becoming a child of is compatible.

Parameters:

parent (Prefab | None) – The parent Prefab this Prefab is becoming a child of. Can be None if there is no parent.

Returns:

True if the parent prefab is compatible, False otherwise.

Return type:

bool

checkChildCompatible(child: Prefab[Any, Any, Any]) bool[source]#

Check whether Prefab becoming a child of this Prefab is compatible.

Parameters:

child (Prefab) – The Prefab becoming a child of this Prefab.

Returns:

True if the child prefab is compatible, False otherwise.

Return type:

bool

addChildPrefabs() None[source]#

Add child prefabs to this prefab.

Override this method and add any child prefabs needed by this prefab.

addMultibodyObjects() None[source]#

Add any multibody objects used by this prefab.

Override this method and add any multibody objects (e.g., PhysicalBodys, Nodes, etc.) used by this prefab.

addKModels() None[source]#

Add KModels needed by this prefab.

Override this method and add any KModels needed by this prefab.

connectKModels() None[source]#

Add any connections needed by KModels.

Override this method and add logic to connect KModels in this prefab to other KModels.

addBody(parent_body: Karana.Dynamics.PhysicalBody, child_body: Karana.Dynamics.PhysicalBody | Karana.Dynamics.SOADyn_types.BodyDS) Karana.Dynamics.PhysicalBody[source]#

Add a body to this Prefab.

If this body already exists, just ensure it has the correct parent. Otherwise, create it with the correct parent.

Parameters:
  • parent_body (PhysicalBody) – The parent body to attach the new body to.

  • child_body (PhysicalBody | BodyDS) – The child_body to add. If BodyDS, then a new body is created; otherwise, we check this has the correct parent and return it.

Returns:

The body that was either checked or created.

Return type:

PhysicalBody

addNode(parent_body: Karana.Dynamics.PhysicalBody, node: Karana.Dynamics.Node | Karana.Dynamics.SOADyn_types.NodeDS) Karana.Dynamics.Node[source]#

Add a node to this Prefab.

If this node already exists, just ensure it has the correct parent. Otherwise, create it with the correct parent.

Parameters:
  • parent_body (PhysicalBody) – The parent node to attach the new node to.

  • child_node (Node | NodeDS) – The child_node to add. If NodeDS, then a new node is created; otherwise, we check this has the correct parent and return it.

Returns:

The node that was either checked or created.

Return type:

Node

toDS() PrefabDS[ConfigType, ContextType, ParamsType][source]#

Create a PrefabDS from this Prefab.

Returns:

The DataStruct that represents this Prefab.

Return type:

PrefabDS

classmethod fromDS(data_struct: PrefabDS[ConfigType, ContextType, ParamsType], parent_or_top: Prefab[Any, Any, Any] | None, *_, **kwargs) Self[source]#

Convert the PrefabDS to an instance of this Prefab.

This will also create instances of the associated children if applicable.

Parameters:
  • data_struct (PrefabDS) – The PrefabDS to create an instance of this Prefab from.

  • parent_or_top (Prefab[Any, Any, Any] | None) – The parent prefab or top_prefab. First, we will try to find the parent string from the PrefabDS on this value. If it exists, we will use that as the parent. Otherwise, we will use this as the parent directly. If None, then we will assume the Prefab being created is the top-level Prefab.

Returns:

An instance of this Prefab.

Return type:

Self

requestFromHierarchy()[source]#

Make a request to the current hierarchy of prefabs.

This works by finding all the prefabs in the hierarchy that match the given path as searched from the path_root. If the path_root is None, then the root of the Prefab hierarchy is used. The provided fn is run on all the returned Prefab’s, and any non-None values returned from it are combined into a list and returned.

Parameters:
  • path (str) – The path to use when finding prefabs in the hierarchy. This supports filesystem-like syntax, so globbing, wildcards, etc. can be used.

  • fn (Callable[[Prefab[Any, Any, Any]], T | None]) – The function to run on each Prefab matching path from the hierarchy.

  • path_root ("Prefab[Any, Any, Any] | None") – The root to search from. If None is provided, then the root of the hierarchy is used.

Returns:

A list of objects returned by user provided function mapped over the Prefabs matching the path query.

Return type:

list[T]

requestUniqueFromHierarchy[T](path: str, fn: Callable[[Prefab[Any, Any, Any]], T | None], path_root: Prefab[Any, Any, Any] | None = None) T[source]#

Make a request to the current hierarchy of prefabs.

This works by finding all the prefabs in the hierarchy that match the given path as searched from the path_root. If the path_root is None, then the root of the Prefab hierarchy is used. The provided fn is run on all the returned Prefabs, if one, unique non-None value is returned from applying fn to these Prefabs, then it is returned. If there is not one, unique non-None value, then an error message is thrown.

Parameters:
  • path (str) – The path to use when finding prefabs in the hierarchy. This supports filesystem-like syntax, so globbing, wildcards, etc. can be used.

  • fn (Callable[[Prefab[Any, Any, Any]], T | None]) – The function to run on each Prefab matching path from the hierarchy.

  • path_root ("Prefab[Any, Any, Any] | None") – The root to search from. If None is provided, then the root of the hierarchy is used.

Returns:

A unique object returned by the user provided function mapped over the Prefabs matching the path query.

Return type:

T

requestClosestFromHierarchy[T](path: str, fn: Callable[[Prefab[Any, Any, Any]], T | None], path_root: Prefab[Any, Any, Any] | None = None) T[source]#

Make a request to the current hierarchy of prefabs.

This works by finding all the prefabs in the hierarchy that match the given path as searched from the path_root. If the path_root is None, then the root of the Prefab hierarchy is used. The provided fn is run on all the returned Prefabs. Then, the one that is closest is returned. If there is a tie for the closest or None are returned, then an error is throw.

Parameters:
  • path (str) – The path to use when finding prefabs in the hierarchy. This supports filesystem-like syntax, so globbing, wildcards, etc. can be used.

  • fn (Callable[[Prefab[Any, Any, Any]], T | None]) – The function to run on each Prefab matching path from the hierarchy.

  • path_root ("Prefab[Any, Any, Any] | None") – The root to search from. If None is provided, then the root of the hierarchy is used.

Returns:

The closest object returned by the user provided function mapped over the Prefabs matching the path query.

Return type:

T