Ausführen eines Python-Scriptes.

Ausführen eines Python-Scriptes. Dem Aufruf kann der komplette Scripttext oder der Pfad auf eine Scriptdatei übergeben werden.Mehr Informationen zur Python Umgebung finden Sie hier.Wenn sie aus CScript ein Python Script aufrufen, ist es möglich Kontextvariablen in begrenztem Maße automatisch an der aufgerufene Python Script zu vererben und ebenfalls zur Verfügung zu stellen.So können z.B. oft benutze Variablen wie gFrame oder gDocument einfach in Ihrem Python Script verwenden.Alle variablen sind teil des comet Moduls - die Variable gFrame z.B. kann dann im Python Script per comet.gFrame verwendet werden.

Änderungen an den Werten dieser Variablen (wie z.B. eine Änderung an dem Wert gNewValue im Sync-Script eines Platzhalters) werden nicht zurück in ihr CScript übertragen und müssen auf anderen Wegen transportiert werden.

Die folgende Tabelle zeigt welche Datentypen automatisch konvertiert werden und welche Datentypen diese im Python Script haben.
CScript Typ Typstring Python Typ
int "int" int
char * "String" str
String "String" str
float "float" float
int (Wenn bool Werte dargestellt werden) "bool" bool
ItemRef (Dokument) "Document" CDocument
ItemRef (Rahmen) "Frame" CFrame
ItemRef (Tabelle) "Table" CFrame
ItemRef (Buch) "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 Typ Default Beschreibung
Return int  

0 oder Fehlercode

Der Wert ist lediglich eine Aussage darüber, ob das Skript fehlerfrei ausgeführt werden konnte oder nicht. Der Wert ist nicht der Wert eines mglw. im Skript definierten return. Den return-Wert des Skriptes können Sie mit Hilfe der Variablen returnValue ermitteln.

script_or_path String oder char* - Vollständiger Skriptcode (Python) oder -pfad
isPath int 0 Ist der Text Python oder ein Dateipfad?

0 : Text
1 : Pfad
showErrorAlert int 1 Bei Skriptfehlern eine Fehlernachricht zeigen?

0 : Nein
1 : Ja
returnValue int* 0 Variable für den return-Wert des Scripts

0 : Ignorieren
flags int 0 1: In das Logfile schreiben, dass ein Skript ausgeführt wird, einschließlich Skriptname, ID, Ausführungszeit usw..
... ... - Die weiteren Funktionsparameter sind zusätzliche Variablen die als globale Variablen als Member des comet Moduls im Python script verfügbar sind.
Variablen werden entsprechend der obigen Tabelle immer als Tripel übergeben:

  • Die Variable selbst
  • Typstring als String oder char *
  • Variablenname

Parse einen JSON string in CScript durch einen Python Aufruf und hole den Ergebnisstring ab.

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 Beispiel von https://www.w3schools.com/python/python_json.asp
    char * JSONData		= "{ \"name\":\"John\", \"age\":30, \"city\":\"New York\" }";
    char * result		= alloc(8192);
    //Setze einen Wert in der globalen Script Zwischenablage, so dass wir auf den Wert in Python zugreifen können
    set_script_clipboard("JSON", JSONData);
    run_python(script);
    //Hole den Ergebniswert der ebenfalls in der globalen Script Zwischenablage im Python Script gesetzt wurde
    get_script_clipboard("result", result);
    showmessage(result);
    //Allen Speicher der Zwischenablage freigeben.
    clear_script_clipboard();
    return 0;
}

Übertrage einige Variable von CScript an das aufgerufene Python Script und gib Sie ins logfile aus.

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