question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

addSlider2D for 2 renderer

See original GitHub issue

Hi marcomusy,

I’m trying to render 2 plots with 1 slider. I found it is hard to realize that updating two or more plt.show() or plt.pop.add() within one plt.addSlider2D at the same time.

I want to slice a mesh object with a plane and show the cross section (in 2D, eg. in x-y-panel) simultaneously.

It seems work by ploting the cross section in matlibplot.pyplot, but there was some lag and delay by dragging the slider, how can I realize this effect just in vedo.plotter. Because further I want to merge 2 more mesh into one object to compare their section profile, it may be better if just use vedo.

The dataset is below if it is needed.

vedo 2021.0.3 | vtk 9.0.1 | python 3.9

Here’s my code 1, display 2 effects by using vedo.plotter and matplotlib.

import matplotlib.pyplot
from vedo import *

settings.immediateRendering = False

# load the mesh and pre_cut
s = Mesh('data/targets/target_AF_bs.stl')
plane_cut = s.cutWithBox([-78, 308, -71, 65, -1000, 1000])  # .c("silver").bc("Silver")
obj1 = vedo2trimesh(plane_cut)

# 8 bit RGBA color (r,g,b,transparent)(https://rgbacolorpicker.com/)
obj1.visual.face_colors = [(122, 122, 122, 100)]

txt = 'cross section of the mesh'

fig, ax = matplotlib.pyplot.subplots()


def slider(widget, event):
    cut_x = widget.GetRepresentation().GetValue()
    # generate cutting path by using trimesh.section -> return path2D object
    mslice = obj1.section(plane_origin=(cut_x, 0, 0), plane_normal=[1, 0, 0])
    # pl = Plane((cut_x, 0, -25), normal=[1, 0, 0], sx=100, sy=200, c='green', alpha=0.5)

    # force path2D object -> planar
    slice_2D, planar = mslice.to_planar()
    # show the cutting path on object and update
    plt.pop().add(mslice)

    ax.set_title('Interface in x = ' + str(int(cut_x)) + "mm")
    ax.autoscale_view()
    ax.set_xlim(-50, 50)
    ax.set_ylim(-75, 75)

    # matplotlib.pyplot the path2D object
    slice_2D.show()

    # matplotlib.pyplot show update by dragging slider
    ax.figure.canvas.draw()
    matplotlib.pyplot.cla()


plt = Plotter(bg="blue9", bg2="white")

plt.addSlider2D(slider,
                -77, 307,
                value=0,
                pos="bottom",
                title="cut in x direction")
plt.show(obj1, txt, axes=1, azimuth=65, elevation=-30, roll=-80).close()

屏幕截图 2022-03-03 015052 屏幕截图 2022-03-03 015107

code 2: use vedo, the old render didn’t show at=0 and didn’t remove/pop at=1 , in addition, the camera angle didn’t work. I want to obserb the cross section just in 2D (in my case Y-Z-panel), like in code 1.

"""Slice in x-direction"""

from vedo import *

settings.useDepthPeeling = True
settings.immediateRendering = False

# declare the instance of the class
plt = Plotter(shape=(1, 2), resetcam=1, sharecam=0, interactive=0, size=[1200, 500])

# load tool mesh
s = Mesh('data/targets/target_AF_bs.stl')
s1 = s.cutWithBox([-78, 308, -71, 65, -1000, 1000]).bc("silver")


def slider(widget, event):
    cut_x = widget.GetRepresentation().GetValue()

    # creat cut plane
    pl = Plane((cut_x, 0, -25), normal=[1, 0, 0], sx=100, sy=200, c='green', alpha=0.4)

    # Intersection
    s2 = s1.intersectWith(pl).lineWidth(1.1).c('black')

    # 1st render
    plt.pop().add(pl, at=0)

    # # 2nd render
    plt.pop().add(s2, at=1)


plt.addSlider2D(slider,
                -77, 307,
                value=0,
                pos="bottom-left",
                title="cut in x direction")

plt.show(s1, __doc__, at=0, axes=1, interactive=1, azimuth=65, elevation=-30, roll=-80)

屏幕截图 2022-03-03 021439

Thank you so much!

target_AF_bs.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kevoyuancommented, Mar 4, 2022

Thank you so much, that’s just what i thought, I really appreciate your work!

1reaction
marcomusycommented, Mar 4, 2022

Yes, try:

from vedo import *
from vedo.pyplot import plot

settings.useDepthPeeling = True


def slider(widget, event):

    cutx = widget.GetRepresentation().GetValue()
    pl.x(cutx)

    # Intersection
    s2 = s1.intersectWith(pl).join(reset=True)
    s2.lineWidth(5).c('black')

    # 1st render
    plt.remove(objs[0], at=0).add(pl, at=0, render=False)
    objs[0] = pl

    # 2nd render
    txt.text(f'nr of points: {s2.NPoints()}')
    x = s2.points()[:,1]
    y = s2.points()[:,2]    
    ps2 = plot(x, y, '-', xlim=(-80,80), ylim=(-80,10))
    plt.remove(objs[1], at=1).add(ps2, at=1, resetcam=1)
    objs[1] = ps2


# load tool mesh
s0 = Mesh('target_AF_bs.stl')
s1 = s0.cutWithBox([-78, 308, -71, 65, -1000, 1000]).bc("silver")
ax = Axes(s1)
txt = Text2D(font='Calco', bg='yellow')

objs = [None, None] # empty placeholders
pl = Grid(resx=1, resy=1, sx=100, sy=200).triangulate()
pl.rotateY(90).z(-30)
pl.c('green').alpha(0.4).wireframe(0).lw(0)

# declare the instance of the class
plt = Plotter(shape=(1,2), sharecam=0, interactive=0, size=[1200, 500])
plt.addSlider2D(slider,
                -77, 307,
                value=-77,
                pos="bottom-left",
                title="cut in x direction",
)
plt.show(s1, ax, at=0, interactive=0, azimuth=65, elevation=-30, roll=-80)
plt.show(txt, at=1, interactive=1)
plt.close()
Screenshot 2022-03-04 at 02 08 51
Read more comments on GitHub >

github_iconTop Results From Across the Web

addSlider2D for multi renderers · Issue #167 · marcomusy/vedo
It seems that addons.addSlider2D does not consider set related renderer. I added sliderWidget.SetCurrentRenderer(plt.renderer) before ...
Read more >
vedo.plotter API documentation
This module defines the main class Plotter to manage actors and 3D rendering … ... def addSlider2D(self, *a, **b). Expand source code
Read more >
ImageRenderer | Apple Developer Documentation
Use ImageRenderer to export bitmap image data from a SwiftUI view. You initialize the renderer with a view, then render images on demand,...
Read more >
First steps 2: Adding and customizing renderers
In this section, you will use different renderer functions to create various other kinds of graphs. You will also customize what your glyphs...
Read more >
Renderers (qiskit_metal.renderers) — Qiskit Metal 0.1.2 0.1.2 ...
Matplotlib handle all rendering of an axis. The axis is given in the function render. Access: self = gui.canvas.metal_renderer. AnimatedText (ax ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found