Author Topic: how to get variables from VC ??  (Read 3727 times)

0 Members and 1 Guest are viewing this topic.

nightfreak

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 0
    • View Profile
how to get variables from VC ??
« on: April 19, 2016, 01:04:48 PM »
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:
Code: [Select]
def conditions(testvar):
x = float(testvar)

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

Here is my VC script:
« Last Edit: April 19, 2016, 01:08:23 PM by nightfreak »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: how to get variables from VC ??
« Reply #1 on: April 19, 2016, 04:00:43 PM »
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:
Code: [Select]
def conditions(testvar):
    x = float(testvar)
    vc.callAction("OSD.ShowText","This is test number %s"%x, None)

Group XML (paste into your command tree)
Code: [Select]
<?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

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: how to get variables from VC ??
« Reply #2 on: April 19, 2016, 04:05:06 PM »
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

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 0
    • View Profile
Re: how to get variables from VC ??
« Reply #3 on: April 19, 2016, 04:55:28 PM »
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):
Code: [Select]
tempC=vc.getObject("LastResult","")
print tempC


But this is not working.....I always get "None" instead of the variable I set with ResultsSetVar?!
Code: [Select]
tempC=vc.getObject("var.testvar","")
print tempC

I read the tutorial 10 times but it doesn't make "click" in my head ;-) sorry...
« Last Edit: April 19, 2016, 05:27:16 PM by nightfreak »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: how to get variables from VC ??
« Reply #4 on: April 19, 2016, 05:37:54 PM »
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

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 0
    • View Profile
Re: how to get variables from VC ??
« Reply #5 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

nightfreak

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 0
    • View Profile
Re: how to get variables from VC ??
« Reply #6 on: April 19, 2016, 06:06:09 PM »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: how to get variables from VC ??
« Reply #7 on: April 19, 2016, 06:26:20 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.