| import streamlit as st | |
| import os | |
| import base64 | |
| from pathlib import Path | |
| import shutil | |
| import random | |
| def load_aframe_and_extras(): | |
| return """ | |
| <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script> | |
| <script src="https://unpkg.com/aframe-event-set-component@5.0.0/dist/aframe-event-set-component.min.js"></script> | |
| <script> | |
| AFRAME.registerComponent('draggable', { | |
| init: function () { | |
| this.el.setAttribute('class', 'raycastable'); | |
| this.el.setAttribute('cursor-listener', ''); | |
| this.dragHandler = this.dragMove.bind(this); | |
| this.el.sceneEl.addEventListener('mousemove', this.dragHandler); | |
| this.el.addEventListener('mousedown', this.onDragStart.bind(this)); | |
| this.el.addEventListener('mouseup', this.onDragEnd.bind(this)); | |
| this.camera = document.querySelector('[camera]'); | |
| }, | |
| remove: function () { | |
| this.el.removeAttribute('cursor-listener'); | |
| this.el.sceneEl.removeEventListener('mousemove', this.dragHandler); | |
| }, | |
| onDragStart: function (evt) { | |
| this.isDragging = true; | |
| this.el.emit('dragstart'); | |
| }, | |
| onDragEnd: function (evt) { | |
| this.isDragging = false; | |
| this.el.emit('dragend'); | |
| }, | |
| dragMove: function (evt) { | |
| if (!this.isDragging) return; | |
| var camera = this.camera; | |
| var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5); | |
| vector.unproject(camera); | |
| var dir = vector.sub(camera.position).normalize(); | |
| var distance = -camera.position.y / dir.y; | |
| var pos = camera.position.clone().add(dir.multiplyScalar(distance)); | |
| this.el.setAttribute('position', pos); | |
| } | |
| }); | |
| AFRAME.registerComponent('bouncing', { | |
| schema: { | |
| speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}}, | |
| dist: {type: 'vec3', default: {x: 2, y: 2, z: 2}} | |
| }, | |
| init: function () { | |
| this.originalPos = this.el.getAttribute('position'); | |
| this.dir = {x: 1, y: 1, z: 1}; | |
| }, | |
| tick: function (time, timeDelta) { | |
| var currentPos = this.el.getAttribute('position'); | |
| var speed = this.data.speed; | |
| var dist = this.data.dist; | |
| ['x', 'y', 'z'].forEach(axis => { | |
| currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000); | |
| if (Math.abs(currentPos[axis] - this.originalPos[axis]) > dist[axis]) { | |
| this.dir[axis] *= -1; | |
| } | |
| }); | |
| this.el.setAttribute('position', currentPos); | |
| } | |
| }); | |
| AFRAME.registerComponent('moving-light', { | |
| schema: { | |
| color: {type: 'color', default: '#FFF'}, | |
| speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}}, | |
| bounds: {type: 'vec3', default: {x: 5, y: 5, z: 5}} | |
| }, | |
| init: function () { | |
| this.dir = {x: 1, y: 1, z: 1}; | |
| this.light = document.createElement('a-light'); | |
| this.light.setAttribute('type', 'point'); | |
| this.light.setAttribute('color', this.data.color); | |
| this.light.setAttribute('intensity', '0.75'); | |
| this.el.appendChild(this.light); | |
| }, | |
| tick: function (time, timeDelta) { | |
| var currentPos = this.el.getAttribute('position'); | |
| var speed = this.data.speed; | |
| var bounds = this.data.bounds; | |
| ['x', 'y', 'z'].forEach(axis => { | |
| currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000); | |
| if (Math.abs(currentPos[axis]) > bounds[axis]) { | |
| this.dir[axis] *= -1; | |
| } | |
| }); | |
| this.el.setAttribute('position', currentPos); | |
| } | |
| }); | |
| function moveCamera(direction) { | |
| var camera = document.querySelector('[camera]'); | |
| var pos = camera.getAttribute('position'); | |
| var rot = camera.getAttribute('rotation'); | |
| var speed = 0.5; | |
| switch(direction) { | |
| case 'up': | |
| pos.y += speed; | |
| break; | |
| case 'down': | |
| pos.y -= speed; | |
| break; | |
| case 'left': | |
| pos.x -= speed; | |
| break; | |
| case 'right': | |
| pos.x += speed; | |
| break; | |
| case 'center': | |
| pos = {x: 0, y: 2, z: 2}; | |
| rot = {x: -45, y: 0, z: 0}; | |
| break; | |
| } | |
| camera.setAttribute('position', pos); | |
| if (direction === 'center') { | |
| camera.setAttribute('rotation', rot); | |
| } | |
| } | |
| </script> | |
| """ | |
| def create_aframe_entity(file_path, file_type, position): | |
| rotation = f"{random.uniform(0, 360)} {random.uniform(0, 360)} {random.uniform(0, 360)}" | |
| bounce_speed = f"{random.uniform(0.05, 0.2)} {random.uniform(0.05, 0.2)} {random.uniform(0.05, 0.2)}" | |
| bounce_dist = f"{random.uniform(0.5, 1.5)} {random.uniform(0.5, 1.5)} {random.uniform(0.5, 1.5)}" | |
| if file_type == 'obj': | |
| return f'<a-entity position="{position}" rotation="{rotation}" obj-model="obj: #{Path(file_path).stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>' | |
| elif file_type == 'glb': | |
| return f'<a-entity position="{position}" rotation="{rotation}" gltf-model="#{Path(file_path).stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>' | |
| elif file_type in ['webp', 'png']: | |
| return f'<a-image position="{position}" rotation="{rotation}" src="#{Path(file_path).stem}" width="1" height="1" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-image>' | |
| elif file_type == 'mp4': | |
| return f'<a-video position="{position}" rotation="{rotation}" src="#{Path(file_path).stem}" width="1" height="1" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-video>' | |
| 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.markdown("### ๐ค 3D AI Using Claude 3.5 Sonnet for AI Pair Programming") | |
| st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True) | |
| st.markdown("### โฌ๏ธ Upload") | |
| uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader") | |
| st.markdown("### ๐ฎ Camera Controls") | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| st.button("โฌ ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'left'})) | |
| with col2: | |
| st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'up'})) | |
| st.button("๐", on_click=lambda: st.session_state.update({'camera_move': 'center'})) | |
| st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'down'})) | |
| with col3: | |
| st.button("โก๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'right'})) | |
| st.markdown("### โน๏ธ Instructions") | |
| st.write("- Click and drag to move objects") | |
| st.write("- Use camera controls or WASD keys to navigate") | |
| st.write("- Objects bounce automatically") | |
| st.write("- Mouse wheel to zoom") | |
| st.write("- Right-click and drag to rotate view") | |
| st.markdown("### ๐ Directory") | |
| directory = st.text_input("Enter path:", ".", key="directory_input") | |
| 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 = """ | |
| <a-scene embedded style="height: 600px; width: 100%;"> | |
| <a-entity id="rig" position="0 2 2" rotation="-45 0 0"> | |
| <a-entity camera look-controls wasd-controls cursor="rayOrigin: mouse" raycaster="objects: .raycastable"></a-entity> | |
| </a-entity> | |
| <a-sky color="#87CEEB"></a-sky> | |
| <a-entity moving-light="color: #FFD700; speed: 0.07 0.05 0.06; bounds: 4 3 4" position="2 2 -2"></a-entity> | |
| <a-entity moving-light="color: #FF6347; speed: 0.06 0.08 0.05; bounds: 4 3 4" position="-2 1 2"></a-entity> | |
| <a-entity moving-light="color: #00CED1; speed: 0.05 0.06 0.07; bounds: 4 3 4" position="0 3 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"{random.uniform(-3, 3)} {random.uniform(0, 3)} {random.uniform(-3, 3)}" | |
| entities += create_aframe_entity(file_path, file_type, position) | |
| assets += "</a-assets>" | |
| aframe_scene += assets + entities + "</a-scene>" | |
| camera_move = st.session_state.get('camera_move', None) | |
| if camera_move: | |
| aframe_scene += f"<script>moveCamera('{camera_move}');</script>" | |
| st.session_state.pop('camera_move') | |
| st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600) | |
| if __name__ == "__main__": | |
| main() |