Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import base64
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def load_aframe_and_extras():
|
| 8 |
+
return """
|
| 9 |
+
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
| 10 |
+
<script src="https://unpkg.com/[email protected]/dist/aframe-event-set-component.min.js"></script>
|
| 11 |
+
<script>
|
| 12 |
+
AFRAME.registerComponent('draggable', {
|
| 13 |
+
init: function () {
|
| 14 |
+
this.el.setAttribute('cursor-listener', '');
|
| 15 |
+
this.dragHandler = e => this.dragMove(e);
|
| 16 |
+
this.el.sceneEl.addEventListener('mousemove', this.dragHandler);
|
| 17 |
+
this.el.setAttribute('event-set__mousedown', 'startDragging');
|
| 18 |
+
this.el.setAttribute('event-set__mouseup', 'stopDragging');
|
| 19 |
+
},
|
| 20 |
+
remove: function () {
|
| 21 |
+
this.el.removeAttribute('cursor-listener');
|
| 22 |
+
this.el.sceneEl.removeEventListener('mousemove', this.dragHandler);
|
| 23 |
+
},
|
| 24 |
+
startDragging: function () {
|
| 25 |
+
this.isDragging = true;
|
| 26 |
+
},
|
| 27 |
+
stopDragging: function () {
|
| 28 |
+
this.isDragging = false;
|
| 29 |
+
},
|
| 30 |
+
dragMove: function (evt) {
|
| 31 |
+
if (this.isDragging) {
|
| 32 |
+
var camera = this.el.sceneEl.camera;
|
| 33 |
+
var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5);
|
| 34 |
+
vector.unproject(camera);
|
| 35 |
+
var dir = vector.sub(camera.position).normalize();
|
| 36 |
+
var distance = -camera.position.y / dir.y;
|
| 37 |
+
var pos = camera.position.clone().add(dir.multiplyScalar(distance));
|
| 38 |
+
this.el.setAttribute('position', pos);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
});
|
| 42 |
+
</script>
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def create_aframe_entity(file_path, file_type, position):
|
| 46 |
+
if file_type == 'obj':
|
| 47 |
+
return f'<a-entity position="{position}" obj-model="obj: #{Path(file_path).stem}" draggable></a-entity>'
|
| 48 |
+
elif file_type == 'glb':
|
| 49 |
+
return f'<a-entity position="{position}" gltf-model="#{Path(file_path).stem}" draggable></a-entity>'
|
| 50 |
+
elif file_type in ['webp', 'png']:
|
| 51 |
+
return f'<a-image position="{position}" src="#{Path(file_path).stem}" width="1" height="1" draggable></a-image>'
|
| 52 |
+
elif file_type == 'mp4':
|
| 53 |
+
return f'<a-video position="{position}" src="#{Path(file_path).stem}" width="1" height="1" draggable></a-video>'
|
| 54 |
+
return ''
|
| 55 |
+
|
| 56 |
+
def encode_file(file_path):
|
| 57 |
+
with open(file_path, "rb") as file:
|
| 58 |
+
return base64.b64encode(file.read()).decode()
|
| 59 |
+
|
| 60 |
+
def main():
|
| 61 |
+
st.set_page_config(layout="wide")
|
| 62 |
+
|
| 63 |
+
with st.sidebar:
|
| 64 |
+
st.title("3D File Viewer ๐")
|
| 65 |
+
|
| 66 |
+
st.markdown("### ๐จ Create Assets")
|
| 67 |
+
st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True)
|
| 68 |
+
|
| 69 |
+
st.markdown("### ๐ Directory")
|
| 70 |
+
directory = st.text_input("Enter path:", ".", key="directory_input")
|
| 71 |
+
|
| 72 |
+
st.markdown("### โฌ๏ธ Upload")
|
| 73 |
+
uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader")
|
| 74 |
+
|
| 75 |
+
st.markdown("### โน๏ธ Instructions")
|
| 76 |
+
st.write("- Click and drag to move objects")
|
| 77 |
+
st.write("- Use mouse wheel to zoom")
|
| 78 |
+
st.write("- Right-click and drag to rotate")
|
| 79 |
+
|
| 80 |
+
if not os.path.isdir(directory):
|
| 81 |
+
st.sidebar.error("Invalid directory path")
|
| 82 |
+
return
|
| 83 |
+
|
| 84 |
+
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
| 85 |
+
|
| 86 |
+
# Handle uploaded files
|
| 87 |
+
if uploaded_files:
|
| 88 |
+
for uploaded_file in uploaded_files:
|
| 89 |
+
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
|
| 90 |
+
if file_extension in file_types:
|
| 91 |
+
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
|
| 92 |
+
shutil.copyfileobj(uploaded_file, f)
|
| 93 |
+
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
|
| 94 |
+
else:
|
| 95 |
+
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
|
| 96 |
+
|
| 97 |
+
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
| 98 |
+
|
| 99 |
+
aframe_scene = """
|
| 100 |
+
<a-scene embedded style="height: 600px; width: 100%;">
|
| 101 |
+
<a-entity camera="userHeight: 1.6" position="0 2 2" rotation="-45 0 0" cursor="rayOrigin: mouse"></a-entity>
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
assets = "<a-assets>"
|
| 105 |
+
entities = ""
|
| 106 |
+
|
| 107 |
+
for i, file in enumerate(files):
|
| 108 |
+
file_path = os.path.join(directory, file)
|
| 109 |
+
file_type = file.split('.')[-1]
|
| 110 |
+
encoded_file = encode_file(file_path)
|
| 111 |
+
|
| 112 |
+
if file_type in ['obj', 'glb']:
|
| 113 |
+
assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
|
| 114 |
+
elif file_type in ['webp', 'png', 'mp4']:
|
| 115 |
+
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
|
| 116 |
+
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
|
| 117 |
+
|
| 118 |
+
position = f"{i} 0 {i}"
|
| 119 |
+
entities += create_aframe_entity(file_path, file_type, position)
|
| 120 |
+
|
| 121 |
+
assets += "</a-assets>"
|
| 122 |
+
aframe_scene += assets + entities + "</a-scene>"
|
| 123 |
+
|
| 124 |
+
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
main()
|