Simulate a moving cable via python by blender game

Good afternoon people. I’m new to Blender and I’m having trouble drawing a Tube cable. I have the coordinates of a cable, and I made the cable design via bpy with

bpy.data.curves.new (" TubesCurve ", type = ‘CURVE’)
, passing the initial vertices, then transformed into a mesh, to use On the Game Engine.

Now I want to read a coordinate file via python and pass these coordinates to the vertices of the cable, thus simulating the cable in the Engine. The cable should change in real-time simulation, but I do not know how to do that. I tried with:

Create Cable:


from bge import logic as G
import bge
import os
import bpy


filename = 'posRef.csv'
directory = '/home/scholl/Dropbox/Mestrado/Dissertacao/Simulador/'  # <-- if you have linux or osx
#directory = r'C:\Users\MarcosScholl\Dropbox\Mestrado\Dissertacao\Simulador'  # <-- if windows, the r is important
# directory = 'c:/some/directory'  # <-- if windows (alternative)


fullpath = os.path.join(directory, filename)


OBJETO = bpy.data.objects.get("mesh")


if OBJETO:
    print ("TEM") 
else:
    print("NAO TEM")
    with open(fullpath, 'r', newline='') as csvfile:


        # split every row at the comma,, 
        rows = (r.split(',') for r in csvfile if r)
        
        # then take the first 3 values and cast them as float
        #verts = [[print(type(float(i))) for i in r[:3]] for r in rows]
        verts = [[0.0, 0.0, 0.0], [0.0012837268, 0.078448594, 0.99691731], [0.0012837268, 0.078448594, 1.9969172], [0.024165375, 0.31076986, 2.9692872], [0.074502848, 0.61565942, 3.9203436], [0.16748734, 0.98687434, 4.844223], [0.32043213, 1.4143264, 5.7352295], [0.55152732, 1.8829411, 6.5878696], [0.87808329, 2.3716667, 7.3968868], [1.3142259, 2.8528759, 8.1572924], [1.8680933, 3.2924564, 8.864399], [2.5387115, 3.6509092, 9.5138474], [3.3128924, 3.8857543, 10.101632], [4.1626806, 3.9554331, 10.624131], [5.0440435, 3.8246953, 11.078122], [5.8975968, 3.4711421, 11.460806], [6.6521206, 2.8921754, 11.769823],[7.2313609, 2.1111605, 12.003268], [7.5641031, 1.1812084, 12.159702], [7.5967212, 0.18482479, 12.238161], [7.3064365, -0.77211553, 12.238161], [6.7125735, -1.572847, 12.159702], [5.8824725, -2.108067, 12.003268], [4.9287863, -2.2977669, 11.769823], [3.9960041, -2.1122251, 11.460806], [3.2363274, -1.5864451, 11.078122], [2.7782586, -0.82220322, 10.624132], [2.6946852, 0.026331298, 10.101634], [2.9796615, 0.78349513, 9.5138493], [3.5430851, 1.2941526, 8.8644009], [4.2290006, 1.4659655, 8.1572943], [4.856319, 1.297876, 7.3968883]]


    if verts:


        out2 = []
        [out2.extend(list(i)+[0.0]) for i in verts] 


        # first coordinate is present by default, we add the (number of total verts) - 1
        num_points_to_add = len(verts) - 1
        curve = bpy.data.curves.new("TubesCurve", type='CURVE')
        curve.dimensions = '3D'
        curve.fill_mode = 'FULL'
        curve.bevel_depth = 0.015
        curve.bevel_resolution = 10
        
        
        polyline = curve.splines.new(type='POLY')
        polyline.points.add(num_points_to_add)
        
        # a flatterened list of [x1, y1, z1, x2, y2 .... y20, z20]
        print(out2)
        polyline.points.foreach_set('co', out2)


        obj = bpy.data.objects.new("mesh", curve)
        scene = bpy.context.scene
        scene.objects.link(obj)

obj = bpy.data.objects ['Tube']

for vertex in obj.data.vertices:

     vertex.co.x = xx [i]

     vertex.co.y = yy [i]

     vertex.co.z = zz [i]

But the cable does not move, but when I shut down the Engine, it is in the final position.


Could anyone guide me how to do this?
Thank you for all the help you get.

you must use the game object referance to the game mesh
and you can’t use curves in the engine only meshes…

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

scene = own.scene
objectToEdit = own.scene[‘objectsName’]
mesh = objectToEdit.meshes[0]

To simulate a cable I would use rigid bodies and rigid body joints and a armature object using copy rot in its bones and

always--------and---------run armature

basically your cable is a ragdoll

if the cable does not need physics,
I would look into making custom curves using bge.render.drawLine

Thanks for the advice BluePrintRandom.

I do not know how I could do this hard body. Could you help me with a simple code with for example Cable with two joints?
I would really appreciate it, because I’ve been trying to find a solution for two days, but I could not.

ok, first off

ragbones = objects using rigid body physics in the physics tab

rigid body joint constraint is in the constraints tab

after making a chain of rigid bodies

make a armature and in each bone use copy rot targeting one of the rigid body objects

it’s actually quite easy once you get the hang of it,
I think Timster has a tutorial I will did for a minute.

so the rigid body chain is your physics simulation,
and the armature mesh is the visual representation of the chain

edit: