Skip to content

Commit c611b5f

Browse files
kaischroederruiterrsdunkelkohakukunfrericp
committed
Add JavaScript bindings
Co-authored-by: Roland Ruiters-Christou <[email protected]> Co-authored-by: Sebastian Dunkel <[email protected]> Co-authored-by: Aura Munoz <[email protected]> Co-authored-by: Philipp Frericks <[email protected]> Co-authored-by: Cedrick Muenstermann <[email protected]>
1 parent 1c83aeb commit c611b5f

File tree

15 files changed

+3622
-0
lines changed

15 files changed

+3622
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
set(PXR_PREFIX jsBindings)
2+
set(PXR_PACKAGE jsBindings)
3+
4+
if (NOT PXR_ENABLE_JS_SUPPORT)
5+
return()
6+
endif()
7+
8+
set(BINDINGS_NAME "jsBindings")
9+
set(BINDING_DEPENDENCIES "")
10+
list(APPEND BINDING_DEPENDENCIES usd usdLux usdGeom usdUtils sdf tf)
11+
12+
set(RESOURCE_PARAMETERS "")
13+
function(add_resources target)
14+
# This local var is needed since list append cannot update the PARENT_SCOPE
15+
set(LOCAL_VAR ${RESOURCE_PARAMETERS})
16+
get_property(RESSOURCES TARGET ${target} PROPERTY EMSCRIPTEN_RESOURCES)
17+
list(APPEND LOCAL_VAR "${RESSOURCES}")
18+
set(RESOURCE_PARAMETERS "${LOCAL_VAR}" PARENT_SCOPE)
19+
endfunction()
20+
21+
# Gather resources (schemas) to make them available to attach them to the bundle
22+
add_resources(usdShade)
23+
add_resources(sdf)
24+
add_resources(usdHydra)
25+
add_resources(usd)
26+
add_resources(usdLux)
27+
add_resources(ar)
28+
add_resources(usdGeom)
29+
add_resources(ndr)
30+
31+
list(APPEND RESOURCE_PARAMETERS "--embed-file ${PROJECT_BINARY_DIR}/plugins_plugInfo.json@/usd/plugInfo.json")
32+
33+
pxr_cpp_bin(${BINDINGS_NAME}
34+
LIBRARIES
35+
${BINDING_DEPENDENCIES}
36+
${RESOURCE_PARAMETERS}
37+
)
38+
39+
set(BUILD_FILES
40+
${CMAKE_CURRENT_BINARY_DIR}/${BINDINGS_NAME}.js
41+
${CMAKE_CURRENT_BINARY_DIR}/${BINDINGS_NAME}.wasm
42+
${CMAKE_CURRENT_BINARY_DIR}/${BINDINGS_NAME}.worker.js
43+
)
44+
45+
install(
46+
FILES
47+
${BUILD_FILES}
48+
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../js/bindings/${CMAKE_BUILD_TYPE}
49+
)

extras/usd/js_bindings/jsBindings.cpp

Whitespace-only changes.

js/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
bindings/

js/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Additional Requirements
2+
-----------------------
3+
4+
Node version 16.0.0^
5+
Emscripten version 2.0.24^
6+
7+
Setup
8+
-----
9+
10+
Build USD with Emscripten for NodeJS (in the root of this repository):
11+
12+
locally
13+
```sh
14+
python build_scripts/build_usd.py --emscripten <build_folder>
15+
```
16+
or in a Docker container
17+
```sh
18+
docker build -t <CONTAINER_TAG> .
19+
docker run -it -w //src -p 6931:6931 <CONTAINER_TAG>
20+
```
21+
22+
and run
23+
24+
```sh
25+
npm install
26+
```
27+
28+
in this `js` folder.
29+
30+
Tests
31+
------
32+
33+
Run
34+
35+
```sh
36+
npm run test
37+
```
38+
39+
or in watch mode
40+
41+
```sh
42+
npm run test -- --watch
43+
```
44+
45+
NPM package consumption
46+
------------------------
47+
48+
Currently we only support consumption of the bindings via Script tags in the browser or via Node.js
49+
50+
If you are using Webpack you can add the bindings to your application with
51+
52+
```
53+
externals: {
54+
"usd": 'usd', // indicates global variable
55+
},
56+
plugins: [
57+
new CopyPlugin({
58+
patterns: [
59+
{ from: "<PATH TO BINDINGS>" },
60+
],
61+
}),
62+
],
63+
```
64+
65+
and after adding `<script src="jsBindings.js"></script>` to your HTML page use it in your code with
66+
67+
```
68+
<script src="jsBindings.js"></script>
69+
<script type="module">
70+
import {UsdStage} from './usd.js';
71+
let stage = UsdStage.CreateNew('HelloWorld.usda');
72+
</script>
73+
```
74+
75+
In Node.Js you can load it via
76+
```
77+
const usdModule = require("usd");
78+
let Usd = await usdModule();
79+
let UsdStage = Usd.UsdStage;
80+
81+
let stage = UsdStage.CreateNew('HelloWorld.usda');
82+
...
83+
```

js/__tests__/stage.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const usdModule = require("usd");
2+
3+
let Usd;
4+
let stage;
5+
6+
beforeEach(async () => {
7+
Usd = await usdModule();
8+
const fileName = "HelloWorld.usda";
9+
stage = Usd.UsdStage.CreateNew(fileName);
10+
});
11+
12+
afterAll(() => {
13+
Usd.PThread.runningWorkers.forEach(x => x.onmessage = function() {});
14+
Usd.PThread.terminateAllThreads();
15+
Usd = null;
16+
stage = null;
17+
process.removeAllListeners('unhandledRejection')
18+
process.removeAllListeners('uncaughtException')
19+
});
20+
21+
describe('USD Stage', () => {
22+
test("CreateNew", () => {
23+
expect(stage).not.toBeUndefined();
24+
});
25+
26+
test("DefinePrim", () => {
27+
let sphere = stage.DefinePrim("/hello/world", "Sphere");
28+
expect(sphere).not.toBeUndefined();
29+
});
30+
31+
test("ExportToString", () => {
32+
stage.DefinePrim("/hello", "Xform");
33+
stage.DefinePrim("/hello/world", "Sphere");
34+
const data = stage.ExportToString();
35+
expect(data).toEqual(expect.stringMatching('Sphere'));
36+
expect(data).toEqual(expect.stringMatching('Xform'));
37+
});
38+
});

0 commit comments

Comments
 (0)