Try to open a file through python


#1

Hey guys, new to Rhino, and running into an issue straight away, been playing around for a while but jsut can;'t seem to get around it,

I need to do a batch script to convert a set of files,
i have got the folder indexed, I can get all the filenames etc, thats all fine, (I’m familiar with python)

however when I try to open the file through script Rhino always prompts me whether I want to save, and there is something about UpdatePromtUpdateBox etc. which means my file never loads (and even if it would, it does this for every file requiring manual clicking,

I also found a ready made batch convert but it does the same thing.

anyways the most basic thing to test seemed to be this:

import rhinoscriptsyntax as rs
path = 'Y/:test.stp’
rs.Command(’_-Open ’ + path)

many thanks for all help

edit:

tried some other stuff like

import rhinoscriptsyntax as rs

rs.DocumentModified(False)
rs.Command("_-Open ““Y/:test.stp”” _Enter", 0)

or

rs.Command("_-Open " & “Y/:test.stp” & " _Enter", 0)

still no avail :frowning:


(Willem Derks) #2

Hi Freek,

Try this:

import rhinoscriptsyntax as rs


path = '"D:\\TEMP\\test.stl"'

rs.DocumentModified(False)
rs.Command('_-Open {} _Enter'.format(path))

Notice the double back slashes to not be caught by pythons special characters in strings

HTH
Groeten
-Willem


#3

thanks that seems to do the trick,
seems it doesn’t like forward slashes very much, interesting,
thank you ever so much!!!


(Willem Derks) #4

It’s a Windows thing:

-Willem


#5

so I seem to have got everything working, except for filenames that include spaces…
any idea on how to handle those? otherwise I got it to do pretty much everything I want :slight_smile:


(Willem Derks) #6

Did you implement it so the path is in double quotes. That will make Rhino able to parse the whole path including the spaces.


#7

thanks got it all to work,
needed to do some crazy string parsing but got it to work in the end ^^
thanks for your help! you’ve been gold!


#8

Could you please explain the braces?


(Willem Derks) #9

Hi Ben, you mean the quotes?

In python a string can be defined both by double quotes and single quotes:


foo = 'string in single quotes'
bar = "string in double quotes"

This leaves the option to use either single or double quotes inside the string as non-special characters


foo = 'string "quoted inside" '
bar = "string 'quoted inside' "

Passing a path in the Rhino commandline will work fine without double quotes, except when there is a space character in the path eg:
D:\test\test file with spaces.stl

Passing that as a string without double quotes will make Rhino catch the space as an enter and you will end up with Rhino not being able to make sense of the input.

So to circumvent that you can define a command string in python with single quotes and inside it, put the path in double quotes, making sure Rhino will interpret the part in between double quotes as a single entity input.

Does this answer your question?

-Willem


#10

If you are referring to the .format statement, the braces represent substitution items. When it is executed, the braces are replaced by what is inside the parentheses using the appropriate formatting. Example:

item=1.0
print "The value of my item is {}".format(item)

>>> 'The value of my item is 1.0'

Pretty much anything can be substituted in, numbers, strings, etc. You can have any number of braces in a format statement, they are executed in order:

a=3
b="eggs"
c=2
d="spam"
print "I had {} {} and {} slices of {} for breakfast this morning".format(a,b,c,d)

>>>'I had 3 eggs and 2 slices of spam for breakfast this morning'

Format statements are very powerful and allow you to easily set up strings using your variables for printing and sending to the command line.

–Mitch


#11

Nope the curly braces … rs.Command(’_-Open {} _Enter’.format(path))

My real question that I was gonna hold off asking until I did my 'forum due diligence" is when I try to open a file when there already is stuff loaded I keep getting a get a save changes dialog box

file_for_open = “c:\myTemp\jeep\M38 Model RFL\STL\” + file_list[3].rstrip()
file_for_open = ‘"’ + file_for_open + ‘"’

rs.DocumentModified(False)
rs.Command("! _-New None")
rs.Command(’
-Open ’ + file_for_open + ’ _Enter’)

If I leave out the ! _-New _None command I get the same problem.


#12

Silly me. I am so wrapped up in the Rhino stuff I forgot about Python stuff…

thanks


(Enrico556) #13

Hi Willem!

i’m facing a similar problem with a file path at present.
Only difference being that i’m passing the file name with the absolute path as a variable.
So i have my variable “fpath” and following is a result when i print the fpath variable:
E:\Folder\New - File.stp

But when i use the open file command in Rhino, it can’t find the file due to the white spaces in the filename.
Following is the command i’ve tried:
rs.Command(’_-Open fpath _Enter’)

What do i need to modify in the Open File command?


(Willem Derks) #14

Hi Enrico,

Try this:

rs.Command('_-Open "{}" _Enter'.format(fpath))

Does that help?
-Willem


#15

Tiny note that didn’t seem to be covered in this (great) thread: Using raw string literals to define paths:

2018-03-09%2014_50_27-Grasshopper%20Python%20Script%20Editor

2018-03-09%2014_50_38-Grasshopper%20Python%20Script%20Editor

It makes it super easy to Shift + Right-Click a file on Windows and go Copy as Path, and then just paste this and prepend an r.


(Enrico556) #16

Perfect!
Thanks a lot!