3D Surfaces and Sketchfab
@ Matthew Turk | Wednesday, Dec 5, 2012 | 5 minute read | Update at Friday, Dec 4, 2020

Let’s talk about exporting surfaces to sketchfab with yt!

3D Surfaces and Sketchfab

Surfaces

For a while now, yt has had the ability to extract isosurfaces from volumetric data using a marching cubes algorithm. The surfaces could be exported in OBJ format, values could be samples at the center of each face of the surface, and flux of a given field could be calculated over the surface. This means you could, for instance, extract an isocontour in density and calculate the mass flux over that isocontour. It also means you could export a surface from yt and view it in something like Blender, MeshLab, or even on your Android or iOS device in MeshPad or MeshLab Android. One important caveat with marching cubes is that with adaptive mesh refinement data, you will see cracks across refinement boundaries unless a “crack-fixing” step is applied to match up these boundaries. yt does not perform such an operation, and so there will be seams visible in 3D views of your isosurfaces.

The methods to do so were methods on data objects – extract_isocontours, calculate_isocontour_flux – which returned just numbers or values. However, recently, I’ve created a new object called AMRSurface that makes this process much easier. You can create one of these objects by specifying a source data object and a field over which to identify a surface at a given value. For example:

   from yt.mods import *
   pf = load("/data/workshop2012/IsolatedGalaxy/galaxy0030/galaxy0030")
   sphere = pf.h.sphere("max", (1.0, "mpc"))
   surface = pf.h.surface(sphere, "Density", 1e-27)

This object, surface, can now be queried for values on the surface. For instance:


   print surface["Temperature"].min(), surface["Temperature"].max()

will return the values 11850.7476943 and 13641.0663899. These values are interpolated to the face centers of every triangle that constitutes a portion of the surface. Note that reading a new field requires re-calculating the entire surface, so it’s not the fastest operation. You can get the vertices of the triangle by looking at the property .vertices.

Exporting to a File

If you want to export this to a i PLY file you can call the routine export_ply, which will write to a file and optionally sample a field at every face or vertex, outputting a color value to the file as well. This file can then be viewed in MeshLab, Blender or on the website Sketchfab.com But if you want to view it on Sketchfab, there’s an even easier way!

Exporting to Sketchfab

Sketchfab is a website that uses WebGL, a relatively new technology for displaying 3D graphics in any browser. It’s very fast and typically requires no plugins. Plus, it means that you can share data with anyone and they can view it immersively without having to download the data or any software packages! Sketchfab provides a free tier for up to 10 models, and these models can be embedded in websites.

There are lots of reasons to want to export to Sketchfab. For instance, if you’re looking at a galaxy formation simulation and you publish a paper, you can include a link to the model in that paper (or in the arXiv listing) so that people can explore and see what the data looks like. You can also embed a model in a website with other supplemental data, or you can use Sketchfab to discuss morphological properties of a dataset with collaborators. It’s also just plain cool.

The AMRSurface object includes a method to upload directly to Sketchfab, but it requires that you get an API key first. You can get this API key by creating an account and then going to your “dashboard,” where it will be listed on the right hand side. Once you’ve obtained it, put it into your ~/.yt/config file under the heading [yt] as the variable sketchfab_api_key. If you don’t want to do this, you can also supply it as an argument to the function export_sketchfab.

Now you can run a script like this:

   from yt.mods import *
   pf = load("redshift0058")
   dd = pf.h.sphere("max", (200, "kpc"))
   rho = 5e-27

   bounds = [(dd.center[i] - 100.0/pf['kpc'],
              dd.center[i] + 100.0/pf['kpc']) for i in range(3)]

   surf = pf.h.surface(dd, "Density", rho)

   upload_id = surf.export_sketchfab(
       title = "RD0058 - 5e-27",
       description = "Extraction of Density (colored by Temperature) at 5e-27 " \
                   + "g/cc from a galaxy formation simulation by Ryan Joung."
       color_field = "Temperature",
       color_map = "hot",
       color_log = True,
       bounds = bounds
   )

and yt will extract a surface, convert to a format that Sketchfab.com understands (PLY, in a zip file) and then upload it using your API key. For this demo, I’ve used data kindly provided by Ryan Joung from a simulation of galaxy formation. Here’s what my newly-uploaded model looks like, using the embed code from Sketchfab:

As a note, Sketchfab has a maximum model size of 50MB for the free account. 50MB is pretty hefty, though, so it shouldn’t be a problem for most needs. We’re working on a way to optionally upload links to the Sketchfab models on the yt Hub, but for now, if you want to share a cool model we’d love to see it!

Thanks to Sketchfab for such a cool service, and for helping us out along the way with their API. The remaining content of your post.

yt extension modules

yt has many extension packages to help you in your scientific workflow! Check these out, or create your own.

ytini

ytini is set of tools and tutorials for using yt as a tool inside the 3D visual effects software Houdini or a data pre-processor externally to Houdini.

Trident

Trident is a full-featured tool that projects arbitrary sightlines through astrophysical hydrodynamics simulations for generating mock spectral observations of the IGM and CGM.

pyXSIM

pyXSIM is a Python package for simulating X-ray observations from astrophysical sources.

ytree

Analyze merger tree data from multiple sources. It’s yt for merger trees!

yt_idv

yt_idv is a package for interactive volume rendering with yt! It provides interactive visualization using OpenGL for datasets loaded in yt. It is written to provide both scripting and interactive access.

widgyts

widgyts is a jupyter widgets extension for yt, backed by rust/webassembly to allow for browser-based, interactive exploration of data from yt.

yt_astro_analysis

yt_astro_analysis is the yt extension package for astrophysical analysis.

Make your own!!

Finally, check out our development docs on writing your own yt extensions!

Contributing to the Blog

Are you interested in contributing to the yt blog?

Check out our post on contributing to the blog for a guide!

We welcome contributions from all members of the yt community. Feel free to reach out if you need any help.

the yt data hub

The yt hub at https://girder.hub.yt/ has a ton of resources to check out, whether you have yt installed or not.

The collections host all sorts of data that can be loaded with yt. Some have been used in publications, and others are used as sample frontend data for yt. Maybe there’s data from your simulation software?

The rafts host the yt quickstart notebooks, where you can interact with yt in the browser, without needing to install it locally. Check out some of the other rafts too, like the widgyts release notebooks – a demo of the widgyts yt extension pacakge; or the notebooks from the CCA workshop – a user’s workshop on using yt.

Social Links