How to use three.js with python syntax

Three.js has been for me one of best JavaScript 3D library out there, but can’t there be a way for those with only python background to also tap into this beautiful library without having to learn JavaScript. I recently published this article Using Three.js with python syntax) using PyWeb3D, an Open Source project that I created to make it very easy to interact three.js library using python and brython

1 Like

Three.js is a JavaScript library for creating and rendering 3D graphics in a web browser. It is typically used with JavaScript syntax, but there are several ways you can use it with Python syntax.

One option is to use a Python-to-JavaScript transpiler, such as Transcrypt or Brython, to write Python code that can be compiled into JavaScript. This would allow you to write Three.js code in Python syntax, but you would still need to include the transpiled JavaScript code in your HTML file to use it in a web browser.

Another option is to use a Python library such as Pyodide or PyV8 to run JavaScript code from within Python. This would allow you to write JavaScript code in Three.js syntax and execute it from a Python script. However, this approach would require some familiarity with both Python and JavaScript syntax.

Here is an example of using Pyodide to run Three.js code from within a Python script:

import pyodide
pyodide.load_package(‘three’)

js_code = “”"
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var geometry = new THREE.BoxGeometry();
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );

camera.position.z = 5;

function animate() {
requestAnimationFrame( animate );

cube.rotation.x += 0.01;
cube.rotation.y += 0.01;

renderer.render( scene, camera );

}
animate();
“”"

pyodide.eval_js(js_code)

1 Like

Nice one Raju :clap::clap: