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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
def load_aframe_and_extras():
|
10 |
+
return """
|
11 |
+
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
12 |
+
<script src="https://unpkg.com/aframe-event-set-component@5.0.0/dist/aframe-event-set-component.min.js"></script>
|
13 |
+
<script>
|
14 |
+
// ๐น๏ธ Make objects draggable
|
15 |
+
AFRAME.registerComponent('draggable', {
|
16 |
+
init: function () {
|
17 |
+
this.el.setAttribute('class', 'raycastable');
|
18 |
+
this.el.setAttribute('cursor-listener', '');
|
19 |
+
this.dragHandler = this.dragMove.bind(this);
|
20 |
+
this.el.sceneEl.addEventListener('mousemove', this.dragHandler);
|
21 |
+
this.el.addEventListener('mousedown', this.onDragStart.bind(this));
|
22 |
+
this.el.addEventListener('mouseup', this.onDragEnd.bind(this));
|
23 |
+
this.camera = document.querySelector('[camera]');
|
24 |
+
},
|
25 |
+
remove: function () {
|
26 |
+
this.el.removeAttribute('cursor-listener');
|
27 |
+
this.el.sceneEl.removeEventListener('mousemove', this.dragHandler);
|
28 |
+
},
|
29 |
+
onDragStart: function (evt) {
|
30 |
+
this.isDragging = true;
|
31 |
+
this.el.emit('dragstart');
|
32 |
+
},
|
33 |
+
onDragEnd: function (evt) {
|
34 |
+
this.isDragging = false;
|
35 |
+
this.el.emit('dragend');
|
36 |
+
},
|
37 |
+
dragMove: function (evt) {
|
38 |
+
if (!this.isDragging) return;
|
39 |
+
var camera = this.camera;
|
40 |
+
var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5);
|
41 |
+
vector.unproject(camera);
|
42 |
+
var dir = vector.sub(camera.position).normalize();
|
43 |
+
var distance = -camera.position.y / dir.y;
|
44 |
+
var pos = camera.position.clone().add(dir.multiplyScalar(distance));
|
45 |
+
this.el.setAttribute('position', pos);
|
46 |
+
}
|
47 |
+
});
|
48 |
+
|
49 |
+
// ๐ฆ Make objects bounce
|
50 |
+
AFRAME.registerComponent('bouncing', {
|
51 |
+
schema: {
|
52 |
+
speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}},
|
53 |
+
dist: {type: 'vec3', default: {x: 0.5, y: 0.5, z: 0.5}}
|
54 |
+
},
|
55 |
+
init: function () {
|
56 |
+
this.originalPos = this.el.getAttribute('position');
|
57 |
+
this.dir = {x: 1, y: 1, z: 1};
|
58 |
+
},
|
59 |
+
tick: function (time, timeDelta) {
|
60 |
+
var currentPos = this.el.getAttribute('position');
|
61 |
+
var speed = this.data.speed;
|
62 |
+
var dist = this.data.dist;
|
63 |
+
|
64 |
+
['x', 'y', 'z'].forEach(axis => {
|
65 |
+
currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000);
|
66 |
+
if (Math.abs(currentPos[axis] - this.originalPos[axis]) > dist[axis]) {
|
67 |
+
this.dir[axis] *= -1;
|
68 |
+
}
|
69 |
+
});
|
70 |
+
|
71 |
+
this.el.setAttribute('position', currentPos);
|
72 |
+
}
|
73 |
+
});
|
74 |
+
|
75 |
+
// ๐ก Create moving light sources
|
76 |
+
AFRAME.registerComponent('moving-light', {
|
77 |
+
schema: {
|
78 |
+
color: {type: 'color', default: '#FFF'},
|
79 |
+
speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}},
|
80 |
+
bounds: {type: 'vec3', default: {x: 5, y: 5, z: 5}}
|
81 |
+
},
|
82 |
+
init: function () {
|
83 |
+
this.dir = {x: 1, y: 1, z: 1};
|
84 |
+
this.light = document.createElement('a-light');
|
85 |
+
this.light.setAttribute('type', 'point');
|
86 |
+
this.light.setAttribute('color', this.data.color);
|
87 |
+
this.light.setAttribute('intensity', '0.75');
|
88 |
+
this.el.appendChild(this.light);
|
89 |
+
},
|
90 |
+
tick: function (time, timeDelta) {
|
91 |
+
var currentPos = this.el.getAttribute('position');
|
92 |
+
var speed = this.data.speed;
|
93 |
+
var bounds = this.data.bounds;
|
94 |
+
|
95 |
+
['x', 'y', 'z'].forEach(axis => {
|
96 |
+
currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000);
|
97 |
+
if (Math.abs(currentPos[axis]) > bounds[axis]) {
|
98 |
+
this.dir[axis] *= -1;
|
99 |
+
}
|
100 |
+
});
|
101 |
+
|
102 |
+
this.el.setAttribute('position', currentPos);
|
103 |
+
}
|
104 |
+
});
|
105 |
+
|
106 |
+
// ๐ท Move the camera
|
107 |
+
function moveCamera(direction) {
|
108 |
+
var camera = document.querySelector('[camera]');
|
109 |
+
var pos = camera.getAttribute('position');
|
110 |
+
var rot = camera.getAttribute('rotation');
|
111 |
+
var speed = 0.5;
|
112 |
+
|
113 |
+
switch(direction) {
|
114 |
+
case 'up':
|
115 |
+
pos.z -= speed;
|
116 |
+
break;
|
117 |
+
case 'down':
|
118 |
+
pos.z += speed;
|
119 |
+
break;
|
120 |
+
case 'left':
|
121 |
+
pos.x -= speed;
|
122 |
+
break;
|
123 |
+
case 'right':
|
124 |
+
pos.x += speed;
|
125 |
+
break;
|
126 |
+
case 'center':
|
127 |
+
pos = {x: 0, y: 10, z: 0};
|
128 |
+
rot = {x: -90, y: 0, z: 0};
|
129 |
+
break;
|
130 |
+
}
|
131 |
+
|
132 |
+
camera.setAttribute('position', pos);
|
133 |
+
if (direction === 'center') {
|
134 |
+
camera.setAttribute('rotation', rot);
|
135 |
+
}
|
136 |
+
}
|
137 |
+
</script>
|
138 |
+
"""
|
139 |
+
|
140 |
+
# ๐๏ธ Create A-Frame entities for each file type
|
141 |
+
def create_aframe_entity(file_path, file_type, position):
|
142 |
+
rotation = f"0 {random.uniform(0, 360)} 0"
|
143 |
+
bounce_speed = f"{random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)}"
|
144 |
+
bounce_dist = f"0.1 0.1 0.1"
|
145 |
+
|
146 |
+
if file_type == 'obj':
|
147 |
+
return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" obj-model="obj: #{Path(file_path).stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
|
148 |
+
elif file_type == 'glb':
|
149 |
+
return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" gltf-model="#{Path(file_path).stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
|
150 |
+
elif file_type in ['webp', 'png']:
|
151 |
+
return f'<a-image position="{position}" rotation="-90 0 0" src="#{Path(file_path).stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-image>'
|
152 |
+
elif file_type == 'mp4':
|
153 |
+
return f'<a-video position="{position}" rotation="-90 0 0" src="#{Path(file_path).stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-video>'
|
154 |
+
return ''
|
155 |
+
|
156 |
+
# ๐ Encode file contents to base64
|
157 |
+
def encode_file(file_path):
|
158 |
+
with open(file_path, "rb") as file:
|
159 |
+
return base64.b64encode(file.read()).decode()
|
160 |
+
|
161 |
+
# ๐ญ Main function to run the Streamlit app
|
162 |
+
def main():
|
163 |
+
st.set_page_config(layout="wide")
|
164 |
+
|
165 |
+
with st.sidebar:
|
166 |
+
st.markdown("### ๐ค 3D AI Using Claude 3.5 Sonnet for AI Pair Programming")
|
167 |
+
|
168 |
+
st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True)
|
169 |
+
|
170 |
+
st.markdown("### โฌ๏ธ Upload")
|
171 |
+
uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader")
|
172 |
+
|
173 |
+
st.markdown("### ๐ฎ Camera Controls")
|
174 |
+
col1, col2, col3 = st.columns(3)
|
175 |
+
with col1:
|
176 |
+
st.button("โฌ
๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'left'}))
|
177 |
+
with col2:
|
178 |
+
st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'up'}))
|
179 |
+
st.button("๐", on_click=lambda: st.session_state.update({'camera_move': 'center'}))
|
180 |
+
st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'down'}))
|
181 |
+
with col3:
|
182 |
+
st.button("โก๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'right'}))
|
183 |
+
|
184 |
+
st.markdown("### โน๏ธ Instructions")
|
185 |
+
st.write("- Click and drag to move objects")
|
186 |
+
st.write("- Use camera controls or WASD keys to navigate")
|
187 |
+
st.write("- Objects bounce automatically")
|
188 |
+
st.write("- Mouse wheel to zoom")
|
189 |
+
st.write("- Right-click and drag to rotate view")
|
190 |
+
|
191 |
+
st.markdown("### ๐ Directory")
|
192 |
+
directory = st.text_input("Enter path:", ".", key="directory_input")
|
193 |
+
|
194 |
+
if not os.path.isdir(directory):
|
195 |
+
st.sidebar.error("Invalid directory path")
|
196 |
+
return
|
197 |
+
|
198 |
+
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
199 |
+
|
200 |
+
if uploaded_files:
|
201 |
+
for uploaded_file in uploaded_files:
|
202 |
+
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
|
203 |
+
if file_extension in file_types:
|
204 |
+
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
|
205 |
+
shutil.copyfileobj(uploaded_file, f)
|
206 |
+
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
|
207 |
+
else:
|
208 |
+
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
|
209 |
+
|
210 |
+
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
211 |
+
|
212 |
+
aframe_scene = """
|
213 |
+
<a-scene embedded style="height: 600px; width: 100%;">
|
214 |
+
<a-entity id="rig" position="0 10 0" rotation="-90 0 0">
|
215 |
+
<a-camera fov="60" look-controls wasd-controls cursor="rayOrigin: mouse" raycaster="objects: .raycastable"></a-camera>
|
216 |
+
</a-entity>
|
217 |
+
<a-sky color="#87CEEB"></a-sky>
|
218 |
+
<a-entity moving-light="color: #FFD700; speed: 0.07 0.05 0.06; bounds: 4 3 4" position="2 2 -2"></a-entity>
|
219 |
+
<a-entity moving-light="color: #FF6347; speed: 0.06 0.08 0.05; bounds: 4 3 4" position="-2 1 2"></a-entity>
|
220 |
+
<a-entity moving-light="color: #00CED1; speed: 0.05 0.06 0.07; bounds: 4 3 4" position="0 3 0"></a-entity>
|
221 |
+
"""
|
222 |
+
|
223 |
+
assets = "<a-assets>"
|
224 |
+
entities = ""
|
225 |
+
|
226 |
+
# ๐บ๏ธ Create a 10x10 grid
|
227 |
+
grid_size = 10
|
228 |
+
tile_size = 1
|
229 |
+
start_x = -(grid_size * tile_size) / 2
|
230 |
+
start_z = -(grid_size * tile_size) / 2
|
231 |
+
|
232 |
+
# ๐ฒ Randomly place models on the grid
|
233 |
+
for i in range(grid_size):
|
234 |
+
for j in range(grid_size):
|
235 |
+
x = start_x + (i * tile_size)
|
236 |
+
z = start_z + (j * tile_size)
|
237 |
+
position = f"{x} 0 {z}"
|
238 |
+
|
239 |
+
if files: # If we have files to place
|
240 |
+
file = random.choice(files)
|
241 |
+
file_path = os.path.join(directory, file)
|
242 |
+
file_type = file.split('.')[-1]
|
243 |
+
|
244 |
+
if file not in assets: # Only add to assets if not already there
|
245 |
+
encoded_file = encode_file(file_path)
|
246 |
+
if file_type in ['obj', 'glb']:
|
247 |
+
assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
|
248 |
+
elif file_type in ['webp', 'png', 'mp4']:
|
249 |
+
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
|
250 |
+
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
|
251 |
+
|
252 |
+
entities += create_aframe_entity(file_path, file_type, position)
|
253 |
+
|
254 |
+
assets += "</a-assets>"
|
255 |
+
aframe_scene += assets + entities + "</a-scene>"
|
256 |
+
|
257 |
+
camera_move = st.session_state.get('camera_move', None)
|
258 |
+
if camera_move:
|
259 |
+
aframe_scene += f"<script>moveCamera('{camera_move}');</script>"
|
260 |
+
st.session_state.pop('camera_move')
|
261 |
+
|
262 |
+
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
|
263 |
+
|
264 |
+
if __name__ == "__main__":
|
265 |
+
main()
|