Working Efficiently in SAMSON: Copy Python Code from Commands and Presets

When automating molecular modeling tasks in SAMSON, many users run into a common issue: how to replicate interactive operations in Python scripts. Whether you’re adding hydrogens, changing color schemes, or setting a visual preset, doing it once through the interface is easy—but redoing this many times, sharing it, or building on top of it for automation can become tedious if you have to figure out the corresponding Python code manually.

Fortunately, SAMSON offers a quick way to bridge this gap by letting you copy Python commands directly from the interface operations. This feature streamlines your scripting workflow, ensures consistency, and removes the guesswork. Here’s how it works.

How to Extract Python Code from Commands

Let’s say you want to add hydrogens to a molecular structure. Usually, this involves clicking a command in the interface. But instead of trying to figure out how to write a script for this from scratch, you can simply:

  • Use the Find everything search bar at the top of SAMSON’s interface.
  • Search for the command, like “.Add hydrogens”.
  • On the right side of the command, look for the Python icon copy icon labeled “Copy as Python code”.
  • Click the icon to copy the exact Python line that performs the same action.

Here’s what that looks like in practice:

Copy Python code for a command

You can then paste this code inside the Code Editor or execute it directly in the Python Console. It’s a simple, precise way to replicate interface actions in a script.

Extracting Code from Color Palettes and Visual Presets

Perhaps you’ve adjusted atom colors using a favorite color palette or set a visual preset that shows your molecular system exactly the way you like it. Repeating those actions every time is inefficient. Here’s how you can automate that too:

  • Open the color palette or visual preset window.
  • Click on the icon to Copy as Python code.

Copy Python code for a color palette

Copy Python code for a visual preset

Now you can apply the exact same color scheme or preset to any model programmatically. This is especially useful when preparing figures for papers or presentations, or when creating reproducible workflows for students or collaborators.

Why This Matters

Being able to copy the exact Python commands from your workflow serves multiple purposes:

  • Workflow automation: Turn routine interface operations into reusable scripts.
  • Collaboration: Share exact steps with minimal room for misinterpretation.
  • Documentation: Easily record and explain what operations were performed.
  • Learning: Understand how interface operations map to SAMSON’s scripting API.

It removes a key pain for molecular modelers who want to automate but aren’t sure how to translate actions into code. It saves time, increases reproducibility, and lowers the entry barrier for new users adopting scripting in SAMSON.

Want to see more scripting features? Visit the full documentation here: https://documentation.samson-connect.net/users/latest/scripting/.

SAMSON and all SAMSON Extensions are free for non-commercial use. You can get SAMSON at https://www.samson-connect.net.

Comments are closed.