1
$\begingroup$

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

enter image description here

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:

enter image description here

  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:

enter image description here

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

$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – TLousky Apr 2 at 9:16
  • $\begingroup$ Unfortunately, I need to do it from python script $\endgroup$ – aura Apr 2 at 9:19
  • 1
    $\begingroup$ Related blender.stackexchange.com/questions/110177/… $\endgroup$ – batFINGER Apr 2 at 10:33
1
$\begingroup$

enter image description here

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 )
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' )
    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' )
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – aura Apr 2 at 13:20
  • 2
    $\begingroup$ You can set the origin of each cylinder prior to running the script. The hook will connect to the origin. $\endgroup$ – TLousky Apr 2 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.