Skip to content

Commit

Permalink
Some minor refactoring of export functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Dec 6, 2023
1 parent 77b9493 commit e61636d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
28 changes: 17 additions & 11 deletions glue_ar/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ def export_meshes(meshes, output_path):
else:
raise ValueError("Unsupported extension!")

def export_gl_by_extension(exporter, filepath):
_, ext = splitext(filepath)
if ext == ".glb":
exporter.export_glb(filepath)
elif ext == ".gltf":
exporter.export_gltf(filepath)
else:
raise ValueError("File extension should be either .glb or .gltf")


# pyvista (well, VTK) doesn't set alphaMode in the exported GLTF
# which means that our opacity won't necessarily be respected.
# Maybe we could fix this upstream? But for now, let's just take
# matters into our own hands.
# We want alphaMode as BLEND
# see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#alpha-coverage
def export_gl_with_alpha(plotter, filepath):
path, ext = splitext(filepath)
gltf_path = path + extsep + "gltf"
plotter.export_gltf(gltf_path)
gltf = GLTF.load(gltf_path)
for material in gltf.model.materials:
material.alphaMode = "BLEND"
if ext == "glb":
gltf.export_glb(filepath)
else:
gltf.export_gltf(filepath)
def export_gl(plotter, filepath, with_alpha=True):
export_gl_by_extension(plotter, filepath)

if with_alpha:
gl = GLTF.load(filepath)
for material in gl.model.materials:
material.alphaMode = "BLEND"
export_gl_by_extension(gl, filepath)


def export_modelviewer(output_path, gltf_path, alt_text):
html = f"""
Expand Down
10 changes: 6 additions & 4 deletions glue_ar/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from glue_ar.common import create_plotter
from glue_ar.scatter import scatter_layer_as_multiblock
from glue_ar.export import export_gl_with_alpha, export_modelviewer
from glue_ar.export import export_gl, export_modelviewer
from glue_ar.volume import create_meshes

__all__ = ["GLScatterExportTool", "GLVolumeExportTool"]
Expand All @@ -32,7 +32,7 @@ def activate(self):
# plotter.export_obj(output_filename)

output_filename = "test.glb"
export_gl_with_alpha(plotter, output_filename)
export_gl(plotter, output_filename, with_alpha=True)

export_modelviewer("test.html", output_filename, "Testing visualization")

Expand All @@ -46,8 +46,10 @@ class GLVolumeExportTool(Tool):

def activate(self):
plotter = pv.Plotter()
meshes = create_meshes(self.viewer.state, use_gaussian_filter=True, smoothing_iteration_count=0)
meshes = create_meshes(self.viewer.state, use_gaussian_filter=True, smoothing_iteration_count=5)
for data in meshes.values():
mesh = data.pop("mesh")
plotter.add_mesh(mesh, color=data["color"], opacity=data["opacity"])
plotter.export_obj("test.obj")
plotter.export_obj("volume.obj")
export_gl(plotter, "volume.gltf", with_alpha=True) # Do we want alpha for volume renderings?
export_modelviewer("volume.html", "volume.gltf", "Testing visualization")

0 comments on commit e61636d

Please sign in to comment.