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

Vtk key binds option #379

Merged
merged 6 commits into from
Apr 12, 2019
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
11 changes: 11 additions & 0 deletions examples/reference/panes/VTK.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
"For layout and styling related parameters see the [customization user guide](../../user_guide/Customization.ipynb).\n",
"\n",
"* **``camera``** (dict): A dictionary reflecting the current state of the VTK camera\n",
"* **``enable_keybindings``** (bool): A boolean to activate/deactivate binded keys. Binded keys are :\n",
" - s: set representation of all actors to *surface*\n",
" - w: set representation of all actors to *wireframe*\n",
" - v: set representation of all actors to *vertex*\n",
" - r: centers the actors and moves the camera so that all actors are visible\n",
"\n",
"**Warning**: These keys bind may not work as expected in a notebook context if they interact with already binded keys\n",
" \n",
"* **``object``** (str or object): Can be a string pointing to a local or remote file with a `.vtkjs` extension, or a vtkRenderWindow object \n",
"\n",
"___"
Expand Down Expand Up @@ -176,6 +184,8 @@
"cylinderActor = vtk.vtkActor()\n",
"cylinderActor.SetMapper(cylinderMapper)\n",
"cylinderActor.GetProperty().SetColor(tomato)\n",
"# We must set ScalarVisibilty to 0 to use tomato Color\n",
"cylinderMapper.SetScalarVisibility(0)\n",
"cylinderActor.RotateX(30.0)\n",
"cylinderActor.RotateY(-45.0)\n",
"\n",
Expand Down Expand Up @@ -215,6 +225,7 @@
"sphereActor = vtk.vtkActor()\n",
"sphereActor.SetMapper(sphereMapper)\n",
"sphereActor.GetProperty().SetColor(tomato)\n",
"sphereMapper.SetScalarVisibility(0)\n",
"sphereActor.RotateX(30.0)\n",
"sphereActor.RotateY(-45.0)\n",
"sphereActor.SetPosition(0.5, 0.5, 0.5)\n",
Expand Down
2 changes: 2 additions & 0 deletions panel/models/vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class VTKPlot(HTMLBox):

camera = Dict(String, Any)

enable_keybindings = Bool(default=False)

height = Override(default=300)

width = Override(default=300)
Expand Down
24 changes: 21 additions & 3 deletions panel/models/vtk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class VTKPlotView extends HTMLBoxView {
protected _rendererEl: any
protected _renderer: any
protected _camera: any
protected _interactor: any
protected _setting: boolean = false

initialize(): void {
Expand All @@ -31,16 +32,31 @@ export class VTKPlotView extends HTMLBoxView {
container: this._container
});
this._renderer = this._rendererEl.getRenderer()
this._interactor = this._rendererEl.getInteractor()
this._camera = this._renderer.getActiveCamera()
this._plot()
this._camera.onModified(() => this._get_camera_state())
this._key_binding()
}
}

connect_signals(): void {
super.connect_signals()
this.connect(this.model.properties.data.change, () => this._plot())
this.connect(this.model.properties.camera.change, () => this._set_camera_state())
this.connect(this.model.properties.enable_keybindings.change, () => this._key_binding())
}

_key_binding(): void {
if (this.model.enable_keybindings) {
document.querySelector('body')!.addEventListener('keypress',this._interactor.handleKeyPress)
document.querySelector('body')!.addEventListener('keydown',this._interactor.handleKeyDown)
document.querySelector('body')!.addEventListener('keyup',this._interactor.handleKeyUp)
} else {
document.querySelector('body')!.removeEventListener('keypress',this._interactor.handleKeyPress)
document.querySelector('body')!.removeEventListener('keydown',this._interactor.handleKeyDown)
document.querySelector('body')!.removeEventListener('keyup',this._interactor.handleKeyUp)
}
}

render() {
Expand Down Expand Up @@ -110,6 +126,7 @@ export namespace VTKPlot {
data: p.Property<string>
append: p.Property<boolean>
camera: p.Property<any>
enable_keybindings: p.Property<boolean>
}
}

Expand All @@ -127,9 +144,10 @@ export class VTKPlot extends HTMLBox {
this.prototype.default_view = VTKPlotView

this.define<VTKPlot.Props>({
data: [ p.String ],
append: [ p.Boolean, false ],
camera: [ p.Any ]
data: [ p.String ],
append: [ p.Boolean, false ],
camera: [ p.Any ],
enable_keybindings: [ p.Boolean, false ]
})

this.override({
Expand Down
17 changes: 12 additions & 5 deletions panel/pane/vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ class VTK(PaneBase):

camera = param.Dict(doc="""State of the rendered VTK camera.""")

enable_keybindings = param.Boolean(default=False, doc="""
Activate/Deactivate keys binding.

Warning: These keys bind may not work as expected in a notebook
context if they interact with already binded keys
""")

_updates = True

@classmethod
Expand Down Expand Up @@ -358,7 +365,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
model = VTKPlot(data=data, **props)
if root is None:
root = model
self._link_props(model, ['data', 'camera'], doc, root, comm)
self._link_props(model, ['data', 'camera', 'enable_keybindings'], doc, root, comm)
self._models[root.ref['id']] = (model, parent)
return model

Expand Down Expand Up @@ -404,7 +411,7 @@ def _vtksjs_from_render_window(self, render_window):
mapper = renProp.GetMapper()
if mapper is None:
continue
dataObject = mapper.GetInputDataObject(0, 0);
dataObject = mapper.GetInputDataObject(0, 0)
dataset = None

if dataObject.IsA('vtkCompositeDataSet'):
Expand All @@ -422,7 +429,7 @@ def _vtksjs_from_render_window(self, render_window):
dataset = gf.GetOutput()
else:
dataset = mapper.GetInput()

if dataset and not isinstance(dataset, (vtk.vtkPolyData)):
# All data must be PolyData surfaces!
gf = vtk.vtkGeometryFilter()
Expand Down Expand Up @@ -471,7 +478,7 @@ def _vtksjs_from_render_window(self, render_window):

if dataArray:
# component = -1 => let specific instance get scalar from vector before mapping
colorArray = lookupTable.MapScalars(dataArray, colorMode, -1);
colorArray = lookupTable.MapScalars(dataArray, colorMode, -1)
colorArrayName = '__CustomRGBColorArray__'
colorArray.SetName(colorArrayName)
colorMode = 0
Expand All @@ -489,7 +496,7 @@ def _vtksjs_from_render_window(self, render_window):
textureName = None
if renProp.GetTexture() and renProp.GetTexture().GetInput():
textureData = renProp.GetTexture().GetInput()
textureName = 'texture_%d' % _get_object_id(textureData, objIds);
textureName = 'texture_%d' % _get_object_id(textureData, objIds)
textureToSave[textureName] = textureData

representation = renProp.GetProperty().GetRepresentation() if hasattr(renProp, 'GetProperty') else 2
Expand Down