3dmol / 3Dmol.js

WebGL accelerated JavaScript molecular graphics library
https://3dmol.org/
Other
794 stars 194 forks source link

[BUG] py3Dmol hover label in viewergrid mode #721

Closed cbouy closed 1 year ago

cbouy commented 1 year ago

Describe the bug

When creating a viewergrid with py3Dmol and adding a hover callback that displays a label, the label is incorrectly placed in multiple ways: it appears in the wrong position relative to the cursor (i.e. it gets triggered by the wrong atom), and can appear on the wrong viewer.

To Reproduce

import py3Dmol

benz='''
     RDKit          3D

  6  6  0  0  0  0  0  0  0  0999 V2000
   -0.9517    0.7811   -0.6622 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.2847    1.3329   -0.3121 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.2365    0.5518    0.3512 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9517   -0.7811    0.6644 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2847   -1.3329    0.3144 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.2365   -0.5518   -0.3489 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  2  0
  2  3  1  0
  3  4  2  0
  4  5  1  0
  5  6  2  0
  6  1  1  0
M  END
$$$$'''

view = py3Dmol.view(viewergrid=(1, 2), linked=False)
view.addModel(benz,'sdf',viewer=(0,0))
view.setStyle({'stick':{}},viewer=(0,0))
view.addModel(benz,'sdf',viewer=(0,1))
view.setStyle({'stick':{"colorscheme": "cyanCarbon"}},viewer=(0,1))
view.setHoverable(
    {},
    True,
    '''function(atom,viewer,event,container) {
        if(!atom.label) {
        atom.label = viewer.addLabel(atom.atom,{position: atom, backgroundColor: 'mintcream', fontColor:'black'});
        }}''',
    '''function(atom,viewer) {
        if(atom.label) {
        viewer.removeLabel(atom.label);
        delete atom.label;
        }
    }''',
    viewer=(0, 1)
)

Expected behavior

When specifying a viewer in setHoverable, the callback label should only appear on that specific viewer and affect the atom being hovered. When no viewer is specified, I'd say that's most likely an error

Screenshots

Here I'm hovering one of the atoms of the grey benzene but the label appears on the cyan. image

Desktop (please complete the following information):

dkoes commented 1 year ago

Please verify the above fix works for you and close the issue.

cbouy commented 1 year ago

Works for me after building from source and clearing my cache, thanks for the quick fix! Any ETA for a patch release?