Create a linear track using Python Script

Please, I need some help to create this linear track. Could someone tell me what I did wrong on my Python Script? Script in While loop doesn’t work. If I put while loop out of OnRun(), application cannot run anything, seems like it’s overloaded.

app = getApplication()
comp = getComponent()
part1 = app.findComponent(“Part1”)
l_mover = comp.findNode(“LinearMover”)
link1 = comp.findNode(“Link_1”)
J1 = l_mover.J1
cable_1 = comp.findFeature(“Block”)
cable_2 = comp.findFeature(“Block_1”)

def OnRun():
while True:
cable_1.Length = str(483 - J1/2)
cable_2.Length = str (282 + J1/2)


This seems to be quite rough to debug without seeing the output or having the vcmx. In these cases I recommend to check with print that what is J1 inside the loop and so on. It also seems somewhat odd to me that cable_1.Length requires a type of “string” instead of type of “float, double, real or similar”

Could you also explain your workflow on “why Python Script”? I get the idea if you want to learn it, but there might be a native way of doing this without Python Script.


Thank you so much for reply. Yes, cable_1.Length requires string type. I use Python Script because I cannot create this linear track using native ways. Could you help me solve this problem? I attached my vcmx file below, thank you again :slight_smile:
practice.vcmx (221.0 KB)


Try to put

J1 = l_mover.J1
Inside the while loop, you store the variable J1 once outside the while loop in a variable and never update it, thus it stays as a constant. Then play the simulation and interact with your linear track.



You saved my life. Thank you so so so so much :smiley: