Run a Python script.
Run a Python script. The function accepts a script as a string or the path to a script file.More information about the Python environment can be found here.When calling a Python script from CScript, it is possible to inherit context variables to the Python script in limited fashion so these variables are automatically available in the Python script aswell.This way you can use commonly used variables like gFrame or gDocument in your Python script.All variables are part of the comet module - e.g. the variable gFrame can then be accessed using comet.gFrame in the Python script.
Changes to the values of these variables (e.g. changing the value of gNewValue in the sync script of a placeholder) are not transferred back to CScript and have to be moved differently.
The following table shows which data types get automatically converted and their resulting type in the Python script.CScript Typ | Typstring | Python Typ |
int | "int" | int |
char * | "String" | str |
String | "String" | str |
float | "float" | float |
int (When representing bool values) | "bool" | bool |
ItemRef (Document) | "Document" | CDocument |
ItemRef (Frame) | "Frame" | CFrame |
ItemRef (Table) | "Table" | CFrame |
ItemRef (Book) | "Book" | CFrame |
Element | "Element" | CElement |
IDType | "IDType" | CIDType |
Parameter | "Parameter" | CParameter |
Planning | "Planning" | CPlanning |
Product | "Product" | CProduct |
Publication | "Publication" | CPublication |
PublicationType | "PublicationType" | CPublicationType |
Rect | "Rect" | CRect |
WorkflowStatus | "WorkflowStatus" | CWorkflowStatus |
List | "List" | list[int] |
List64 | "List64" | list[int] |
FloatList | "FloatList" | list[float] |
StringList | "StringList" | list[str] |
ItemList | "FrameList" | list[CFrame] |
ItemList | "TableList" | list[CTable] |
ElementList | "ElementList" | list[CElement] |
IDTypeList | "IDTypeList" | list[CIDType] |
ParameterList | "ParameterList" | list[CParameter] |
PlanningList | "PlanningList" | list[CPlanning] |
ProductList | "ProductList" | list[CProduct] |
PublicationList | "PublicationList" | list[CPublication] |
PublicationTypeList | "PublicationTypeList" | list[CPublicationType] |
RectList | "RectList" | list[CRect] |
WorkflowStatusList | "WorkflowStatusList" | list[CWorkflowStatus] |
KeyValues | "KeyValues" | dict[str, str] |
Name | Type | Default | Description |
Return | int | 0 or ErrorCode This value indicates wether the script execution was successful or not. This value is not the return value of the script itself! To retrieve the return value of the script, use returnValue. |
|
script_or_path | String or char* | - | Complete script text or complete path to a Python file |
isPath | int | 0 | Does script_or_path contains the Python text or a file path? 0 : Script text 1 : Path |
showErrorAlert | int | 1 | Show error alert on script errors? 0 : No 1 : Yes |
returnValue | int* | 0 | Variable for the return value of the script 0: Ignore |
flags | int | 0 | 1: Write to log that a script is being executed, including script name, ID, execution time etc… |
... | ... | - | Further function parameters are additional variables which are made available in the Python script
as global variables as members of the comet module. Variablen are passed as triples according to the table above:
|
Parse JSON from CScript using a call to run_python and fetching a value back.
char * script ="#!py import json import comet def main(): JSONStr = comet.getScriptClipboard('JSON') JSONData = json.loads(JSONStr) data = JSONData['name'] comet.setScriptClipboard('result', data) return 0 "; int main () { //JSON example from https://www.w3schools.com/python/python_json.asp char * JSONData = "{ \"name\":\"John\", \"age\":30, \"city\":\"New York\" }"; char * result = alloc(8192); //Set a value to the global script clipboard so it can be accessed in Python set_script_clipboard("JSON", JSONData); run_python(script); //Get the result value back from the global script clipboard which has been set by Python get_script_clipboard("result", result); showmessage(result); //Free any memory we used from the clipboard clear_script_clipboard(); return 0; }
Pass some variables from CScript to the called Python script and output them to the logfile.
char * script = "#!py import comet def main(): comet.wlog(f'comet.cInt: {str(comet.cInt)}'); comet.wlog(f'comet.cFloat: {str(comet.cFloat)}'); comet.wlog(f'comet.cStr1: {str(comet.cStr1)}'); comet.wlog(f'comet.cStr2: {str(comet.cStr2)}'); comet.wlog(f'comet.cRect.getValues(): {str(comet.cRect.getValues())}'); comet.wlog(f'comet.cListInt: {str(comet.cListInt)}'); comet.wlog(f'comet.cListFloat: {str(comet.cListFloat)}'); comet.wlog(f'comet.cListString: {str(comet.cListString)}');
comet.wlog('comet.cListRect:') for entry in comet.cListRect: comet.wlog(f'\t{str(entry.getValues())}');
comet.wlog(f'comet.cKeyValues: {str(comet.cKeyValues)}');
return 0 "; int main() { int cInt = 9999; float cFloat = 444.555; char * cStr1 = alloc(8192); String cStr2 = string::alloc(); Rect cRect = rect::alloc(10.0, 10.0, 20.0, 20.0); List cListInt = list::alloc(); FloatList cListFloat = floatlist::alloc(); StringList cListString = stringlist::alloc(); RectList cListRect = rectlist::alloc(); KeyValues cKeyValues = keyvalues::alloc();
strcpy(cStr1, "ABCD"); string::set(cStr2, "EFGH");
list::append(cListInt, 2); list::append(cListInt, 4); list::append(cListInt, 6); list::append(cListInt, 8);
floatlist::append(cListFloat, 2.1); floatlist::append(cListFloat, 4.2); floatlist::append(cListFloat, 6.3); floatlist::append(cListFloat, 8.4);
stringlist::append(cListString, "AAA"); stringlist::append(cListString, "BBB"); stringlist::append(cListString, "CCC"); stringlist::append(cListString, "DDD");
rectlist::append_by_value(cListRect, 10.0, 10.0, 20.0, 20.0); rectlist::append_by_value(cListRect, 20.0, 20.0, 30.0, 30.0); rectlist::append_by_value(cListRect, 30.0, 30.0, 40.0, 40.0);
keyvalues::put(cKeyValues, "AAA", "BBB"); keyvalues::put(cKeyValues, "CCC", "DDD"); keyvalues::put(cKeyValues, "EEE", "FFF");
run_python( script, 0, 1, 0, 0, cInt, "int", "cInt", cFloat, "float", "cFloat", cStr1, "String", "cStr1", cStr2, "String", "cStr2", cRect, "Rect", "cRect", cListInt, "List", "cListInt", cListFloat, "FloatList", "cListFloat", cListString, "StringList", "cListString", cListRect, "RectList", "cListRect", cKeyValues, "KeyValues", "cKeyValues" );
return 0; }
Alphabetic index HTML hierarchy of classes or Java