In [1]:
import k3d
import vtk
import numpy as np
from k3d.helpers import download
from pyunpack import Archive
import ipywidgets as widgets

filename = download('https://github.com/To-Fujita/Babylon.js_3D_Graphics/raw/master/scenes/stl/Cute%20Darth%20Vader.stl')

reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
reader.Update()

plot = k3d.plot()
plot += k3d.vtk_poly_data(reader.GetOutput(), color=0x222222)
plot.display()

debug_info = widgets.HTML()
display(debug_info)



In [3]:
plot.camera = [95.27, 101.43, 224.85,
 53.44, 93.47, 46.74,
 0.0, 1.0, 0.0]

In [4]:
plot.mode = 'callback'
obj = plot.objects[0]
obj.vertices.shape, obj.indices.shape


Out[4]:
((235825, 3), (471694, 3))

In [5]:
import json 

g = None
def foo(params):
    global g, debug_info
    
    g = params
    debug_info.value = '<pre>' + json.dumps(params, indent=4) + "</pre>"

obj.click_callback = foo
obj.hover_callback = foo

please click or hover on Darth Vader


In [ ]:
obj.click_callback = None
obj.hover_callback = None

while plot.objects:
    plot -= plot.objects[-1]

In [ ]:
def generate(dim):
    data = np.zeros((dim, dim, dim), dtype=np.uint8)

    x = np.linspace(-0.5, 0.5, dim, dtype=np.float32)
    y = np.linspace(-0.5, 0.5, dim, dtype=np.float32)
    z = np.linspace(-0.5, 0.5, dim, dtype=np.float32)

    x, y, z = np.meshgrid(x, y, z)

    c, s = np.cos(1.5 * x), np.sin(1.5 * x)

    my = y * c - z * s 
    mz = y * s + z * c

    my = np.fmod(my + 0.5, 0.333) * 3 - 0.5
    mz = np.fmod(mz + 0.5, 0.333) * 3 - 0.5

    displace = np.sin(60.0 * x) * np.sin(60.0 * my) * np.sin(60.0 * mz) * 0.1

    data = np.sqrt(my**2 + mz**2) * (2.5 + 0.8 * np.sin(x * 50)) + displace
    
    return data.astype(np.float32)

scalar_field = generate(256)

obj = k3d.marching_cubes(scalar_field, level=0.5, color=0xaa0000)
plot += obj

In [ ]:
obj.click_callback = foo
obj.hover_callback = foo

please click or hover on mesh


In [ ]:
obj.click_callback = None
obj.hover_callback = None

while plot.objects:
    plot -= plot.objects[-1]

In [ ]:
x, y = np.meshgrid(np.linspace(-3, 3, 60),np.linspace(-3, 3, 60))
heights = np.sin(x**2 + y**2)
obj = k3d.surface(heights.astype(np.float32), bounds=[-3,3,-3,3])
plot += obj

In [ ]:
obj.click_callback = foo
obj.hover_callback = foo

please click or hover on surface


In [ ]:
obj.click_callback = None
obj.hover_callback = None

while plot.objects:
    plot -= plot.objects[-1]

In [ ]:
obj = k3d.texture(open('assets/texture.png', 'br').read(), 'png')
plot += obj

In [ ]:
obj.click_callback = foo
obj.hover_callback = foo

please click or hover on texture


In [ ]:
obj.click_callback = None
obj.hover_callback = None

while plot.objects:
    plot -= plot.objects[-1]

In [ ]:
x, y = np.meshgrid(np.linspace(-3, 3, 60),np.linspace(-3, 3, 60))
data = (np.sin(x*2.0)**2 + np.cos(y*np.sin(x))**2)
obj = k3d.texture(attribute=data)
plot += obj

In [ ]:
obj.click_callback = foo
obj.hover_callback = foo

please click or hover on texture


In [ ]: