Author Topic: Render active viewport not updating histogram during animation saving  (Read 101 times)

trochetm

  • Newbie
  • *
  • Posts: 2
Hi,

I have the following problem with my python script overlay on top of the whole data pipepline compute by my script:
I can see the update of the two graphs in the GUI when frame changes.

But for some reason when I try to save an animation (I would like .mp4 but for the purpose of this message I uploaded two frames in png) the two graphs does not get update.
Even stranger the variable in the text label overlay is still updated normally.

   
Any suggestion ?

I have the following python script in the Overlay onglets :

Code: [Select]
import matplotlib
#matplotlib.use('Agg') # Activate 'agg' backend for off-screen plotting.
#matplotlib.use('Qt5Agg') # Activate 'agg' backend for off-screen plotting.

import matplotlib.pyplot as plt
import PyQt5.QtGui

def render(args):
    # Request the output data collection from the current pipeline:
    data = args.scene.selected_pipeline.compute()
    print(data.series)
    # Look up the DataSeries object generated by the CoordinationAnalysisModifier:
    #if 'coordination-rdf' not in data.series:
    #    raise RuntimeError('No RDF data found')
    #rdf_data = data.series['coordination-rdf'].as_table()
    if 'histogram[Charge]' not in data.series:
        raise RuntimeError('No Charge data found')
    charge_data = data.series['histogram[Charge]'].as_table()
    if  'binning[Selection]' not in data.series:
        raise RuntimeError('No spatial binning data found')
    spatialbinning_data = data.series['binning[Selection]'].as_table()

    #  Compute plot size in inches (DPI determines label size)
    dpi = 160
    plot_width = 0.4 * args.size[0] / dpi
    plot_height = 0.95 * args.size[1] / dpi
   
    # Create matplotlib figure:
    fig, ax = plt.subplots(nrows=2,figsize=(plot_width,plot_height), dpi=dpi)
    fig.patch.set_alpha(0.5)
    ax[0].set_title('average local Cr concentration')
    ax[0].set_ylim(bottom=0,top=800)

    # Plot Charge histogram data
    ax[0].bar(charge_data[:,0], charge_data[:,1],width=1/59)
    ax[1].plot(spatialbinning_data[:,0], spatialbinning_data[:,1])
    ax[1].set_title('planar concentration of Cr atoms')
    ax[1].set_ylim(bottom=0,top=1)
    ax[1].set_xlabel('along x axis')
    plt.tight_layout()
   
    # Render figure to an in-memory buffer.
    buf = fig.canvas.print_to_buffer()
   
    # Create a QImage from the memory buffer
    res_x, res_y = buf[1]
    img = PyQt5.QtGui.QImage(buf[0], res_x, res_y, PyQt5.QtGui.QImage.Format_RGBA8888)
   
    # Paint QImage onto viewport canvas
    args.painter.drawImage(0, 0, img)

Thank !
« Last Edit: August 02, 2019, 01:45:30 PM by Constanze Kalcher »

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Render active viewport not updating histogram during animation saving
« Reply #1 on: August 02, 2019, 01:38:53 PM »
Hi,

I think you need to pass the current frame to the compute() function like this:
Code: [Select]
data = args.scene.selected_pipeline.compute(args.frame)otherwise you will only see the graphs get updated when you move the time slider in the GUI but not when you use the render active viewport function for the whole time series. In the latter case the args.scene.selected_pipeline.compute() will always use the currently selected frame in the GUI (which doesn't change during rendering).
Does that solve your problem?

-Constanze

trochetm

  • Newbie
  • *
  • Posts: 2
Re: Render active viewport not updating histogram during animation saving
« Reply #2 on: August 05, 2019, 10:05:57 AM »
Hi Constanze,

I knew it was something silly like you mentionned.

Thank you very much. It works !

Please, as a reward, accept the following animation (in .mp4) with the scene as well as plots updating as asked.

It illustrates the demixtion of an initial configuration of 50% random FeCr alloys at 800K done by Canonical Monte Carlo.
The two plots are the local concentration of chromium atom within the 5NN sphere and the planar concentration of Cr atoms along the x axis.

Sincerely Mickaël.



 


Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Render active viewport not updating histogram during animation saving
« Reply #3 on: August 06, 2019, 09:36:36 AM »
Nice work Mickaël!