# How to add a Line between two moving Objects with Python?

I'd like to add a line between two moving objects with python. For example if I have two cylinders...

I want that light blue line between the two 3D object (it could be a cylinder also). It has tree parts:

1. and 2. there is one shorter line which comes from the side of the cylinders (not from the top or the bottom), and the other ends which are marked with red arrows, 'point' to the other object.

From the top view:

1. The third part is connecting these points. And if I move these objects, or rotate them, I'd like to have the line with the same features:

I would like to add the line with a Python script. I've tried many things, but couldn't solve the problem yet.

• Any particular reason you want to do this with a script? Seems like it would be simple enough to solve manually if its not something you need to repeat or automate many times... – TLousky Apr 2 '19 at 9:16
• Unfortunately, I need to do it from python script – aura Apr 2 '19 at 9:19
• – batFINGER Apr 2 '19 at 10:33

This script will generate a simple single edge mesh, whose verts are hook connected to two pre-existing objects (the cylinders C1 and C2 in this example), which will also work simple animations of these two objects:

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])

bm.to_mesh(m)

o = bpy.data.objects.new( 'connector', m )

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

# Select vertex
bpy.ops.object.mode_set(mode='OBJECT')
o.data.vertices[i].select = True
bpy.ops.object.mode_set(mode='EDIT')

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

bpy.ops.object.mode_set(mode='OBJECT')
o.data.vertices[i].select = False

o.modifiers.new('Skin', 'SKIN')
bpy.ops.object.select_all( action = 'DESELECT' )

• Thank you! It helps a lot. Do you have an idea, how to solve that, if you rotate the cylindet, the 'line' is always connected to the point of the cylinder which is the closest to the other object, but i want it to connect to a point of the cylinders 'side', not to the top or the bottom – aura Apr 2 '19 at 13:20
• You can set the origin of each cylinder prior to running the script. The hook will connect to the origin. – TLousky Apr 2 '19 at 13:33