Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions gltflib/gltf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import codecs
from os import path
from urllib.parse import urlparse, unquote
from typing import Tuple, List, Iterator, Iterable, Optional, Set, BinaryIO
from typing import Tuple, List, Iterator, Iterable, Optional, Set, BinaryIO, Type
from .gltf_resource import (
GLTFResource, FileResource, ExternalResource, GLBResource, Base64Resource, GLB_JSON_CHUNK_TYPE,
GLB_BINARY_CHUNK_TYPE)
Expand All @@ -20,8 +20,8 @@ def __init__(self, model: GLTFModel = None, resources: List[GLTFResource] = None
self.resources = resources

@classmethod
def load(cls: 'GLTF', filename: str, load_file_resources=False, resources: List[GLTFResource] = None,
encoding: str = None) -> 'GLTF':
def load(cls: Type['GLTF'], filename: str, load_file_resources: bool = False,
resources: Optional[List[GLTFResource]] = None, encoding: Optional[str] = None) -> 'GLTF':
"""
Loads a GLTF or GLB model from a filename. The model format will be inferred from the filename extension.
:param filename: Path to the GLTF or GLB file
Expand All @@ -47,9 +47,8 @@ def load(cls: 'GLTF', filename: str, load_file_resources=False, resources: List[
f'the filename does not follow the convention but the format is known.')

@classmethod
def load_gltf(cls: 'GLTF', filename: str, load_file_resources=False, resources: List[GLTFResource] = None,
encoding: str = None) \
-> 'GLTF':
def load_gltf(cls: Type['GLTF'], filename: str, load_file_resources: bool = False,
resources: Optional[List[GLTFResource]] = None, encoding: Optional[str] = None) -> 'GLTF':
"""
Loads a model in GLTF format from a filename
:param filename: Path to the GLTF file
Expand All @@ -68,11 +67,10 @@ def load_gltf(cls: 'GLTF', filename: str, load_file_resources=False, resources:
with open(filename, 'rb') as f:
return cls.read_gltf(f, load_file_resources=load_file_resources, resources=resources,
encoding=encoding, basepath=basepath)
return gltf

@classmethod
def load_glb(cls: 'GLTF', filename: str, load_file_resources=False, resources: List[GLTFResource] = None,
encoding: str = None) -> 'GLTF':
def load_glb(cls: Type['GLTF'], filename: str, load_file_resources: bool = False,
resources: Optional[List[GLTFResource]] = None, encoding: Optional[str] = None) -> 'GLTF':
"""
Loads a model in GLB format from a filename
:param filename: Path to the GLB file
Expand All @@ -91,12 +89,11 @@ def load_glb(cls: 'GLTF', filename: str, load_file_resources=False, resources: L
with open(filename, 'rb') as f:
return cls.read_glb(f, load_file_resources=load_file_resources, resources=resources,
encoding=encoding, basepath=basepath)
return gltf
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code seems unreachable, due to the return on previous line. Same with the other return gltf below


@classmethod
def read_gltf(cls: 'GLTF', stream: BinaryIO, load_file_resources = False, resources: List[GLTFResource] = None,
encoding: str = None, basepath: str = None) \
-> 'GLTF':
def read_gltf(cls: Type['GLTF'], stream: BinaryIO, load_file_resources: bool = False,
resources: Optional[List[GLTFResource]] = None, encoding: Optional[str] = None,
basepath: Optional[str] = None) -> 'GLTF':
"""
Loads a model in GLTF format from a stream
:param stream: The stream withthe GLB data
Expand All @@ -120,8 +117,9 @@ def read_gltf(cls: 'GLTF', stream: BinaryIO, load_file_resources = False, resour
return gltf

@classmethod
def read_glb(cls: 'GLTF', stream: BinaryIO, load_file_resources = False, resources: List[GLTFResource] = None,
encoding: str = None, basepath: str = None) -> 'GLTF':
def read_glb(cls: Type['GLTF'], stream: BinaryIO, load_file_resources: bool = False,
resources: Optional[List[GLTFResource]] = None, encoding: Optional[str] = None,
basepath: Optional[str] = None) -> 'GLTF':
"""
Loads a model in GLB format from a filename
:param stream: The stream withthe GLTF data
Expand Down Expand Up @@ -217,7 +215,7 @@ def write_gltf(self, stream: BinaryIO, save_file_resources=True, basepath=None):
return gltf

def write_glb(self, stream: BinaryIO, embed_buffer_resources=True, embed_image_resources=True,
save_file_resources=True, basepath: str=None) -> None:
save_file_resources=True, basepath: str=None) -> 'GLTF':
"""
Exports the model to a GLB stream
:param stream: Output stream
Expand Down Expand Up @@ -472,7 +470,7 @@ def convert_to_external_resource(self, resource: GLTFResource, uri: str) -> Exte
return resource

@classmethod
def _decode_bytes(cls: 'GLTF', data: bytes, encoding: str = None) -> str:
def _decode_bytes(cls: Type['GLTF'], data: bytes, encoding: Optional[str] = None) -> str:
if encoding is not None:
return data.decode(encoding, errors='replace')
elif data.startswith(codecs.BOM_UTF16_BE):
Expand Down