Hi,
I have a bug that i don’t understand.
I store my script on my Dropbox so i can use it on 2 computer.
import rhinoscriptsyntax as rs
rs.Command('-_import ' + chr(34) + 'C:\Users\PC1\Dropbox\Rhino Core OrthoUI\Template\Insole\Medium/39.3dm' + chr(34) +' _Enter')
rs.Command('-_import ' + chr(34) + 'C:\Users\PC2\Dropbox\Rhino Core OrthoUI\Template\Insole\Medium/39.3dm' + chr(34) +' _Enter')
If i execute this scrip on my PC1, Rhino import the file twice.
Command: _-RunPythonScript
Python Script <C:\Users\PC1\Dropbox\Rhino Core OrthoUI\Script\InsoleMedium\InsoleMedium39.py> ( ResetEngine ): "InsoleMedium39.py"
Python Script <C:\Users\PC1\Dropbox\Rhino Core OrthoUI\Script\InsoleMedium\InsoleMedium39.py> ( ResetEngine ): -_import
Name of the file to import ( Browse ): "C:\Users\PC1\Dropbox\Rhino Core OrthoUI\Template\Insole\Medium/39.3dm"
Import scaling: Keep <ModelSize> ( DimensionNumbers ): _Enter
Import scaling: Keep <ModelSize> ( DimensionNumbers )
Successfully read file "C:\Users\PC1\Dropbox\Rhino Core OrthoUI\Template\Insole\Medium/39.3dm"
Command: -_import
Name of the file to import ( Browse ): "C:\Users\PC2\Dropbox\Rhino Core OrthoUI\Template\Insole\Medium/39.3dm"
Import scaling: Keep <ModelSize> ( DimensionNumbers ): _Enter
Import scaling: Keep <ModelSize> ( DimensionNumbers )
Successfully read file "C:\Users\PC1\Dropbox\Rhino Core OrthoUI\Template\Insole\Medium\39.3dm"
At the 2nd import, for PC2, Rhino import from PC1…
There is a way to avoid this. I need to create 2 different script? I use the same .rui interface file, that why i prefer 1 script.
thanks
Since PC1 appears to be a file folder and not an entirely separate PC, you are essentially just running the same script twice yes.
You could keep it in the same script and do something like this where you prompt for the username/PC selection first:
import rhinoscriptsyntax as rs
# Prompt user for the username
user_name = rs.GetString("Please enter your username")
# Replace 'PC1' and 'PC2' with the user-provided username in the file path
file_path = 'C:\\Users\\{0}\\Dropbox\\Rhino Core OrthoUI\\Template\\Insole\\Medium\\39.3dm'.format(user_name)
# Import the files
rs.Command('-_import ' + chr(34) + file_path + chr(34) + ' _Enter')
Just something to watch out for: including forward slashes (/
) in file names on Windows is a recipe for disaster. Don’t do that.
Unfortunately, for historical reasons, the directory separator character on Windows, the backslash: \
, is also the escape character in strings in Python (and in JSON, C and many other places).
Further more \U
starts a 32-bit unicode hex escape, and \N
starts a Unicode named escape.
2. Lexical analysis — Python 3.11.4 documentation You have both in your file path, but Python probably didn’t apply them as escapes.
So when entering string literals in Python containing Windows file paths, use a raw string, e.g. r'c:\my_module.py'
.
I’m not going to install them just to test this in CPython2 or in Iron Python, so it probably won’t work in Rhino or Grasshopper, but when interpreting the string as a file path, CPython 3 doesn’t care if you use a forward slash (the posix directory separator) instead of a backslash: 'c:/my_module.py'
. This works great, but is inconvenient for copying and pasting paths, e.g. from File Explorer, and means such paths can’t be used elsewhere in Windows. And you might as well use pathlib if using Python 3 (there is a Python 2 backport of pathlib, but I’ve not tried it: pathlib2 · PyPI)
Otherwise in a normal string literal, correct syntax demands that the escape character, must itself be escaped (with itself). If you insist on this, have fun counting backslashes if you ever play with regular expressions: 'c:\\my_module.py'
P.S. You know chr(34) == '"'
, right? Python is perfectly happy for single unescaped double quote characters to appear within single (or triple) quoted strings.