Hi!
I’m new to Visual components, and I’m trying to create a Python add-on and I’m having 2 problems.
One of them is, when clicking in test_button, i doesn’t call the printJoints function.
The other is when I ran the code i get an error: NameError: global name 'delay' is not defined (delay in the last line) I’ve seen delay used in the onRun function in multiple places on the forum, but i can’t figure out why doesn’t it work in my case.
Thanks for your help!
from vcCommand import *
from vcHelpers.Application import *
from vcCollisionDetector import *
DIALOG_NAME = 'Robot Placement'
ROBOT_TAB_NAME = 'Robots'
COLLISION_TAB_NAME = 'Collision detectors'
app = getApplication()
cmd = getCommand()
simu = getSimulation()
def printJoints(arg):
robot = app.TeachContext.ActiveRobot
if not robot:
print('No robot selected.')
return
print("\n---{} Joint Angles---".format(robot.Name))
for property in enumerate(robot.Properties):
p = property[1]
if p.Name[0] == 'J' and p.Name[1] in ['0','1','2','3','4','5','6','7','8','9'] :
print(" Joint {} : {:.2f} degrees".format(p.Name, p.Value))
print("----------------------------")
def OnStart():
robots = []
areas = []
for component in app.Components:
if component.Category == 'Robots':
robots.append(component)
elif 'area' in component.Name:
areas.append(component)
dlg = getDialog(DIALOG_NAME)
robot1_name = dlg.addProperty('{}::Select robot:'.format(ROBOT_TAB_NAME) ,VC_STRING, None, VC_PROPERTY_STEP)
robot1_name.StepValues = [r.Name for r in robots]
area1_name = dlg.addProperty('{}::Select placement area:'.format(ROBOT_TAB_NAME), VC_STRING, None, VC_PROPERTY_STEP)
area1_name.StepValues = [a.Name for a in areas]
robot2_name = dlg.addProperty('{}::Select other robot:'.format(ROBOT_TAB_NAME) ,VC_STRING, None, VC_PROPERTY_STEP)
robot2_name.StepValues = [r.Name for r in robots]
area2_name = dlg.addProperty('{}::Select other placement area:'.format(ROBOT_TAB_NAME), VC_STRING, None, VC_PROPERTY_STEP)
area2_name.StepValues = [a.Name for a in areas]
robot_ready = dlg.addProperty('{}::Ready'.format(ROBOT_TAB_NAME), VC_BOOLEAN, None)
robot_ready.Value = False
test_button = dlg.addProperty('TEST::Button', VC_BUTTON, printJoints)
dlg.showTabs()
dlg.show()
if robot_ready.Value:
detector = simu.newCollisionDetector()
robot1 = next((c for c in app.Components if c.Name == robot1_name.Value), None)
robot2 = next((c for c in app.Components if c.Name == robot2_name.Value), None)
print(robot1)
print(robot2)
if robot1 and robot2:
detector.NodeListA = [(robot1, VC_NODELIST_INCLUDE, VC_NODELIST_TREE )]
detector.NodeListB = [(robot2, VC_NODELIST_INCLUDE, VC_NODELIST_TREE )]
if not simu.IsRunning:
simu.reset()
simu.run()
while simu.IsRunning:
delay(0.1)
hit = detector.testOneCollision(0.1)
if hit:
print('{} and {} collided at {}'.format(detector.getHitFeatureA(), detector.getHitFeatureB(), simu.SimTime))
else:
print("Error: One or both robots could not be found.")
else:
delay(5.0)