News:

There is now a ChangeLog page on the wiki
New developments are added at the bottom of the page.

Main Menu

how to get variables from VC ??

Started by nightfreak, April 19, 2016, 01:04:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nightfreak

after 5h of reading forum posts and trying different ways, i give up :-/

Anyone knows how to get variables from VC to my python testscript? - can't get it running :-/

Here is my code:

def conditions(testvar):
x = float(testvar)

vc.callAction("OSD.ShowText","This is test number %s"%x, None)


Here is my VC script:

jitterjames

Your example might be a tad oversimplified so I don't know if this will really help you accomplish what you want but it should be a good start.

Notice that your function accepts a variable.  So you just need to call the function and pass it the variable from VC when you call it.  Generally it's a good idea to load your python functions only once at startup instead of reloading them everytime you need them.  You can do this using the event VC.Loaded

Python:

def conditions(testvar):
    x = float(testvar)
    vc.callAction("OSD.ShowText","This is test number %s"%x, None)


Group XML (paste into your command tree)
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.1.7-->
<commandGroup open="True" name="py vars" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="251" name="python define functions on startup" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecFile</cmdType>
      <params>
        <param>c:\users\voxcommando\desktop\test.py</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <event>VC.Loaded</event>
  </command>
  <command id="234" name="test my python function" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>conditions(5.5)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>test my python function</phrase>
  </command>
</commandGroup>

jitterjames

In case you need help using the XML I posted: http://voxcommando.com/mediawiki/index.php?title=XML_on_the_forum

In case it can save a few hours, I recommend starting with the wiki before the forum:
http://voxcommando.com/mediawiki/index.php?title=Python

nightfreak

#3
thank you very much, James!
Now It gets a little bit clearer, but I am wondering if there is no simple way to get a variable from ResultsSetVar into the Python script.

For Example (working):

tempC=vc.getObject("LastResult","")
print tempC



But this is not working.....I always get "None" instead of the variable I set with ResultsSetVar?!

tempC=vc.getObject("var.testvar","")
print tempC


I read the tutorial 10 times but it doesn't make "click" in my head ;-) sorry...

jitterjames

Read http://voxcommando.com/mediawiki/index.php?title=Python#getObject

to see which objects you can "get".  It lists "lastresult" there but there is nothing there about "var.xxx"

In version 2.2.1.7 I added "evalvars" as an object which lets you get anything that would be in {curly brackets} in VC, but I think the important question is: "Why do you need to access variables that were defined in VC and why can't you define them a different way, or pass them to your python functions?"  It's not so easy to answer your question when I don't know what you are trying to do.

Note that it is easy to set a python variable from VC if that is what you want to do, but generally it's better practice to pass your variables to python in the call to a function.  It will make debugging a hell of a lot easier.

nightfreak

because I'm not good in writing Code, so it would be "the simple way" to get variables from VC.
Anyway...I try to solve it the way you described in your example

thanks again

nightfreak

oh, evalvars is exactly what I want, i tried it before but it did not work.
Now I have seen that my VC is too old and doesn't support this function.
updated now --> works like a charm :-)

jitterjames

Quote from: nightfreak on April 19, 2016, 05:51:27 PM
because I'm not good in writing Code, so it would be "the simple way" to get variables from VC.
Anyway...I try to solve it the way you described in your example

thanks again

If you can give me more of a real world example of what you are trying to do I will try to suggest a better (but still as simple as possible) way.