awacke1 commited on
Commit
67d6118
·
verified ·
1 Parent(s): eaf5eea

Create app.py

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