import streamlit as st
import os
import base64
from pathlib import Path
import shutil
def load_aframe_and_extras():
return """
"""
def create_aframe_entity(file_path, file_type, position):
if file_type == 'obj':
return f''
elif file_type == 'glb':
return f''
elif file_type in ['webp', 'png']:
return f''
elif file_type == 'mp4':
return f''
return ''
def encode_file(file_path):
with open(file_path, "rb") as file:
return base64.b64encode(file.read()).decode()
def main():
st.set_page_config(layout="wide")
with st.sidebar:
st.title("3D File Viewer đ")
st.markdown("### đ¨ Create Assets")
st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True)
st.markdown("### đ Directory")
directory = st.text_input("Enter path:", ".", key="directory_input")
st.markdown("### âŦī¸ Upload")
uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader")
st.markdown("### âšī¸ Instructions")
st.write("- Click and drag to move objects")
st.write("- Use mouse wheel to zoom")
st.write("- Right-click and drag to rotate")
if not os.path.isdir(directory):
st.sidebar.error("Invalid directory path")
return
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
if uploaded_files:
for uploaded_file in uploaded_files:
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
if file_extension in file_types:
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
shutil.copyfileobj(uploaded_file, f)
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
else:
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
aframe_scene = """
"""
assets = ""
entities = ""
for i, file in enumerate(files):
file_path = os.path.join(directory, file)
file_type = file.split('.')[-1]
encoded_file = encode_file(file_path)
if file_type in ['obj', 'glb']:
assets += f''
elif file_type in ['webp', 'png', 'mp4']:
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}">{file_type}>'
position = f"{i} 0 {i}"
entities += create_aframe_entity(file_path, file_type, position)
assets += ""
aframe_scene += assets + entities + ""
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
if __name__ == "__main__":
main()