Skip to main content
Chemistry LibreTexts

5.2.8.5: Using pythreejs to generate and view three-dimensional models

  • Page ID
    346911
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    It is possible to use output from pythreejs in Libretexts pages. The examples below are taken from the pythreejs documentation and are licensed under the BSD 3-Clause License.

    Primitive 3D shapes can be constructed and viewed as follows. Click and drag to explore the shape.

    from pythreejs import BoxGeometry
    BoxGeometry(
        width=5,
        height=10,
        depth=15,
        widthSegments=5,
        heightSegments=10,
        depthSegments=15)
    

    More complex shapes can be constructed and viewed as well:

    from IPython.display import display
    from pythreejs import (ParametricGeometry, Mesh, PerspectiveCamera, Scene,
                           MeshLambertMaterial, DirectionalLight, AmbientLight,
                           Renderer, OrbitControls, PerspectiveCamera)
    
    f = """
    function f(origu, origv, out) {
        // scale u and v to the ranges I want: [0, 2*pi]
        var u = 2*Math.PI*origu;
        var v = 2*Math.PI*origv;
    
        var x = Math.sin(u);
        var y = Math.cos(v);
        var z = Math.cos(u+v);
    
        out.set(x,y,z);
    }
    """
    surf_g = ParametricGeometry(func=f, slices=16, stacks=16)
    
    surf = Mesh(geometry=surf_g, material=MeshLambertMaterial(color='green', side='FrontSide'))
    surf2 = Mesh(geometry=surf_g, material=MeshLambertMaterial(color='yellow', side='BackSide'))
    c = PerspectiveCamera(position=[5, 5, 3], up=[0, 0, 1],
                          children=[DirectionalLight(color='white',
                                                     position=[3, 5, 1],
                                                     intensity=0.6)])
    scene = Scene(children=[surf, surf2, c, AmbientLight(intensity=0.5)])
    renderer = Renderer(camera=c, scene=scene, controls=[OrbitControls(controlling=c)], width=400, height=400)
    display(renderer)

     


    5.2.8.5: Using pythreejs to generate and view three-dimensional models is shared under a CC BY 1.3 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?