Logo
Toggle Menu
  • home
  • blog
  • about
    • about this site
    • contact
    • privacy policy
  • cinema 4d
    • plugins
    • creating c4d plugins
    • plugin cookbook
    • software
  • models
    • nodes
    • plants 1
    • other models - 1
    • other models - 2
  • osl
    • writing osl shaders
    • osl shaders for download

Creating a new plugin: 10

An extra feature (or two)

Before starting on nucelotides, there is an additional feature which I wanted to add. We can already colour all the bonds by a user-defined colour, or colour them by chain. What we can't do is colour them by nucleotide and this is what I will add now.

It turned out to be more complex than at first sight. The problem is that the method we use for generating the bonds for each chain results in a multi-segment spline dropped into a sweep object to create the geometry of the bond object. However, using only one sweep object might be efficient but it can only have one colour, making colouring the nucleotides differently impossible. The only solution to this is have a spline and a sweep object per nucleotide. I didn't want to do this because it increases the number of objects Cinema has to handle in the viewport, but there is no other way.

UI change

The first thing I did was add another option to the interface to enable colouring by residue, then in the GenerateBonds() function set a Bool named 'isResidues' to true if this option is selected. This means I can preserve the original method of generating bonds but add extra code to handle each residue separately.

As before, we iterate through all the atoms in the array, but instead of waiting until the end of a chain to create a spline, we create a spline at the end of each residue. Another required change is that when the new bond spline is created, we must pass the residue ID to the function which handles the spline creation. This is so the sweep object added for the residue can be given the correct colour, as determined by the residue ID.

And that raises a question: what colours to use? Traditionally these are seen in the RasMol protein visualisation app, and it seems pointless developing another set of colours. RasMol gives two possible schemes, the 'amino' and 'shapely' schemes, so we'll implement both of those.

Peptide bonds

The problem with the above method is how to include peptide bonds between residues. In the original code, these were all added in a block at the end of the array of points for the bonds within the residues, but we can't do that here. This was the trickiest part of the process. The 'N' atom in the first residue of the chain doesn't take part in a peptide link, and neither does the 'C' atom of the last residue. Worse, to make a link between residues we need the 'C' atom of one residue and the 'N' atom of the next residue. We could, in fact, colour peptide bonds differently to the residue bonds but that seems pointless, so I took the decision to colour the peptide bond the same as the bonds in the residue containing the 'C' atom of the peptide link. That is, if there are two residues in a chain, the first coloured red and the second blue, the peptide bond will take the colour of the first residue (red).

Getting the 'C' atom was done in the same way as the original method, except for adding a test to ignore that atom in the last residue in the chain. Getting the 'N' atom was more problematic. We already have a variable called 'atomCounter' which is an index into the atoms array and which always points to the first atom in a residue. As each residue is processed, this variable is updated to point to the first atom in the next residue, like so:

Updating the atomCounter to point to the next residue.

As long as the current residue is not the last one in the chain, we can use 'atomCounter' to get the first atom in the next residue, then iterate through that residue to find it's 'N' atom. In every PDB file I've looked at, 'N' is always the first atom in the residue, but that isn't guaranteed and given how...erratic these files can be, it's safer not to assume that will always be the case. So we have code like this:

Getting the 'N' atom of the next residue in the chain.

All the work here is done by the 'GetNextN' function, which looks like this:

Function to get the 'N' atom from the next residue.

If the atom is found, the function returns 'true' and with the atom's position in the 'pepN' variable. If both the 'C' and 'N' atoms were found, the two vectors are then added to the points array.

Generating the spline

Finally, the function to create the spline itself and a sweep object is called, and the object returned inserted under the null object that GenerateBonds() will return. When creating the sweep object, all we do is pass the residue ID (a string such as 'GLY' or 'LEU') to a small function that returns the correct colour for that residue, which is then applied to the sweep.

The result

Here is glucagon again with each residue coloured using the 'amino' colour scheme:

Glucagon with bonds coloured by residue.

It would in fact be simple to convert the code so that the splines were always generated per residue, regardless of the colour mode, and I toyed with this idea because it would simplify the code used. However, a quick test shows that while there is subjectively no difference in the time taken to build the molecule and display it on screen, during animation there is a noticeable drop in the frame rate when colouring by residues. This is a consequence of generating many more objects in that mode compared to the single spline and sweep object method used originally. So, tempting though it is to simplify the code by only using the spline-per-residue approach, I decided to stick with the original method developed earlier except when colouring by residue.

We're almost done, but there is one other thing we can do. Take a look at this complex molecule where we colour the bonds by chains and display both bonds and atoms:

Colour by chain with atoms shown.

Although you can see that there are different colours for each chain, it isn't obvious because the atom colours obscure the bond colours. We could hide the atom display altogether so we only see bonds:

Colour by chain with no atoms shown.

That's much better, but for the best results we can turn the atoms back on, but now add an override capability so that the atoms are coloured with their corresponding residue or chain colour like so:

Colour by chain with atoms shown and override colour on.

I think this looks pretty nice. It won't always be wanted, but for maximum distinction between chains or residues, this gives the best results.

Page last updated June 12th 2026

Blog articles

Creating a new plugin: 10 (June 12th 2026)

Creating a new plugin: 9 (June 7th 2026)

Creating a new plugin: 8 (June 4th 2026)

Creating a new plugin: 7 (May 31st 2026)

Creating a new plugin: 6 (May 28th 2026)

Creating a new plugin: 5 (May 26th 2026)

Creating a new plugin: 4 (May 23rd 2026)

Creating a new plugin: 3 (May 22nd 2026)

Creating a new plugin: 2 (May 20th 2026)

Creating a new plugin: 1 (May 17th 2026)

The 'Space' plugins (May 7th 2026)

Creating PBR materials (March 24th 2026)

Data Storage - Then and Now (March 6th 2026)

Old Poser assets (March 2nd 2026)

More Noise, please (January 18th 2026)

Using GIT in VS 2022 (December 26th 2025)

Handling missing plugins (December 12th 2025)

Plugin compatibility with R2026 (November 24th 2025)

Affinity is now free! (November 3rd 2025)

World Creator 2025.1 (October 10th 2025)

So that was Cinema R2026? (September 19th 2025)

How to browse 3D assets (August 24th 2025)

Using Unity assets in Cinema 4D (August 15th 2025)

Plant Factory->Cinema 4D->World Creator (August 12th 2025)

Viewing glTF files (August 9th 2025)

Tessellation part 2 (August 5th 2025)

Shader writing with OSL - 3 (July 11th 2025)

Tessellation (June 23rd 2025)

Creating plants for C4D (June 15th 2025)

Adobe alternatives (May 28th 2025)

Using Graswald assets in C4D (May 7th 2025)

Which Mac for plugin development? (May 3rd 2025)

Why do plugin writers do it? (April 11th 2025)

Updating StarScape (February 26th 2025)

Using Cinema 4D shaders in Redshift (January 31st 2025)

PHP and MySQL (December 19th 2024)

Shader writing with OSL - 2 (November 11th 2024)

Shader writing with OSL (October 29th 2024)

StarScape (September 25th 2024)

Converting plugins from C4D 2024 to 2025 (September 16th 2024)

Cinema 4D 2025 and macOS plugins (September 15th 2025)

© 2021-2025 Microbion. All Rights Reserved.