Python 马雅维同步照相机

Python 马雅维同步照相机,python,pyside,mayavi,mayavi.mlab,Python,Pyside,Mayavi,Mayavi.mlab,我遵循mayaVI官方网站()给出的脚本,并希望使用sync\u-camera命令在qt GUI中同步两个图形(如图所示),这样一个图形中的任何旋转/缩放等都会以完全相同的方式同时自动旋转/缩放另一个图形 sync\u-camera命令在另一个官方的mayaVI页面上简要介绍了它,但是我还没有找到关于如何正确使用它以在类层次结构中成功利用它的更多信息 是否有人有此程序或建议的经验 import numpy as np from traits.api import HasTraits, Inst

我遵循mayaVI官方网站()给出的脚本,并希望使用
sync\u-camera
命令在qt GUI中同步两个图形(如图所示),这样一个图形中的任何旋转/缩放等都会以完全相同的方式同时自动旋转/缩放另一个图形

sync\u-camera
命令在另一个官方的mayaVI页面上简要介绍了它,但是我还没有找到关于如何正确使用它以在类层次结构中成功利用它的更多信息

是否有人有此程序或建议的经验

import numpy as np

from traits.api import HasTraits, Instance, Button, \
    on_trait_change
from traitsui.api import View, Item, HSplit, Group

from mayavi import mlab
from mayavi.core.ui.api import MlabSceneModel, SceneEditor


class MyDialog(HasTraits):

    scene1 = Instance(MlabSceneModel, ())
    scene2 = Instance(MlabSceneModel, ())

    button1 = Button('Redraw')
    button2 = Button('Redraw')

    @on_trait_change('button1')
    def redraw_scene1(self):
        self.redraw_scene(self.scene1)

    @on_trait_change('button2')
    def redraw_scene2(self):
        self.redraw_scene(self.scene2)

    def redraw_scene(self, scene):
        # Notice how each mlab call points explicitely to the figure it
        # applies to.
        mlab.clf(figure=scene.mayavi_scene)
        x, y, z, s = np.random.random((4, 100))
        mlab.points3d(x, y, z, s, figure=scene.mayavi_scene)

    # The layout of the dialog created
    view = View(HSplit(
                  Group(
                       Item('scene1',
                            editor=SceneEditor(), height=250,
                            width=300),
                       'button1',
                       show_labels=False,
                  ),
                  Group(
                       Item('scene2',
                            editor=SceneEditor(), height=250,
                            width=300, show_label=False),
                       'button2',
                       show_labels=False,
                  ),
                ),
                resizable=True,
                )


m = MyDialog()
m.configure_traits()

解决方案是不使用2-figure-in-1方法(如最初发布的那样),而是创建两个单独的图形。出于我的需要,我重写了初始代码,使每个图形都在它自己的类中,然后简单地将它们并排放置在新的帧中。我认为如果没有这样的分离,就不可能使用
sync\u摄像头
功能,因为它需要两个单独的图形作为输入。结果基本一致。我成功地实现了同步摄像头功能,如下所示:

import sys, os, time
import numpy as np
os.environ['ETS_TOOLKIT'] = 'qt4'
from pyface.qt import QtGui, QtCore
from traits.api import HasTraits, Instance, on_trait_change, Str, Float, Range
from traitsui.api import View, Item, HSplit, Group
from mayavi import mlab
from mayavi.core.api import PipelineBase, Engine
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor

class Mayavi1(HasTraits):
    scene = Instance(MlabSceneModel, ())

    @on_trait_change('scene.activated')
    def update_plot(self):
        Mayavi1.fig1 = mlab.figure(1)
        self.scene.mlab.clf(figure=Mayavi1.fig1)

        x, y, z, s = np.random.random((4, 100))
        splot = self.scene.mlab.points3d(x, y, z, s, figure=Mayavi1.fig1)
        #splot.actor.actor.scale = np.array([25,25,25]) #if plot-types different

    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                 height=300, width=300, show_label=False),
            resizable=True
            )

class Mayavi2(HasTraits):
    scene = Instance(MlabSceneModel, ())

    @on_trait_change('scene.activated')
    def update_plot(self):
        Mayavi2.fig2 = mlab.figure(2)
        self.scene.mlab.clf(figure=Mayavi2.fig2)

        x, y, z, s = np.random.random((4, 100))
        cplot = self.scene.mlab.points3d(x, y, z, s, figure=Mayavi2.fig2)
        #cplot.actor.actor.position = np.array([1,1,1]) #if plot-types different

    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                 height=300, width=300, show_label=False),
            resizable=True
            )

class P1(QtGui.QWidget):
    def __init__(self, parent=None):
        super(P1, self).__init__(parent)
        layout = QtGui.QGridLayout(self)
        layout.setContentsMargins(20,20,20,20) #W,N,E,S
        layout.setSpacing(10)

        self.visualization1 = Mayavi1()
        self.ui1 = self.visualization1.edit_traits(parent=self, kind='subpanel').control
        layout.addWidget(self.ui1, 0, 0, 1, 1)
        self.ui1.setParent(self)

        self.visualization2 = Mayavi2()
        self.ui2 = self.visualization2.edit_traits(parent=self, kind='subpanel').control
        layout.addWidget(self.ui2, 0, 2, 1, 1)
        self.ui2.setParent(self)

        mlab.sync_camera(self.visualization1,self.visualization2)
        mlab.sync_camera(self.visualization2,self.visualization1)
        #self.visualization1.scene.mlab.view(0,0,10,[1,1,1])

class Hierarchy(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Hierarchy, self).__init__(parent)
        self.setGeometry(50, 50, 400, 400) #(int x, int y, int w, int h)
        self.gotoP1()

    def gotoP1(self):
        self.P1f = P1(self)
        self.setWindowTitle("Page1")
        self.setCentralWidget(self.P1f)
        self.show()       

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    #app = QtGui.QApplication(sys.argv)
    w = Hierarchy()
    sys.exit(app.exec_())       
但是,在我自己的版本中,我在每个图中使用两个不同的数据源(一个是散点图,另一个是等高线图,感兴趣的等高线图原点与散点图不同),并且由于相机连接,两个数据源都不在屏幕上同时出现(两个数据源的本机坐标都不同)

因此,如果您一次只能在帧中看到一个3d对象,请调整任一图形的
def update\u plot(self)
内的位置,直到它们同时在屏幕上显示。这可以通过以下命令完成:

splot.actor.actor.scale = np.array([25,25,25]) #with splot for fig1

cplot.actor.actor.position = np.array([-64,-64,-64]) #with cplot for fig2
我强烈建议实际进入mayaVI管道(点击红灯以实时查看输出代码),根据需要调整绘图。如果有人需要进一步的帮助,请告诉我