In [1]:
import numpy as np
import chemview
import time

from chemview import RepresentationViewer
from IPython.display import display

Points

initialization


In [2]:
coordinates = np.array([[0.0, 1.1, 0.1], [1, 0, 0]], 'float32')
colors = [0xFFFFFF, 0xFF999F]
sizes = [1.0, 2.0]

rv = RepresentationViewer(300, 300)
point_id = rv.add_representation('points', {'coordinates': coordinates, 'colors': colors, 'sizes': sizes})
rv

update


In [25]:
for i in range(30):
    rv.update_representation(point_id, {'coordinates': coordinates + [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]})
    time.sleep(0.1)

Lines


In [29]:
coordinates = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]], 'float32')
colors = np.array([0xFF0000, 0x00FF00, 0x0000FF])

rv = RepresentationViewer()
lines_id = rv.add_representation('lines', {'startCoords': coordinates[[0, 1, 2]],
                                           'endCoords': coordinates[[1, 2, 0]],
                                           'startColors': colors[[0, 1, 2]].tolist(),
                                           'endColors': colors[[1, 2, 0]].tolist()})
rv

In [6]:
for i in range(30):
    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]
    rv.update_representation(lines_id, {'startCoords': coordinates[[0, 1, 2]] + offset,
                                        'endCoords': coordinates[[1, 2, 0]] + offset})
    time.sleep(0.1)

Spheres


In [7]:
coordinates = np.array([[0.0, 1.1, 0.1], [1, 0, 0]], 'float32')
colors = [0xFFFFFF, 0xFF999F]
sizes = [1.0, 2.0]

rv = RepresentationViewer()
spheres_id = rv.add_representation('spheres', {'coordinates': coordinates, 
                                               'colors': colors, 
                                               'radii': sizes, 
                                               'resolution': 32})
rv

In [8]:
for i in range(30):
    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]
    rv.update_representation(spheres_id, {'coordinates': coordinates + offset})
    time.sleep(0.1)

Cylinders


In [10]:
coordinates = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]], 'float32')
colors = np.array([0xFF0000, 0x00FF00, 0x0000FF])

rv = RepresentationViewer()
cylinders_id = rv.add_representation('cylinders', {'startCoords': coordinates[[0, 1, 2]],
                                           'endCoords': coordinates[[1, 2, 0]],
                                           'radii': [0.2, 0.1, 0.1],
                                           'colors': colors[[0, 1, 2]].tolist()})
rv

In [11]:
for i in range(30):
    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]
    rv.update_representation(cylinders_id, {'startCoords': coordinates[[0, 1, 2]] + offset,
                                        'endCoords': coordinates[[1, 2, 0]] + offset})
    time.sleep(0.1)

SmoothTube


In [12]:
# We want to draw a helix
r = 0.3
coordinates = []
for t in range(50):
    coordinates.append([r * np.cos(t),
                        r * np.sin(t),
                        t * 0.1])

coordinates = np.array(coordinates, 'float32')

rv = RepresentationViewer()
smooth_id = rv.add_representation('smoothtube', {'coordinates': coordinates, 
                                                 'radius': 0.05, 
                                                 'color' : 0xff99ff,
                                                 'resolution': 8})
rv

In [13]:
for i in range(30):
    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]
    rv.update_representation(smooth_id, {'coordinates': coordinates + offset})
    time.sleep(0.1)

SmoothLine


In [14]:
# We want to draw a helix
r = 0.3
coordinates = []
for t in range(50):
    coordinates.append([r * np.cos(t),
                        r * np.sin(t),
                        t * 0.1])

coordinates = np.array(coordinates, 'float32')

rv = RepresentationViewer()
smooth_id = rv.add_representation('smoothline', {'coordinates': coordinates, 'resolution': 8})
rv

In [15]:
for i in range(30):
    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]
    rv.update_representation(smooth_id, {'coordinates': coordinates + offset})
    time.sleep(0.1)

TrajectoryControl widget


In [4]:
from IPython.display import display
from chemview import TrajectoryControls
controls = TrajectoryControls(10) # 10 frames

def callback(frame):
    print("Current frame %d" % frame)

controls.on_frame_change(callback)
display(controls)


Current frame 1
Current frame 2
Current frame 3
Current frame 4
Current frame 5
Current frame 6
Current frame 7
Current frame 8
Current frame 9
Current frame 1
Current frame 2
Current frame 3
Current frame 4
Current frame 5
Current frame 6
Current frame 7
Current frame 8
Current frame 9
Current frame 8
Current frame 7
Current frame 6
Current frame 5
Current frame 4
Current frame 3
Current frame 2
Current frame 1
Current frame 0
Current frame 1
Current frame 2
Current frame 3
Current frame 4
Current frame 5
Current frame 6
Current frame 7
Current frame 8
Current frame 9