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

1 Answer 1

4
$\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$
5
  • $\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
    Commented Apr 2, 2019 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
    Commented Apr 2, 2019 at 13:33
  • $\begingroup$ @TLousky is this working with a multitude of items (list)? In this case bm.edges.new is accepting just a list of 2 items. $\endgroup$
    – Andy Andy
    Commented Jul 4, 2020 at 18:59
  • $\begingroup$ @AndyAndy No, it's designed to connect two specific objects with specific names. But this code is an example you can adapt to run on a list of object pairs. You will need, however, to devise some kind of logic for pairing the objects (perhaps object name based). $\endgroup$
    – TLousky
    Commented Jul 5, 2020 at 7:58
  • $\begingroup$ @TLousky thank you. blender.stackexchange.com/questions/185259/… I have managed to create the spline between cubes however I need to do the hooking manually, I didn't find the method yet to assing by script. $\endgroup$
    – Andy Andy
    Commented Jul 5, 2020 at 10:14

You must log in to answer this question.

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