Colorization

Colorization

Python Scripting provides an easy way for custom colorization of nodes in the data graph (e.g., molecules, atoms, etc).

You can find the code in SAMSON Python Scripting samples on GitHub.

To colorize a node, you need to create the color you want and apply it to the node:

red = sam.DataModel.Type.Color(255, 0, 0)                     # RGB color
node.setColor(red)                                            # apply color to the node

Internally, the setColor function creates a constant color scheme based on a given color, removes material from the node, creates a new one, and adds it to the node:

red = sam.DataModel.Type.Color(255, 0, 0)                     # RGB color
colorScheme = sam.DataModel.Color.ColorSchemeConstant(red)    # create a color scheme based on the color
material = sam.DataModel.DataGraph.Material()                 # create material
material.setColorScheme(colorScheme)                          # set a color scheme for the material
node.removeMaterial()                                         # remove an existing material from the node
node.addMaterial(material)                                    # add a given material to the node

Below is an example of atom colorization according to their x-coordinate.

SAMSON.importFromFile('/path/to/data/graphene.xyz')     # import your file
 
indexer = SAMSON.getNodes('n.t atom')                   # get all atom nodes
 
direction = ['x','y','z']
ipos = 0                                                # parameter that determines the coordinate according to which the colorization should be done
print('Colorize set of %d nodes in %s-direction' % (indexer.size, direction[ipos]))
 
SAMSON.setBusy(True)                                    # notify user that SAMSON is busy
 
minx = maxx = indexer[0].getPosition()[ipos].value      # set an initial value for minimal and maximal coordinates
for atom in indexer:                                    # compute minimal and maximal coordinates
    val = atom.getPosition()[ipos].value
    if val > maxx: maxx = val
    if val < minx: minx = val
 
for atom in indexer:                                    # colorize each atom according to its coordinate
                                                        # get RGB color for a node based on HSV color
    color = sam.DataModel.Type.Color.fromHSV(239.5 / 360.0 * (atom.getPosition()[ipos].value - minx) / (maxx - minx), 205.0/255.0, 1.0)
    atom.setColor(color)                                # set the color of the node
 
SAMSON.setBusy(False)                                   # SAMSON is not busy anymore

Comments are closed.