3D-tester/utils/utils.py

151 lines
5.0 KiB
Python
Raw Normal View History

2021-06-05 18:59:19 +02:00
"""
Copyright 2021 Hangar.org
This file is part of 3D-tester.
3D-tester is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
from pathlib import Path
2021-06-08 11:08:02 +02:00
BASE_DIR = os.path.abspath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'../')
)
OBJECT_DIR = os.path.join(BASE_DIR, 'objects')
2021-06-13 14:34:58 +02:00
OBJECT_TYPES = ['gltf', 'glb', 'obj', 'fbx', 'stl', 'dae', 'json', 'ply']
2021-06-05 18:59:19 +02:00
def get_directory_content(dir_path):
content = []
for p in Path(dir_path).iterdir():
file = Path(p)
model_type = (Path(file.name).suffix).lstrip('.').lower()
2021-06-08 11:08:02 +02:00
is_object_file = True if model_type in OBJECT_TYPES else False
2021-06-05 18:59:19 +02:00
item = {
'name': os.path.basename(p),
'size': human_readable_bytes(os.path.getsize(p)),
'is_dir': True if p.is_dir() else False,
'is_object_file': is_object_file
}
content.append(item)
return content
2021-06-08 14:19:24 +02:00
def find_file_with_extension(dir, extension):
for p in Path(dir).iterdir():
if Path(p).suffix.lstrip('.').lower() == extension:
return str(p)
return None
def get_object(file_path, result={}):
parts = file_path.split('/')
file_name = parts.pop()
file_dir = os.path.join(OBJECT_DIR, *parts)
print('get_object(file_dir): ', file_dir)
print('get_object(file_name): ', file_name)
print('get_object(file_path): ', os.path.join(OBJECT_DIR, *parts, file_name))
if not os.path.exists(os.path.join(OBJECT_DIR, *parts, file_name)):
print("cant find file")
return result
2021-06-08 11:08:02 +02:00
if not result:
result['urls'] = []
result['type'] = None
2021-06-08 14:19:24 +02:00
extension = Path(file_name).suffix.lstrip('.').lower()
print('get_object(extension):', extension)
2021-06-08 11:08:02 +02:00
2021-06-08 14:19:24 +02:00
if extension == "gltf":
result['type']="gltf"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
if extension == "obj":
result['type']="obj"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
mtl_file = find_file_with_extension(file_dir, 'mtl')
if mtl_file:
result = get_object(mtl_file.replace(OBJECT_DIR, ""), result)
result['type']="obj-mtl"
return result
if extension == 'stl':
result['type'] = "stl"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
if extension == 'mtl':
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
if extension == 'fbx':
result['type'] = "fbx"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
if extension == 'dae':
result['type'] = "dae"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
2021-06-13 14:11:22 +02:00
if extension == 'glb':
result['type'] = "glb"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
2021-06-13 14:28:28 +02:00
if extension == 'json':
result['type'] = "json"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
2021-06-13 14:34:58 +02:00
if extension == 'ply':
result['type'] = "ply"
object_path = str(file_path).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
2021-06-08 14:19:24 +02:00
return None
2021-06-08 11:08:02 +02:00
"""
2021-06-08 14:19:24 +02:00
if 'obj' in extensions and 'mtl' in extensions:
2021-06-08 11:08:02 +02:00
print("p: ",p)
return 'obj-mtl'
2021-06-08 14:19:24 +02:00
if 'obj' in extensions:
2021-06-08 11:08:02 +02:00
return 'obj'
2021-06-08 14:19:24 +02:00
if 'gltf' in extensions:
2021-06-08 11:08:02 +02:00
return 'gltf'
2021-06-08 14:19:24 +02:00
if 'fbx' in extensions:
2021-06-08 11:08:02 +02:00
return 'fbx'
2021-06-08 14:19:24 +02:00
if 'stl' in extensions:
2021-06-08 11:08:02 +02:00
return 'stl'
"""
2021-06-05 18:59:19 +02:00
def human_readable_bytes(bytes):
""" 1 KibiByte == 1024 Bytes
1 Mebibyte == 1024*1024 Bytes
1 GibiByte == 1024*1024*1024 Bytes
"""
if bytes == 0:
2021-06-08 11:08:02 +02:00
return "0 bytes"
if bytes < 1024:
return f"{bytes} bytes"
2021-06-05 18:59:19 +02:00
if bytes < 1024*1024:
return f"{int(round(bytes/(1024), 2))} KB"
if bytes < 1024*1024*1024:
return f"{int(round(bytes/(1024*1024), 2))} MB"
return f"{int(round(bytes/(1024*1024*1024), 2))} GB"