Destroying vrTimer (Python, Vred 2017.2)

Destroying vrTimer (Python, Vred 2017.2)

Anonymous
Not applicable
596 Views
1 Reply
Message 1 of 2

Destroying vrTimer (Python, Vred 2017.2)

Anonymous
Not applicable



        

Hello,

 

Currently I'm working with async operations within VRED 2017.2.

 

(If I could solve problem 1 differently I might be able to prevent problem 2)

 

  1. If I use Pythons threading module I cannot change the nodes transformationmatrix from another thread, because it seem not be threadsafe (Vred crashes). I haven't found a solution to invoke a method in the mainthread by using something like dotnet baseinstance.Invoke(delegateToMethodInMainThread). QT seems to work quite different anyway... So I circumevent this Problem by creating a wrapper class around my node and only change floating fields (locked) and update the nodes transform with a timer each interval. This works quite well, eventhough this is a rather unelegant way. Now by using vrTimer there is another problem, which is rather a universial:

2. If I create class for instance called Sync. In there I like to store a vrTimer.  Everytime I press the editors 'Run'button there is a new Instance of Sync generated, now what I need is,is to delete all existing vrTimer, in order to prevent multiple vrtimer instances running. Now everytime I change the code I need to restart vred, which is kind of annoying... how can I destroy these instances propely?There is no deconstructor callable, and because its not a python object it seems not to get deleted by garbage collection. See attached example Code:

 

Thank you for any help.

 

 

import gc
import sys

# Test to detect active instances from previous runs

for obj in gc.get_objects():
    if isinstance(obj, vrTimer):
        gc.collect()
        print(obj) # if pressed multiple times it is >1
        print sys.getrefcount(obj) # foreach obj it is 4
        #want to delete all timers from previous runs (del(obj) doesn't work)

 

class Sync:
    def __init__(self):
        self.timer = vrTimer(0.5)
        self.timer.setActive(False)
        self.keyS = vrKey(Key_S)
        self.keyS.connect(self.activate)
        self.keyW = vrKey(Key_W)
        self.keyW.connect(self.deactivate)
        self.timer.connect(Sync.onSync,self)
    def activate(self):
        self.timer.setActive(True)
        print "activated"
    def deactivate(self):
        self.timer.setActive(False)
        print "deactivated"
    def onSync(self):
        print("Update")


sync = Sync()

0 Likes
597 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

I solved it by not using classes in my SYNC part. By declaring my vrTimer as a global variable, each time I press "run", it simply overrides the last vrTimer-Instance. However not being able to use classes without potential risking memoryleaks, makes it really hard to keep control on code which is bigger than 500 lines in my oppinion.

0 Likes