Skip to content

Commit

Permalink
Merge pull request #32 from julianstirling/glb-support
Browse files Browse the repository at this point in the history
Add binary gltf support (.glb)
  • Loading branch information
jmwright authored May 20, 2024
2 parents 9117b7a + 2f15060 commit c7a5f5c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/cq_cli/cqcodecs/cq_codec_glb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os, tempfile
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_glb.glb")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
# Put the GLB output into the temp file
# Check to see if we are dealing with an assembly or a single object
if type(build_result.first_result.shape).__name__ == "Assembly":
build_result.first_result.shape.save(temp_file, binary=True)
else:
raise ValueError(
"GLB export is only available for CadQuery assemblies at this time"
)

# Read the GLB output back in
with open(temp_file, "rb") as file:
glb_data = file.read()

return glb_data
21 changes: 21 additions & 0 deletions tests/test_glb_codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
import tests.test_helpers as helpers


def test_glb_codec():
"""
Basic test of the GLB codec plugin.
"""
test_file = helpers.get_test_file_location("cube_assy.py")

command = [
"python",
"src/cq_cli/main.py",
"--codec",
"glb",
"--infile",
test_file,
]
out, err, exitcode = helpers.cli_call(command)

assert out.decode().startswith("b'glTF")

0 comments on commit c7a5f5c

Please sign in to comment.