Last active
May 12, 2024 01:11
-
-
Save stwind/d3842d2c4d9db6f817b0ad659d07dc0f to your computer and use it in GitHub Desktop.
gltf.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyOZx62YiMDlgCyqcdRCnnNG", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/stwind/d3842d2c4d9db6f817b0ad659d07dc0f/gltf.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "X7YMndYWI1dM", | |
"outputId": "b56e8148-2fef-42cc-8638-c3dc630e3e49" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m693.9/693.9 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25h" | |
] | |
} | |
], | |
"source": [ | |
"!pip install -Uq trimesh" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import os\n", | |
"import json\n", | |
"\n", | |
"def read_gltf(path):\n", | |
" with open(path, \"r\") as f:\n", | |
" gltf = json.loads(f.read())\n", | |
" return gltf\n", | |
"\n", | |
"def gltf_uris(gltf):\n", | |
" return [x['uri'] for x in gltf['images']] + [x['uri'] for x in gltf['buffers']]" | |
], | |
"metadata": { | |
"id": "94Vk9gNbNnUp" | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"url = \"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/DamagedHelmet.gltf\"\n", | |
"\n", | |
"!wget -qc {url}\n", | |
"\n", | |
"for uri in gltf_uris(read_gltf(os.path.basename(url))):\n", | |
" resource_url = os.path.join(os.path.dirname(url), uri)\n", | |
" print(resource_url)\n", | |
" !wget -qc {resource_url}" | |
], | |
"metadata": { | |
"id": "Vu7sTwaCNoJO", | |
"outputId": "ead881fb-485c-441e-91a5-58fda6c6947c", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/Default_albedo.jpg\n", | |
"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/Default_metalRoughness.jpg\n", | |
"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/Default_emissive.jpg\n", | |
"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/Default_AO.jpg\n", | |
"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/Default_normal.jpg\n", | |
"https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/DamagedHelmet/glTF/DamagedHelmet.bin\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import trimesh" | |
], | |
"metadata": { | |
"id": "2UnK5M7cKoBu" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"scene = trimesh.load(\"DamagedHelmet.gltf\")" | |
], | |
"metadata": { | |
"id": "YDbxUjeiJaBB" | |
}, | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"geometries = list(scene.geometry.values())\n", | |
"geometry = geometries[0]" | |
], | |
"metadata": { | |
"id": "lGqSM6cJKsi2" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"geometry.vertex_normals.shape" | |
], | |
"metadata": { | |
"id": "oQ4hDdJ1WLO-", | |
"outputId": "87dceaab-8b77-460e-dc78-7912447f2b33", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(14556, 3)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"geometry.vertex_normals[[0,9193,14455]]" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "nKB841ZiKuOH", | |
"outputId": "3cd7f86c-5e41-43a3-dcac-33ede4f1f6e7" | |
}, | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[-0.91830194, 0.38380077, 0.09683523],\n", | |
" [-0.93508714, 0.32273322, -0.14633626],\n", | |
" [-0.81426436, -0.57469404, -0.08157597]])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 12 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "HJetAwpILhvM" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment