Skip to content

Instantly share code, notes, and snippets.

@mikebind
Last active December 3, 2021 07:56
Show Gist options
  • Save mikebind/88e19559c6b1c104d71f75083bcffb7c to your computer and use it in GitHub Desktop.
Save mikebind/88e19559c6b1c104d71f75083bcffb7c to your computer and use it in GitHub Desktop.
Function which allows shifting 3D Slicer volume rendering scalar opacity transfer function similar to "Shift" slider in Volume Rendering module
def shiftVolumeRendering(volumePropertyNode, xOffset=0):
""" Shift the scalar opacity control point values by an
offset value (similar to moving the "Shift" slider in the volume rendering module)
"""
volProp = volumePropertyNode.GetVolumeProperty()
scalarOpacity = volProp.GetScalarOpacity() # this is a vtkPiecewiseFunction
pointIdx = 0
opacityPointValues = [] # list to hold values
# Gather Existing Values
for pointIdx in range( scalarOpacity.GetSize() ):
opacityPointValue = [0,0,0,0]
scalarOpacity.GetNodeValue(pointIdx, opacityPointValue)
# opacityPointValue now holds [xLocation, opacity, midpoint, sharpness] for this control point
opacityPointValues.append(opacityPointValue) # store values in a list
# Add offset
for opacityPointValue in opacityPointValues:
opacityPointValue[0] = opacityPointValue[0] + xOffset # change the location value (leave all others unchanged)
# Set new values (not sure if there is any way to batch these?)
scalarOpacity.RemoveAllPoints()
for opacityPointValue in opacityPointValues:
scalarOpacity.AddPoint( *opacityPointValue )
# Trigger updates (not totally sure if this is necessary or not)
scalarOpacity.Modified()
# Example usage
volumePropertyName = 'CT-AAA' # change this to whatever volume property name you are using
volPropNode = slicer.util.getNode(volumePropertyName)
amountToShiftBy = 100
shiftVolumeRendering(volPropNode, amountToShiftBy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment