To create replicas of structures using SAMSON’s Python API, call their clone()
function. You can then programatically edit these structures.
The example below shows how to start from a single nanotube to create a model of a nanotube “fabric” through a series of undoable steps.
Precisely, the scripts starts from a nanotube and performs the following steps:
- replicate the nanotube in two directions to create a grid of nanotubes;
- apply sine function to these nanotubes in the third direction to weave them with each other.
Visit the corresponding tutorial for the full explanation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# get an indexer of all structural models in the active document indexerOfStructuralModels = SAMSON.getNodes('n.t sm') # get the first structural model from the indexer nanotube = indexerOfStructuralModels[0] # get an indexer of all atoms in the nanotube indexerOfAtoms = nanotube.getNodes('n.t a') a = indexerOfAtoms[0] minX = maxX = a.getX() minY = maxY = a.getY() # go through all atoms in the indexer for a in indexerOfAtoms: x = a.getX() y = a.getY() minX = min(minX, x) maxX = max(maxX, x) minY = min(minY, y) maxY = max(maxY, y) length = (maxX - minX) # the nanotube's length diameter = (maxY - minY) # the nanotube's diameter numReplicasX = 10 # the number of replicas in one direction distance = length / numReplicasX # set еру distance between nanotubes based on the number of replicas A = 0.75 * diameter # set an amplitude for sine function # get the active document document = SAMSON.getActiveDocument() # start holding to allow for undo/redo SAMSON.beginHolding("Replicate nanotubes") for r in range(numReplicasX-1): # create a replica replica = nanotube.clone() # clone the original nanotube SAMSON.hold(replica) # hold the replica node for undo/redo replica.create() # create the replica document.addChild(replica) # add the replica to the document # shift the replica in Y-direction dy = (r + 1) * distance # get an indexer of all atoms in the replicated nanotube indexerOfAtoms = replica.getNodes('n.t a') # loop over all atoms in the replicated nanotube for a in indexerOfAtoms: # shift atoms in the Y-direction a.setY(a.getY() + dy) # end holding for undo/redo SAMSON.endHolding() # import the necessary functions and constants from math import sin, pi # get an indexer of all structural models in the active document indexerOfStructuralModels = SAMSON.getNodes('n.t sm') sign = 1 # used to change the phase of the sine function # start holding to allow for undo/redo SAMSON.beginHolding("Replicate nanotubes") # loop through all structural models (all nanotubes) for original in indexerOfStructuralModels: # create a replica replica = original.clone() # clone the original nanotube SAMSON.hold(replica) # hold the replica node for undo/redo replica.create() # create the replica document.addChild(replica) # add it to the document # for shifting nanotubes from the border shift = distance / 2.0 # get an indexer of all atoms in the replicated nanotube indexerOfAtomsInReplica = replica.getNodes('n.t a') # rotate the replica by 90 degrees by changing x and y coordinates of its atoms # and shift the replica from the borders to have '#' pattern for a in indexerOfAtomsInReplica: x = a.getX() y = a.getY() a.setY(x - shift) a.setX(y + shift) # apply sine function in Z-direction to original and replicated nanotubes # get an indexer of all atoms in the original nanotube indexerOfAtomsInOriginal = original.getNodes('n.t a') # loop over all atoms in the original nanotube for a in indexerOfAtomsInOriginal: w = a.getX() / distance # [dimensionless quantity] # apply sine function to the Z-coordinate of an atom a.setZ(a.getZ() + A * sign * sin(pi * w.value)) # loop over all atoms in the replicated nanotube for a in indexerOfAtomsInReplica: # [dimensionless quantity], take into account the shift we did previously w = (a.getY() - shift) / distance # apply sine function to the Z-coordinate of an atom a.setZ(a.getZ() + A * sign * sin(pi * w.value)) # change the phase to the opposite one sign = -1 * sign # end holding for undo/redo SAMSON.endHolding() |
Press O to orbit.