Python API

Python API

ir
Advocate Advocate
1,153 Views
3 Replies
Message 1 of 4

Python API

ir
Advocate
Advocate

After VRED has launched, is there any way of adding functions to a python class which is defined in a script being read while VRED launches?  

0 Likes
1,154 Views
3 Replies
Replies (3)
Message 2 of 4

Bob.Bon2000
Collaborator
Collaborator

If I understand correctly...

 

You can go to settings>script> this is script that gets executed when vred starts. You can add line at the end something like:

 

execfile("pathFIle")

 

If I'm not wrong... try that? 

0 Likes
Message 3 of 4

ir
Advocate
Advocate

let me elaborate, what we are looking for, is to do something like this:

 

after launching VRED, do the following:

 

class MyClass(object):
    pass

 

def func(self):
    print "func"

 

MyClass.myMethod = func

 

=> this is standard Python and works fine!

 

now, what we want:

 

# MyClass is some class defined in a  different script which is read by VRED at startup time 

 

def func(self):
    print "func"

 

MyClass.myMethod = func

 

calling func() now, throws the error message=> NameError: name 'MyClass' is not defined

 

So, the question, how to add a method to MyClass after launching VRED?

0 Likes
Message 4 of 4

Bob.Bon2000
Collaborator
Collaborator

Hey

 

Very nasty and very long shot but try :

 

at the end of script options in preferences:

 

script = '''

class MyClass(object):
    pass

 

def func(self):
    print "func"

 

MyClass.myMethod = func

'''

executePython(script)

 

See if this works.

 

By the way, did the execfile("path") not work for you? 

 

0 Likes