Spaces:
Runtime error
Runtime error
File size: 3,146 Bytes
550acf8 f217a67 edee69e 550acf8 f217a67 aaac327 f217a67 aaac327 f217a67 0438bd7 f217a67 0438bd7 aaac327 0438bd7 edee69e f217a67 0438bd7 f217a67 0438bd7 f217a67 0438bd7 f217a67 550acf8 f217a67 aaac327 f217a67 aaac327 f217a67 aaac327 f217a67 aaac327 f217a67 550acf8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import streamlit as st
import streamlit.components.v1 as components
import random
import json
import os
# Initialize session state
if "entities" not in st.session_state:
st.session_state.entities = []
# Function to get random 3D model file
def get_random_3d_model():
model_dir = "models"
model_files = [f for f in os.listdir(model_dir) if f.endswith(('.glb', '.obj'))]
return os.path.join(model_dir, random.choice(model_files)) if model_files else None
# Function to add a new entity to the scene
def add_entity(entity_type):
model_path = get_random_3d_model()
if model_path:
new_entity = {
'type': entity_type,
'model': model_path,
'position': {
'x': random.uniform(-5, 5),
'y': 0,
'z': random.uniform(-5, 5)
},
'rotation': {
'x': 0,
'y': random.uniform(0, 360),
'z': 0
}
}
st.session_state.entities.append(new_entity)
# Function to generate A-Frame entities
def generate_aframe_entities():
entities_html = ""
for entity in st.session_state.entities:
entities_html += f'''
<a-entity
gltf-model="#{entity['type']}"
position="{entity['position']['x']} {entity['position']['y']} {entity['position']['z']}"
rotation="{entity['rotation']['x']} {entity['rotation']['y']} {entity['rotation']['z']}">
</a-entity>
'''
return entities_html
# HTML template
html_template = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chofko's Ecosystem Simulator</title>
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-assets>
<a-asset-item id="skybike" src="{skybike_model}"></a-asset-item>
<a-asset-item id="heart" src="{heart_model}"></a-asset-item>
</a-assets>
<a-sky color="#ECECEC"></a-sky>
<a-plane position="0 0 0" rotation="-90 0 0" width="10" height="10" color="#7BC8A4"></a-plane>
{entities}
<a-entity camera look-controls position="0 1.6 0"></a-entity>
</a-scene>
</body>
</html>
'''
# Streamlit app
def main():
st.set_page_config(page_title="Chofko's Ecosystem Simulator", layout="wide")
# Sidebar
st.sidebar.title("Chofko's Ecosystem Controls")
if st.sidebar.button("🚲 Add SkyBike"):
add_entity("skybike")
if st.sidebar.button("❤️ Add Heart"):
add_entity("heart")
if st.sidebar.button("🗑️ Clear All Entities"):
st.session_state.entities = []
st.sidebar.subheader("Current Entities")
st.sidebar.json(st.session_state.entities)
# Main area - HTML5 canvas
skybike_model = "models/SkyBike.glb"
heart_model = "models/Heart.obj"
entities_html = generate_aframe_entities()
html_content = html_template.format(
skybike_model=skybike_model,
heart_model=heart_model,
entities=entities_html
)
components.html(html_content, height=600)
if __name__ == "__main__":
main() |