Skip to content

Commit bb89fb8

Browse files
Set mesh opacity via frontend API (#9011)
### Steps to test: - Open a DS or an annotation in webknossos and load any segments mesh - Open the browser console and insert (with any color or opacity) ``` window.webknossos.apiReady(3).then((api) => { api.data.setSegmentColor(<segmentId>, [0.3,0.5,0.7],<layerName>,0.5) }); ``` - check that the meshes opacity is changed accordingly. ### Issues: - fixes #8974 ------ (Please delete unneeded items, merge only when none are left open) - [x] Added changelog entry (create a `$PR_NUMBER.md` file in `unreleased_changes` or use `./tools/create-changelog-entry.py`) - [ ] Added migration guide entry if applicable (edit the same file as for the changelog) - [ ] Updated [documentation](../blob/master/docs) if applicable - [ ] ~~Adapted [wk-libs python client](https://github.com/scalableminds/webknossos-libs/tree/master/webknossos/webknossos/client) if relevant API parts change~~ -> not needed, as the new function is backwards-compatible - [x] Removed dev-only changes like prints and application.conf edits - [x] Considered [common edge cases](../blob/master/.github/common_edge_cases.md) - [ ] Needs datastore update after deployment
1 parent e53f54c commit bb89fb8

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

frontend/javascripts/viewer/api/api_latest.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import {
9797
refreshMeshesAction,
9898
removeMeshAction,
9999
updateCurrentMeshFileAction,
100+
updateMeshOpacityAction,
100101
updateMeshVisibilityAction,
101102
} from "viewer/model/actions/annotation_actions";
102103
import { setLayerTransformsAction } from "viewer/model/actions/dataset_actions";
@@ -2756,14 +2757,17 @@ class DataApi {
27562757

27572758
/**
27582759
* Set the RGB color of a segment (and its mesh) for a given segmentation layer. If layerName is not passed,
2759-
* the currently visible segmentation layer will be used.
2760+
* the currently visible segmentation layer will be used. If the mesh is loaded, optionally update its opacity.
27602761
*
27612762
* @example
2762-
* api.data.setSegmentColor(3, [0, 1, 1]);
2763+
* api.data.setSegmentColor(3, [0, 1, 1], "segmentation", 0.5);
27632764
*/
2764-
setSegmentColor(segmentId: number, rgbColor: Vector3, layerName?: string) {
2765+
setSegmentColor(segmentId: number, rgbColor: Vector3, layerName?: string, meshOpacity?: number) {
2766+
const state = Store.getState();
2767+
const additionalCoordinates = state.flycam.additionalCoordinates;
2768+
const additionalCoordKey = getAdditionalCoordinatesAsString(additionalCoordinates);
27652769
const effectiveLayerName = getRequestedOrVisibleSegmentationLayerEnforced(
2766-
Store.getState(),
2770+
state,
27672771
layerName,
27682772
).name;
27692773

@@ -2778,6 +2782,23 @@ class DataApi {
27782782
true,
27792783
),
27802784
);
2785+
2786+
if (meshOpacity != null) {
2787+
if (meshOpacity < 0 || meshOpacity > 1) {
2788+
throw new Error(`meshOpacity must be between 0 and 1, but got ${meshOpacity}`);
2789+
}
2790+
if (
2791+
state.localSegmentationData[effectiveLayerName]?.meshes?.[additionalCoordKey]?.[
2792+
segmentId
2793+
] != null
2794+
) {
2795+
Store.dispatch(updateMeshOpacityAction(effectiveLayerName, segmentId, meshOpacity));
2796+
} else {
2797+
throw new Error(
2798+
`Mesh for segment ${segmentId} was not found in State.localSegmentationData.`,
2799+
);
2800+
}
2801+
}
27812802
}
27822803
}
27832804
/**

unreleased_changes/9011.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Added
2+
- Added an optional `meshOpacity` parameter to `api.data.setSegmentColor`, allowing callers to set a segment's mesh opacity.

0 commit comments

Comments
 (0)