blueprints.utils.view module#

If the children attributes of a NodeThing are called, those children will be returned in a View. The View can be used to get and set attributes of multiple objects as well as to call methods on those child Things simultaneously. The following example shows how to build a simplified hand model using Views.

Getting attributes#
>>> names = ['thumb', 'index', 'middle', 'ring', 'pinky']
>>> fingers = [blue.Body(name=name) for name in names]
>>> palm = blue.Body(name='palm', bodies=fingers)
>>> view = palm.bodies # calling child attributes creates a view
>>> view
View[5|palm.bodies]
>>> view.name
['thumb', 'index', 'middle', 'ring', 'pinky']
>>> view.bodies
View[0|palm.bodies.bodies]
>>> intermediate = blue.Body(name='intermediate')
>>> distal = blue.Body(name='distal')
>>> phalanges = [distal, intermediate, intermediate, intermediate, intermediate]
>>> view.attach.distribute(phalanges)
>>> view.bodies[1:].attach(blue.Body(name='distal'))
Setting attributes#
>>> view.bodies[0].name = 'distal_thumb'
>>> view.bodies[1:].name = ['intermediate_index',
                            'intermediate_middle',
                            'intermediate_ring',
                            'intermediate_pinky']
>>> view.bodies.name
['distal_thumb',
 'intermediate_index',
 'intermediate_middle',
 'intermediate_ring',
 'intermediate_pinky']
Applying functions#
>>> renaming = lambda x: x.__setattr__('name', f'distal_{x.parent.parent.name}')
>>> view.bodies.bodies.apply(renaming)
>>> view.bodies.bodies.name
['distal_index', 'distal_middle', 'distal_ring', 'distal_pinky']
XML structure#
<body name="palm">
    <body name="thumb">
        <body name="distal_thumb" />
    </body>
    <body name="index">
        <body name="intermediate_index">
            <body name="distal_index" />
        </body>
    </body>
    <body name="middle">
        <body name="intermediate_middle">
            <body name="distal_middle" />
        </body>
    </body>
    <body name="ring">
        <body name="intermediate_ring">
            <body name="distal_ring" />
        </body>
    </body>
    <body name="pinky">
        <body name="intermediate_pinky">
            <body name="distal_pinky" />
        </body>
    </body>
</body>
class blueprints.utils.view.View[source]#

Bases: ViewType

The View class is used to handle the access to children of nodes in a kinematic tree and their properties.

__init__(elements, name, parent)[source]#

The View can be used to get and set attributes of multiple objects at the same time.

Parameters:
  • elements (list) – All Things, that are part of the View.

  • name (str) – The name of the attribute that was requested for this View.

  • parent (blue.ThingType) – The parent of the View.

Return type:

None

apply(function, *args, **kwargs)[source]#

Applies a function to all Things contained in View. Arguments and keyword arguments are passed to each individual call in an all to all relation.

>>> # keeping the view object from __setattr__
>>> renaming = lambda x: x.__setattr__('name', f'distal_{x.parent.parent.name}')
>>> view.bodies.bodies.apply(renaming)
>>> view.bodies.bodies.name
['distal_index', 'distal_middle', 'distal_ring', 'distal_pinky']
Parameters:
  • function (Callable) – Functions methods and classes are valid arguments.

  • *args – Arguments passed to each individual call.

  • **kwargs – Kexword arguments passed to each individual call.

Returns:

If any individual call results in a value that is not None, a list of all returned values gets returned, None otherwise.

Return type:

None | list

class blueprints.utils.view.FunctionHandle[source]#

Bases: FunctionHandleType

This class is used to handle access to a function or method called from a View.

>>> palm.bodies
View[5|palm.bodies]
>>> palm.bodies.shift
ViewFunction[5|palm.bodies.shift]

Arguments given to the function or method call are passed to each individual Thing in a many to one relation.

>>> palm.bodies.pos
[array([0., 0., 0.], dtype=float32),
 array([0., 0., 0.], dtype=float32),
 array([0., 0., 0.], dtype=float32),
 array([0., 0., 0.], dtype=float32),
 array([0., 0., 0.], dtype=float32)]
>>> many_to_one = palm.bodies.shift([2, 0, -1])
>>> many_to_one
View[5|palm.bodies.shift(...)]
>>> many_to_one.pos
[array([ 2.,  0., -1.], dtype=float32),
 array([ 2.,  0., -1.], dtype=float32),
 array([ 2.,  0., -1.], dtype=float32),
 array([ 2.,  0., -1.], dtype=float32),
 array([ 2.,  0., -1.], dtype=float32)]

If the distributed() over all Things contained in the parenting View in a one to one relation by using the distribute submethod.

>>> one_to_one = palm.bodies.shift.distribute([[0, 0, 1],
                                               [0, 1, 0],
                                               [0, 1, 1],
                                               [1, 0, 0],
                                               [1, 0, 1]])
>>> one_to_one.pos
[array([0., 0., 1.], dtype=float32),
 array([0., 1., 0.], dtype=float32),
 array([0., 1., 1.], dtype=float32),
 array([1., 0., 0.], dtype=float32),
 array([1., 0., 1.], dtype=float32)]

If the result of all method or function calls are None, None is returned.

>>> palm.bodies.attach()
None

If all results are Things, a View of them is returned. This allows for functions and methods that return Things to be chained.

>>> palm.bodies.copy()
View[5|palm.bodies.copy(...)]
>>> palm.bodies.shift([0, 0, 3]).rotate(alpha=PI).copy()
View[5|palm.bodies.shift(...).rotate(...).copy(...)]

Otherwise a list of all individually returned values is returned.

>>> palm.bodies._mujoco_specs()
[{'name': 'thumb'},
 {'name': 'index'},
 {'name': 'middle'},
 {'name': 'ring'},
 {'name': 'pinky'}]
__init__(functions, name, parent)[source]#

The FunctionHandle takes the functions or methods to be called, the name by which it was called and the initial parent from which the first View was called.

Parameters:
  • functions (list) – All functions or methods from the Views Things.

  • name (str) – The name of the called function or method.

  • parent (blue.ThingType) – The parent from which the first View was called.

Return type:

None

distribute(*args, **kwargs)[source]#

The call is redirected to each function or method in the FunctionHandle. The arguments are unrolled along the first axis and distributed over all calls in a one to one relation.

Parameters:
  • *args – The arguments to be passed to the call.

  • **kwargs – The keywordarguments to be passed to the call.

Returns:

The aggregated result of each call.

Return type:

None | list | View

class blueprints.utils.view.AllView[source]#

Bases: AllViewType, View

The AllView is analogous to the View for all descendants instead of children. Attribute getting and setting is handeled for all valid descendants. A ThingsType attribute is then converted back to a normal view.

XML Structure:#
<body name="A">
        <body name="B" />
        <geom mass="42.0" type="sphere" name="C" />
</body>
Children and Descendants View Comparison:#
>>> world.bodies
View[1|anonymous_model.bodies]
>>> world.bodies.name
['A']
>>> world.all
View[3|anonymous_model.all]
>>> world.all.name
['A', 'B', 'C']
>>> world.all.bodies
View[2|anonymous_model.all.bodies]
>>> world.bodies.name
['A', 'B']

If only some descendants have a certain attribute, the view restricts to this valid subset. For example, only Geoms have mass, so retrieving all.mass excludes all non-Geoms.

Children and Descendants View Comparison:#
>>> world.all.mass
[42.0]
__init__(descendants, parent)[source]#
Parameters:
  • descendants (dict) – The descendants property invoked on parent

  • parent (blue.ThingType) – The parent Thing from which .all was retrieved.

Return type:

None

class blueprints.utils.view.LatticeView[source]#

Bases: LatticeViewType

A LatticeView is created whenever a Lattice is indexed. Attribute getter and setter are handled through this View similar to View. For details have a look at Lattice.

__init__(things, parent, keys=())[source]#
Parameters:
  • things (list) – The indexed Things of the parent Lattice

  • parent (blue.LatticeType) – The Lattice which was indexed to create the LatticeView

  • keys (tuple[int | slice]) – The keys used to index the Lattice

property n_dim: int#

The number of dimensions in the Lattice

Return type:

int