Hi,
I’ve written a Python script as a command in the Rhino Python Editor.
When I run it in debug mode it runs great.
but if I run it from a Rhino command it runs twice.
Interestingly at the end of the first run it prints the ‘Finished…’ message, but not at the end of the seconf time round.
I have exited, rebooted and tried all permutations of -zBldID, or '-’ or just ‘-’ or ‘_’ or nothing.
I have also changed the position of ver= and True/False etc etc.
I am just not seeing the wood for the trees…
Any ideas?
Thanks and regards,
Zoltan
import rhinoscriptsyntax as rs
commandname = “zBldID”
def RunCommand( is_interactive ):
job = rs.DocumentName().split(‘.’)[0]
blds = rs.ObjectsByLayer(‘Building’, False)
n_blds = len(blds)
n_bld = 0
for bld in blds:
n_bld += 1
oldid = rs.GetUserText(bld, “#0", False)
if oldid == None:
oldid = ‘z’ + '{0:06d}'.format(n_bld)
bldid = job + ‘_{0:06d}’.format(n_bld)
rs.UnselectAllObjects()
rs.Command(”_SelBoundary _SelectionMode=Crossing selid {} _Enter".format(str(bld)), False)
rfps = rs.SelectedObjects()
rs.UnselectAllObjects()
n_rfps = len(rfps)
if n_rfps > 0:
print(‘{0:6d}’.format(n_bld) + ’ of {0:6d}‘.format(n_blds) + ’ Changing {0:6d}’.format(n_rfps) +
’ rooftops from ’ + oldid.rjust(15) + ’ to ’ + bldid.ljust(15))
bld_bool = rs.SetUserText(bld, “#_0”, bldid)
for rfp in rfps:
rfp_bool = rs.SetUserText(rfp, “#_0”, bldid)
del oldid
if n_blds > 0:
rs.DocumentModified(True)
return 0
rs.ObjectsByLayer isn’t selecting (and clicking?) the same button you’ve got the script in?
Comment out more and more parts of the code, and see if it still runs twice. Keep going until you find the line that’s the culprit, or you’ve got a function that does nothing but print something.
Python scripts are interpreted when they are imported, and Python commands work by importing the file as a module in the Python interpreter, then running its RunCommand method.
That means that the RunCommand(True) line at the bottom of your script will run at import, and then Rhino will call the RunCommand method “again”, as it normally does for a Python command. You need to remove that line or make sure it is not called at import, see item #6 in that help page:
The traditional way in Python to have a script work both as a script (debug mode) and a module that is imported without side effects is to have this at the bottom (instead of the simple RunCommand(False) line):
if __name__ == "__main__":
RunCommand(False)
You’ll want to move the print('Finished running TRUE ' + __commandname__ + ' v' + ver) line to inside the RunCommand function as well.
Hi @diana.ofcg, do you have a python script in your grasshopper file? If not, then this is not the same issue.
In that case, it would be better to open a new thread describing what you are trying to accomplish, the problem you are experiencing, and including your grasshopper definition so others can look into it and help you more easily.
I cannot figure out why the object is baking twice in Revit, I’d appreciate any help I can get. I have been seeing a few similar posts with no obvious solution.
Twice baking it seems, although I’m not sure how to tell. I have this also hapenning when I use the ‘Get Geometry’ command without the python script. It seems it’s the baking that is the issue.
I’ve taken a look. I didn’t install Rhino.Inside for AutoDesk. The only script component I found, is a Rhino 7 C# component, and I don’t know if those are different to Python ones or not.
If I were to take a guess based on general Grasshopper experience, the most likely explanation is the .gh file does the same thing when it is first opened either in Grasshopper Player or in Grasshopper (that maybe has gone unnoticed), that it also does when activated in GH (e.g. toggling a boolean, if the boolean is set to True, and then the definition saved).
Human UI can do some clever stuff. I don’t know if it can do background listeners, but perhaps it’s theoretically possible if remote that something changes in the Grasshopper Player environment, that causes one of the outputs from the first execution of the script, to raise an event that a listener reacts to, and triggers the script again (in such a way that it executes twice only, not in an infinite loop).
There’re a lot of moving parts in that file overall. Try disabling parts of it until the fewest ones necessary to cause it to run twice is isolated.
I actually managed to do a human ui script that doesn’t duplicate, but my issue for this senario is that I want to create a rhino button that runs a grasshopper player script to select objects from rhino and bake them into Revit.
Example 01:
I am running this within the grasshopper environment, which seems to be working fine and is not duplicating the geometry when baking, that’s great.
Example 02:
Here I am running the Grasshopper script through the player and attached it to a button like this
-GrasshopperPlayer “file path.gh”
The problem is that it’s baking it once when I press enter the first time, but then it comes up with the second menue with this
and this (undo) option is what it’s causing it to bake a second time.
How can I avoid that from hapenning?
I can open another ticket with this info if it doesn’t derail this ticket so much.
Thanks for uploading those videos, but I can’t tell what’s what in them. I’ve not come across that undo option before, but I’ve not used GrasshopperPlayer.