This question is related to this thread How to add a Line between two moving Objects with Python?

But the answer in this thread uses an edge, while I want it to be a cylinder (just like a bond connecting two atoms).

How do I extend from that answer to achieve this? Thank you.


1 Answer 1


enter image description here

All you need to change in the previous answer so that it would generate cylinder connectors instead of boxes, is setting the Skin modifier to smooth shading, and adding a subdivision surface modifier on top of it.

import bpy, bmesh
C = bpy.context

# Reference two cylinder objects
c1 = bpy.data.objects['C1']
c2 = bpy.data.objects['C2']

# Create new connector mesh and mesh object and link to scene
m = bpy.data.meshes.new('connector')

bm = bmesh.new()
v1 = bm.verts.new( c1.location )
v2 = bm.verts.new( c2.location )
e  = bm.edges.new([v1,v2])


o = bpy.data.objects.new( 'connector', m )
C.scene.collection.objects.link( o )

# Hook connector vertices to respective cylinders
for i, cyl in enumerate([ c1, c2 ]):
    bpy.ops.object.select_all( action = 'DESELECT' )
    C.view_layer.objects.active = o # Set connector as active

    # Select vertex
    o.data.vertices[i].select = True    

    bpy.ops.object.hook_add_selob() # Hook to cylinder

    o.data.vertices[i].select = False 

m = o.modifiers.new('Skin', 'SKIN')

## New bit starts here
m.use_smooth_shade = True

m = o.modifiers.new('Subsurf', 'SUBSURF' )
m.levels = 2
m.render_levels = 2

## End of new bit
bpy.ops.object.select_all( action = 'DESELECT' )
  • $\begingroup$ Thanks so much the answer. I have a few questions. Why do I need to set both level and render_levels to 2? And, are there any resources related to this that you'd like to recommend? Blender Python resources seem so scattered for me and involve only very basic stuff. I'm pretty familiar with Python and I don't want to deal with the GUI Blender. $\endgroup$ Commented Apr 16, 2019 at 20:03
  • $\begingroup$ Also, how do I customize the bond (color, diameter)? $\endgroup$ Commented Apr 16, 2019 at 20:19
  • $\begingroup$ In my experience, the best way to learn blender scripting is to understand the app and its capabilities in depth. You can use tooltips to figure out how to script manual actions, then read the documentation and answers here in Stack to find more efficient ways to do stuff than simple automation of manual actions (but first it's always best to simply get things to work). There are also nice scripting courses in CG Cookie and lots of blogs and resources if you look for 'em. $\endgroup$
    – TLousky
    Commented Apr 16, 2019 at 20:43
  • $\begingroup$ To change the color you need to apply a material to the bond object, then change it properties (see answers related to this topic here in Stack). To change the diameter you need to change the skin_vertices radius as shown here: blender.stackexchange.com/a/1593/15861 $\endgroup$
    – TLousky
    Commented Apr 16, 2019 at 20:46
  • $\begingroup$ After rendering, z-fighting occurs. I wonder which part of the above code causes the overlapping faces that led to z-fighting. This is the related question: blender.stackexchange.com/questions/140607/… $\endgroup$ Commented May 15, 2019 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .