Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/SOF-7438 feat: media creation nb #200

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Prev Previous commit
Next Next commit
update: wait for gif to generate, reset viewer
  • Loading branch information
VsevolodX committed Jan 16, 2025
commit d465ba69b5471869e8162144c59b233f5f4f5ecd
124 changes: 104 additions & 20 deletions other/media_generation/wave_control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
{
"metadata": {},
"cell_type": "code",
"source": [
"import time\n",
"\n",
"URL = \"http://localhost:3002\"\n"
],
"id": "79ee60afd06b7b3f",
"outputs": [],
"execution_count": null,
"source": "URL = \"http://localhost:3002\"\n",
"id": "79ee60afd06b7b3f"
"execution_count": null
},
{
"metadata": {},
Expand All @@ -23,19 +27,17 @@
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"material_json = {\n",
"material_json_1 = {\n",
" \"name\": \"New Material\",\n",
" \"basis\": {\n",
" \"elements\": [\n",
" {\"id\": 0, \"value\": \"O\"},\n",
" {\"id\": 1, \"value\": \"C\"}\n",
" {\"id\": 0, \"value\": \"Si\"},\n",
" {\"id\": 1, \"value\": \"Si\"}\n",
" ],\n",
" \"coordinates\": [\n",
" {\"id\": 0, \"value\": [0, 0, 0]},\n",
" {\"id\": 1, \"value\": [0.25, 0.25, 0.5]}\n",
" {\"id\": 1, \"value\": [0.25, 0.25, 0.25]}\n",
" ],\n",
" \"units\": \"crystal\",\n",
" \"cell\": [\n",
Expand All @@ -61,12 +63,52 @@
" \"isNonPeriodic\": False\n",
"}\n",
"\n",
"material_json_2 = {\n",
" \"name\": \"Another Material\",\n",
" \"basis\": {\n",
" \"elements\": [\n",
" {\"id\": 0, \"value\": \"Si\"},\n",
" {\"id\": 1, \"value\": \"O\"}\n",
" ],\n",
" \"coordinates\": [\n",
" {\"id\": 0, \"value\": [0, 0, 0]},\n",
" {\"id\": 1, \"value\": [0.15, 0.15, 0.15]}\n",
" ],\n",
" \"units\": \"crystal\",\n",
" \"cell\": [\n",
" [5.0, 0, 0],\n",
" [0, 5.0, 0],\n",
" [0, 0, 5.0]\n",
" ],\n",
" \"constraints\": []\n",
" },\n",
" \"lattice\": {\n",
" \"a\": 5.0, \"b\": 5.0, \"c\": 5.0,\n",
" \"alpha\": 90, \"beta\": 90, \"gamma\": 60,\n",
" \"units\": {\"length\": \"angstrom\", \"angle\": \"degree\"},\n",
" \"type\": \"CUB\",\n",
" \"vectors\": {\n",
" \"a\": [5.0, 0, 0],\n",
" \"b\": [0, 5.0, 0],\n",
" \"c\": [0, 0, 5.0],\n",
" \"alat\": 1,\n",
" \"units\": \"angstrom\"\n",
" }\n",
" },\n",
" \"isNonPeriodic\": False\n",
"}\n",
"\n",
"materials_settings = [{\n",
" \"material_json\": material_json,\n",
" \"name\": \"New Material.gif\"\n",
" \"material_json\": material_json_1,\n",
" \"name\": \"New Material 1 (Si2).gif\"\n",
"},\n",
"{\"material_json\": material_json_2,\n",
" \"name\": \"New Material 2 (SiO).gif\"\n",
"}]"
],
"id": "191df844bfc61a4e"
"id": "191df844bfc61a4e",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
Expand All @@ -78,11 +120,27 @@
"metadata": {},
"cell_type": "code",
"source": [
"from IPython.display import display, IFrame, Javascript\n",
"from IPython.display import display, IFrame, Javascript, HTML\n",
"import json\n",
"\n",
"from utils.visualize import visualize_materials\n",
"\n",
"def reset_iframe():\n",
" \"\"\"\n",
" Resets the iframe content by reloading it.\n",
" \"\"\"\n",
" js_code = \"\"\"\n",
" (function() {\n",
" const iframe = document.querySelector('iframe');\n",
" if (iframe) {\n",
" iframe.src = iframe.src; // Reload the iframe\n",
" console.log('Iframe reset');\n",
" } else {\n",
" console.error('Iframe not found');\n",
" }\n",
" })();\n",
" \"\"\"\n",
" display(Javascript(js_code))\n",
"\n",
"def send_message_to_iframe(message):\n",
" \"\"\"\n",
Expand Down Expand Up @@ -134,7 +192,7 @@
" send_message_to_iframe(message)\n",
"\n",
"def set_camera( pos=(0, 5, 0), target=(0, 0, 0)):\n",
" func_str = f\"(wave) => {{ wave.camera.position.set({pos[0]}, {pos[1]}, {pos[2]}); wave.camera.lookAt({target[0]}, {target[1]}, {target[2]}); wave.render(); }}\"\n",
" func_str = f\"(wave) => {{ alert('set_camera'); wave.camera.position.set({pos[0]}, {pos[1]}, {pos[2]}); wave.rebuildScene(); }}\"\n",
" message = {\n",
" \"action\": \"doFunc\",\n",
" \"parameters\": [func_str]\n",
Expand All @@ -147,6 +205,13 @@
" \"parameters\": [None]\n",
" }\n",
" send_message_to_iframe(message)\n",
"\n",
"def handle_reset_viewer():\n",
" message = {\n",
" \"action\": \"handleResetViewer\",\n",
" \"parameters\": [None]\n",
" }\n",
" send_message_to_iframe(message)\n",
"\n"
],
"id": "93be5f1320e953c6",
Expand All @@ -164,25 +229,44 @@
"cell_type": "code",
"source": [
"# Example usage with a material configuration:\n",
"iframe = IFrame(src=URL, width=\"800\", height=\"600\")\n",
"display(iframe)\n"
],
"id": "6f65f2a33a405f16",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"\n",
"\n",
"for material_settings in materials_settings:\n",
" GIF_NAME = material_settings[\"name\"] or material_settings[\"material_json\"][\"name\"]\n",
" iframe = IFrame(src=URL, width=\"800\", height=\"600\")\n",
" display(iframe)\n",
" # Send the material configuration to the iframe at the specified URL\n",
" set_material_in_iframe(material_json)\n",
" time.sleep(2)\n",
" set_material_in_iframe(material_settings[\"material_json\"])\n",
" time.sleep(2)\n",
" toggle_bonds()\n",
" time.sleep(2)\n",
" set_camera()\n",
" record_gif( GIF_NAME, 60, 0.05)\n"
" time.sleep(2)\n",
" record_gif( GIF_NAME, 60, 0.05)\n",
"\n",
" # We should wait until the gif is generated and saved before moving to the next material\n",
" time.sleep(30)\n",
" handle_reset_viewer()"
],
"id": "6f65f2a33a405f16",
"id": "bd1e9c79e5457f3b",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"id": "bd1e9c79e5457f3b",
"id": "20f597fe3c2c6887",
"outputs": [],
"execution_count": null
}
Expand Down