123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // The graphics rendering engine GLScene http://glscene.org
- //
- unit GXS.Python.Script;
- (*
- Python implementation for the GXScene scripting layer.
- This unit is experimental.
- *)
- interface
- uses
- System.Classes,
- System.SysUtils,
- GXS.XCollection,
- GXS.ScriptBase,
- Stage.Manager,
- Python.Engine; // from ... current version of pyscript
- type
- (* This class only adds manager registration logic to the TPythonEngine
- class to enable the XCollection items (ie. TgxScriptPython) retain it's
- assigned compiler from design to run -time. *)
- TgxPythonEngine = class(TPythonEngine)
- public
- constructor Create(AOnwer: TComponent); override;
- destructor Destroy; override;
- end;
- // Implements Python scripting functionality through the abstracted GLS.ScriptBase
- TgxScriptPython = class(TgxScriptBase)
- private
- FEngine: TgxPythonEngine;
- FEngineName: String;
- FCompiled, FStarted: Boolean;
- protected
- procedure SetEngine(const Value: TgxPythonEngine);
- procedure ReadFromFiler(reader: TReader); override;
- procedure WriteToFiler(writer: TWriter); override;
- procedure Loaded; override;
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- function GetState: TgxScriptState; 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: TgxPythonEngine read FEngine write SetEngine;
- end;
- procedure Register;
- // --------------------------------------------------
- implementation
- // --------------------------------------------------
- // ----------
- // ---------- TgxPythonEngine ----------
- // ----------
- constructor TgxPythonEngine.Create(AOnwer: TComponent);
- begin
- inherited;
- RegisterManager(Self);
- end;
- destructor TgxPythonEngine.Destroy;
- begin
- DeregisterManager(Self);
- inherited;
- end;
- // ---------------
- // --------------- TgxScriptPython ---------------
- // ---------------
- destructor TgxScriptPython.Destroy;
- begin
- Invalidate;
- inherited;
- end;
- procedure TgxScriptPython.Assign(Source: TPersistent);
- begin
- inherited;
- if Source is TgxScriptPython then
- begin
- Engine := TgxScriptPython(Source).Engine;
- end;
- end;
- procedure TgxScriptPython.ReadFromFiler(reader: TReader);
- var
- archiveVersion: Integer;
- begin
- inherited;
- archiveVersion := reader.ReadInteger;
- Assert(archiveVersion = 0);
- with reader do
- begin
- FEngineName := ReadString;
- end;
- end;
- procedure TgxScriptPython.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 TgxScriptPython.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 TgxScriptPython.Notification(AComponent: TComponent;
- Operation: TOperation);
- begin
- if (AComponent = Engine) and (Operation = opRemove) then
- Engine := nil;
- end;
- class function TgxScriptPython.FriendlyName: String;
- begin
- Result := 'TgxScriptPython';
- end;
- class function TgxScriptPython.FriendlyDescription: String;
- begin
- Result := 'Python script';
- end;
- class function TgxScriptPython.ItemCategory: String;
- begin
- Result := '';
- end;
- procedure TgxScriptPython.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 TgxScriptPython.Execute;
- begin
- Compile;
- end;
- procedure TgxScriptPython.Invalidate;
- begin
- FStarted := False;
- FCompiled := False;
- end;
- procedure TgxScriptPython.Start;
- begin
- Compile;
- FStarted := True;
- end;
- procedure TgxScriptPython.Stop;
- begin
- FStarted := False;
- end;
- function TgxScriptPython.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 TgxScriptPython.SetEngine(const Value: TgxPythonEngine);
- begin
- if Value <> FEngine then
- begin
- FEngine := Value;
- Invalidate;
- end;
- end;
- function TgxScriptPython.GetState: TgxScriptState;
- begin
- Result := ssUncompiled;
- if Assigned(Engine) and FCompiled and FStarted then
- Result := ssRunning;
- end;
- procedure Register;
- begin
- RegisterClasses([TgxPythonEngine, TgxScriptPython]);
- RegisterComponents('GXScene Python', [TgxPythonEngine]);
- end;
- // --------------------------------------------------
- initialization
- // --------------------------------------------------
- RegisterXCollectionItemClass(TgxScriptPython);
- // --------------------------------------------------
- finalization
- // --------------------------------------------------
- UnregisterXCollectionItemClass(TgxScriptPython);
- end.
|