Skip to content

Commit

Permalink
Codegen for space view python blueprint classes (#6100)
Browse files Browse the repository at this point in the history
### What

* Fixes #6081

Extends our codegen to accept a fourth object type, space views (in
addition to datatypes/components/archetypes). Meaning we can do special
codegen for space views now, making it easy to set them up consistently
and define what properties a space view can be constructed from.
Building up on the previous PR that exposed background3d color, this
generates almost identical code now.

Doesn't add yet any new view properties - this is going to happen in a
follow-up PR since it requires getting a few more things serializable
and subsequently also testing.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6100?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6100?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6100)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
Wumpf authored Apr 25, 2024
1 parent 45ec26a commit 17e646e
Show file tree
Hide file tree
Showing 32 changed files with 725 additions and 271 deletions.
4 changes: 1 addition & 3 deletions crates/re_types/definitions/python/attributes.fbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace python.attributes;

/// Defines the type aliases for a component, e.g. the types that make up `ComponentLike`.
///
/// Only applies to structs/unions that are components.
/// Defines the type aliases for a component or view property archetype, e.g. the types that make up `ComponentLike`.
attribute "attr.python.aliases";

/// Defines the array type aliases for a component, e.g. the types that make up `ComponentArrayLike`.
Expand Down
7 changes: 6 additions & 1 deletion crates/re_types/definitions/rerun/attributes.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ attribute "attr.rerun.component_optional";
/// For lists this will apply to the inner element.
attribute "attr.rerun.override_type";

/// Set the scope of the type
/// Set the scope of the type.
///
/// This is used for example to scope blueprint types.
attribute "attr.rerun.scope";

/// Specifies the type identifier for a space view type.
///
/// This is mandatory for space view types.
attribute "attr.rerun.view_identifier";

/// Marks something as deprecated followed by a (mandatory!) migration note.
///
/// If specified on an object (struct/enum/union), it becomes deprecated such
Expand Down
8 changes: 8 additions & 0 deletions crates/re_types/definitions/rerun/blueprint.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ include "./blueprint/archetypes/viewport_blueprint.fbs";

include "./blueprint/archetypes/plot_legend.fbs";
include "./blueprint/archetypes/scalar_axis.fbs";

include "./blueprint/views/bar_chart.fbs";
include "./blueprint/views/spatial2d.fbs";
include "./blueprint/views/spatial3d.fbs";
include "./blueprint/views/tensor.fbs";
include "./blueprint/views/text_document.fbs";
include "./blueprint/views/text_log.fbs";
include "./blueprint/views/time_series.fbs";
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace rerun.blueprint.archetypes;

/// Configuration for the background of the 3D space view.
table Background3D (
"attr.rerun.scope": "blueprint"
"attr.rerun.scope": "blueprint",
"attr.python.aliases": "datatypes.Rgba32Like, blueprint_components.Background3DKindLike"
) {
// --- Required ---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A bar chart view.
table BarChartView (
"attr.rerun.view_identifier": "BarChart"
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A Spatial 2D view.
table Spatial2DView (
"attr.rerun.view_identifier": "2D"
) {
}
11 changes: 11 additions & 0 deletions crates/re_types/definitions/rerun/blueprint/views/spatial3d.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A Spatial 3D view.
table Spatial3DView (
"attr.rerun.view_identifier": "3D"
) {
/// Configuration for the background of the 3D space view.
background: rerun.blueprint.archetypes.Background3D (order: 1000);
}
9 changes: 9 additions & 0 deletions crates/re_types/definitions/rerun/blueprint/views/tensor.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A tensor view.
table TensorView (
"attr.rerun.view_identifier": "Tensor"
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A text document view.
table TextDocumentView (
"attr.rerun.view_identifier": "TextDocument"
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A text log view.
table TextLogView (
"attr.rerun.view_identifier": "TextLog"
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "rerun/attributes.fbs";

namespace rerun.blueprint.views;

/// A time series view.
table TimeSeriesView (
"attr.rerun.view_identifier": "TimeSeries"
) {
}
4 changes: 2 additions & 2 deletions crates/re_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! This crate contains both the IDL definitions for Rerun types (flatbuffers) as well as the code
//! generated from those using `re_types_builder`.
//!
//! All builtin archetypes, components and datatypes can be found in their respective top-level
//! modules.
//! All builtin archetypes, components, datatypes and space view definitions can be found in their
//! respective top-level modules.
//!
//! ## Contributing
//!
Expand Down
8 changes: 8 additions & 0 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ impl crate::CodeGenerator for CppCodeGenerator {

ObjectKind::ALL
.par_iter()
.filter(|&&object_kind| {
// TODO(#5521): Implement view codegen for Rust.
object_kind != ObjectKind::View
})
.flat_map(|object_kind| {
scopes
.par_iter()
Expand Down Expand Up @@ -405,6 +409,10 @@ impl QuotedObject {
ObjectKind::Archetype => {
Ok(Self::from_archetype(obj, hpp_includes, hpp_type_extensions))
}
ObjectKind::View => {
// TODO(#5521): Implement view codegen for Rust.
unimplemented!();
}
},
ObjectClass::Enum => Ok(Self::from_enum(objects, obj, hpp_includes)),
ObjectClass::Union => Ok(Self::from_union(
Expand Down
10 changes: 10 additions & 0 deletions crates/re_types_builder/src/codegen/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl CodeGenerator for DocsCodeGenerator {
ObjectKind::Datatype => datatypes.push(object),
ObjectKind::Component => components.push(object),
ObjectKind::Archetype => archetypes.push(object),
ObjectKind::View => {
// TODO(#6082): Implement view docs generation.
continue;
}
}

let page = object_page(reporter, object, object_map);
Expand Down Expand Up @@ -211,6 +215,9 @@ fn object_page(reporter: &Reporter, object: &Object, object_map: &ObjectMap) ->
write_fields(&mut page, object, object_map);
}
ObjectKind::Archetype => write_archetype_fields(&mut page, object, object_map),
ObjectKind::View => {
// TODO(#6082): Implement view docs generation.
}
}

{
Expand Down Expand Up @@ -284,6 +291,9 @@ fn object_page(reporter: &Reporter, object: &Object, object_map: &ObjectMap) ->
}
}
}
ObjectKind::View => {
// TODO(#6082): Implement view docs generation.
}
}

page
Expand Down
63 changes: 42 additions & 21 deletions crates/re_types_builder/src/codegen/python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Implements the Python codegen pass.
mod views;

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use anyhow::Context as _;
Expand All @@ -19,6 +21,8 @@ use crate::{
ObjectKind, Objects, Reporter, Type, ATTR_PYTHON_ALIASES, ATTR_PYTHON_ARRAY_ALIASES,
};

use self::views::code_for_view;

use super::common::ExampleInfo;

/// The standard python init method.
Expand Down Expand Up @@ -322,7 +326,7 @@ impl PythonCodeGenerator {
]
}
}
ObjectKind::Archetype => vec![obj.name.clone()],
ObjectKind::View | ObjectKind::Archetype => vec![obj.name.clone()],
};

// NOTE: Isolating the file stem only works because we're handling datatypes, components
Expand All @@ -342,14 +346,18 @@ impl PythonCodeGenerator {
code.push_indented(0, &format!("# {}", autogen_warning!()), 1);
if let Some(source_path) = obj.relative_filepath() {
code.push_indented(0, &format!("# Based on {:?}.", format_path(source_path)), 2);
code.push_indented(
0,
&format!(
"# You can extend this class by creating a {:?} class in {:?}.",
ext_class.name, ext_class.file_name
),
2,
);

if obj.kind != ObjectKind::View {
// View type extension isn't implemented yet (shouldn't be hard though to add if needed).
code.push_indented(
0,
&format!(
"# You can extend this class by creating a {:?} class in {:?}.",
ext_class.name, ext_class.file_name
),
2,
);
}
}

let manifest = quote_manifest(names);
Expand Down Expand Up @@ -436,13 +444,17 @@ impl PythonCodeGenerator {

let obj_code = match obj.class {
crate::objects::ObjectClass::Struct => {
code_for_struct(reporter, arrow_registry, &ext_class, objects, obj)
if obj.kind == ObjectKind::View {
code_for_view(reporter, objects, obj)
} else {
code_for_struct(reporter, arrow_registry, &ext_class, objects, obj)
}
}
crate::objects::ObjectClass::Enum => {
code_for_enum(reporter, arrow_registry, &ext_class, objects, obj)
}
crate::objects::ObjectClass::Union => {
code_for_union(arrow_registry, &ext_class, objects, obj)
code_for_union(reporter, arrow_registry, &ext_class, objects, obj)
}
};

Expand All @@ -458,7 +470,7 @@ impl PythonCodeGenerator {
files_to_write.insert(filepath.clone(), code);
}

// rerun/[{scope}]/{datatypes|components|archetypes}/__init__.py
// rerun/[{scope}]/{datatypes|components|archetypes|space_views}/__init__.py
write_init_file(&kind_path, &mods, files_to_write);
write_init_file(&test_kind_path, &test_mods, files_to_write);
for (scope, mods) in scoped_mods {
Expand All @@ -476,6 +488,10 @@ fn write_init_file(
mods: &BTreeMap<String, Vec<String>>,
files_to_write: &mut BTreeMap<Utf8PathBuf, String>,
) {
if mods.is_empty() {
return;
}

let path = kind_path.join("__init__.py");
let mut code = String::new();
let manifest = quote_manifest(mods.iter().flat_map(|(_, names)| names.iter()));
Expand Down Expand Up @@ -766,6 +782,9 @@ fn code_for_struct(
1,
);
}
ObjectKind::View => {
unreachable!("View processing shouldn't reach struct generation code.");
}
}

code
Expand Down Expand Up @@ -915,12 +934,16 @@ return pa.UnionArray.from_buffers(
1,
);
}
ObjectKind::View => {
reporter.error(&obj.virtpath, &obj.fqname, "A view cannot be an enum");
}
}

code
}

fn code_for_union(
reporter: &Reporter,
arrow_registry: &ArrowRegistry,
ext_class: &ExtensionClass,
objects: &Objects,
Expand Down Expand Up @@ -1062,7 +1085,7 @@ fn code_for_union(
match kind {
ObjectKind::Archetype => (),
ObjectKind::Component => {
unreachable!("component may not be a union")
reporter.error(&obj.virtpath, &obj.fqname, "An component cannot be an enum");
}
ObjectKind::Datatype => {
code.push_indented(
Expand All @@ -1071,6 +1094,9 @@ fn code_for_union(
1,
);
}
ObjectKind::View => {
reporter.error(&obj.virtpath, &obj.fqname, "An view cannot be an enum");
}
}

code
Expand Down Expand Up @@ -1970,15 +1996,10 @@ fn quote_init_method(
})
.collect::<Vec<_>>()
};
let doc_typedesc = match obj.kind {
ObjectKind::Datatype => "datatype",
ObjectKind::Component => "component",
ObjectKind::Archetype => "archetype",
};

let mut doc_string_lines = vec![format!(
"Create a new instance of the {} {doc_typedesc}.",
obj.name
"Create a new instance of the {} {}.",
obj.name,
obj.kind.singular_name().to_lowercase()
)];
if !parameter_docs.is_empty() {
doc_string_lines.push("\n".to_owned());
Expand Down
Loading

0 comments on commit 17e646e

Please sign in to comment.