[slicer-users] Automated rendering using VolumeRendering Module

Thomas Ballinger tomb at bwh.harvard.edu
Mon Jun 21 11:44:57 EDT 2010


As per Steve Pieper's suggestion, here's the relevant excerpt of the script
I was using.  I'm trying to take many snapshots of a scene with a moving
camera, but when I use a rendered volume I just get the background color in
all of my snapshots.  Attached is the full script.  I've also realized that
a lot of this fuctionality already exists as I've explored the API.

Tom

scene = Slicer.slicer.MRMLScene
> cam = scene.GetNodeByID('vtkMRMLCameraNode1')
> appli = Slicer.slicer.ApplicationGUI
> slicesgui = appli.GetSlicesGUI()
> firstslicegui = slicesgui.GetNthSliceGUI(0)
> firstslicelogic = firstslicegui.GetLogic()
> camera = cam.GetCamera()
>
> final = tempfile.mkdtemp()
> currentPictureNumber = 0
>
> *def saveDisplay():
>     global currentPictureNumber
>     filename = final+'/'+str(currentPictureNumber)+'.png'
>     Slicer.tk.tk.eval('SlicerSaveLargeImage %s 1'%filename)
>     currentPictureNumber = currentPictureNumber + 1*
>
> def exportMovie(moviename,r=30,q=8):
>     """Uses ffmpeg to make a movie from the images taken so far"""
>
> a=subprocess.Popen(['/projects/schiz/ra/tomb/ffmpeg/ffmpeg/ffmpeg','-i',final+'/%d.png','-qmax',str(q),'-r','-y',str(r),moviename])
>     os.waitpid(a.pid,0)
>     os.system('nautilus '+os.path.abspath(os.path.dirname(moviename))+' &')
>
> def checkMovie():
>     """Uses eog to view your image files"""
>     os.system('eog '+final+'/* &')
>
> def saveRotatePics(theta,phi,howManyFrames=0,save=True,timeDelay=0):
> # This is physics spherical coordinates, not wikipedia ones
>     if not howManyFrames:
>         save=False
>         howManyFrames = 2
>     theta = float(theta)
>     phi = float(phi)
>     theta = theta * (2 * math.pi) / 360
>     phi = phi*(2 * math.pi) / 360
>     (startx,starty,startz) = camera.GetPosition()
>     r = math.sqrt(startx**2+starty**2+startz**2)
>     startTheta = math.atan2(starty,startx)
>     startPhi = math.acos(startz/r)
>     dTheta = theta / (howManyFrames - 1)
>     dPhi = phi / (howManyFrames - 1)
>     for i in range(howManyFrames):
>         theta = startTheta + dTheta*i
>         phi = startPhi + dPhi*i
>         x = r * math.sin(phi) * math.cos(theta)
>         y = r * math.sin(phi) * math.sin(theta)
>         z = r * math.cos(phi)
>         cam.SetPosition(x,y,z)
>         if save:
> *            camera.Modified()
>             appli.GetActiveRenderWindowInteractor().Render()
>             appli.UpdateMain3DViewers()
>             saveDisplay()
> *
> def saveZoomAndPanPics(r,u,f,howFar,howManyFrames=0,save=True,timeDelay=0):
>     howManyFrames = int(howManyFrames)
>     if not howManyFrames:
>         save=False
>         howManyFrames = 2
>     r = float(r)
>     u = float(u)
>     f = float(f)
>     (startfx,startfy,startfz) = camera.GetFocalPoint()
>     (startpx,startpy,startpz) = camera.GetPosition()
>     distFromFocalPoint =
> math.sqrt((startpx-startfx)**2+(startpy-startfy)**2+(startpz-startfz)**2)
>     if howFar >= distFromFocalPoint:
>         return False
>     (upx,upy,upz) = camera.GetViewUp()
>     cameraToFocalPoint = [startfx-startpx,startfy-startpy,startfz-startpz]
>     norm = numpy.linalg.norm(cameraToFocalPoint)
>     (fdx,fdy,fdz) = cameraToFocalPoint/norm
>     (rightx,righty,rightz) = numpy.cross([upx,upy,upz],[fdx,fdy,fdz])
>     dx = (r*rightx+u*upx+f*fdx)/(howManyFrames-1)
>     dy = (r*righty+u*upy+f*fdy)/(howManyFrames-1)
>     dz = (r*rightz+u*upz+f*fdz)/(howManyFrames-1)
>     zdx = (howFar*fdx)/(howManyFrames-1)
>     zdy = (howFar*fdy)/(howManyFrames-1)
>     zdz = (howFar*fdz)/(howManyFrames-1)
>     for i in range(howManyFrames):
>         camera.SetFocalPoint(startfx+dx*i,startfy+dy*i,startfz+dz*i)
>
> camera.SetPosition(startpx+(dx+zdx)*i,startpy+(dy+zdy)*i,startpz+(dz+zdz)*i)
>         if save:
> *            camera.Modified()
>             appli.GetActiveRenderWindowInteractor().Render()
>             appli.UpdateMain3DViewers()
>             saveDisplay()
>
> *
> On Fri, Jun 18, 2010 at 3:36 PM, Steve Pieper <pieper at bwh.harvard.edu>wrote:
>
>> Hi Thomas -
>>
>> Can you send the scripts you are using so we can have a look?
>>
>> -Steve
>>
>>
>> On Jun/18/10 11:20 AM, Thomas Ballinger wrote:
>>
>>> I've been using Slicer to create short videos of sometimes complex
>>> scenes.  Of particular use has been the tcl script
>>> "SlicerSaveEachRender", but in order to get smoother rotations, pans and
>>> zooms I've been using python bindings to rotate the camera and call the
>>> tcl script SlicerSaveLargeImage repeatedly.  I've been calling
>>> ApplicationGUI.UpdateMain3DViewers() to update the scene before saving
>>> the picture, which seems like overkill, but has been working fine for
>>> ordinary models.  However, VolumeRendering models don't seem to get
>>> updated this way.  Setting camera.Modified() will update the view once
>>> my script stops, but not for each camera move.
>>>
>>> What can I call to update a rendered model after changing the camera so
>>> I can save the new rendered image?
>>>
>>> Thanks,
>>>
>>> Thomas Ballinger
>>>
>>>
>>>
>>> _______________________________________________
>>> slicer-users mailing list
>>> slicer-users at bwh.harvard.edu
>>> http://massmail.spl.harvard.edu/mailman/listinfo/slicer-users
>>> To unsubscribe: send email to
>>> slicer-users-request at massmail.spl.harvard.edu with unsubscribe as the
>>> subject
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://massmail.spl.harvard.edu/pipermail/slicer-users/attachments/20100621/a28e00e3/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smv.py
Type: text/x-python-script
Size: 6631 bytes
Desc: not available
URL: <http://massmail.spl.harvard.edu/pipermail/slicer-users/attachments/20100621/a28e00e3/attachment.bin>


More information about the slicer-users mailing list