0

When I try to run the script (I'm on a Mac; I press command B, right?), instead of generating the txt file, nothing happens. (The only Traceback info is something that, I think, always comes up and usually the code will run anyway--

Traceback (most recent call last):
File "/Users/[my name]/Downloads/ImportImageTextFile.py", line 2, in <module>
import rhinoscriptsyntax as rs
ImportError: No module named rhinoscriptsyntax

Here's the code so far (sorry that some of this is not relevant--the code I am pasting 1. turns a jpg into a text file and 2. puts the text file into rhino).

# Import points from a text file
import rhinoscriptsyntax as rs
from System.Drawing import Color


recreateImage = False


def main(): 
    # import the converted pixel image and store the RGB values in "pixels":
    # note: the resultant list will be organized as follows:

    # the first 2 [] indicates the y and x coordinates
    # the last [] indicates the color (red = 0 , green = 1 , blue = 2)

    #   pixels[y][x] = [R,G,B]  to get the blue value at pixel 11 in the first row:  pixels[11][0][2]
    #   you can get the width of the pixel image by using:  w = len(pixels[0])   (this returns the length of the list of the first row or X-dimension)
    #   you can get the height of the pixel image by using:  h = len(pixels)   (this returns the length/number of "columns" or Y-dimension)


    pixels = importPoints(recreateImage)





    # the 4 is the "step value"

    for x in range (0, len(pixels[0]) , 2):
        for y in range (0, len(pixels) , 2):
            r = pixels[y][x][0]
            g = pixels[y][x][1]
            b = pixels[y][x][2]

            # we save the "AddSphere" into the phrase spId
            # we subtract the y to not let the image mirror/ make the pixelated image higher than the original image, otherwise it would be directly above the original image
            # the "3 - (r+g+b)/765" represents the sphere radius size

            # spId = rs.AddSphere([x,len(pixels)-y,(r+g+b)/765*30], 3 - (r+g+b)/765 )
            # rs.ObjectColor(spId, [r,g,b])

            # lines have a start point and an end point
            # [] / brackets indicate a new array/list

            lineId = rs.AddLine ( [x, -y , r+g+b] , [x, -y , 0] )
            rs.ObjectColor (lineId, [r,g,b])





def importPoints(makeImage):
    #prompt the user for a file to import
    filter = "Text file (*.txt)|*.txt|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Pixel File", filter)
    if not filename: return

    #read each line from the file
    file = open(filename, "r")
    contents = file.readlines()

    if (makeImage):
        previousLayer = rs.CurrentLayer()
        rs.AddLayer("Image")
        rs.CurrentLayer("Image")

    counterX = -1
    counterY = -1

    width = 0
    height = 0

    imagePixels = []
    pixelRow = []

    #browse through each line in the text file:
    for text in contents:
        items = text.strip("()\r\n").split(";")

        #print (str(items))        



        if (counterX == -1 and counterY == -1):
            counterY +=1

            width = int(items[1].split(",")[1])
            height = int(items[2].split(",")[1])
            print ("Image size: "+str(width)+" x "+str(height))

        else:
            r = int(items[0])
            g = int(items[1])
            b = int(items[2])
            counterX +=1

            if (counterX == width):
                counterY +=1
                counterX = 0
                imagePixels.append(pixelRow)
                pixelRow = []

            if (makeImage):
                ptId = rs.AddPoint ([counterX, -counterY, 0])
                rs.ObjectName (ptId, str(counterX)+" - "+str(counterY)+" : "+str(r)+","+str(g)+","+str(b))
                rs.ObjectColor (ptId, [r,g,b])

            pixelRow.append([r,g,b])


    file.close()

    if (makeImage):
        rs.CurrentLayer(previousLayer)

    return imagePixels



main()

Thank you so much!

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Ava
  • 1
  • 2
  • 1
    what is `rhinoscriptsyntax`? – Padraic Cunningham May 04 '15 at 10:01
  • rhinoscriptsyntax is a command library for the 3D CAD program Rhino (so it has to do with the second part of my code, uploading the image into Rhino). I always get that error but the code runs okay anyway, so I don't think that's the problem... – Ava May 04 '15 at 10:05
  • So you do have it installed? If so how did you install? – Padraic Cunningham May 04 '15 at 10:06
  • If `rhinoscriptsyntax` is a module, you'd better install it properly. Try something like `pip install rhinoscriptsyntax`. – ForceBru May 04 '15 at 10:06
  • @Ava, you say your code runs OK. How could that be possible without this module?? – ForceBru May 04 '15 at 10:07
  • It's a mystery to me--all I can say is that this error comes up a lot but still properly pulls from the library. But if there's a possibility this is causing some kind of bug, I'll try to install it right--do I paste `pip install rhinoscriptsyntax` into my script or into Terminal? (Sorry, I'm obviously a total newbie!) – Ava May 04 '15 at 10:11
  • @Ava, is this on windows? I doubt very much you will be able to use pip for this, don't go installing anything yet either way. How did you install Rhino? – Padraic Cunningham May 04 '15 at 10:13
  • @ Padraic Cunningham, No, I'm using a Mac OS X. Thanks for your help by the way. – Ava May 04 '15 at 10:14
  • How did you install Rhino? – Padraic Cunningham May 04 '15 at 10:14
  • @Padraic Cunningham--I installed Rhino through this link [https://www.rhino3d.com/download/rhino-for-mac/5.0/wip](https://www.rhino3d.com/download/rhino-for-mac/5.0/wip) and I installed the rhinoscriptsyntax through this link [http://www.rhino3d.com/download/Rhino/4.0/rhinoscript] (http://www.rhino3d.com/download/Rhino/4.0/rhinoscript). Out of the rhinoscript folder were three files--one .chm, one .rhp, and one .tlb--but I didn't do anything past just downloading (and I think installing) the folder? – Ava May 04 '15 at 10:18
  • can you `import rhinoscript` from a python shell? What is in your `Rhinoceros` folder? – Padraic Cunningham May 04 '15 at 10:19
  • If I understand your question right, I am using Sublime Text 2, and importing rhinoscript from it has worked on other computers with other scripts (although the same error message always comes up). I don't think I have a Rhinoceros folder, just the application. – Ava May 04 '15 at 10:22
  • There should be a `rhinoscriptsyntax.py` file included with your installation. – Padraic Cunningham May 04 '15 at 10:24
  • If not https://github.com/mcneel/rhinoscriptsyntax – Padraic Cunningham May 04 '15 at 10:27
  • OK, I just downloaded linked github file. It did not include a file called "rhinoscriptsyntax.py." (More coming in next comment--please let me know if it would be appropriate to move this to chat by the way, and thanks again) – Ava May 04 '15 at 10:33
  • The above info is in a file called rhcommand.template. – Ava May 04 '15 at 10:35
  • did you *Start Rhino and run EditPythonScript or RunPythonScript. This will cause the python plug-in for Rhino to unpack the scripts defined in this project to your computer*, chat would be more appropriate but you do not have enough rep! – Padraic Cunningham May 04 '15 at 10:37
  • By project, do you mean the script I am working on? Apparently, when it works properly it is first run on Sublime Text 2 (which gives me the text file), and only after that on Rhino (where I think I run the script again and am prompted to input the text file). I think the place I might be going wrong is that I am supposed to input the jpg file name somewhere before running the script, and I'm not sure where. I have tried to input it from `file = open(filename, "r")` and `filename = rs.OpenFileName("Open Pixel File", filter)`, to no avail. – Ava May 04 '15 at 10:44
  • @Padraic Cunningham--Big mistake--you were right all along--the problem has to do with the module. I got the same error as I posted in the above comment when I tried a code that normally works. Do you know what is wrong with my module? – Ava May 04 '15 at 11:17

0 Answers0