protected override void Initialize()
        {
            base.Initialize();

            if (Doc != null)
            {
                Doc.SolutionEnd += OnDocSolutionEnd;
            }

            m_py = PythonScript.Create();
            if (m_py != null)
            {
                SetScriptTransientGlobals();
                m_py.Output = m_py_output.Write;
                m_py.SetVariable("__name__", "__main__");
                m_env = new PythonEnvironment(this, m_py);

                m_py.SetVariable(PARENT_ENVIRONMENT_NAME, m_env);
                m_py.SetIntellisenseVariable(PARENT_ENVIRONMENT_NAME, m_env);

                m_py.ContextId = 2;                                      // 2 is Grasshopper

                m_env.LoadAssembly(typeof(GH_Component).Assembly);       //add Grasshopper.dll reference
                m_env.LoadAssembly(typeof(ZuiPythonComponent).Assembly); //add GHPython.dll reference

                UnpackScriptResources();

                m_env.AddGhPythonPackage();
            }
        }
예제 #2
0
 public override GH_LoadingInstruction PriorityLoad()
 {
     try
       {
     _gha_environment = CreateEnvironment();
     LoadExternalPythonAssemblies();
     SetupMainDirListener();
       }
       catch (Exception ex)
       {
     Global_Proc.ASSERT(Guid.Empty, "GhPython last exception boundary", ex);
       }
       return GH_LoadingInstruction.Proceed;
 }
        protected override void Initialize()
        {
            base.Initialize();

            if (Doc != null)
            {
                Doc.SolutionEnd += OnDocSolutionEnd;
            }

            // ksteinfe
            _py = PythonScript.Create();
            if (_py != null)
            {
                SetScriptTransientGlobals();
                _py.Output = m_py_output.Write;
                _py.SetVariable("__name__", "__main__");
                _env = new PythonEnvironment(this, _py);
                _py.SetVariable(PARENT_ENVIRONMENT_NAME, _env);
                _py.SetIntellisenseVariable(PARENT_ENVIRONMENT_NAME, _env);

                _py.ContextId = 2; // 2 is Grasshopper
            }
        }
예제 #4
0
        private static bool LoadOneAddon(PythonEnvironment p, string path)
        {
            var engine = p.Engine as dynamic;
              var runtime = engine.Runtime;
              var ops = engine.Operations;

              if(ExternalUnsafe.HasZoneIdetifier(path))
              {
            if (MessageBox.Show("A FILE IS BLOCKED: \n\n" +
              path +
              "\n\nBefore being able to use it, this file should be unblocked.\n" +
              "Do you want attempt to unblock it now?", "GhPython Assembly is blocked",
              MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2,
              MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
            {
              if (!ExternalUnsafe.Unblock(path))
              {
            Global_Proc.ASSERT(Guid.Empty, "You need to unblock \"" + path + "\" manually."); return false;
              }
            }
              }

              AssemblyName assName;
              try
              {
            assName = AssemblyName.GetAssemblyName(path);
              }
              catch (SecurityException ex)
              {
            Global_Proc.ASSERT(Guid.Empty, "You have not enough rights to load \"" + path + "\".", ex); return false;
              }
              catch (BadImageFormatException ex)
              {
            Global_Proc.ASSERT(Guid.Empty, "The assembly \"" + path + "\" has a bad format.", ex); return false;
              }
              catch (FileLoadException ex)
              {
            Global_Proc.ASSERT(Guid.Empty, "The assembly \"" + path + "\" is found but cannot be loaded.", ex); return false;
              }

              var appDomain = AppDomain.CreateDomain("Temp");
              try
              {
            var farAssembly = appDomain.CreateInstanceFrom(path, "DLRCachedCode");
              }
              catch (FileLoadException ex)
              {
            int error = Marshal.GetHRForException(ex);
            if (error == -0x40131515)
            {
              Global_Proc.ASSERT(Guid.Empty, "The file \"" + path + "\" is blocked.", ex); return false;
            }
            Global_Proc.ASSERT(Guid.Empty, "The assembly at \"" + path + "\" cannot be loaded.", ex); return false;
              }
              catch (BadImageFormatException ex)
              {
            Global_Proc.ASSERT(Guid.Empty, "This assembly \"" + path + "\" has a bad inner format.", ex); return false;
              }
              catch (TypeLoadException ex)
              {
            Global_Proc.ASSERT(Guid.Empty, "\"" + path + "\" is not a valid Python assembly. Please remove it.", ex); return false;
              }
              catch (MissingMethodException ex)
              {
            Global_Proc.ASSERT(Guid.Empty, "This assembly \"" + path + "\" is ruined.", ex); return false;
              }
              finally
              {
            if (appDomain != null)
            {
              AppDomain.Unload(appDomain);
            }
              }

              Assembly assembly = Assembly.LoadFile(path);
              var cachedCode = assembly.GetType("DLRCachedCode", false, false);

              if (cachedCode == null) return false; //should be already ruled out

              dynamic info = cachedCode.InvokeMember("GetScriptCodeInfo",
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
            null, null, new object[0]);
              string[] modules = info.GetValue(2)[0];

              runtime.LoadAssembly(assembly);

              bool toReturn = false;

              foreach (var module in modules)
              {
            var statement = "import " + module;
            p.Script.ExecuteScript(statement);

            dynamic ns = p.Script.GetVariable(module);

            var dict = ns.Get__dict__();
            var vars = dict.Keys;

            foreach (var v in vars)
            {
              var text = v as string;

              if (text == null) continue;
              object o = dict[text];

              if (o == null) continue;
              Type type = o.GetType();

              if (type.FullName != "IronPython.Runtime.Types.PythonType") continue;

              var basesEnum = (IEnumerable)type.InvokeMember(
            "get_BaseTypes",
            BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, o, null);
              if (basesEnum == null) continue;

              foreach (var baseObj in basesEnum)
              {
            Type finalSystemType = (Type)baseObj.GetType().InvokeMember(
              "get_FinalSystemType",
              BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, baseObj, null);
            if (finalSystemType == null) continue;

            if (typeof(IGH_Component).IsAssignableFrom(finalSystemType))
            {
              var instance = Instantiate(ops as object, o);
              var proxy = new PythonInstantiatorProxy(instance, o, ops as object, path);

              toReturn |= Grasshopper.GH_InstanceServer.ComponentServer.AddProxy(proxy);
            }
              }
            }

            p.Script.ExecuteScript("del " + module);
              }
              return toReturn;
        }
        protected override void Initialize()
        {
            base.Initialize();

              if (Doc != null)
            Doc.SolutionEnd += OnDocSolutionEnd;

              m_py = PythonScript.Create();
              if (m_py != null)
              {
            //UnpackScriptResources();
            SetScriptTransientGlobals();
            m_py.Output = m_py_output.Write;
            m_py.SetVariable("__name__", "__main__");
            m_env = new PythonEnvironment(this, m_py);

            m_py.SetVariable(PARENT_ENVIRONMENT_NAME, m_env);
            m_py.SetIntellisenseVariable(PARENT_ENVIRONMENT_NAME, m_env);

            m_py.ContextId = 2; // 2 is Grasshopper

            m_env.LoadAssembly(typeof(GH_Component).Assembly); //add Grasshopper.dll reference
              }
        }
        protected override void Initialize()
        {
            base.Initialize();

              if (Doc != null)
            Doc.SolutionEnd += OnDocSolutionEnd;

              _py = PythonScript.Create();
              if (_py != null)
              {
            SetScriptTransientGlobals();
            _py.Output = m_py_output.Write;
            _py.SetVariable("__name__", "__main__");
            _env = new PythonEnvironment(this, _py);
            _py.SetVariable(PARENT_ENVIRONMENT_NAME, _env);
            _py.SetIntellisenseVariable(PARENT_ENVIRONMENT_NAME, _env);

            _py.ContextId = 2; // 2 is Grasshopper
              }
        }