Skip to content

Commit

Permalink
converter: Handle case where glTF "extras" is not an object/dict
Browse files Browse the repository at this point in the history
Fixes #112
  • Loading branch information
Moguri committed Jan 8, 2023
1 parent 6435956 commit 948d0e9
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions gltf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class GltfSettings:
flatten_nodes: bool = False


def get_extras(gltf_data):
extras = gltf_data.get('extras', {})
if not isinstance(extras, dict):
# weird, but legal; fail silently for now
return {}
return extras


class Converter():
_COMPONENT_TYPE_MAP = {
5120: GeomEnums.NT_int8,
Expand Down Expand Up @@ -171,6 +179,7 @@ def add_node(root, gltf_scene, nodeid, jvtmap, cvsmap):
print("Could not find node with index: {}".format(nodeid))
return

scene_extras = get_extras(gltf_scene)
node_name = gltf_node.get('name', 'node'+str(nodeid))
if nodeid in self._joint_nodes:
# Handle non-joint children of joints, but don't add joints themselves
Expand Down Expand Up @@ -213,8 +222,8 @@ def add_node(root, gltf_scene, nodeid, jvtmap, cvsmap):
if nodeid in self.skeletons:
self.build_character(panda_node, nodeid, jvtmap, cvsmap, gltf_data)

if 'extras' in gltf_scene and 'hidden_nodes' in gltf_scene['extras']:
if nodeid in gltf_scene['extras']['hidden_nodes']:
if 'hidden_nodes' in scene_extras:
if nodeid in scene_extras['hidden_nodes']:
panda_node = panda_node.make_copy()

if 'mesh' in gltf_node:
Expand Down Expand Up @@ -328,10 +337,9 @@ def add_node(root, gltf_scene, nodeid, jvtmap, cvsmap):
phynp = np.attach_new_node(phynode)
for geomnode in np.find_all_matches('+GeomNode'):
geomnode.reparent_to(phynp)
if 'extras' in gltf_node:
for key, value in gltf_node['extras'].items():
np.set_tag(key, str(value))

for key, value in get_extras(gltf_node).items():
np.set_tag(key, str(value))

for child_nodeid in gltf_node.get('children', []):
add_node(np, gltf_scene, child_nodeid, jvtmap, cvsmap)
Expand All @@ -344,13 +352,14 @@ def visible_recursive(node, visible):
node.hide()
for child in node.get_children():
visible_recursive(child, visible)
if 'extras' in gltf_scene and 'hidden_nodes' in gltf_scene['extras']:
if nodeid in gltf_scene['extras']['hidden_nodes']:
#print('Hiding', np)
visible_recursive(np, False)
else:
#print('Showing', np)
visible_recursive(np, True)

hidden_nodes = scene_extras.get('hidden_nodes', [])
if nodeid in hidden_nodes:
#print('Hiding', np)
visible_recursive(np, False)
else:
#print('Showing', np)
visible_recursive(np, True)

# Check if we need to deal with negative scale values
scale = panda_node.get_transform().get_scale()
Expand All @@ -373,8 +382,8 @@ def visible_recursive(node, visible):
scene_root = NodePath(ModelRoot(scene_name))

node_list = gltf_scene['nodes']
if 'extras' in gltf_scene and 'hidden_nodes' in gltf_scene['extras']:
node_list += gltf_scene['extras']['hidden_nodes']
hidden_nodes = get_extras(gltf_scene).get('hidden_nodes', [])
node_list += hidden_nodes

for nodeid in node_list:
add_node(scene_root, gltf_scene, nodeid, {}, {})
Expand All @@ -390,11 +399,11 @@ def visible_recursive(node, visible):
self.active_scene = self.scenes[sceneid]
if 'scenes' in gltf_data:
gltf_scene = gltf_data['scenes'][sceneid]
if 'extras' in gltf_scene:
if 'background_color' in gltf_scene['extras']:
self.background_color = gltf_scene['extras']['background_color']
if 'active_camera' in gltf_scene['extras']:
self.active_camera = gltf_scene['extras']['active_camera']
scene_extras = get_extras(gltf_scene)
if 'background_color' in scene_extras:
self.background_color = scene_extras['background_color']
if 'active_camera' in scene_extras:
self.active_camera = scene_extras['active_camera']

def load_matrix(self, mat):
lmat = LMatrix4()
Expand Down Expand Up @@ -792,7 +801,7 @@ def load_primitive(self, geom_node, gltf_primitive, gltf_mesh, gltf_data):
# Check for morph target columns.
targets = gltf_primitive.get('targets')
if targets:
target_names = gltf_mesh.get('extras', {}).get('targetNames', [])
target_names = get_extras(gltf_mesh).get('targetNames', [])

for i, target in enumerate(targets):
if i < len(target_names):
Expand Down Expand Up @@ -1367,7 +1376,7 @@ def create_slider(nodeid):
num_targets = max(len(targets), num_targets)

if num_targets > 0:
target_names = gltf_mesh.get('extras', {}).get('targetNames', [])
target_names = get_extras(gltf_mesh).get('targetNames', [])
num_targets = max(len(target_names), num_targets)

if not weights:
Expand Down Expand Up @@ -1594,7 +1603,7 @@ def create_channels(parent, nodeid, target_names, default_weights):
gltf_mesh = gltf_data['meshes'][meshid]
weights = gltf_mesh.get('weights')
if weights:
target_names = gltf_mesh.get('extras', {}).get('targetNames', [])
target_names = get_extras(gltf_mesh).get('targetNames', [])
if len(target_names) < len(weights):
target_names += [str(i) for i in range(len(target_names), len(weights))]

Expand Down

0 comments on commit 948d0e9

Please sign in to comment.