Skip to content
Snippets Groups Projects
Commit d1d3d6fc authored by Bill Lorensen's avatar Bill Lorensen Committed by GitHub
Browse files

Merge pull request #38 from ajpmaclean/add_python_snippets

Adding Python snippets.
parents 6b17e1d4 f062e3a2
No related branches found
No related tags found
No related merge requests found
### Description
Snippets are chuncks of code that can be cut (*snipped*) and pasted into examples. We want each example tobe stand-alone, so we do not keep the snippet code in a library.
Snippets are chunks of code that can be cut (*snipped*) and pasted into examples. We want each example to be stand-alone, so we do not keep the snippet code in a library.
### Available snippets
......
### Description
Snippets are chunks of code that can be cut (*snipped*) and pasted into examples. We want each example to be stand-alone, so we do not keep the snippet code in a library.
### Available snippets
#### [GetProgramParameters](/Python/Snippets/GetProgramParameters.md)
Get the program parameters.
#### [ReadPolyData](/Python/Snippets/ReadPolyData.md)
Uses the appropriate vtkPolyData reader to read any vtkPolyData file.
#### [WritePNG](/Python/Snippets/WritePNG)
Takes a screen shot from the render window and writes it to a png file.
### Description
Get the program parameters.
This particular snippet requires a file name and an optional figure number.
To use the snippet, click the *Copy to clipboard* at the upper left of the code blocks.
### Implementation
``` python
def get_program_parameters():
import argparse
description = 'What the program does.'
epilogue = '''
An expanded description of what the program does.
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue)
parser.add_argument('filename', help='A required filename.')
parser.add_argument('figure', default=0, type=int, nargs='?', help='An optional fugure number.')
args = parser.parse_args()
return args.filename, args.figure
```
### Typical usage
``` python
file_name, figure = get_program_parameters()
```
\ No newline at end of file
### Description
Given a filename, uses the appropriate vtkPolyData reader to read any vtkPolyData file.
To use the snippet, click the *Copy to clipboard* at the upper left of the code blocks.
### Implementation
``` python
def ReadPolyData(file_name):
import os
path, extension = os.path.splitext(file_name)
extension = extension.lower()
if extension == ".ply":
reader = vtk.vtkPLYReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
elif extension == ".vtp":
reader = vtk.vtkXMLpoly_dataReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
elif extension == ".obj":
reader = vtk.vtkOBJReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
elif extension == ".stl":
reader = vtk.vtkSTLReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
elif extension == ".vtk":
reader = vtk.vtkpoly_dataReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
elif extension == ".g":
reader = vtk.vtkBYUReader()
reader.SetGeometryFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
else:
# Return a None if the extension is unknown.
poly_data = None
return poly_data
```
### Description
Given a filename, render window and optionally a rgba value, itakes a screen shot from the render window and writes it to a png file.
To use the snippet, click the *Copy to clipboard* at the upper left of the code blocks.
### Implementation
``` python
def WritePNG(fileName, ren_win, rgba = True):
"""
Write the render window view to a png file.
:param fileName:
:param ren_win: The render window.
:param rgba: Used to det the buffer type.
:return:
"""
"""
Save a screenshot.
:param fileName:
:param renWin:
:return:
"""
windowto_image_filter = vtk.vtkWindowToImageFilter()
windowto_image_filter.SetInput(ren_win)
windowto_image_filter.SetScale(1) # image quality
if rgba:
windowto_image_filter.SetInputBufferTypeToRGBA()
else:
windowto_image_filter.SetInputBufferTypeToRGB()
# Read from the front buffer.
windowto_image_filter.ReadFrontBufferOff()
windowto_image_filter.Update()
writer = vtk.vtkPNGWriter()
writer.SetFileName(fileName)
writer.SetInputConnection(windowto_image_filter.GetOutputPort())
writer.Write()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment