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(); } }
Unit target_unit = null; // 対象ユニット public BattleMapEffectScriptConector(string script_path, bool is_hit, int effect_value, Unit action_unit, Unit target_unit) { var script_manager = new ScriptManager(script_path); effect = new Effect(script_manager); action = new Action(); python_script = new PythonScript(script_path); python_script.SetVariable("effect", effect); python_script.SetVariable("action", action); this.is_hit = is_hit; this.effect_value = effect_value; this.action_unit = action_unit; this.target_unit = target_unit; }
protected override void SolveInstance(IGH_DataAccess DA) { bool S = false; DA.GetData(0, ref S); PythonScript script = PythonScript.Create(); script.SetVariable("bakeornot", S ? 1 : 0); script.ExecuteScript("import scriptcontext as sc\nsc.sticky['NOAH_BAKE_INFO'] = bakeornot"); foreach (IGH_DocumentObject obj in ghDoc.Objects) { if (obj is GH_Cluster) { GH_Cluster objCluster = (GH_Cluster)obj; GH_Document clusterDoc = objCluster.Document(""); foreach (IGH_DocumentObject clusterObj in clusterDoc.Objects) { if (clusterObj.ComponentGuid == new Guid("79EF4718-2B5A-4BFF-AB97-76A036598DB9")) { clusterObj.ExpireSolution(true); } } obj.ExpireSolution(true); } else { if (obj.ComponentGuid == new Guid("79EF4718-2B5A-4BFF-AB97-76A036598DB9")) { obj.ExpireSolution(true); } } } }
protected override void SolveInstance(IGH_DataAccess DA) { string str = ""; object obj = null; DA.GetData <string>(0, ref str); DA.GetData <object>(1, ref obj); PythonScript val = PythonScript.Create(); val.SetVariable("V", obj); val.ExecuteScript("import scriptcontext as sc\nsc.sticky['" + str + "'] = V"); }
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 } }
private double EvaluateCell(int cell_index, double cur_val, List <double> n_vals) { //Dictionary<string, double> h_dict; var t = Type.GetType("IronPython.Runtime.List,IronPython"); IList n_list = Activator.CreateInstance(t) as IList; foreach (double d in n_vals) { object cast = d; n_list.Add(cast); } Dictionary <string, double> h_dict = new Dictionary <string, double>(); h_dict.Add("a", 1.0); GH_Dict test = GH_Dict.create("a", cur_val); _py.SetVariable("n_vals", n_list); _py.SetVariable("h_val", cur_val); _py.SetVariable("h_idx", cell_index); _py.SetVariable("h_dict", h_dict); _py.SetVariable("test", test); try { _compiled_py.Execute(_py); } catch (Exception ex) { AddErrorNicely(m_py_output, ex); } //object o = _py.GetVariable("h_val"); GH_Dict out_dict = (GH_Dict)_py.GetVariable("test"); var o = out_dict.val["b"]; return(System.Convert.ToDouble(o)); }
protected override void SafeSolveInstance(IGH_DataAccess DA) { if (_py == null) { DA.SetData(0, "No Python engine available. This component needs Rhino v5"); return; } DA.DisableGapLogic(0); m_py_output.Reset(); var rhdoc = RhinoDoc.ActiveDoc; var prevEnabled = (rhdoc != null) && rhdoc.Views.RedrawEnabled; try { // set output variables to "None" for (int i = HideCodeOutput ? 0 : 1; i < Params.Output.Count; i++) { string varname = Params.Output[i].NickName; _py.SetVariable(varname, null); } // caching variable to keep things as fast as possible bool showing_code_input = CodeInputVisible; // Set all input variables. Even null variables may be used in the // script, so do not attempt to skip these for optimization purposes. // Skip "Code" input parameter // Please pay attention to the input data structure type for (int i = showing_code_input ? 1 : 0; i < Params.Input.Count; i++) { string varname = Params.Input[i].Name; // ksteinfe: changed from Params.Input[i].Nickname object o = _marshal.GetInput(DA, i); _py.SetVariable(varname, o); //_py.SetIntellisenseVariable(varname, o); // ksteinfe: i think this set the Intellisense thingos for all the input variables. we are converting, so Intellisense would just be confusing. } // the "code" string could be embedded in the component itself if (showing_code_input || _compiled_py == null) { string script; if (!showing_code_input) { script = CodeInput; } else { script = null; DA.GetData(0, ref script); } if (string.IsNullOrWhiteSpace(script)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No script to execute"); return; } // ksteinfe - i think we put our hack here. //_py.ExecuteScript(DcPython.Decodes.DecodesAppendedCode.header); //script = script + DcPython.Decodes.DecodesAppendedCode.footer; script = DcPython.Decodes.DecodesAppendedCode.header + script + DcPython.Decodes.DecodesAppendedCode.footer; if (_compiled_py == null || string.Compare(script, _previousRunCode, StringComparison.InvariantCulture) != 0) { if (!(_inDocStringsMode = DocStringUtils.FindApplyDocString(script, this))) { ResetAllDescriptions(); } _compiled_py = _py.Compile(script); _previousRunCode = script; } } if (_compiled_py != null) { _compiled_py.Execute(_py); // Python script completed, attempt to set all of the // output paramerers for (int i = HideCodeOutput ? 0 : 1; i < Params.Output.Count; i++) { string varname = Params.Output[i].NickName; object o = _py.GetVariable(varname); // ksteinfe: this is a tree coming in from gh python out. can we just scan the tree and grab the attributes to store somewhere, then bake each param? _marshal.SetOutput(o, DA, i); } } else { m_py_output.Write("There was a permanent error parsing this script. Please report to [email protected]."); } } catch (Exception ex) { AddErrorNicely(m_py_output, ex); SetFormErrorOrClearIt(DA, m_py_output); throw; } finally { if (rhdoc != null && prevEnabled != rhdoc.Views.RedrawEnabled) { rhdoc.Views.RedrawEnabled = true; } } SetFormErrorOrClearIt(DA, m_py_output); }
protected override void SafeSolveInstance(IGH_DataAccess da) { m_env.DataAccessManager = da; if (m_py == null) { da.SetData(0, "No Python engine available. This component needs Rhino v5"); return; } if (!HiddenOutOutput) { da.DisableGapLogic(0); } m_py_output.Reset(); var rhdoc = RhinoDoc.ActiveDoc; var prevEnabled = (rhdoc != null) && rhdoc.Views.RedrawEnabled; try { // set output variables to "None" for (int i = HiddenOutOutput ? 0 : 1; i < Params.Output.Count; i++) { string varname = Params.Output[i].NickName; m_py.SetVariable(varname, null); } // caching variable to keep things as fast as possible bool showing_code_input = !HiddenCodeInput; // Set all input variables. Even null variables may be used in the // script, so do not attempt to skip these for optimization purposes. // Skip "Code" input parameter // Please pay attention to the input data structure type for (int i = showing_code_input ? 1 : 0; i < Params.Input.Count; i++) { string varname = Params.Input[i].NickName; object o = m_marshal.GetInput(da, i); m_py.SetVariable(varname, o); m_py.SetIntellisenseVariable(varname, o); } // the "code" string could be embedded in the component itself if (showing_code_input || m_compiled_py == null) { string script; if (!showing_code_input) { script = Code; } else { script = null; da.GetData(0, ref script); } if (string.IsNullOrWhiteSpace(script)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No script to execute"); return; } if (m_compiled_py == null || string.Compare(script, m_previousRunCode, StringComparison.InvariantCulture) != 0) { if (!(m_inDocStringsMode = DocStringUtils.FindApplyDocString(script, this))) { ResetAllDescriptions(); } m_compiled_py = m_py.Compile(script); m_previousRunCode = script; } } if (m_compiled_py != null) { string localPath; bool added = AddLocalPath(out localPath); m_compiled_py.Execute(m_py); if (added) { RemoveLocalPath(localPath); } // Python script completed, attempt to set all of the // output paramerers for (int i = HiddenOutOutput ? 0 : 1; i < Params.Output.Count; i++) { string varname = Params.Output[i].NickName; object o = m_py.GetVariable(varname); m_marshal.SetOutput(o, da, i); } } else { m_py_output.Write("There was a permanent error parsing this script. Please report to [email protected]."); } } catch (Exception ex) { AddErrorNicely(m_py_output, ex); SetFormErrorOrClearIt(da, m_py_output); throw; } finally { if (rhdoc != null && prevEnabled != rhdoc.Views.RedrawEnabled) { rhdoc.Views.RedrawEnabled = true; } } SetFormErrorOrClearIt(da, m_py_output); }