RunPythonScript TXT FILE export BROKEN in Build 2014-06-30

I just downloaded the new Rhino Mac (wip_20140630). After installing IronPython (python20140630) I can run scripts, but when I try to generate a textile via the python script it does not create the txt file. I also don’t get any errors and I know that the script is woking correctly. Any ideas on how to fix this?
Thanks!

Do you have a sample to test?

–Mitch

################################################################
#Export the coordinates of point and point cloud objects to a text file.
################################################################
import rhinoscriptsyntax as rs

def ExportPoints():
  #Get the points to export
  objectIds = rs.GetObjects("Select Points",rs.filter.point | rs.filter.pointcloud,True,True)
  if( objectIds==None ): return

  #Get the filename to create
  filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
  filename = rs.SaveFileName("Save point coordinates as", filter)
  if( filename==None ): return
  
  file = open(filename, "w")
  for id in objectIds:
    #process point clouds
    if( rs.IsPointCloud(id) ):
      points = rs.PointCloudPoints(id)
      for pt in points:
        file.write(str(pt))
        file.write("\n")
    elif( rs.IsPoint(id) ):
      point = rs.PointCoordinates(id)
      file.write(str(point))
      file.write("\n")

  file.close()


##########################################################################
# Here we check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == '__main__' ):
  ExportPoints()

Yep, looks like there is a problem here, I have been able to determine that rs.SaveFileName is returning None, thus the script simply exits before anything is done…

Edit: I tried to hard code a path to a file in the script, but honestly it’s still a mystery to me how you specify an absolute file path in Mac, nothing I tried worked.

–Mitch

Do you know if there is any way to download the IronPython for wip_20140610? It used to work fine then…
Thanks,
Kai

No, I don’t…

I managed to succeed in writing a file with a name collected using rs.GetString() in a user-selected folder with rs.BrowseForFolder(), so it’s just rs.SaveFileName() that’s not working.


import rhinoscriptsyntax as rs
import os

def ExportPoints():
  #Get the points to export
  objectIds = rs.GetObjects("Select Points",rs.filter.point | rs.filter.pointcloud,True,True)
  if( objectIds==None ): return

  #Get the filename to create
  #filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
  #filename = rs.SaveFileName("Save point coordinates as", filter)
  #if( filename==None ): #return
  folder=rs.BrowseForFolder()
  fName=rs.GetString("File name?")
  if not fName: return
  filename=os.path.join(folder,fName+".txt")
  
  file = open(filename, "w")
  for id in objectIds:
    #process point clouds
    if( rs.IsPointCloud(id) ):
      points = rs.PointCloudPoints(id)
      for pt in points:
        file.write(str(pt)+"\n")
    elif( rs.IsPoint(id) ):
      point = rs.PointCoordinates(id)
      file.write(str(point)+"\n")
  file.close()

if( __name__ == '__main__' ):
  ExportPoints()

Great - thanks! That helped for now…

kai

Now it also seems like the rs.OpenFileName() is not working either. Any suggestions on how to fix that?

here is my python and a txt file that I’m trying to run:

# Import points from a text file
import rhinoscriptsyntax as rs

counter = 0

def ImportPoints():
    #prompt the user for a file to import
    print 'init.txt file'
    
    filter = "Text file (*.txt)|*.txt|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Point File", filter)
    if not filename: return
    
    
    #read each line from the file
    file = open(filename, "r")
    contents = file.readlines()
    file.close()

    

    # local helper function    
    def __point_from_string(text):
        #print "gr"
        #print text
        items = text.split(" ") #.strip("()\n")

        if items[0]=="v":

            #counter+=1
            x = float(items[1])
            y = float(items[2])
            z = float(items[3])

            rs.AddPoint (x,y,z)

            print 'x = '+str(x)
            return x, y, z

    contents = [__point_from_string(line) for line in contents]


##########################################################################
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
    ImportPoints()

test.txt (1004.8 KB)

I am working on a fix for this right now. I should have a new python build available within an hour

Thanks a lot. It’s working well again.
Kai

Hi steve,

I am facing this issue as well, can I get the build containing this fix somewhere?

thanks ad best regards,
Tim

The latest build of Mac Rhino can always be downloaded at
http://www.rhino3d.com/download/rhino-for-mac/5.0/wip

There is no separate download for python support anymore; that feature is now included in Rhino for Mac

Hi Steve,

Thanks for the quick response, I have installed this already (details below). However rs.SaveFileName() always returns “None”. Similar to what Helvetosaur described. As you have made a fix for this, it seems ‘my’ Rhino is not using the correct version of python. Do you have any suggestions?

best regards,
Tim

Software versions
Rhinoceros version: 5.0 WIPVersionSuffix (5A683)
IronPython version: 5.1.2014.1006
Language: nl (MacOS default)
OS X version: Version 10.10.1 (Build 14B25)

Ok, I’ll have to take a look and see if I can repeat this.

i have a little piece of code from @Helvetosaur which gives a list of subdirectories for scriptcontext… it uses rs.SaveFileName and it works for me in the latest mac rhino release.
?

import rhinoscriptsyntax as rs
import scriptcontext as sc

def ExportClassDirectory(direc):
    filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
    filename = rs.SaveFileName("Save directory as", filter)
    if not filename: return
    namext=filename.split("\\")
    name=namext[-1].split(".")

    file=open(filename,"w")
    file.write(name[0]+"\n\n")
    for item in direc:
        file.write(item+"\n")
    file.close()

direc=dir(sc.doc)
ExportClassDirectory(direc)

does the above script work for you?

(it should create a .txt file)

i think that’s the correct or current version of ironpython in mac rhino… (which is a different thing than the current python which ships with osx… that being Python 2.7.6

@jeff_hammond

thank you Jeff, but as rs.SaveFileName still returns “None”. the script exits at the next line.

however “python -V” in terminal returns “Python 2.7.8”. could this version be conflicting with 2.7.6?

Best regards,
tim

do you mean that exact script i posted just above fails for you in mac rhino?

if so, then yeah, that’s odd… i just tried it again to be certain and it’s definitely working here…

software wise, it looks like we’re running the same stuff:

  Software versions
    Rhinoceros version:  5.0 WIPVersionSuffix (5A683)
    IronPython version:  5.1.2014.1006
    Language:  en (MacOS default)
    OS X version:  Version 10.10.1 (Build 14B25)

the difference is that i have python 2.7.6 and you have 2.7.8…

whether or not this is a source of conflict is beyond my knowledge :wink:

(although i have seen instances around here of people using older versions of python than me (2.7.2 which ships with mountainLion) having issues)



edit

as another example:
if i run this script:

import rhinoscriptsyntax as rs
filename = rs.SaveFileName()
rs.MessageBox(filename)

i’m first prompted with an osx save dialog…

then upon clicking ‘save’, i get the rhino message box:

?


edit2 @timcastelijn
i also see we’re running different languages (you=nl me=en)…
but again, whether or not this is causing a difference with rs.SaveFileName is beyond me

Do you see the user interface for the save dialog and then None is returned no matter what?

hi Steve,
that is correct. selecting a file to save, or open always returns None. Regardless whether I proceed or escape. I have isolated the issue in the script below.

# import rhinoscriptsytax
import rhinoscriptsyntax as rs


# Check to see if this file is being executed as the "main" python script
if( __name__ == "__main__" ):
    
    filename = rs.SaveFileName ("Save", "Toolpath Files (*.nc)|*.nc||")
    print(filename) # returns None, after giving filename
    
    filename2 = rs.OpenFileName ("Save", "Toolpath Files (*.nc)|*.nc||")
    print(filename2) # returns None after selecting file 
    
    rs.MessageBox("check function") # throws error -> Message: ShowMessageBox() takes at most 4 arguments (5 given)

Thanks, I think I know what is going on. I’ll probably have a few more questions as I work through this:)