LoginSignup
0
0

Docker上のjupyterlabからmayaviで3Dプロット

Last updated at Posted at 2024-02-23

概要

3Dプロットしたものを動かすためにmayaviを入れようとして色々した

環境

Windows 11 Pro
Docker Desktop
JupyterLab 4.1.2

既にVcXsrvを入れていたが必要あるか分からない。(必要な場合)入れ方は下記参考

結果何をすればよかったか

python:3.12-slim->python:3.10.slim
vtk9.3.0->9.2.2
にバージョンを落とした。

追記

import順でkernel restartしたり通常実行する
具体的にはimport cv2を先にするとkernel restartになる、原因不明

やったこと

以下時系列で散文的に書く。
まずそのままで

pip install mayavi

するとbuild時点で

error: subprocess-exited-with-error
  × Getting requirements to build wheel did not run successfully.
...
NameError: name 'build_src' is not defined python

とエラーが出る

にはMicrosoft Visual C++ Build Toolsが関わってるような雰囲気もあったが、よく分からない。代わりに

pip install https://github.com/enthought/mayavi/zipball/master

として

from mayavi import mlab

を実行するとKernel restartとなる。

に倣って足りてないものを適宜apt installする

ldd /usr/local/lib/python3.12/site-packages/PyQt5/Qt5/plugins/platforms/libqxcb.so | grep "not found"
        libxcb-icccm.so.4 => not found
        libxcb-image.so.0 => not found
        libxcb-util.so.1 => not found
        libxcb-keysyms.so.1 => not found
        libxcb-render-util.so.0 => not found
        libxcb-shape.so.0 => not found
        libxcb-xinerama.so.0 => not found
        libxcb-xkb.so.1 => not found
        libxkbcommon-x11.so.0 => not found
        libxcb-icccm.so.4 => not found
        libxcb-image.so.0 => not found
        libxcb-util.so.1 => not found
        libxcb-keysyms.so.1 => not found
        libxcb-render-util.so.0 => not found
        libxcb-shape.so.0 => not found
        libxcb-xinerama.so.0 => not found
        libxcb-xkb.so.1 => not found
        libxkbcommon-x11.so.0 => not found

jupyterlabでデバッグモードにする場合は

import os
os.environ["QT_DEBUG_PLUGINS"] = "1"

再度

from mayavi import mlab

すると

ImportError: TVTK not built properly. Unable to find either a directory:
/usr/local/lib/python3.12/site-packages/tvtk/tvtk_classes or a file: 
/usr/local/lib/python3.12/site-packages/tvtk/tvtk_classes.zip with the TVTK classes.

コンテナのfiles内を見てみると確かにtvtk_classesはない。
立ち返り、色んなgithub issueでversion下げるとうまくいったとあったので、Dockerfileを

FROM python:3.12-slim-> FROM python:3.10-slim

に変更してimageをrebuild。すると公式にあるように

pip install mayavi
pip install PyQt5

が問題なく実行できた。再度

from mayavi import mlab

を試すと

WARNING: Imported VTK version (9.3) does not match the one used to build the TVTK classes (9.2). 
This may cause problems. Please rebuild TVTK.

とあり、バージョンの差が問題となってるようなので

pip uninstall vtk
pip install vtk==9.2.2

としてバージョンを合わせるとうまくいった。以下は描画例。

from mayavi import mlab
import numpy as np

# 3D物体の座標データ
x, y, z = np.random.random((3, 100))

# 3D物体を描画
mlab.points3d(x, y, z)

# デフォルトで視点変更が可能なウィンドウを表示
mlab.show()

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0