Spaces:
Running
Running
Delete backup.pm.2.tilemap.app.py
Browse files- backup.pm.2.tilemap.app.py +0 -175
backup.pm.2.tilemap.app.py
DELETED
@@ -1,175 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
import base64
|
4 |
-
from pathlib import Path
|
5 |
-
import shutil
|
6 |
-
import random
|
7 |
-
|
8 |
-
# 🌈 Load A-Frame and custom components
|
9 |
-
@st.cache_data
|
10 |
-
def load_aframe_and_extras():
|
11 |
-
return """
|
12 |
-
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
13 |
-
<script src="https://unpkg.com/[email protected]/dist/aframe-event-set-component.min.js"></script>
|
14 |
-
<script>
|
15 |
-
// 🕹️ Make objects draggable
|
16 |
-
AFRAME.registerComponent('draggable', {
|
17 |
-
// ... (draggable component code remains the same)
|
18 |
-
});
|
19 |
-
|
20 |
-
// 🦘 Make objects bounce
|
21 |
-
AFRAME.registerComponent('bouncing', {
|
22 |
-
// ... (bouncing component code remains the same)
|
23 |
-
});
|
24 |
-
|
25 |
-
// 💡 Create moving light sources
|
26 |
-
AFRAME.registerComponent('moving-light', {
|
27 |
-
// ... (moving-light component code remains the same)
|
28 |
-
});
|
29 |
-
|
30 |
-
// 📷 Move the camera
|
31 |
-
function moveCamera(direction) {
|
32 |
-
// ... (moveCamera function remains the same)
|
33 |
-
}
|
34 |
-
</script>
|
35 |
-
"""
|
36 |
-
|
37 |
-
# 🏗️ Create A-Frame entities for each file type
|
38 |
-
def create_aframe_entity(file_stem, file_type, position):
|
39 |
-
rotation = f"0 {random.uniform(0, 360)} 0"
|
40 |
-
bounce_speed = f"{random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)}"
|
41 |
-
bounce_dist = f"0.1 0.1 0.1"
|
42 |
-
|
43 |
-
if file_type == 'obj':
|
44 |
-
return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" obj-model="obj: #{file_stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
|
45 |
-
elif file_type == 'glb':
|
46 |
-
return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" gltf-model="#{file_stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
|
47 |
-
elif file_type in ['webp', 'png']:
|
48 |
-
return f'<a-image position="{position}" rotation="-90 0 0" src="#{file_stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-image>'
|
49 |
-
elif file_type == 'mp4':
|
50 |
-
return f'<a-video position="{position}" rotation="-90 0 0" src="#{file_stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-video>'
|
51 |
-
return ''
|
52 |
-
|
53 |
-
# 🔐 Encode file contents to base64
|
54 |
-
@st.cache_data
|
55 |
-
def encode_file(file_path):
|
56 |
-
with open(file_path, "rb") as file:
|
57 |
-
return base64.b64encode(file.read()).decode()
|
58 |
-
|
59 |
-
# 🗺️ Generate tilemap grid
|
60 |
-
@st.cache_data
|
61 |
-
def generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5):
|
62 |
-
assets = "<a-assets>"
|
63 |
-
entities = ""
|
64 |
-
tile_size = 1
|
65 |
-
start_x = -(grid_width * tile_size) / 2
|
66 |
-
start_z = -(grid_height * tile_size) / 2
|
67 |
-
|
68 |
-
# Limit the number of unique models
|
69 |
-
unique_files = random.sample(files, min(len(files), max_unique_models))
|
70 |
-
encoded_files = {}
|
71 |
-
|
72 |
-
for file in unique_files:
|
73 |
-
file_path = os.path.join(directory, file)
|
74 |
-
file_type = file.split('.')[-1]
|
75 |
-
encoded_file = encode_file(file_path)
|
76 |
-
encoded_files[file] = encoded_file
|
77 |
-
|
78 |
-
if file_type in ['obj', 'glb']:
|
79 |
-
assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
|
80 |
-
elif file_type in ['webp', 'png', 'mp4']:
|
81 |
-
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
|
82 |
-
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
|
83 |
-
|
84 |
-
for i in range(grid_width):
|
85 |
-
for j in range(grid_height):
|
86 |
-
x = start_x + (i * tile_size)
|
87 |
-
z = start_z + (j * tile_size)
|
88 |
-
position = f"{x} 0 {z}"
|
89 |
-
|
90 |
-
if unique_files:
|
91 |
-
file = random.choice(unique_files)
|
92 |
-
file_type = file.split('.')[-1]
|
93 |
-
entities += create_aframe_entity(Path(file).stem, file_type, position)
|
94 |
-
|
95 |
-
assets += "</a-assets>"
|
96 |
-
return assets, entities
|
97 |
-
|
98 |
-
# 🎭 Main function to run the Streamlit app
|
99 |
-
def main():
|
100 |
-
st.set_page_config(layout="wide")
|
101 |
-
|
102 |
-
with st.sidebar:
|
103 |
-
st.markdown("### 🤖 3D AI Using Claude 3.5 Sonnet for AI Pair Programming")
|
104 |
-
|
105 |
-
st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True)
|
106 |
-
|
107 |
-
st.markdown("### ⬆️ Upload")
|
108 |
-
uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader")
|
109 |
-
|
110 |
-
st.markdown("### 🎮 Camera Controls")
|
111 |
-
col1, col2, col3 = st.columns(3)
|
112 |
-
with col1:
|
113 |
-
st.button("⬅️", on_click=lambda: st.session_state.update({'camera_move': 'left'}))
|
114 |
-
with col2:
|
115 |
-
st.button("⬆️", on_click=lambda: st.session_state.update({'camera_move': 'up'}))
|
116 |
-
st.button("🔄", on_click=lambda: st.session_state.update({'camera_move': 'center'}))
|
117 |
-
st.button("��️", on_click=lambda: st.session_state.update({'camera_move': 'down'}))
|
118 |
-
with col3:
|
119 |
-
st.button("➡️", on_click=lambda: st.session_state.update({'camera_move': 'right'}))
|
120 |
-
|
121 |
-
st.markdown("### 🗺️ Grid Size")
|
122 |
-
grid_width = st.slider("Grid Width", 1, 8, 8)
|
123 |
-
grid_height = st.slider("Grid Height", 1, 5, 5)
|
124 |
-
|
125 |
-
st.markdown("### ℹ️ Instructions")
|
126 |
-
st.write("- Click and drag to move objects")
|
127 |
-
st.write("- Use camera controls or WASD keys to navigate")
|
128 |
-
st.write("- Objects bounce automatically")
|
129 |
-
st.write("- Mouse wheel to zoom")
|
130 |
-
st.write("- Right-click and drag to rotate view")
|
131 |
-
|
132 |
-
st.markdown("### 📁 Directory")
|
133 |
-
directory = st.text_input("Enter path:", ".", key="directory_input")
|
134 |
-
|
135 |
-
if not os.path.isdir(directory):
|
136 |
-
st.sidebar.error("Invalid directory path")
|
137 |
-
return
|
138 |
-
|
139 |
-
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
140 |
-
|
141 |
-
if uploaded_files:
|
142 |
-
for uploaded_file in uploaded_files:
|
143 |
-
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
|
144 |
-
if file_extension in file_types:
|
145 |
-
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
|
146 |
-
shutil.copyfileobj(uploaded_file, f)
|
147 |
-
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
|
148 |
-
else:
|
149 |
-
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
|
150 |
-
|
151 |
-
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
152 |
-
|
153 |
-
aframe_scene = f"""
|
154 |
-
<a-scene embedded style="height: 600px; width: 100%;">
|
155 |
-
<a-entity id="rig" position="0 {max(grid_width, grid_height)} 0" rotation="-90 0 0">
|
156 |
-
<a-camera fov="60" look-controls wasd-controls cursor="rayOrigin: mouse" raycaster="objects: .raycastable"></a-camera>
|
157 |
-
</a-entity>
|
158 |
-
<a-sky color="#87CEEB"></a-sky>
|
159 |
-
<a-entity moving-light="color: #FFD700; speed: 0.07 0.05 0.06; bounds: 4 3 4" position="2 2 -2"></a-entity>
|
160 |
-
<a-entity moving-light="color: #FF6347; speed: 0.06 0.08 0.05; bounds: 4 3 4" position="-2 1 2"></a-entity>
|
161 |
-
<a-entity moving-light="color: #00CED1; speed: 0.05 0.06 0.07; bounds: 4 3 4" position="0 3 0"></a-entity>
|
162 |
-
"""
|
163 |
-
|
164 |
-
assets, entities = generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5)
|
165 |
-
aframe_scene += assets + entities + "</a-scene>"
|
166 |
-
|
167 |
-
camera_move = st.session_state.get('camera_move', None)
|
168 |
-
if camera_move:
|
169 |
-
aframe_scene += f"<script>moveCamera('{camera_move}');</script>"
|
170 |
-
st.session_state.pop('camera_move')
|
171 |
-
|
172 |
-
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
|
173 |
-
|
174 |
-
if __name__ == "__main__":
|
175 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|