Skip to content

Commit

Permalink
CAP-WIP: solved the output GHcomp charging from script
Browse files Browse the repository at this point in the history
  • Loading branch information
9and3 committed Jan 28, 2024
1 parent f36e559 commit 87928e5
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions GH/PyGH/scriptsyncGH_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, path, path_lock, name):
self.component_on_canvas = True

def run(self):
""" Run the thread. """
self.check_file_change(self.path, self.path_lock)

def check_if_component_on_canvas(self):
Expand All @@ -37,6 +38,7 @@ def check_file_change(self, path, path_lock):
Check if the file has changed on disk.
:param path: The path of the file to check.
:param path_lock: The lock for the path.
"""
with path_lock:
last_modified = os.path.getmtime(path)
Expand All @@ -54,23 +56,6 @@ def check_file_change(self, path, path_lock):
Rhino.RhinoApp.InvokeOnUiThread(System.Action(self.update_component))


def safe_exec(path, globals, locals):
"""
Execute Python3 code safely.
:param path: The path of the file to execute.
:param globals: The globals dictionary.
:param locals: The locals dictionary.
"""
try:
with open(path, 'r') as f:
code = compile(f.read(), path, 'exec')
exec(code, globals, locals)
return locals # return the locals dictionary
except Exception as e:
err_msg = str(e)
return e

# define a custom Exception class
class ScriptSyncError(Exception):
def __init__(self, msg, thread):
Expand All @@ -96,6 +81,23 @@ def __init__(self):
# FIXME: output cannot be set by componentizer, redirect the output of python to
# a custom string output

def safe_exec(self, path, globals, locals):
"""
Execute Python3 code safely.
:param path: The path of the file to execute.
:param globals: The globals dictionary.
:param locals: The locals dictionary.
"""
try:
with open(path, 'r') as f:
code = compile(f.read(), path, 'exec')
exec(code, globals, locals)
return locals # return the locals dictionary
except Exception as e:
err_msg = str(e)
return e

def RunScript(self, x, y):
""" This method is called whenever the component has to be recalculated. """
# check the file is path
Expand All @@ -104,11 +106,6 @@ def RunScript(self, x, y):
if not os.path.exists(self.path):
raise Exception("script-sync::File does not exist")

print(f"script-sync::x value: {x}")



# # FIXME: the thread is created new every time the component is executed, it should not be like this
# get the guid instance of the component
self.thread_name : str = f"script-sync-thread::{ghenv.Component.InstanceGuid}"
if self.thread_name not in [t.name for t in threading.enumerate()]:
Expand All @@ -128,17 +125,13 @@ def RunScript(self, x, y):
for t in threading.enumerate():
print(t.name) #<<<





# we need to add the path of the modules
path_dir = self.path.split("\\")
path_dir = "\\".join(path_dir[:-1])
sys.path.insert(0, path_dir)

# run the script
res = safe_exec(self.path, globals(), locals())
res = self.safe_exec(self.path, globals(), locals())
if isinstance(res, Exception):
err_msg = f"script-sync::Error in the code: {res}"
print(err_msg)
Expand All @@ -152,15 +145,19 @@ def RunScript(self, x, y):
if k in outparam_names:
self._var_output.append(v)


return self._var_output

# FIXME: problem with indexing return
def AfterRunScript(self):
"""
This method is called as soon as the component has finished
its calculation. It is used to load the GHComponent outputs
with the values created in the script.
"""
outparam = ghenv.Component.Params.Output
var_output_dict = dict(zip([p.NickName for p in outparam if p.NickName != "out"], self._var_output))
for idx, outp in enumerate(outparam):
if outp.NickName != "out":
# if outp.NickName == self._var_output[idx]:
ghenv.Component.Params.Output[idx].VolatileData.Clear()
ghenv.Component.Params.Output[idx].AddVolatileData(gh.Kernel.Data.GH_Path(0), 0, self._var_output[idx])
self._var_output = ["None"]
value = var_output_dict.get(outp.NickName, "None")
ghenv.Component.Params.Output[idx].AddVolatileData(gh.Kernel.Data.GH_Path(0), 0, value)
self._var_output = ["None"]

0 comments on commit 87928e5

Please sign in to comment.