Skip to content

Commit

Permalink
converter: Add flatten_nodes option
Browse files Browse the repository at this point in the history
This option attempts to optimize the node tree and remove unneeded
nodes. This will generate a different hierarchy than the input glTF
file, so it is off by default.

Fixes #101
  • Loading branch information
Moguri committed Jan 8, 2023
1 parent 8a191e8 commit 6435956
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
7 changes: 7 additions & 0 deletions gltf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def main():
help='control what to do with animation data'
)

parser.add_argument(
'--flatten-nodes',
action='store_true',
help='attempt to flatten resulting node structure'
)

args = parser.parse_args()

settings = gltf.GltfSettings(
Expand All @@ -99,6 +105,7 @@ def main():
textures=args.textures,
legacy_materials=args.legacy_materials,
skip_animations=args.animations == 'skip',
flatten_nodes=args.flatten_nodes,
)

src = p3d.Filename(args.src)
Expand Down
4 changes: 4 additions & 0 deletions gltf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class GltfSettings:
textures: str = 'ref'
legacy_materials: bool = False
skip_animations: bool = False
flatten_nodes: bool = False


class Converter():
Expand Down Expand Up @@ -378,6 +379,9 @@ def visible_recursive(node, visible):
for nodeid in node_list:
add_node(scene_root, gltf_scene, nodeid, {}, {})

if self.settings.flatten_nodes:
scene_root.flatten_medium()

self.scenes[sceneid] = scene_root

# Set the active scene
Expand Down
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

import pytest
import panda3d.core as p3d
from direct.showbase.ShowBase import ShowBase

@pytest.fixture(scope='session')
def showbase():
prcdata = (
'window-type none\n'
'audio-library-name null\n'
'model-cache-dir\n'
)
p3d.load_prc_file_data('', prcdata)
base = ShowBase()

type_registry = p3d.LoaderFileTypeRegistry.get_global_ptr()
ftype = type_registry.get_type_from_extension('gltf')
assert ftype.getName() == 'Python loader'

return base


@pytest.fixture
def modelroot():
Expand Down
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import subprocess


import panda3d.core as p3d


def test_cli_basic(modelroot, tmp_path):
src = (modelroot / 'BoxTextured.gltf').to_os_specific()
dst = tmp_path / 'tmp.bam'
Expand All @@ -26,3 +29,22 @@ def test_cli_copy_textures(modelroot, tmp_path):

assert os.path.exists(dst)
assert os.path.exists(tmp_path / 'CesiumLogoFlat.png')

def test_cli_flatten_nodes(modelroot, tmp_path, showbase):
# Load showbase to ensure using the loader does not mess with the type registry
assert showbase

src = (modelroot / 'Fox.glb').to_os_specific()
dst = tmp_path / 'tmp.bam'
subprocess.check_call([
'gltf2bam',
'--flatten-nodes',
src,
dst,
])

loader = p3d.Loader.get_global_ptr()
scene = p3d.NodePath(loader.load_sync(dst, p3d.LoaderOptions.LF_no_cache))

panda_nodes = scene.find_all_matches('**/*/-PandaNode')
assert not panda_nodes
20 changes: 1 addition & 19 deletions tests/test_load.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import panda3d.core as p3d
from direct.showbase.ShowBase import ShowBase
import pytest #pylint:disable=wrong-import-order

#pylint:disable=redefined-outer-name


@pytest.fixture(scope='session')
def showbase():
prcdata = (
'window-type none\n'
'audio-library-name null\n'
'model-cache-dir\n'
)
p3d.load_prc_file_data('', prcdata)
base = ShowBase()

type_registry = p3d.LoaderFileTypeRegistry.get_global_ptr()
ftype = type_registry.get_type_from_extension('gltf')
assert ftype.getName() == 'Python loader'

return base

#pylint:disable=redefined-outer-name
@pytest.fixture
def modelpath(modelroot):
return p3d.Filename(modelroot, 'test.gltf')
Expand Down

0 comments on commit 6435956

Please sign in to comment.