| import streamlit as st | |
| import os | |
| import base64 | |
| from pathlib import Path | |
| def load_aframe(): | |
| return """ | |
| <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script> | |
| """ | |
| def create_aframe_entity(file_path, file_type, position): | |
| if file_type == 'obj': | |
| return f'<a-entity position="{position}" obj-model="obj: #{Path(file_path).stem}"></a-entity>' | |
| elif file_type == 'glb': | |
| return f'<a-entity position="{position}" gltf-model="#{Path(file_path).stem}"></a-entity>' | |
| elif file_type in ['webp', 'png']: | |
| return f'<a-image position="{position}" src="#{Path(file_path).stem}" width="1" height="1"></a-image>' | |
| elif file_type == 'mp4': | |
| return f'<a-video position="{position}" src="#{Path(file_path).stem}" width="1" height="1"></a-video>' | |
| return '' | |
| def encode_file(file_path): | |
| with open(file_path, "rb") as file: | |
| return base64.b64encode(file.read()).decode() | |
| def main(): | |
| st.title("Local File 3D Viewer") | |
| directory = st.text_input("Enter the directory path:", ".") | |
| if not os.path.isdir(directory): | |
| st.error("Invalid directory path") | |
| return | |
| file_types = ['obj', 'glb', 'webp', 'png', 'mp4'] | |
| files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types] | |
| aframe_scene = """ | |
| <a-scene embedded style="height: 500px; width: 100%;"> | |
| <a-entity camera="userHeight: 1.6" position="0 2 2" rotation="-45 0 0"></a-entity> | |
| """ | |
| assets = "<a-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'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>' | |
| 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 += "</a-assets>" | |
| aframe_scene += assets + entities + "</a-scene>" | |
| st.components.v1.html(load_aframe() + aframe_scene, height=500) | |
| if __name__ == "__main__": | |
| main() |