Hello,
I have this pretty simple script that prompts the user to draw a line via RhinoScriptSyntax (rs.GetLine) in Rhino and then uses that to Scale a Grasshopper object but when I complete the rhino input portion it sometimes locks the gh interface in a weird way where I can’t click on my canvas components.
I figure this is probably an issue with script context switching, component expiration, or some kind of “rhino command is complete” value being returned but I’m stuck and cannot figure out how to get it to prevent the canvas clicking from bugging out.
Graph Space:
Python Code:
#! python 3
__author__ = "Michael Vollrath"
__version__ = "2024.07.14"
# Made With ♥ In Dallas, TX
ghenv.Component.Description = "Given a user-drawn line, scales an object to the user-specified dimension."
ghenv.Component.Name = "Scale Line"
ghenv.Component.NickName = "SL"
ghenv.Component.Params.Input[0].Name = "Object"
ghenv.Component.Params.Input[0].NickName = "O"
ghenv.Component.Params.Input[0].Description = "Object To Scale"
ghenv.Component.Params.Output[0].Name = "Object Scaled"
ghenv.Component.Params.Output[0].NickName = "OS"
ghenv.Component.Params.Output[0].Description = "Object Scale Result"
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def scale_object():
# Prompt the user to select the object to scale
obj = O
if not obj:
return
# Prompt the user to draw a line
line = rs.GetLine()
if not line:
return
pt1, pt2 = line[0], line[1]
if not pt1 or not pt2:
print("Invalid line drawn. Please draw a valid line.")
return
# Calculate the current distance between the two points
current_distance = rs.Distance(pt1, pt2)
if current_distance == 0:
print("The distance between the points is zero. Please draw a valid line.")
return
# Prompt the user for the desired dimension
desired_dimension_str = rs.GetString("Enter the desired dimension between the two points")
try:
desired_dimension = float(desired_dimension_str)
except ValueError:
print("Invalid input for the desired dimension. Please enter a valid number.")
return
# Calculate the scale factor
scale_factor = desired_dimension / current_distance
# Perform the scaling operation
scaled_obj = rs.ScaleObject(obj, pt1, (scale_factor, scale_factor, scale_factor))
if not scaled_obj:
print("Scale Failed.")
return
if "scaled_obj" not in sc.sticky:
sc.sticky["scaled_obj"] = scaled_obj
res = sc.sticky["scaled_obj"]
print("Object scaled successfully.")
return res
# Execute the function
if __name__ == "__main__":
if E:
OS = scale_object()
elif "scaled_obj" in sc.sticky:
OS = sc.sticky["scaled_obj"]
else:
OS = None
# OS = scale_object()
sc.doc = ghdoc
System Info (click for more details)
Thank you all for your help!
Please see attached .gh:
20240714_RS_Command_Preventing_GH_Mouse_Interaction_01a.gh (658.9 KB)
5 posts - 3 participants
