123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- //
- // The multimedia graphics platform GLScene https://github.com/glscene
- //
- unit Pythons.Script;
- (*
- Python implementation for the GLScene scripting layer.
- This unit is experimental.
- *)
- interface
- uses
- System.Classes,
- System.SysUtils,
- GLS.XCollection,
- GLS.ScriptBase,
- GLS.Manager,
- Python.Engine;
- type
- (* This class only adds manager registration logic to the TPythonEngine
- class to enable the XCollection items (ie. TGLScriptPython) retain it's
- assigned compiler from design to run -time. *)
- TGLPythonEngine = class(TPythonEngine)
- public
- constructor Create(AOnwer: TComponent); override;
- destructor Destroy; override;
- end;
- // Implements Python scripting functionality through the abstracted GLS.ScriptBase
- TGLScriptPython = class(TGLScriptBase)
- private
- FEngine: TGLPythonEngine;
- FEngineName: String;
- FCompiled, FStarted: Boolean;
- protected
- procedure SetEngine(const Value: TGLPythonEngine);
- procedure ReadFromFiler(reader: TReader); override;
- procedure WriteToFiler(writer: TWriter); override;
- procedure Loaded; override;
- procedure Notification(AComponent: TComponent;
- Operation: TOperation); override;
- function GetState: TGLScriptState; override;
- public
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure Compile; override;
- procedure Start; override;
- procedure Stop; override;
- procedure Execute; override;
- procedure Invalidate; override;
- function Call(aName: String; aParams: array of Variant): Variant; override;
- class function FriendlyName: String; override;
- class function FriendlyDescription: String; override;
- class function ItemCategory: String; override;
- published
- property Engine: TGLPythonEngine read FEngine write SetEngine;
- end;
- procedure Register;
- // --------------------------------------------------
- implementation
- // --------------------------------------------------
- // ----------
- // ---------- TGLPythonEngine ----------
- // ----------
- constructor TGLPythonEngine.Create(AOnwer: TComponent);
- begin
- inherited;
- RegisterManager(Self);
- end;
- destructor TGLPythonEngine.Destroy;
- begin
- DeregisterManager(Self);
- inherited;
- end;
- // ---------------
- // --------------- TGLScriptPython ---------------
- // ---------------
- destructor TGLScriptPython.Destroy;
- begin
- Invalidate;
- inherited;
- end;
- procedure TGLScriptPython.Assign(Source: TPersistent);
- begin
- inherited;
- if Source is TGLScriptPython then
- begin
- Engine := TGLScriptPython(Source).Engine;
- end;
- end;
- procedure TGLScriptPython.ReadFromFiler(reader: TReader);
- var
- archiveVersion: Integer;
- begin
- inherited;
- archiveVersion := reader.ReadInteger;
- Assert(archiveVersion = 0);
- with reader do
- begin
- FEngineName := ReadString;
- end;
- end;
- procedure TGLScriptPython.WriteToFiler(writer: TWriter);
- begin
- inherited;
- writer.WriteInteger(0); // archiveVersion
- with writer do
- begin
- if Assigned(FEngine) then
- WriteString(FEngine.GetNamePath)
- else
- WriteString('');
- end;
- end;
- procedure TGLScriptPython.Loaded;
- var
- temp: TComponent;
- begin
- inherited;
- if FEngineName <> '' then
- begin
- temp := FindManager(TGLPythonEngine, FEngineName);
- if Assigned(temp) then
- Engine := TGLPythonEngine(temp);
- FEngineName := '';
- end;
- end;
- procedure TGLScriptPython.Notification(AComponent: TComponent;
- Operation: TOperation);
- begin
- if (AComponent = Engine) and (Operation = opRemove) then
- Engine := nil;
- end;
- class function TGLScriptPython.FriendlyName: String;
- begin
- Result := 'TGLScriptPython';
- end;
- class function TGLScriptPython.FriendlyDescription: String;
- begin
- Result := 'Python script';
- end;
- class function TGLScriptPython.ItemCategory: String;
- begin
- Result := '';
- end;
- procedure TGLScriptPython.Compile;
- begin
- Invalidate;
- if Assigned(Engine) then
- begin
- Engine.ExecStrings(Text);
- FCompiled := True;
- FStarted := False;
- end
- else
- raise Exception.Create('No engine assigned!');
- end;
- procedure TGLScriptPython.Execute;
- begin
- Compile;
- end;
- procedure TGLScriptPython.Invalidate;
- begin
- FStarted := False;
- FCompiled := False;
- end;
- procedure TGLScriptPython.Start;
- begin
- Compile;
- FStarted := True;
- end;
- procedure TGLScriptPython.Stop;
- begin
- FStarted := False;
- end;
- function TGLScriptPython.Call(aName: String; aParams: array of Variant)
- : Variant;
- var
- func: PPyObject;
- args: array of TVarRec;
- i: Integer;
- begin
- if State = ssUncompiled then
- Start;
- if State = ssRunning then
- begin
- func := Engine.FindFunction('__main__', aName);
- if Assigned(func) then
- if Length(aParams) > 0 then
- begin
- SetLength(args, Length(aParams));
- for i := 0 to Length(aParams) - 1 do
- begin
- args[i].VType := vtVariant;
- args[i].VVariant := @aParams[i];
- end;
- Result := Engine.EvalFunction(func, args);
- end
- else
- Result := Engine.EvalFunctionNoArgs(func);
- end;
- end;
- procedure TGLScriptPython.SetEngine(const Value: TGLPythonEngine);
- begin
- if Value <> FEngine then
- begin
- FEngine := Value;
- Invalidate;
- end;
- end;
- function TGLScriptPython.GetState: TGLScriptState;
- begin
- Result := ssUncompiled;
- if Assigned(Engine) and FCompiled and FStarted then
- Result := ssRunning;
- end;
- procedure Register;
- begin
- RegisterClasses([TGLPythonEngine, TGLScriptPython]);
- RegisterComponents('GLScene Python', [TGLPythonEngine]);
- end;
- // --------------------------------------------------
- initialization
- // --------------------------------------------------
- RegisterXCollectionItemClass(TGLScriptPython);
- // --------------------------------------------------
- finalization
- // --------------------------------------------------
- UnregisterXCollectionItemClass(TGLScriptPython);
- end.
|