OMC::Javascript Class Reference

Detailed Description

An interface for providing access to javascript classes and functions.

The Javascript interface can be used to call javascript methods in the application. It can also be used to maintain a JS environment form within Python.

Note: In order to maintain the interface between Javascript and Python, the Javascript code and its resulting objects remain persistent within Javascript when loaded through Python. This allows for a consistent level of persistence between Python and Javascript.

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
#Original Javascript
js_script = "var getFrame = function(){ return frame.current(); }; var setFrame = function(setValue){ frame.setCurrent( setValue ); };"
#Evaluated Javascript -- this will not contain the evaluated JS code.
wrapped_js = js.load_string(js_script)
#Use the getFrame function to return the current frame within the environment, via Javascript.
current_frame = wrapped_js["getFrame"].call()
print( "Current Frame : %s"%current_frame )
next_frame = current_frame + 1
print( "Setting Next Frame : %s"%next_frame )
wrapped_js["setFrame"].call( [next_frame] )

Public Member Functions

QVariant evaluate (const QString &javascript)
 Evaluate a javascript string and return the results. More...
 
QVariant eval (const QString &javascript)
 Alias to evaluate() method. More...
 
QVariant call_function (const QString &javascript, const QString &functionName, const QVariant &arguments, const QVariant &selfValue=QVariant())
 Evaluate and call a javascript function and return the results. More...
 
OMC::JavascriptObjectload_file (const QString &path, bool delayedInitialization=false)
 Loads a javascript object from a file at the given path. More...
 
OMC::JavascriptObjectload_string (const QString &script, bool delayedInitialization=false)
 Loads a javascript object from an evaluated string. More...
 
QStringList available_scripts () const
 Returns a list of all javascripts available to the application in the script paths. More...
 
OMC::JavascriptObject [] eval_available_script (const QString &availableScriptName, const QString &functionName, const QVariant &arguments=QVariant(), const QVariant &selfValue=QVariant()) const
 Evaluates a function with its arguments inside a given available script. More...
 
QStringList available_script_functions (const QString &scriptName) const
 Returns a list of all the functions available for calling inside a given available script. More...
 

Member Function Documentation

◆ available_script_functions()

QStringList OMC::Javascript::available_script_functions ( const QString &  scriptName) const

Returns a list of all the functions available for calling inside a given available script.

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
list_scripts = js.available_scripts()
for script_name in list_scripts:
print( "%s: "%( script_name ) )
try:
script_funcs = js.available_script_functions( script_name )
for func_name in script_funcs:
print( " %s "%( func_name ) )
except:
print( " Failed to load script" )
Returns
List of available function names.

◆ available_scripts()

QStringList OMC::Javascript::available_scripts ( ) const

Returns a list of all javascripts available to the application in the script paths.

List Available Scripts

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
list_scripts = js.available_scripts()
for script_filename in list_scripts:
print( script_filename )
Returns
List of available scripts names.

◆ call_function()

QVariant OMC::Javascript::call_function ( const QString &  javascript,
const QString &  functionName,
const QVariant &  arguments,
const QVariant &  selfValue = QVariant() 
)

Evaluate and call a javascript function and return the results.

Evaluates and calls a function in Javascript and returns the resulting object. May take arguments as a single element or as an array.

Parameters
javascript- A string providing javascript with the available function to be called.
functionName- The function to call within the provided javascript.
arguments- A list of arguments with which the javascript function will be called.
selfValue- Optional, a script value that will be bound to the this context of the function.

Call a Test Function with Arguments and Context

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
my_javascript = """
function test( arg1, arg2, arg3 ){
MessageLog.trace( arg1 + " : " + arg2 + " : " + arg3 );
MessageLog.trace( this.context );
return true;
}
"""
results = js.call_function( my_javascript, "test", ["Value1", "Value2", "Value3"], { "context" : "Hello World" } )
Returns
The object returned when evaluating the javascript string – this value can be null.

◆ eval()

QVariant OMC::Javascript::eval ( const QString &  javascript)

Alias to evaluate() method.

Returns
The object returned when evaluating the javascript string – this value can be null.

◆ eval_available_script()

OMC::JavascriptObject [] OMC::Javascript::eval_available_script ( const QString &  availableScriptName,
const QString &  functionName,
const QVariant &  arguments = QVariant(),
const QVariant &  selfValue = QVariant() 
) const

Evaluates a function with its arguments inside a given available script.

Call an Available Script

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
#Call the TB_orderNetworkDown function in the packaged TB_orderNetworkDown.js script.
js.eval_available_script( "TB_orderNetworkDown.js", "TB_orderNetworkDown", []
Returns
The object returned when evaluating the javascript string – this value can be null.

◆ evaluate()

QVariant OMC::Javascript::evaluate ( const QString &  javascript)

Evaluate a javascript string and return the results.

Evaluates a string in javascript and returns the resulting object. To evaluate with arguments, use call_function() method instead.

Evaluate Javascript - inspect it in Python

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
eval_script = """val = { "object001" : 1, "object002" : { "value": 2 }, "object003" : ["A","B","C","D","E"], "object004": frame.current() } """
results = js.evaluate( eval_script )
print( "object001: %s"%results["object001"] )
print( "object002: %s"%results["object002"]["value"] )
print( "object003: " )
for x,n in enumerate(results["object003"]):
print( " %s: %s"%(x,n) )
print( "object004: %s"%results["object004"] )
Returns
The object returned when evaluating the javascript string – this value can be null.

◆ load_file()

OMC::JavascriptObject* OMC::Javascript::load_file ( const QString &  path,
bool  delayedInitialization = false 
)

Loads a javascript object from a file at the given path.

Evaluates and loads the javascript object as an accessible object in Python. This object and the functions and objects within it will remain persistent as long as the Python object is maintained.

See load_file() for an example.

Returns
The OMC::JavascriptObject representing the loaded file.

◆ load_string()

OMC::JavascriptObject* OMC::Javascript::load_string ( const QString &  script,
bool  delayedInitialization = false 
)

Loads a javascript object from an evaluated string.

Evaluates and loads the javascript object as an accessible object in Python. This object and the functions and objects within it will remain persistent as long as the Python object is maintained.

The Javasscript is only evaluated once – and the internal objects will retain any changes made to them.
A Demo of the Javascript Persistence

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
js_string = """
globalObject = 0;
function increment()
{
globalObject++;
return globalObject;
}
"""
js_obj = js.load_string( js_string )
print( js_obj["increment"].call() ) #1
print( js_obj["increment"].call() ) #2
print( js_obj["increment"].call() ) #3
print( js_obj["increment"].call() ) #4
#Forcefully re-evaluate the Javascript.
js_obj.reload()
#Note that the calls restart from 1 again.
print( js_obj["increment"].call() ) #1
print( js_obj["increment"].call() ) #2
print( js_obj["increment"].call() ) #3
print( js_obj["increment"].call() ) #4
Returns
The OMC::JavascriptObject representing the loaded string.
Inheritance diagram for OMC::Javascript:
Collaboration diagram for OMC::Javascript: