traits - Interactively update color in Mayavi, Python -


it first post on stackoverflow. writing mayavi python program. tell me how update/modify color of point interactively? example, in points3d(), changing color of point in real-time when interactively modify position. tried under @on_trait_change, doesn't work. color cannot changed. following code:

import mayavi import mayavi.mlab numpy import arange, pi, cos, sin  traits.api import hastraits, range, instance, \         on_trait_change traitsui.api import view, item, hgroup  mayavi.core.api import pipelinebase mayavi.core.ui.api import mayaviscene, sceneeditor, \                 mlabscenemodel  def luc_func(x, y, z):     return x + y + z;  class visualization(hastraits):     x1 = range(1, 30, 5)     z1 = range(1, 30, 5)     scene      = instance(mlabscenemodel, ())     def __init__(self):         # not forget call parent's __init__         hastraits.__init__(self)         z = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]         y = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5]         x = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]         self.plot = self.scene.mlab.points3d(x, y, z, luc_func, scale_mode = 'none')         #self.plot2 = self.scene.mlab.points3d(z, x, y, color = (0, 0, 1))      @on_trait_change('x1,z1')     def update_plot(self):         x = [1,2,3,4,self.x1,1,2,3,4,self.x1,1,2,3,4,self.x1,1,2,3,4,self.x1,1,2,3,4,self.x1]         z = [1,1,1,1,self.z1,1,1,1,1,self.z1,1,1,1,1,self.z1,1,1,1,1,self.z1,1,1,1,1,self.z1]         luc_func = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,self.z1]         self.plot.mlab_source.reset(x = x, z = z, luc_func = luc_func)         #self.plot2.mlab_source.set(y = y, z = z)       # layout of dialog created     view = view(item('scene', editor=sceneeditor(scene_class=mayaviscene),                     height=250, width=300, show_label=false),                 hgroup(                         '_', 'x1', "z1",                     ),                 ) visualization = visualization() visualization.configure_traits() 

thanks help!

i have noticed bug in interactivity of points3d similar describing here. don't know origin of bug regularly use following workaround. basic idea avoid mlab.points3d , instead call mlab.pipeline.glyph directly, in:

def virtual_points3d(coords, figure=none, scale_factor=none, color=none,      name=none):      c = np.array(coords)     source = mlab.pipeline.scalar_scatter( c[:,0], c[:,1], c[:,2],         figure=figure)      return mlab.pipeline.glyph( source, scale_mode='none',          scale_factor=scale_factor,         mode='sphere', figure=figure, color=color, name=name) 

later can change colors referring vtk object directly, rather mayavi trait isn't connected properly:

glyph = virtual_points3d(coords) glyph.mlab_source.dataset.point_data.scalars = new_values 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -