1

I'm a bit of an self-uneducated programming bungler, so please forgive the abysmal coding and general ignorance.

I'm using FreeCAD which has an API to access a point in a CAD model.

class mySnapper:
  def __init__(self):
    self.point = None
  def getPoint(self):
    FreeCADGui.Snapper.getPoint(callback=self.clicked)
  def clicked(self,point,extra):
    self.point = point
    print point
s = mySnapper()
s.getPoint()
print "point outside is:",s.point
print "~~~~"

This gives me the following result:

point outside is:None

~~~~

Vector(320.0, -3414.0, 168.15)

Clearly the class function returns before the the coordinate point is picked (and when the point value is "None").

When the point is picked, then the vector values are printed, but by this time the class function has already returned, so I can't actually use them.

How can I access the value within the "clicked" callback function? Would it be possible to instantiate a global variable which is updated?

I hope this makes sense...

Dave Coventry
  • 129
  • 4
  • 11

1 Answers1

0

Try polling until the value is set:

import time
class mySnapper:
    def __init__(self):
    self.point = None
    def getPoint(self):
        FreeCADGui.Snapper.getPoint(callback=self.clicked)
    def clicked(self,point,extra):
        self.point = point

s = mySnapper()
while not s.getPoint():
    time.sleep(0.01) # wait for 10 ms before retrying
print s.getPoint() # now it should print Vector(320.0, -3414.0, 168.15)
Milad.H
  • 116
  • 4
  • Thanks for your input. No, the program just hangs as it goes into the sleep loop. Previously the program waited for the user to click in the scene. – Dave Coventry Mar 05 '17 at 08:25