Skip to content

Commit

Permalink
Simplify logic now that we have lazy recording-initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed May 9, 2023
1 parent 18153ca commit d3091e5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 76 deletions.
2 changes: 1 addition & 1 deletion crates/re_viewer/src/blueprint_components/panel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize};
use re_data_store::ComponentName;
use re_log_types::{Component, EntityPath};
use re_log_types::Component;

/// A Panel component
/// TODO(jleibs): Some serde back here might be nice
Expand Down
5 changes: 3 additions & 2 deletions examples/python/blueprint/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Fork transform3d

import rerun as rr
import numpy as np
import math

import numpy as np
import rerun as rr
from scipy.spatial.transform import Rotation

rr.init("Space", spawn=True)
Expand Down
22 changes: 4 additions & 18 deletions examples/python/blueprint/blueprint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run one of these 4"
"## Run one of these 3"
]
},
{
Expand All @@ -42,36 +42,22 @@
"metadata": {},
"outputs": [],
"source": [
"# No blueprint control... set_panel doesn't work\n",
"# Default behavior\n",
"rr.init(\"Space\")\n",
"rr.start_web_viewer_server()\n",
"out = rr.memory_recording()\n",
"\n",
"ENABLE_HEURISTICS = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Blueprint control, but no default view\n",
"rr.init(\"Space\", init_logging=True, init_blueprint=True)\n",
"rr.start_web_viewer_server()\n",
"out = rr.memory_recording()\n",
"\n",
"ENABLE_HEURISTICS = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Blueprint control on top of existing auto\n",
"rr.init(\"Space\", init_logging=True, init_blueprint=True, append_blueprint=True)\n",
"rr.init(\"Space\", append_blueprint=True)\n",
"rr.start_web_viewer_server()\n",
"out = rr.memory_recording()\n",
"\n",
Expand All @@ -85,7 +71,7 @@
"outputs": [],
"source": [
"# Blueprint control on top of manual auto (for notebooks this is almost identical to the above)\n",
"rr.init(\"Space\", init_logging=True, init_blueprint=True)\n",
"rr.init(\"Space\")\n",
"rr.start_web_viewer_server()\n",
"\n",
"ENABLE_HEURISTICS = True\n",
Expand Down
6 changes: 1 addition & 5 deletions examples/python/blueprint/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import argparse
import time

import rerun as rr


def main() -> None:
parser = argparse.ArgumentParser(description="Different options for how we might use blueprint")
parser.add_argument(
"--init-logging", action="store_true", help="'Accidentally initialize the logging stream as well'"
)
parser.add_argument("--append", action="store_true", help="Append to the blueprint instead of overwriting it")
parser.add_argument("--enable-heuristics", action="store_true", help="Enable heuristics for the blueprint")

args = parser.parse_args()

rr.init("Space", init_blueprint=True, init_logging=args.init_logging, append_blueprint=args.append)
rr.init("Space", append_blueprint=args.append)
rr.connect()

if args.enable_heuristics:
Expand Down
Binary file added examples/python/blueprint/truck.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 0 additions & 8 deletions rerun_py/rerun_sdk/rerun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ def init(
application_id: str,
recording_id: Optional[str] = None,
spawn: bool = False,
init_logging: bool = True,
init_blueprint: bool = False,
append_blueprint: bool = False,
default_enabled: bool = True,
strict: bool = False,
Expand Down Expand Up @@ -171,10 +169,6 @@ def init(
default_enabled
Should Rerun logging be on by default?
Can overridden with the RERUN env-var, e.g. `RERUN=on` or `RERUN=off`.
init_logging
Should we initialize the logging for this application?
init_blueprint
Should we initialize the blueprint for this application?
append_blueprint
Should the blueprint append to the existing blueprint instead of replacing it?
strict
Expand Down Expand Up @@ -216,8 +210,6 @@ def init(
application_id=application_id,
recording_id=recording_id,
application_path=application_path,
init_logging=init_logging,
init_blueprint=init_blueprint,
append_blueprint=append_blueprint,
default_enabled=default_enabled,
)
Expand Down
95 changes: 53 additions & 42 deletions rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,19 @@ fn no_active_recording(origin: &str) {
);
}

fn no_active_blueprint(origin: &str) {
re_log::warn_once!(
"No active blueprint - call to {origin}() ignored (have you called rr.init()?)",
);
}

// --- Init ---

#[pyfunction]
#[pyo3(signature = (
application_id,
recording_id=None,
application_path=None,
init_logging=true,
init_blueprint=false,
append_blueprint=false,
default_enabled=true,
))]
Expand All @@ -182,8 +186,6 @@ fn init(
application_id: String,
recording_id: Option<String>,
application_path: Option<PathBuf>,
init_logging: bool,
init_blueprint: bool,
append_blueprint: bool,
default_enabled: bool,
) -> PyResult<()> {
Expand Down Expand Up @@ -217,33 +219,25 @@ fn init(
};

let mut data_stream = global_data_stream();
*data_stream = if init_logging {
RecordingStreamBuilder::new(application_id.clone())
.is_official_example(is_official_example)
.recording_id(recording_id)
.recording_source(re_log_types::RecordingSource::PythonSdk(python_version(py)))
.default_enabled(default_enabled)
.buffered()
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?
.into()
} else {
None
};
*data_stream = RecordingStreamBuilder::new(application_id.clone())
.is_official_example(is_official_example)
.recording_id(recording_id)
.recording_source(re_log_types::RecordingSource::PythonSdk(python_version(py)))
.default_enabled(default_enabled)
.buffered()
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?
.into();

let mut bp_ctx = global_blueprint_stream();
*bp_ctx = if init_blueprint {
RecordingStreamBuilder::new(application_id)
.is_official_example(is_official_example)
.recording_id(blueprint_id)
.recording_source(re_log_types::RecordingSource::PythonSdk(python_version(py)))
.blueprint()
.default_enabled(default_enabled)
.buffered()
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?
.into()
} else {
None
};
*bp_ctx = RecordingStreamBuilder::new(application_id)
.is_official_example(is_official_example)
.recording_id(blueprint_id)
.recording_source(re_log_types::RecordingSource::PythonSdk(python_version(py)))
.blueprint()
.default_enabled(default_enabled)
.buffered()
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?
.into();

Ok(())
}
Expand Down Expand Up @@ -322,21 +316,26 @@ fn shutdown(py: Python<'_>) {

#[pyfunction]
fn connect(addr: Option<String>) -> PyResult<()> {
let data_stream = global_data_stream();
let Some(data_stream) = data_stream.as_ref() else {
no_active_recording("connect");
return Ok(());
};

let bp_stream = global_blueprint_stream();
let Some(bp_stream) = bp_stream.as_ref() else {
no_active_blueprint("connect");
return Ok(());
};

let addr = if let Some(addr) = addr {
addr.parse()?
} else {
rerun::default_server_addr()
};

if let Some(data_stream) = global_data_stream().as_ref() {
re_log::info!("Connecting the logging stream");
data_stream.connect(addr);
}

if let Some(bp_stream) = global_blueprint_stream().as_ref() {
re_log::info!("Connecting the blueprint stream");
bp_stream.connect(addr);
}
data_stream.connect(addr);
bp_stream.connect(addr);

Ok(())
}
Expand All @@ -357,9 +356,20 @@ fn save(path: &str) -> PyResult<()> {
/// Create an in-memory rrd file
#[pyfunction]
fn memory_recording() -> PyMemorySinkStorage {
let data_stream = global_data_stream();
let bp_stream = global_blueprint_stream();

if data_stream.is_none() {
no_active_recording("memory");
};

if bp_stream.is_none() {
no_active_blueprint("memory");
};

PyMemorySinkStorage {
recording: global_data_stream().as_ref().map(|s| s.memory()),
blueprint: global_blueprint_stream().as_ref().map(|s| s.memory()),
recording: data_stream.as_ref().map(|s| s.memory()),
blueprint: bp_stream.as_ref().map(|s| s.memory()),
}
}

Expand Down Expand Up @@ -410,6 +420,7 @@ fn serve(open_browser: bool, web_port: Option<u16>, ws_port: Option<u16>) -> PyR

let _guard = enter_tokio_runtime();

// TODO(jleibs): --serve also needs to support blueprints
data_stream.set_sink(
rerun::web_viewer::new_sink(
open_browser,
Expand Down Expand Up @@ -443,11 +454,11 @@ fn disconnect(py: Python<'_>) {
// cleanup a python object.
py.allow_threads(|| {
if let Some(data_stream) = global_data_stream().as_ref() {
re_log::info!("Disconnecting the data stream");
re_log::debug!("Disconnecting the data stream");
data_stream.disconnect();
};
if let Some(bp_stream) = global_blueprint_stream().as_ref() {
re_log::info!("Disconnecting the blueprint stream");
re_log::debug!("Disconnecting the blueprint stream");
bp_stream.disconnect();
}
});
Expand Down

0 comments on commit d3091e5

Please sign in to comment.