Factories

Factories#

Factories, used by calling the create_with_new_body_in_world method of a semantic annotation inheriting from HasRootBody, are convenience builders that usually a create the respective semantic annotation as well as a new body with geometry that was generated based on parameters in the world provided to the factory. Factories always spawn bodies relative to the world root. If you for some reason only have a transform relative to some other body, world.transform(parent_T_your_body, world.root) to get the correct root_T_your_body. They are ideal for quickly setting up generic geometries of environments without having to wire all bodies, connections, and semantic annotations manually.

Each factory has a specification counterpart: create_with_new_body_in_world builds the annotation in a world right away, while get_specification (fed by get_default_root_specification) returns the same geometry as a reusable specification you can materialize later. See Building Worlds with Specifications for when to prefer one over the other.

Used Concepts:

Create a drawer with a handle#

from semantic_digital_twin.spatial_types.spatial_types import HomogeneousTransformationMatrix
from semantic_digital_twin.semantic_annotations.semantic_annotations import Drawer, Handle
from semantic_digital_twin.spatial_computations.raytracer import RayTracer
from semantic_digital_twin.world_description.geometry import Scale
from semantic_digital_twin.world import World


# Build a simple drawer with a centered handle
world = World.create_with_root_body()

with world.modify_world():
    drawer = Drawer.create_with_new_body_in_world(
        name="drawer",
        scale=Scale(0.2, 0.4, 0.2),
        world=world,
        world_root_T_self=HomogeneousTransformationMatrix(),
    )
    handle = Handle.create_with_new_body_in_world(
        name="drawer_handle",
        world_root_T_self=HomogeneousTransformationMatrix.from_xyz_rpy(x=-0.1),
        world=world,
        scale=Scale(0.05, 0.02, 0.1)
    )
    drawer.add(handle)

assert drawer.handle is handle
assert drawer in world.semantic_annotations
assert handle in world.semantic_annotations
print(*world.semantic_annotations, sep="\n")
rt = RayTracer(world)
rt.update_scene()
rt.scene.show("jupyter")
Drawer(name=PrefixedName('None/drawer'), id=UUID('a4a7f426-f0c5-4933-a07c-ec2073a13ad9'), root=Body(name=PrefixedName('None/drawer'), id=UUID('6faec365-7a60-418d-89d4-731915e30665'), index=1), mechanical_joint=None, handle=Handle(name=PrefixedName('None/drawer_handle'), id=UUID('bf71562b-f6fd-4169-8ce2-f2f8e08d1bd8'), root=Body(name=PrefixedName('None/drawer_handle'), id=UUID('62748cbd-e37b-49d7-b69f-96eb4c1dcf72'), index=2)), objects=[], supporting_surface=None)
Handle(name=PrefixedName('None/drawer_handle'), id=UUID('bf71562b-f6fd-4169-8ce2-f2f8e08d1bd8'), root=Body(name=PrefixedName('None/drawer_handle'), id=UUID('62748cbd-e37b-49d7-b69f-96eb4c1dcf72'), index=2))