Python add-on help

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)

Hi traamcazs,

Could you explain what are you trying to do in the first place? Did you also use AI to generate the code?

br,
Lefa

Hi,

I’m working on a project, where they want to replace 6-axis Universal robots with 7-axis Kassow robots, while keeping as much as they can. There are already predefined paths for the robots from Process Simulate, but we need to figure out where to put the new robots.
In PS there is a tool called ‘Smart Place’ where you can select an area in which the robot will be placed at multiple location, and at every location it will evaluate whether the robot could reach all the point without collisions and changing configuration.
I want to recreate something similar in VC using a python add-on. This code is mainly just testing different part of it.
What I ultimately want is an add-on, which after selecting the robot, with the corresponding areas (I used Wire Rectangles from the Basic Shape category) I want to place the robot, it runs the simulation in the different robot positions and outputs a csv file with the joint angles every 0,1 s and whether it collided with the other robot or the world.

Tom

Please try to make test_button a global variable.