Pythons.Script.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit Pythons.Script;
  5. (*
  6. Python implementation for the GLScene scripting layer.
  7. This unit is experimental.
  8. *)
  9. interface
  10. uses
  11. System.Classes,
  12. System.SysUtils,
  13. GLS.XCollection,
  14. GLS.ScriptBase,
  15. GLS.Manager,
  16. Python.Engine;
  17. type
  18. (* This class only adds manager registration logic to the TPythonEngine
  19. class to enable the XCollection items (ie. TGLScriptPython) retain it's
  20. assigned compiler from design to run -time. *)
  21. TGLPythonEngine = class(TPythonEngine)
  22. public
  23. constructor Create(AOnwer: TComponent); override;
  24. destructor Destroy; override;
  25. end;
  26. // Implements Python scripting functionality through the abstracted GLS.ScriptBase
  27. TGLScriptPython = class(TGLScriptBase)
  28. private
  29. FEngine: TGLPythonEngine;
  30. FEngineName: String;
  31. FCompiled, FStarted: Boolean;
  32. protected
  33. procedure SetEngine(const Value: TGLPythonEngine);
  34. procedure ReadFromFiler(reader: TReader); override;
  35. procedure WriteToFiler(writer: TWriter); override;
  36. procedure Loaded; override;
  37. procedure Notification(AComponent: TComponent;
  38. Operation: TOperation); override;
  39. function GetState: TGLScriptState; override;
  40. public
  41. destructor Destroy; override;
  42. procedure Assign(Source: TPersistent); override;
  43. procedure Compile; override;
  44. procedure Start; override;
  45. procedure Stop; override;
  46. procedure Execute; override;
  47. procedure Invalidate; override;
  48. function Call(aName: String; aParams: array of Variant): Variant; override;
  49. class function FriendlyName: String; override;
  50. class function FriendlyDescription: String; override;
  51. class function ItemCategory: String; override;
  52. published
  53. property Engine: TGLPythonEngine read FEngine write SetEngine;
  54. end;
  55. procedure Register;
  56. // --------------------------------------------------
  57. implementation
  58. // --------------------------------------------------
  59. // ----------
  60. // ---------- TGLPythonEngine ----------
  61. // ----------
  62. constructor TGLPythonEngine.Create(AOnwer: TComponent);
  63. begin
  64. inherited;
  65. RegisterManager(Self);
  66. end;
  67. destructor TGLPythonEngine.Destroy;
  68. begin
  69. DeregisterManager(Self);
  70. inherited;
  71. end;
  72. // ---------------
  73. // --------------- TGLScriptPython ---------------
  74. // ---------------
  75. destructor TGLScriptPython.Destroy;
  76. begin
  77. Invalidate;
  78. inherited;
  79. end;
  80. procedure TGLScriptPython.Assign(Source: TPersistent);
  81. begin
  82. inherited;
  83. if Source is TGLScriptPython then
  84. begin
  85. Engine := TGLScriptPython(Source).Engine;
  86. end;
  87. end;
  88. procedure TGLScriptPython.ReadFromFiler(reader: TReader);
  89. var
  90. archiveVersion: Integer;
  91. begin
  92. inherited;
  93. archiveVersion := reader.ReadInteger;
  94. Assert(archiveVersion = 0);
  95. with reader do
  96. begin
  97. FEngineName := ReadString;
  98. end;
  99. end;
  100. procedure TGLScriptPython.WriteToFiler(writer: TWriter);
  101. begin
  102. inherited;
  103. writer.WriteInteger(0); // archiveVersion
  104. with writer do
  105. begin
  106. if Assigned(FEngine) then
  107. WriteString(FEngine.GetNamePath)
  108. else
  109. WriteString('');
  110. end;
  111. end;
  112. procedure TGLScriptPython.Loaded;
  113. var
  114. temp: TComponent;
  115. begin
  116. inherited;
  117. if FEngineName <> '' then
  118. begin
  119. temp := FindManager(TGLPythonEngine, FEngineName);
  120. if Assigned(temp) then
  121. Engine := TGLPythonEngine(temp);
  122. FEngineName := '';
  123. end;
  124. end;
  125. procedure TGLScriptPython.Notification(AComponent: TComponent;
  126. Operation: TOperation);
  127. begin
  128. if (AComponent = Engine) and (Operation = opRemove) then
  129. Engine := nil;
  130. end;
  131. class function TGLScriptPython.FriendlyName: String;
  132. begin
  133. Result := 'TGLScriptPython';
  134. end;
  135. class function TGLScriptPython.FriendlyDescription: String;
  136. begin
  137. Result := 'Python script';
  138. end;
  139. class function TGLScriptPython.ItemCategory: String;
  140. begin
  141. Result := '';
  142. end;
  143. procedure TGLScriptPython.Compile;
  144. begin
  145. Invalidate;
  146. if Assigned(Engine) then
  147. begin
  148. Engine.ExecStrings(Text);
  149. FCompiled := True;
  150. FStarted := False;
  151. end
  152. else
  153. raise Exception.Create('No engine assigned!');
  154. end;
  155. procedure TGLScriptPython.Execute;
  156. begin
  157. Compile;
  158. end;
  159. procedure TGLScriptPython.Invalidate;
  160. begin
  161. FStarted := False;
  162. FCompiled := False;
  163. end;
  164. procedure TGLScriptPython.Start;
  165. begin
  166. Compile;
  167. FStarted := True;
  168. end;
  169. procedure TGLScriptPython.Stop;
  170. begin
  171. FStarted := False;
  172. end;
  173. function TGLScriptPython.Call(aName: String; aParams: array of Variant)
  174. : Variant;
  175. var
  176. func: PPyObject;
  177. args: array of TVarRec;
  178. i: Integer;
  179. begin
  180. if State = ssUncompiled then
  181. Start;
  182. if State = ssRunning then
  183. begin
  184. func := Engine.FindFunction('__main__', aName);
  185. if Assigned(func) then
  186. if Length(aParams) > 0 then
  187. begin
  188. SetLength(args, Length(aParams));
  189. for i := 0 to Length(aParams) - 1 do
  190. begin
  191. args[i].VType := vtVariant;
  192. args[i].VVariant := @aParams[i];
  193. end;
  194. Result := Engine.EvalFunction(func, args);
  195. end
  196. else
  197. Result := Engine.EvalFunctionNoArgs(func);
  198. end;
  199. end;
  200. procedure TGLScriptPython.SetEngine(const Value: TGLPythonEngine);
  201. begin
  202. if Value <> FEngine then
  203. begin
  204. FEngine := Value;
  205. Invalidate;
  206. end;
  207. end;
  208. function TGLScriptPython.GetState: TGLScriptState;
  209. begin
  210. Result := ssUncompiled;
  211. if Assigned(Engine) and FCompiled and FStarted then
  212. Result := ssRunning;
  213. end;
  214. procedure Register;
  215. begin
  216. RegisterClasses([TGLPythonEngine, TGLScriptPython]);
  217. RegisterComponents('GLScene Python', [TGLPythonEngine]);
  218. end;
  219. // --------------------------------------------------
  220. initialization
  221. // --------------------------------------------------
  222. RegisterXCollectionItemClass(TGLScriptPython);
  223. // --------------------------------------------------
  224. finalization
  225. // --------------------------------------------------
  226. UnregisterXCollectionItemClass(TGLScriptPython);
  227. end.