-
Notifications
You must be signed in to change notification settings - Fork 192
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
WIP: GLTF loading support #321
Open
reductor
wants to merge
2
commits into
jupyter-widgets:master
Choose a base branch
from
reductor:WIP/GLTFAsset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Asset Loading" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%gui asyncio" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pythreejs import *\n", | ||
"from ipywidgets import Output, Text" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import asyncio\n", | ||
"def wait_for_change(widget, value):\n", | ||
" future = asyncio.Future()\n", | ||
" def getvalue(change):\n", | ||
" # make the new value available\n", | ||
" future.set_result(change.new)\n", | ||
" widget.unobserve(getvalue, value)\n", | ||
" widget.observe(getvalue, value)\n", | ||
" return future" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"application/vnd.jupyter.widget-view+json": { | ||
"model_id": "2a9300ccfd0d40fa99fc6d0c1d807f96", | ||
"version_major": 2, | ||
"version_minor": 0 | ||
}, | ||
"text/plain": [ | ||
"Renderer(camera=PerspectiveCamera(children=(DirectionalLight(color='white', intensity=0.5, position=(3.0, 5.0,…" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"application/vnd.jupyter.widget-view+json": { | ||
"model_id": "ab0cd8cb585941de846b1e0e05d0e343", | ||
"version_major": 2, | ||
"version_minor": 0 | ||
}, | ||
"text/plain": [ | ||
"Output()" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"application/vnd.jupyter.widget-view+json": { | ||
"model_id": "e7ca01b0ce554cd097bcc3b0794aadda", | ||
"version_major": 2, | ||
"version_minor": 0 | ||
}, | ||
"text/plain": [ | ||
"Text(value='adfas')" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Task pending name='Task-1' coro=<show_model() running at <ipython-input-4-ab9b2bd45a01>:1>>" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"async def show_model(out, t, scene):\n", | ||
" print(out)\n", | ||
" t.value = 'pre-out'\n", | ||
" with out:\n", | ||
" t.value += ', pre-asset'\n", | ||
" print(\"pre-asset\")\n", | ||
" gltf_model = GLTFAsset(\"assets/pythreejs.gltf\")\n", | ||
" t.value += ', post-asset' + str(gltf_model)\n", | ||
" print(\"post-asset\")\n", | ||
" print(gltf_model)\n", | ||
"\n", | ||
" x = await wait_for_change(gltf_model, 'scene')\n", | ||
" t.value = 'post-wait'\n", | ||
" print(x)\n", | ||
" print(gltf_model)\n", | ||
"\n", | ||
"\n", | ||
" assert gltf_model.scene is not None\n", | ||
" print(scene)\n", | ||
" t.value = str(scene)\n", | ||
" scene.children = gltf_model.scene.children\n", | ||
" print(scene)\n", | ||
"\n", | ||
"\n", | ||
"ball = Mesh(geometry=SphereGeometry(),\n", | ||
" material=MeshLambertMaterial(color='red'))\n", | ||
"key_light = DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)\n", | ||
"\n", | ||
"c = PerspectiveCamera(position=[0, 5, 5], up=[0, 1, 0], children=[key_light])\n", | ||
"\n", | ||
"scene = Scene(children=[ball, c, AmbientLight(color='#777777')], background=None)\n", | ||
"\n", | ||
"\n", | ||
"renderer = Renderer(camera=c,\n", | ||
" scene=scene,\n", | ||
" alpha=True,\n", | ||
" clearOpacity=0,\n", | ||
" controls=[OrbitControls(controlling=c)])\n", | ||
"\n", | ||
"out = Output()\n", | ||
"with out:\n", | ||
" print(\"test\")\n", | ||
"t = Text()\n", | ||
"t.value = \"adfas\"\n", | ||
"display(renderer,out, t)\n", | ||
"with out:\n", | ||
" print(\"test2\")\n", | ||
"asyncio.ensure_future(show_model(out, t, scene))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
var Promise = require('bluebird'); | ||
var GLTFAssetBase = require('./GLTFAsset.autogen'); | ||
|
||
var widgets = require('@jupyter-widgets/base'); | ||
|
||
// HACK: examples expect THREE in globals | ||
global.THREE = require('three'); | ||
|
||
require('three/examples/js/loaders/GLTFLoader.js'); | ||
|
||
var THREE = require('three'); | ||
|
||
var GLTFAssetModel = GLTFAssetBase.GLTFAssetModel.extend({ | ||
constructThreeObjectAsync: function() { | ||
var manager = THREE.DefaultLoadingManager; | ||
var loader = new THREE.GLTFLoader(manager); | ||
// Ensure we resolve any local paths according to current notebook location: | ||
var gltfUriPromise = this.widget_manager.resolveUrl(this.get('gltfUri')); | ||
|
||
var p = new Promise(function(resolve, reject) { | ||
gltfUriPromise.then(function (gltfUri) { | ||
loader.load( | ||
gltfUri, | ||
function(gltf) { | ||
console.debug('Successfully loaded ' + gltfUri); | ||
return resolve(gltf); | ||
}, | ||
function(xhr) { | ||
console.debug(gltfUri + ': ' + (xhr.loaded / xhr.total * 100) + '%'); | ||
}, | ||
function(xhr) { | ||
console.log('Error loading GLTF: ' + gltfUri); | ||
return reject(new Error(xhr)); | ||
} | ||
); | ||
}, reject); | ||
}); | ||
return p.bind(this).then(function(gltf) { | ||
this.set({ scene: gltf.scene }, 'pushFromThree'); | ||
return gltf; | ||
}); | ||
|
||
}, | ||
|
||
}); | ||
|
||
module.exports = { | ||
GLTFAssetModel: GLTFAssetModel, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this always produce a scene? If so, maybe this should inherit
Scene
instead. It will simplify the logic a lot,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my use case it will, same with many others use cases I suspect, so could simplify it to inherit from that, and just name it GLTFScene which would simplify it to just support that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that would be my recommendation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately this also has some issues as children and other properties do not exist within python so do not have the required model id's or ipymodel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need to interact/modify the loaded model in Python, you would probably need to implement the loader in Python. Otherwise you are likely to end up in an asynchronous mess where the Python code has to wait for the JS to do stuff and sync it back before being able to continue executing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I need to be able interact with the scene that is loaded, so I'm fine with having python wait for a model loading, after doing some more work I have identified a few more issues
createUninitializedChildren
only works onInitializedThreeType
parameters, for something being created in JS first this should ideally be all propertiescreateUninitializedChildren
does not handle nested properties (and handling this also means handling the allow_single arrays)utils.createModel
does not update theinitPromise
to include its additional setup for comms_ref_geometry
), sosyncToModel
fails because it can't useobj[propName]
on theseI've managed to use the mesh and parts that get loaded and pushed from this, however I've hit some very unusual issues where the data does not exist in python until I refresh the page.
Unfortunately I need to stop working on this for the time being, so anyone feel free to take over, I may return to it in a few weeks.