Spaces:
Sleeping
Sleeping
kolibril13
commited on
Commit
•
a822c43
1
Parent(s):
27c51dc
add widget
Browse files- pages/02_widget.ipynb +23 -43
- requirements.txt +1 -1
pages/02_widget.ipynb
CHANGED
@@ -20,59 +20,39 @@
|
|
20 |
"outputs": [],
|
21 |
"source": [
|
22 |
"import bpy\n",
|
23 |
-
"from
|
24 |
-
"import molecularnodes as mn\n",
|
25 |
"from anywidget_3dviewer import Viewer\n",
|
26 |
"import solara\n",
|
|
|
27 |
"\n",
|
28 |
-
"# Ensure the glTF exporter is activated\n",
|
29 |
-
"bpy.ops.preferences.addon_enable(module=\"io_scene_gltf2\")\n",
|
30 |
-
"\n",
|
31 |
-
"# Delete all mesh objects from the scene\n",
|
32 |
-
"bpy.ops.object.select_all(action=\"DESELECT\")\n",
|
33 |
-
"bpy.ops.object.select_by_type(type=\"MESH\")\n",
|
34 |
-
"bpy.ops.object.delete()\n",
|
35 |
-
"\n",
|
36 |
-
"obj = mn.load.molecule_rcsb(\"7TYG\", starting_style=\"cartoon\", center_molecule=True)\n",
|
37 |
-
"obj.scale = [10, 10, 10]\n",
|
38 |
-
"\n",
|
39 |
-
"bpy.context.view_layer.objects.active = obj\n",
|
40 |
-
"obj.modifiers[\"MolecularNodes\"].node_group.nodes[\"MN_color_attribute_random\"].inputs[\"Lightness\"].default_value = 0.5\n",
|
41 |
-
"obj.modifiers[\"MolecularNodes\"].node_group.nodes[\"MN_color_attribute_random\"].inputs[\"Saturation\"].default_value = 1\n",
|
42 |
-
"obj.modifiers[\"MolecularNodes\"].node_group.nodes[\"MN_color_attribute_random\"].inputs[\"Seed\"].default_value = 1\n",
|
43 |
-
"\n",
|
44 |
-
"for modifier in obj.modifiers:\n",
|
45 |
-
" bpy.ops.object.modifier_apply(modifier=modifier.name)\n",
|
46 |
-
"bpy.ops.geometry.color_attribute_render_set(name=\"Color\")\n",
|
47 |
-
"\n",
|
48 |
-
"\n",
|
49 |
-
"# Exporting the model to .glb format for Three.js\n",
|
50 |
-
"# Specify the path where you want to save the exported model\n",
|
51 |
-
"gltf_path = Path() / \"_MY_MODEL_DATA.gltf\"\n",
|
52 |
-
"\n",
|
53 |
-
"bpy.ops.geometry.color_attribute_render_set(name=\"Color\")\n",
|
54 |
-
"\n",
|
55 |
-
"# Select only the object\n",
|
56 |
-
"bpy.ops.object.select_all(action=\"DESELECT\")\n",
|
57 |
-
"obj.select_set(True)\n",
|
58 |
-
"\n",
|
59 |
-
"# Export the selected obj object to a .glb file\n",
|
60 |
-
"bpy.ops.export_scene.gltf(\n",
|
61 |
-
" filepath=str(gltf_path), export_format=\"GLTF_EMBEDDED\", use_selection=True\n",
|
62 |
-
")\n",
|
63 |
-
"\n",
|
64 |
-
"data = gltf_path.read_text()\n",
|
65 |
-
"\n",
|
66 |
-
"gltf_path.unlink()\n",
|
67 |
"\n",
|
|
|
|
|
|
|
|
|
68 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
"\n",
|
70 |
-
"
|
71 |
"\n",
|
72 |
-
"viewer = Viewer(gltf_data=data)\n",
|
73 |
"@solara.component\n",
|
74 |
"def Page():\n",
|
|
|
75 |
" display(viewer)\n",
|
|
|
|
|
76 |
"Page()"
|
77 |
]
|
78 |
},
|
|
|
20 |
"outputs": [],
|
21 |
"source": [
|
22 |
"import bpy\n",
|
23 |
+
"from IPython.display import display\n",
|
|
|
24 |
"from anywidget_3dviewer import Viewer\n",
|
25 |
"import solara\n",
|
26 |
+
"from pathlib import Path\n",
|
27 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
"\n",
|
29 |
+
"def make_gltf_model():\n",
|
30 |
+
" bpy.ops.preferences.addon_enable(module=\"io_scene_gltf2\")\n",
|
31 |
+
" bpy.ops.object.select_all(action=\"DESELECT\")\n",
|
32 |
+
" bpy.ops.object.select_by_type(type=\"MESH\")\n",
|
33 |
"\n",
|
34 |
+
" bpy.ops.object.delete()\n",
|
35 |
+
" bpy.ops.mesh.primitive_cube_add(size=2, align=\"WORLD\", location=(0, 1, 1))\n",
|
36 |
+
" cube = bpy.context.active_object\n",
|
37 |
+
" gltf_path = \"./model_cube.gltf\"\n",
|
38 |
+
" bpy.ops.object.select_all(action=\"DESELECT\")\n",
|
39 |
+
" cube.select_set(True)\n",
|
40 |
+
" bpy.ops.export_scene.gltf(\n",
|
41 |
+
" filepath=gltf_path, export_format=\"GLTF_EMBEDDED\", use_selection=True\n",
|
42 |
+
" )\n",
|
43 |
+
" gltf_path = Path() / \"model_cube.gltf\"\n",
|
44 |
+
" data = gltf_path.read_bytes()\n",
|
45 |
+
" gltf_path.unlink()\n",
|
46 |
+
" return data\n",
|
47 |
"\n",
|
48 |
+
"data = make_gltf_model()\n",
|
49 |
"\n",
|
|
|
50 |
"@solara.component\n",
|
51 |
"def Page():\n",
|
52 |
+
" viewer = Viewer(gltf_data=data)\n",
|
53 |
" display(viewer)\n",
|
54 |
+
"\n",
|
55 |
+
"\n",
|
56 |
"Page()"
|
57 |
]
|
58 |
},
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
bpy
|
2 |
solara
|
3 |
-
|
|
|
1 |
bpy
|
2 |
solara
|
3 |
+
anywidget_3dviewer
|