Jump to content

"vs.GetPt()" - awaiting callback


Recommended Posts

Hello,

Is there a way to let vwx wait for a specific callback before moving on with the rest of the script?

Simple example:

def callback(pt):
  vs.Message(f"selected point is: {pt}")

def main():
	vs.Message("Start task...")
	vs.Wait(1)
	vs.GetPt(callback)
	vs.Wait(1)
	vs.Message("Task finished!")

if __name__ == "__main__":	
	main()

 

In this example above you see the first message and only after the second message i can pick a point.
When i searched for a solution they said i need to set my second message inside the callbackfunction wich would be fine in the simple example but in my real command i want to click on a button inside a custom dialog → close the dialog to select a point → re-open the dialog with some data that was selected.

the problem is that i can only pick a point after the dialog is closed and vwx will crashe.

 

...
def DialogHandler(item, data):
	if item == kSetupDialogC:
		_b = vs.SetLayoutDialogPosition(dialog, 200, 150)

	if item == kselect:
		def callback(pt):
			h = vs.PickObject(pt)
			vint1 = int(vs.GetDimText(h))
			vs.SetEditInteger(dialog, kint1, vint1)

		vs.GetPt(callback)
	
	if item == kOK:
		_b, rundialog.maatlijn = vs.GetEditInteger(dialog, kint1)

	if item == kCancel:
		rundialog.cancel = True
        return item
...

 

Can someone explain how this is done or if it isn't possible at all.
Thanks a lot!

Link to comment

@spettitt I think i found a solution..

At least for the simple example it works so I hope I can use this at larger scale as wel but the idea i had was to use 2 global toglles and run the main code 2 times.

See code snippet below:

toglle_1 = False
toglle_2 = True


def callback(pt):
	global toglle_1
	global toglle_2
	
	vs.Message(f"selected point is: {pt}")
	toglle_1 = True
	toglle_2 = False
	main()

def main():
	# Code before the callback:
	# callback will run def main() again.
	# inside the callback toggle_2 -> False / toglle_1 -> True
	if toglle_2:
		vs.AlertCritical("Start task...", "")
		
		vs.GetPt(callback)	
	
	# First runtime toglle_1 == False so return gets called.
	# Second runtime toglle_1 == True so nothing happens in the if statement. Run continues...
	if not toglle_1:
		vs.Message("Callback wordt geactiveerd...")
		return
	
	# Only gets here in the second run off the script.
	vs.AlertCritical("Task finished!", "")


if __name__ == "__main__":	
	main()

 

I hope you can use this as well but it would make much more sense if vwx would would run the callback before running the rest off the script..

Link to comment
4 hours ago, koenr said:

@spettitt I think i found a solution..

At least for the simple example it works so I hope I can use this at larger scale as wel but the idea i had was to use 2 global toglles and run the main code 2 times.

See code snippet below:

toglle_1 = False
toglle_2 = True


def callback(pt):
	global toglle_1
	global toglle_2
	
	vs.Message(f"selected point is: {pt}")
	toglle_1 = True
	toglle_2 = False
	main()

def main():
	# Code before the callback:
	# callback will run def main() again.
	# inside the callback toggle_2 -> False / toglle_1 -> True
	if toglle_2:
		vs.AlertCritical("Start task...", "")
		
		vs.GetPt(callback)	
	
	# First runtime toglle_1 == False so return gets called.
	# Second runtime toglle_1 == True so nothing happens in the if statement. Run continues...
	if not toglle_1:
		vs.Message("Callback wordt geactiveerd...")
		return
	
	# Only gets here in the second run off the script.
	vs.AlertCritical("Task finished!", "")


if __name__ == "__main__":	
	main()

 

I hope you can use this as well but it would make much more sense if vwx would would run the callback before running the rest off the script..

Thank you, I'll put this on the list to test this week, appreciate that. I may be wrong, but I think the issue with the script progressing is more to do with how Python execution works rather than anything in the VW interfacing. I'm sure I've seen this mentioned somewhere before but can't immediately locate it.

Link to comment
On 6/15/2024 at 3:26 PM, spettitt said:

Thank you, I'll put this on the list to test this week, appreciate that. I may be wrong, but I think the issue with the script progressing is more to do with how Python execution works rather than anything in the VW interfacing. I'm sure I've seen this mentioned somewhere before but can't immediately locate it.

I may be wrong but i'm pretty sure python just excecutes everything one after another.
If you want to do multiple things together you must use multiple threads or use async function.
It may be that this is the case for the "vs.GetPt()" under the hood but i can't check that.

If you run just a normal callback you can see that it will execute the way it should.

def callback():
	vs.AlertCritical("Callback is running...", "")

def foo(func):
	vs.AlertCritical("Function running...", "")
	func()
	vs.AlertCritical("Function ending...", "")

def main():
	vs.AlertCritical("Starting...", "")
	foo(callback)
	vs.AlertCritical("Ending...", "")
	
if __name__ == "__main__":
	main()


I also tried to fix my bigger script but with no luck..
Since my solution ends the main function and needs to run it again i can't use it inside a for loop.
I hope there will come a better way of doing this..

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...