3D Models

Open In Colab

Install Urchin

Urchin is a Python package stored on PyPI, the following code needs to be run the first time you use Urchin in a Python environment.

Urchin’s full documentation can be found on our website.

[ ]:
#Installing urchin
!pip install oursin -U
[ ]:
!pip install PyWavefront numpy-stl

Setup Urchin and open the renderer webpage

By default Urchin opens the 3D renderer in a webpage. Make sure pop-ups are enabled, or the page won’t open properly. You can also open the renderer site yourself by replacing [ID here] with the ID that is output by the call to .setup() at https://data.virtualbrainlab.org/Urchin/?ID=[ID here]

Note that Urchin communicates to the renderer webpage through an internet connection, we don’t currently support offline use (we hope to add support in the future).

[1]:
import oursin as urchin
urchin.setup()
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
[2]:
import requests
import numpy as np

Load an STL file

We’ll just load a cube as an example. The cube is the size of the CCF for convenience.

[3]:
# obj_url = 'https://drive.google.com/uc?id=11VIfFz7dO9TOPfkL9sPgihapjs1hNANe'
stl_url = 'https://drive.google.com/uc?id=1TUsqumhZWZRL_VEjjlxs4gLeEuicDS34'

# obj_response = requests.get(obj_url, stream=True)
stl_response = requests.get(stl_url, stream=True)
[4]:
## Load STL/OBJ files
from stl import mesh
import tempfile

def loadSTL(stl_bytes):
    # Convert bytes to string
    with tempfile.NamedTemporaryFile(delete=False) as temp_stl:
        temp_stl.write(stl_bytes)

    # Load STL file using numpy-stl
    stl_mesh = mesh.Mesh.from_file(temp_stl.name)

    #get unique vertices
    vertices = np.unique(np.reshape(stl_mesh.points,(-1,3)), axis=0)

    #triangles: go through each triangle and figure out which indexes it corresponds to
    triangles = []
    for vector in stl_mesh.vectors:
        for vi in np.arange(0,3):
            index = np.where((vertices == vector[vi]).all(axis=1))[0][0]
            triangles.append(index)

    return vertices, triangles
[5]:

# Example usage: stl_vertices, stl_triangles = loadSTL(stl_response.content) vertices = urchin.utils.list_of_list2vector3(stl_vertices) triangles = [int(x) for x in stl_triangles]
[40]:
# Note that an atlas must be loaded to create custom meshes, otherwise no reference coordinate information will be available
urchin.ccf25.load()
(Warning) Atlas was already loaded, the renderer can have issues if you try to load an atlas twice.
[41]:
mesh = urchin.custom.CustomMesh(vertices, triangles)
[30]:
mesh.set_position([3,1,-1], True)
[46]:
mesh.set_scale([1, 1, 1])
[47]:
urchin.custom.clear()
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
packet queue is empty, aborting
(URN) disconnected from server
(URN) connected to server
Login sent with ID: 3f54bbeb, copy this ID into the renderer to connect.
[ ]: