Getting Cable Positions with Scripts

It is possible to use Python scripting in order to retrieve the position and orientation of generic cables in your simulation.

To add a script to fetch cable position:

  1. In the Toolbox, select Simulation, then double-click Script. This adds a script extension.
  2. In the resulting dialog box, click the Add button in the Inputs section. An input row is now added.
  3. In the new input's Name field, enter "Cable_Extension".
  4. For the new input's type, select Extension Pointer.
  5. Click Ok at the bottom of the dialog box.
  6. Select the script in the Explorer panel to display its Properties panel.
  7. In the Inputs section of the script's Properties panel, click the Browse button at the end of the field and select the generic cable from the Explorer panel. (Alternatively, you can drag and drop the generic cable from the Explorer panel into the input field.)
  8. In the Code section, enter the following code, then click Apply.
from VxSim import *
import pyvx

def on_add_to_universe(self, universe):
	""" Called when the script is added to the universe.
	Use this method to define specific dynamics actions that must be taken at initialization."""

	# An Inputs for the Script was added and this input was of type Extension Pointer
	# This input is set with the Dynamics Generic Cable
	self.cableExt = self.inputs.Cable_Extension.value

def post_step(self):
	""" Called after the collision detection and after the dynamic solver.
	Use this method to set outputs or get values from dynamics objects."""

	cables = self.cableExt.getOutput('Cables')
	cablesContainer = cables.toContainer()

	# Check if the cable is active.
	# If not active, the number of cables will be zero.
	nbCables = cablesContainer["Number Of Cables"].toInteger()

	if nbCables > 0:
		# Print the position and the orientation (a quaternion)
		ptInfos = cablesContainer['Point Infos']
		infos = ptInfos[0]
		nbPts = infos["Number of Points"].toInteger()

		# Get Points Positions
		positions = infos['Positions'].toVectorVector3()
		# Get Points Orientations
		orientations = infos['Orientations'].toVectorQuaternion()
		for i in range(nbPts):
			pos = positions[i]
			q = orientations[i]
			print("{} Position: [{}, {}, {}] Orientation:[{}, {}, {}, {}]").format(i, pos.x, pos.y, pos.z, q.x, q.y, q.z, q.w)

The information concerning the position and orientation of the cable will display in the output console.

You must repeat this process with a new Script extension for each generic cable whose position you want to fetch.