Pythreejs是一个为Jupyter提供交互式的python的3D图形库
1 说明:
=====
1.1 A Python / ThreeJS bridge utilizing the Jupyter widget infrastructure.
1.2 Pythreejs是基于Jupyter,连接python和threejs的一个3d可视化图形库。
1.3 使用Jupyter Interactive Widgets中的three.js,为Jupyter笔记本提供交互式3D图形。
编辑2 准备:
=====
2.1 官网:https://pythreejs.readthedocs.io/en/stable/index.html https://github.com/jupyter-widgets/pythreejs
2.2 安装:pip install pythreejs #本机安装 sudo pip3.8 install -i https://mirrors.aliyun.com/pypi/simple pythreejs
2.3 环境:
华为笔记本电脑、深度deepin-linux操作系统、谷歌浏览器、python3.8和微软vscode编辑器。
===例子===
注意以下均是用微软编辑器vscode调用jupyter,格式:#%%
3 长方体:
3.1 代码:#%% from pythreejs import * g = BoxGeometry( width=5, height=10, depth=15, widthSegments=5, heightSegments=10, depthSegments=15, ) g #%%
3.2 操作和效果图:后面就放效果图。
4 三条线坐标轴:
============
4.1 代码:#%% from pythreejs import * import numpy as np size = 4 linesgeom = Geometry(vertices=[[0, 0, 0],[size, 0, 0],[0, 0, 0],[0, size, 0], [0, 0, 0],[0, 0, size]], colors = ["red", "red", "green", "green", "white", "orange"]) lines = Line(geometry=linesgeom, material=LineBasicMaterial(linewidth=5, vertexColors="VertexColors"), type="LinePieces", ) scene = Scene(children=[ lines, DirectionalLight(color="#ccaabb", position=[0,10,0]), AmbientLight(color="#cccccc"), ]) c = PerspectiveCamera(position=[10, 10, 10]) renderer = Renderer(camera=c, background="black", background_opacity=1, scene=scene, controls=[OrbitControls(controlling=c)], width=400, height=400) renderer #%%
4.2 效果图:
5 彩色正方体:
==========
5.1 colorcube的代码:#%% from pythreejs import * import numpy as np #vertices==顶点 vertices = np.asarray([ [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1] ], dtype="float32") faces = np.asarray([ [0, 1, 3], [0, 3, 2], [0, 2, 4], [2, 6, 4], [0, 4, 1], [1, 4, 5], [2, 3, 6], [3, 7, 6], [1, 5, 3], [3, 5, 7], [4, 6, 5], [5, 6, 7] ], dtype="uint16").ravel() # We need to flatten index array vertexcolors = np.asarray([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)], dtype="float32") #Geometry==几何==位置 cubeGeometry = BufferGeometry(attributes=dict( #BufferAttribute==缓冲属性 position=BufferAttribute(vertices, normalized=False), index=BufferAttribute(faces, normalized=False), color=BufferAttribute(vertexcolors), )) myobjectCube = Mesh( geometry=cubeGeometry, material=MeshLambertMaterial(vertexColors="VertexColors"), position=[-0.5, -0.5, -0.5] # Center the cube ) cCube = PerspectiveCamera( position=[3, 3, 3], fov=20, children=[DirectionalLight(color="#ffffff", position=[-3, 5, 1], intensity=0.5)]) sceneCube = Scene(children=[myobjectCube, cCube, AmbientLight(color="#dddddd")]) rendererCube = Renderer(camera=cCube, background="black", background_opacity=1, scene = sceneCube, controls=[OrbitControls(controlling=cCube)]) rendererCube #%%
5.2 效果图:
6 origugv:
========
6.1 代码:#%% from pythreejs import * import numpy as np f = """ function f(origu, origv, out) { // scale u and v to the ranges I want: [0, 2*pi] var u = 2*Math.PI*origu; var v = 2*Math.PI*origv; var x = Math.sin(u); var y = Math.cos(v); var z = Math.cos(u+v); out.set(x,y,z) } """ surf_g = ParametricGeometry(func=f, slices=16, stacks=16) surf = Mesh(geometry=surf_g, material=MeshLambertMaterial(color="green", side="FrontSide")) surf2 = Mesh(geometry=surf_g, material=MeshLambertMaterial(color="yellow", side="BackSide")) c = PerspectiveCamera(position=[5, 5, 3], up=[0, 0, 1], children=[DirectionalLight(color="white", position=[3, 5, 1], intensity=0.6)]) scene = Scene(children=[surf, surf2, c, AmbientLight(intensity=0.5)]) renderer = Renderer(camera=c, scene=scene, controls=[OrbitControls(controlling=c)], width=400, height=400) renderer #%%
6.2 效果图:
7 ball:
=====
7.1 代码:#%% from pythreejs import * import numpy as np ball = Mesh(geometry=SphereGeometry(radius=1, widthSegments=32, heightSegments=24), material=MeshLambertMaterial(color="red"), position=[2, 1, 0]) c = PerspectiveCamera(position=[0, 5, 5], up=[0, 1, 0], children=[DirectionalLight(color="white", position=[3, 5, 1], intensity=0.5)]) scene = Scene(children=[ball, c, AmbientLight(color="#777777")]) renderer = Renderer(camera=c, scene=scene, controls=[OrbitControls(controlling=c)]) renderer #%%
7.2 效果图:
8 附注几张gif图:代码省略。
===自己整理并分享出来===
喜欢的人,请点赞、关注、评论、转发和收藏。