@@ -18,8 +18,10 @@ namespace GhPython.Component
1818 public abstract class ScriptingAncestorComponent : SafeComponent
1919 {
2020 static bool g_resources_unpacked = false ;
21+ internal static GrasshopperDocument g_document = new GrasshopperDocument ( ) ;
22+
2123 private readonly StringList m_py_output = new StringList ( ) ; // python output stream is piped here
22- internal static GrasshopperDocument m_document = new GrasshopperDocument ( ) ;
24+
2325 internal ComponentIOMarshal m_marshal ;
2426 protected PythonScript m_py ;
2527 private PythonCompiledCode m_compiled_py ;
@@ -80,7 +82,7 @@ private static void UnpackScriptResources()
8082 // if we get to here, we need to unpack the resources
8183 if ( ! Directory . Exists ( settings_directory ) )
8284 Directory . CreateDirectory ( settings_directory ) ;
83- string ghpython_package_dir = Path . Combine ( settings_directory , "lib" , "ghpython " ) ;
85+ string ghpython_package_dir = Path . Combine ( settings_directory , "lib" , "gh_python " ) ;
8486 if ( Directory . Exists ( ghpython_package_dir ) )
8587 Directory . Delete ( ghpython_package_dir , true ) ;
8688 Directory . CreateDirectory ( ghpython_package_dir ) ;
@@ -127,7 +129,6 @@ protected override void Initialize()
127129 m_py = PythonScript . Create ( ) ;
128130 if ( m_py != null )
129131 {
130- //UnpackScriptResources();
131132 SetScriptTransientGlobals ( ) ;
132133 m_py . Output = m_py_output . Write ;
133134 m_py . SetVariable ( "__name__" , "__main__" ) ;
@@ -139,6 +140,11 @@ protected override void Initialize()
139140 m_py . ContextId = 2 ; // 2 is Grasshopper
140141
141142 m_env . LoadAssembly ( typeof ( GH_Component ) . Assembly ) ; //add Grasshopper.dll reference
143+ m_env . LoadAssembly ( typeof ( ZuiPythonComponent ) . Assembly ) ; //add GHPython.dll reference
144+
145+ UnpackScriptResources ( ) ;
146+
147+ m_env . AddGhPythonPackage ( ) ;
142148 }
143149 }
144150 #endregion
@@ -320,6 +326,8 @@ private static Param_String ConstructOutOutputParam()
320326
321327 protected override void SafeSolveInstance ( IGH_DataAccess da )
322328 {
329+ m_env . DataAccessManager = da ;
330+
323331 if ( m_py == null )
324332 {
325333 da . SetData ( 0 , "No Python engine available. This component needs Rhino v5" ) ;
@@ -333,7 +341,7 @@ protected override void SafeSolveInstance(IGH_DataAccess da)
333341
334342 var rhdoc = RhinoDoc . ActiveDoc ;
335343 var prevEnabled = ( rhdoc != null ) && rhdoc . Views . RedrawEnabled ;
336-
344+
337345 try
338346 {
339347 // set output variables to "None"
@@ -424,7 +432,7 @@ protected override void SafeSolveInstance(IGH_DataAccess da)
424432
425433 private bool AddLocalPath ( out string path )
426434 {
427- string probe_location = m_document . Path ;
435+ string probe_location = g_document . Path ;
428436 if ( string . IsNullOrWhiteSpace ( probe_location ) ) { path = null ; return false ; }
429437
430438 probe_location = Path . GetDirectoryName ( probe_location ) ;
@@ -533,8 +541,24 @@ protected virtual void SetScriptTransientGlobals()
533541
534542 private void OnDocSolutionEnd ( object sender , GH_SolutionEventArgs e )
535543 {
536- if ( m_document != null )
537- m_document . Objects . Clear ( ) ;
544+ if ( g_document != null )
545+ g_document . Objects . Clear ( ) ;
546+ }
547+
548+ internal void RemoveAllVariables ( )
549+ {
550+ var variables = m_py . GetVariableNames ( ) . ToArray ( ) ;
551+
552+ for ( int i = 0 ; i < variables . Length ; i ++ )
553+ {
554+ var variable = variables [ i ] ;
555+
556+ if ( string . IsNullOrWhiteSpace ( variable ) ) continue ;
557+ if ( variable . StartsWith ( "_" ) ) continue ;
558+ if ( variable == DOCUMENT_NAME || variable == PARENT_ENVIRONMENT_NAME ) continue ;
559+
560+ m_py . RemoveVariable ( variables [ i ] ) ;
561+ }
538562 }
539563
540564 #endregion
@@ -815,5 +839,9 @@ protected override void Dispose(bool disposing)
815839 }
816840 }
817841 #endregion
842+
843+ #region Properties Access
844+ internal PythonScript PythonScript { get { return m_py ; } }
845+ #endregion
818846 }
819847}
0 commit comments