Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable background color from python code (POC for space view properties from code) #6068

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store

# Codegen stuff:
crates/re_types/source_hash.txt§
crates/re_types/source_hash.txt
crates/re_types_builder/source_hash.txt

# C++ and CMake stuff:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ table Background3D (
) {
// --- Required ---

/// The type of the background. Defaults to DirectionalGradient
/// The type of the background. Defaults to Background3DKind.GradientDark.
kind: rerun.blueprint.components.Background3DKind ("attr.rerun.component_required", order: 1000);

// --- Optional ---
Expand Down
4 changes: 0 additions & 4 deletions crates/re_types/source_hash.txt

This file was deleted.

2 changes: 1 addition & 1 deletion crates/re_types/src/blueprint/archetypes/background3d.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun/blueprint/archetypes/background3d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions rerun_py/rerun_sdk/rerun/blueprint/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions rerun_py/rerun_sdk/rerun/blueprint/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import rerun_bindings as bindings

from .._baseclasses import AsComponents
from .._spawn import _spawn_viewer
from ..datatypes import EntityPathLike, Utf8ArrayLike, Utf8Like
from ..memory import MemoryRecording
Expand Down Expand Up @@ -41,6 +42,7 @@ def __init__(
origin: EntityPathLike,
contents: SpaceViewContentsLike,
name: Utf8Like | None,
properties: dict[str, AsComponents] = {},
):
"""
Construct a blueprint for a new space view.
Expand All @@ -58,13 +60,16 @@ def __init__(
contents
The contents of the space view specified as a query expression. This is either a single expression,
or a list of multiple expressions. See [rerun.blueprint.archetypes.SpaceViewContents][].
properties
Dictionary of property archetypes to add to space view's internal hierarchy.

"""
self.id = uuid.uuid4()
self.class_identifier = class_identifier
self.name = name
self.origin = origin
self.contents = contents
self.properties = properties

def blueprint_path(self) -> str:
"""
Expand Down Expand Up @@ -104,6 +109,9 @@ def _log_to_stream(self, stream: RecordingStream) -> None:

stream.log(self.blueprint_path(), arch, recording=stream) # type: ignore[attr-defined]

for prop_name, prop in self.properties.items():
stream.log(f"{self.blueprint_path()}/{prop_name}", prop, recording=stream) # type: ignore[attr-defined]

def _repr_html_(self) -> Any:
"""IPython interface to conversion to html."""
return as_html(blueprint=self)
Expand Down
33 changes: 5 additions & 28 deletions rerun_py/rerun_sdk/rerun/blueprint/archetypes/background3d.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions rerun_py/rerun_sdk/rerun/blueprint/archetypes/background3d_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from __future__ import annotations

from typing import Any

from ... import datatypes
from ...blueprint import components as blueprint_components
from ...error_utils import catch_and_log_exceptions


class Background3DExt:
"""Extension for [Background3D][rerun.blueprint.archetypes.Background3D]."""

def __init__(
self: Any,
color_or_kind: datatypes.Rgba32Like | blueprint_components.Background3DKindLike | None = None,
*,
color: datatypes.Rgba32Like | None = None,
kind: blueprint_components.Background3DKindLike | None = None,
):
"""
Create a new instance of the Background3D archetype.

Parameters
----------
color_or_kind:
Either a color for solid background color or kind of the background (see `Background3DKind`).
If set, `color` and `kind` must not be set.

kind:
The type of the background. Defaults to Background3DKind.GradientDark.
color:
Color used for Background3DKind.SolidColor.

Defaults to White.

"""

with catch_and_log_exceptions(context=self.__class__.__name__):
if color_or_kind is not None:
if color is not None or kind is not None:
raise ValueError("Only one of `color_or_kind` and `color`/`kind` can be set.")

if isinstance(color_or_kind, blueprint_components.Background3DKind):
kind = color_or_kind
else:
color = color_or_kind # type: ignore[assignment]

if kind is None:
if color is None:
kind = blueprint_components.Background3DKind.GradientDark
else:
kind = blueprint_components.Background3DKind.SolidColor

self.__attrs_init__(kind=kind, color=color)
return
self.__attrs_clear__()
23 changes: 21 additions & 2 deletions rerun_py/rerun_sdk/rerun/blueprint/space_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from ..datatypes import EntityPathLike, Utf8Like
from .._baseclasses import AsComponents
from ..datatypes import EntityPathLike, Rgba32Like, Utf8Like
from . import archetypes as blueprint_archetypes
from . import components as blueprint_components
from .api import SpaceView, SpaceViewContentsLike


Expand Down Expand Up @@ -69,6 +72,11 @@ def __init__(
origin: EntityPathLike = "/",
contents: SpaceViewContentsLike = "$origin/**",
name: Utf8Like | None = None,
# TODO(andreas): codegen everything that comes below:
background: blueprint_components.Background3DKindLike
| Rgba32Like
| blueprint_archetypes.Background3D
| None = None,
):
"""
Construct a blueprint for a new spatial 3D view.
Expand All @@ -83,9 +91,20 @@ def __init__(
or a list of multiple expressions. See [rerun.blueprint.archetypes.SpaceViewContents][].
name
The name of the view.
background:
Configuration for the background of the 3D space view.

"""
super().__init__(class_identifier="3D", origin=origin, contents=contents, name=name)
properties: dict[str, AsComponents] = {}
# TODO(andreas): codegen creation of the properties dict
if background is not None:
properties["Background3D"] = (
background
if isinstance(background, blueprint_archetypes.Background3D)
else blueprint_archetypes.Background3D(background)
)

super().__init__(class_identifier="3D", origin=origin, contents=contents, name=name, properties=properties)


class TensorView(SpaceView):
Expand Down
25 changes: 25 additions & 0 deletions rerun_py/tests/unit/test_background3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import annotations

import pytest
import rerun as rr
import rerun.blueprint as rrb


def test_background_3d_construction() -> None:
rr.set_strict_mode(True)

assert rrb.Background3D((1.0, 0.0, 0.0)) == rrb.Background3D(
color=(1.0, 0.0, 0.0), kind=rrb.Background3DKind.SolidColor
)
assert rrb.Background3D(rrb.Background3DKind.GradientBright) == rrb.Background3D(
color=None, kind=rrb.Background3DKind.GradientBright
)

with pytest.raises(ValueError):
rrb.Background3D(rrb.Background3DKind.GradientBright, kind=rrb.Background3DKind.GradientDark)
with pytest.raises(ValueError):
rrb.Background3D(rrb.Background3DKind.GradientBright, color=(0.0, 1.0, 0.0))
with pytest.raises(ValueError):
rrb.Background3D((1.0, 0.0, 0.0), kind=rrb.Background3DKind.GradientDark)
with pytest.raises(ValueError):
rrb.Background3D((1.0, 0.0, 0.0), color=(0.0, 1.0, 0.0))
Loading