Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 666ec78

Browse files
committed
Merge branch 'master' of https://github.com/mcneel/ghpython
2 parents 1ab72f0 + 9484265 commit 666ec78

8 files changed

Lines changed: 236 additions & 53 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ GhPython.suo
99
/references/Grasshopper.xml
1010
/references/RhinoCommon.dll
1111
/references/GH_Util.dll
12-
/references/Grasshopper.pdb
12+
/references/Grasshopper.pdb
13+
/*.suo

Component/PythonEnvironment.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace GhPython.Component
1010
{
1111
public class PythonEnvironment
1212
{
13-
internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScript script)
13+
internal PythonEnvironment(GH_Component component, PythonScript script)
1414
{
1515
Component = component;
1616
Script = script;
@@ -26,7 +26,7 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
2626
}
2727

2828
var intellisenseField = scriptType.GetField("m_intellisense",
29-
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField);
29+
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
3030
if (intellisenseField != null)
3131
{
3232
Intellisense = intellisenseField.GetValue(script);
@@ -56,9 +56,9 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
5656
}
5757
}
5858

59-
var scopeInfo = hostType.GetProperty("Scope", System.Reflection.BindingFlags.NonPublic |
60-
System.Reflection.BindingFlags.GetProperty |
61-
System.Reflection.BindingFlags.Static);
59+
var scopeInfo = hostType.GetProperty("Scope", BindingFlags.NonPublic |
60+
BindingFlags.GetProperty |
61+
BindingFlags.Static);
6262
if (scopeInfo != null)
6363
ScriptScope = scopeInfo.GetValue(null, null);
6464
}
@@ -84,7 +84,7 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
8484

8585
public Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
8686

87-
public void LoadAssembly(System.Reflection.Assembly assembly)
87+
public void LoadAssembly(Assembly assembly)
8888
{
8989
FunctionalityLoad(assembly);
9090

@@ -94,25 +94,31 @@ public void LoadAssembly(System.Reflection.Assembly assembly)
9494
// we really want to get intellisense right away. No matter what
9595
// so, we first make it cache, then add to it
9696

97-
var intellisenseType = Intellisense.GetType();
98-
var m = intellisenseType.GetMethod("GetModuleList", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod);
97+
var intellisense_type = Intellisense.GetType();
98+
var m = intellisense_type.GetMethod("GetModuleList", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod);
9999
m.Invoke(Intellisense, null);
100100

101-
var ex_m_autocomplete_modules = intellisenseType.GetField("m_autocomplete_modules",
101+
var ex_m_autocomplete_modules = intellisense_type.GetField("m_autocomplete_modules",
102102
BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
103103

104104
if (ex_m_autocomplete_modules == null) return;
105105
var list = ex_m_autocomplete_modules.GetValue(Intellisense) as IList;
106106

107107
if (list == null) return;
108+
109+
// add ghpython package
110+
if (!list.Contains("ghpython"))
111+
list.Add("ghpython");
112+
113+
108114
foreach (var namesp in GetToplevelNamespacesForAssembly(assembly))
109115
{
110116
if (!list.Contains(namesp))
111117
list.Add(namesp);
112118
}
113119
}
114120

115-
private void FunctionalityLoad(System.Reflection.Assembly assembly)
121+
private void FunctionalityLoad(Assembly assembly)
116122
{
117123
var runtime = Runtime as dynamic;
118124
runtime.LoadAssembly(assembly);

Component/SafeComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected sealed override void SolveInstance(IGH_DataAccess DA)
7979
SafeSolveInstance(DA);
8080
}
8181

82-
protected abstract void SafeSolveInstance(IGH_DataAccess DA);
82+
protected abstract void SafeSolveInstance(IGH_DataAccess da);
8383

8484
private void GrasshopperDocumentClosed(GH_DocumentServer sender, GH_Document doc)
8585
{

Component/ScriptingAncestorComponent.cs

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace GhPython.Component
1717
{
1818
public abstract class ScriptingAncestorComponent : SafeComponent
1919
{
20+
static bool g_resources_unpacked = false;
2021
private readonly StringList m_py_output = new StringList(); // python output stream is piped here
2122
internal static GrasshopperDocument m_document = new GrasshopperDocument();
2223
internal ComponentIOMarshal m_marshal;
@@ -39,9 +40,81 @@ protected ScriptingAncestorComponent()
3940
{
4041
}
4142

42-
public override void AddRuntimeMessage(GH_RuntimeMessageLevel level, string text)
43+
private static void UnpackScriptResources()
4344
{
44-
base.AddRuntimeMessage(level, text);
45+
if (g_resources_unpacked)
46+
return;
47+
Guid python_plugin_id = new Guid("814d908a-e25c-493d-97e9-ee3861957f49");
48+
var plugin = Rhino.PlugIns.PlugIn.Find(python_plugin_id);
49+
if (plugin == null)
50+
return;
51+
52+
g_resources_unpacked = true;
53+
54+
// Unpack ghcomponents.py to the Python plug directory
55+
string settings_directory = plugin.SettingsDirectory;
56+
string marker_file = Path.Combine(settings_directory, "ghpy_version.txt");
57+
try
58+
{
59+
if (File.Exists(marker_file))
60+
{
61+
string text = File.ReadAllText(marker_file);
62+
Version markedversion = new Version(text);
63+
Version this_version = typeof(ZuiPythonComponent).Assembly.GetName().Version;
64+
if (markedversion == this_version)
65+
{
66+
#if !DEBUG
67+
// everything looks good, bail out
68+
return;
69+
#endif
70+
}
71+
}
72+
}
73+
catch (Exception ex)
74+
{
75+
HostUtils.ExceptionReport(ex);
76+
}
77+
78+
try
79+
{
80+
// if we get to here, we need to unpack the resources
81+
if (!Directory.Exists(settings_directory))
82+
Directory.CreateDirectory(settings_directory);
83+
string ghpython_package_dir = Path.Combine(settings_directory, "lib", "ghpython");
84+
if (Directory.Exists(ghpython_package_dir))
85+
Directory.Delete(ghpython_package_dir, true);
86+
Directory.CreateDirectory(ghpython_package_dir);
87+
System.Reflection.Assembly a = typeof(ZuiPythonComponent).Assembly;
88+
string[] names = a.GetManifestResourceNames();
89+
90+
const string prefix = "GhPython.package.";
91+
92+
foreach (string name in names)
93+
{
94+
if (!name.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))
95+
continue;
96+
97+
Stream resource_stream = a.GetManifestResourceStream(name);
98+
if (resource_stream == null)
99+
continue;
100+
StreamReader stream = new StreamReader(resource_stream);
101+
string s = stream.ReadToEnd();
102+
stream.Close();
103+
string filename = name.Replace(prefix, "");
104+
string path = Path.Combine(ghpython_package_dir, filename);
105+
File.WriteAllText(path, s);
106+
}
107+
108+
// Write the marker file at the very end to ensure that we actually got to this point.
109+
// If an exception occured for some reason like a file was in use, then the plug-in
110+
// will just attempt to unpack the resources next time.
111+
string str = a.GetName().Version.ToString();
112+
File.WriteAllText(marker_file, str);
113+
}
114+
catch (Exception ex)
115+
{
116+
HostUtils.DebugString("Exception while unpacking resources: " + ex.Message);
117+
}
45118
}
46119

47120
protected override void Initialize()
@@ -54,6 +127,7 @@ protected override void Initialize()
54127
m_py = PythonScript.Create();
55128
if (m_py != null)
56129
{
130+
UnpackScriptResources();
57131
SetScriptTransientGlobals();
58132
m_py.Output = m_py_output.Write;
59133
m_py.SetVariable("__name__", "__main__");
@@ -244,16 +318,16 @@ private static Param_String ConstructOutOutputParam()
244318

245319
#region Solving
246320

247-
protected override void SafeSolveInstance(IGH_DataAccess DA)
321+
protected override void SafeSolveInstance(IGH_DataAccess da)
248322
{
249323
if (m_py == null)
250324
{
251-
DA.SetData(0, "No Python engine available. This component needs Rhino v5");
325+
da.SetData(0, "No Python engine available. This component needs Rhino v5");
252326
return;
253327
}
254328

255329
if(!HiddenOutOutput)
256-
DA.DisableGapLogic(0);
330+
da.DisableGapLogic(0);
257331

258332
m_py_output.Reset();
259333

@@ -278,7 +352,7 @@ protected override void SafeSolveInstance(IGH_DataAccess DA)
278352
for (int i = showing_code_input ? 1 : 0; i < Params.Input.Count; i++)
279353
{
280354
string varname = Params.Input[i].NickName;
281-
object o = m_marshal.GetInput(DA, i);
355+
object o = m_marshal.GetInput(da, i);
282356
m_py.SetVariable(varname, o);
283357
m_py.SetIntellisenseVariable(varname, o);
284358
}
@@ -292,7 +366,7 @@ protected override void SafeSolveInstance(IGH_DataAccess DA)
292366
else
293367
{
294368
script = null;
295-
DA.GetData(0, ref script);
369+
da.GetData(0, ref script);
296370
}
297371

298372
if (string.IsNullOrWhiteSpace(script))
@@ -326,7 +400,7 @@ protected override void SafeSolveInstance(IGH_DataAccess DA)
326400
{
327401
string varname = Params.Output[i].NickName;
328402
object o = m_py.GetVariable(varname);
329-
m_marshal.SetOutput(o, DA, i);
403+
m_marshal.SetOutput(o, da, i);
330404
}
331405
}
332406
else
@@ -337,15 +411,15 @@ protected override void SafeSolveInstance(IGH_DataAccess DA)
337411
catch (Exception ex)
338412
{
339413
AddErrorNicely(m_py_output, ex);
340-
SetFormErrorOrClearIt(DA, m_py_output);
414+
SetFormErrorOrClearIt(da, m_py_output);
341415
throw;
342416
}
343417
finally
344418
{
345419
if (rhdoc != null && prevEnabled != rhdoc.Views.RedrawEnabled)
346420
rhdoc.Views.RedrawEnabled = true;
347421
}
348-
SetFormErrorOrClearIt(DA, m_py_output);
422+
SetFormErrorOrClearIt(da, m_py_output);
349423
}
350424

351425
private bool AddLocalPath(out string path)

Component/ZUIPythonComponent.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ namespace GhPython.Component
1111
[Guid("410755B1-224A-4C1E-A407-BF32FB45EA7E")]
1212
public class ZuiPythonComponent : ScriptingAncestorComponent, IGH_VariableParameterComponent
1313
{
14-
public ZuiPythonComponent()
15-
{
16-
}
17-
18-
protected override void AddDefaultInput(GH_Component.GH_InputParamManager pManager)
14+
protected override void AddDefaultInput(GH_InputParamManager pManager)
1915
{
2016
pManager.AddParameter(CreateParameter(GH_ParameterSide.Input, pManager.ParamCount));
2117
pManager.AddParameter(CreateParameter(GH_ParameterSide.Input, pManager.ParamCount));
2218
}
2319

24-
protected override void AddDefaultOutput(GH_Component.GH_OutputParamManager pManager)
20+
protected override void AddDefaultOutput(GH_OutputParamManager pManager)
2521
{
2622
pManager.RegisterParam(CreateParameter(GH_ParameterSide.Output, pManager.ParamCount));
2723
}
@@ -41,45 +37,45 @@ internal override void FixGhInput(Param_ScriptVariable i, bool alsoSetIfNecessar
4137
i.TypeHint = i.Hints[1];
4238
}
4339

44-
static readonly List<IGH_TypeHint> m_hints = new List<IGH_TypeHint>();
40+
static readonly List<IGH_TypeHint> g_hints = new List<IGH_TypeHint>();
4541
static List<IGH_TypeHint> GetHints()
4642
{
47-
lock (m_hints)
43+
lock (g_hints)
4844
{
49-
if (m_hints.Count == 0)
45+
if (g_hints.Count == 0)
5046
{
51-
m_hints.Add(new NoChangeHint());
52-
m_hints.Add(new GhDocGuidHint());
47+
g_hints.Add(new NoChangeHint());
48+
g_hints.Add(new GhDocGuidHint());
5349

54-
m_hints.AddRange(PossibleHints);
50+
g_hints.AddRange(PossibleHints);
5551

56-
m_hints.RemoveAll(t =>
52+
g_hints.RemoveAll(t =>
5753
{
5854
var y = t.GetType();
5955
return (y == typeof (GH_DoubleHint_CS) || y == typeof (GH_StringHint_CS));
6056
});
61-
m_hints.Insert(4, new NewFloatHint());
62-
m_hints.Insert(6, new NewStrHint());
57+
g_hints.Insert(4, new NewFloatHint());
58+
g_hints.Insert(6, new NewStrHint());
6359

64-
m_hints.Add(new GH_BoxHint());
60+
g_hints.Add(new GH_BoxHint());
6561

66-
m_hints.Add(new GH_HintSeparator());
62+
g_hints.Add(new GH_HintSeparator());
6763

68-
m_hints.Add(new GH_LineHint());
69-
m_hints.Add(new GH_CircleHint());
70-
m_hints.Add(new GH_ArcHint());
71-
m_hints.Add(new GH_PolylineHint());
64+
g_hints.Add(new GH_LineHint());
65+
g_hints.Add(new GH_CircleHint());
66+
g_hints.Add(new GH_ArcHint());
67+
g_hints.Add(new GH_PolylineHint());
7268

73-
m_hints.Add(new GH_HintSeparator());
69+
g_hints.Add(new GH_HintSeparator());
7470

75-
m_hints.Add(new GH_CurveHint());
76-
m_hints.Add(new GH_MeshHint());
77-
m_hints.Add(new GH_SurfaceHint());
78-
m_hints.Add(new GH_BrepHint());
79-
m_hints.Add(new GH_GeometryBaseHint());
71+
g_hints.Add(new GH_CurveHint());
72+
g_hints.Add(new GH_MeshHint());
73+
g_hints.Add(new GH_SurfaceHint());
74+
g_hints.Add(new GH_BrepHint());
75+
g_hints.Add(new GH_GeometryBaseHint());
8076
}
8177
}
82-
return m_hints;
78+
return g_hints;
8379
}
8480

8581
#region IGH_VariableParameterComponent implementation

GhPython.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,14 @@
6969
<Private>False</Private>
7070
</Reference>
7171
<Reference Include="Microsoft.CSharp" />
72-
<Reference Include="Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL" />
7372
<Reference Include="RhinoCommon">
7473
<HintPath>references\RhinoCommon.dll</HintPath>
7574
<Private>False</Private>
7675
</Reference>
7776
<Reference Include="System" />
78-
<Reference Include="System.Data" />
7977
<Reference Include="System.Drawing" />
8078
<Reference Include="System.Numerics" />
8179
<Reference Include="System.Windows.Forms" />
82-
<Reference Include="System.Xml" />
8380
</ItemGroup>
8481
<ItemGroup>
8582
<Compile Include="Assemblies\PyghaLoader.cs" />
@@ -156,6 +153,8 @@
156153
</BootstrapperPackage>
157154
</ItemGroup>
158155
<ItemGroup>
156+
<EmbeddedResource Include="package\components.py" />
157+
<EmbeddedResource Include="package\__init__.py" />
159158
<Content Include="Samples\sampleCommon.py" />
160159
<None Include="Samples\helpText.html" />
161160
<Content Include="Samples\sampleScript.py" />

package/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GhPython package
2+
__all__ = ["components"]

0 commit comments

Comments
 (0)