View RSS Feed

Steve Baer

Outside the Box 2 - Calling the Shapeways API from Python

Rate this Entry
I received a question on accessing the ShapeWays API from my last "Outside the Box" blog and figured I should dig in and figure out what is going on. Shapeways has a web service API that uses SOAP and a WSDL page (http://en.wikipedia.org/wiki/Web_Ser...ption_Language) as described here
http://www.shapeways.com/api

This API is a bit different than the REST API which I wrote a script to access in my previous blog post. In order to use this API, I ended up modifying a script originally put together by Michael Foord (author of "IronPython in Action") that creates a .NET assembly on the fly for a given WSDL url.

Here are the two scripts:
useshapeways.py
wsdlprovider.py

Place both of these scripts in the same directory and open the "useshapeways.py" script. Here's the script itself
Python Code:
    """Sample script that accesses the shapeways API
    [url]http://www.shapeways.com/api[/url]
    """
    import wsdlprovider
     
    wsdl_url = "http://api.shapeways.com/v1/wsdl.php"
    username = "username"
    password = "password"
    application_id = "rhinotest"
     
    assembly = wsdlprovider.GetWebservice(wsdl_url)
    shapeways = assembly.SWwsdlService()
    session_id = shapeways.login(username, password, application_id)
    if session_id:
        #get list of printers available
        printers = shapeways.getPrinters(session_id, "", application_id)
        if printers:
            for printer in printers:
                print "printer:", printer.title
                for material in printer.materials:
                    print " - material ", material.title

The script uses the wsdlprovider script to generate a .NET assembly from the shapeways wsdl url. This assembly has a class in it called SWwsdlService which we create an instance of and call functions on. It looks like a normal class to python, but all of the function calls are sent to ShapeWays over the internet and response are turned into classes that you can use. This sample simply logs into shapeways to get a "session id" and then asks Shapeways for a list of it's available printers along with what materials each printer supports.

At the time of this blog post, the printed output from this script is

Code:
printer: Somatech FDM
 - material  Grey Robust
printer: Somatech Objet 720
 - material  Black Detail
 - material  White Detail
 - material  Transparent Detail
printer: SLS Printer
 - material  White Strong & Flexible
printer: Metal Printer matt
 - material  Gold Plated Glossy
 - material  Antique Bronze Glossy
 - material  Antique Bronze Matte
 - material  Stainless Steel
printer: SLS Color Printer
 - material  Black Strong & Flexible
printer: Silver Printer
 - material  Silver Glossy
 - material  Silver
printer: ZPrinter 650
 - material  Sandstone
 - material  Full Color Sandstone
printer: SLS Alumide
 - material  Alumide
printer: Glass Printer
 - material  High Gloss Black Glass
 - material  High Gloss White Glass
 - material  Milky White Matte Glass
printer: Metal printer Gold
 - material  Gold Plated Matte
printer: SLS Color Printer New
 - material  Dark Grey Strong and Flexible
 - material  Indigo Strong and Flexible
 - material  Winter Red Strong and Flexible
printer: HD printer
 - material  Frosted Detail
printer: UHD printer
 - material  Frosted Ultra Detail
printer: SLS Printer polished
 - material  White Strong & Flexible Polished
printer: Ceramics printer
 - material  Glazed Ceramics
Pretty neat!
Tags: python, soap Add / Edit Tags
Categories
Uncategorized

Comments

  1. gsnover -
    gsnover's Avatar
    Hi Steve,

    I tried uploading a model to shapeways using your example. The problem I am having is that the shapeways API function submitModel requires an argument for the model as a custom data type "SWModel". The API only has two custom datatypes and the other is returned by a method. SWModel needs to be created.
    I couldn't figure out how to cast a variable to that datatype. If I just made a list of the arguments ("STL", Model,Scale,etc) it returns an error saying a SWModel type is needed not a list.
    Can you help me? thanks.
    -G
  2. gsnover -
    gsnover's Avatar
    """Sample script that accesses the shapeways API
    http://www.shapeways.com/api
    """
    import rhinoscriptsyntax as rs
    import wsdlprovider

    wsdl_url = "http://api.shapeways.com/v1/wsdl.php"
    username = "gsnover"
    password = "988panels"
    application_id = "rhinotest"

    assembly = wsdlprovider.GetWebservice(wsdl_url)
    shapeways = assembly.SWwsdlService()
    session_id = shapeways.login(username, password, application_id)
    if session_id:
    #get list of printers available
    printers = shapeways.getPrinters(session_id, "", application_id)
    if printers:
    for printer in printers:
    print "printer:", printer.title
    for material in printer.materials:
    print " - material ", material.title

    rhmodel = rs.GetObject("Select Model to Upload to Shapeways")
    Dir = rs.BrowseForFolder("Select Folder to save STL file. ","Open Folder")
    saveName = rs.SaveFileName("Enter file name","", Dir,"", ".stl")
    rs.Command("_-SaveAs " + saveName + " enter enter")

    savedSTL = open(saveName)

    #model = SWModel("STL", savedSTL, 0.001)

    upload = shapeways.submitModel(session_id, model,0, "rhinotest")

    print" Upload returned: " + upload
  3. wzesk -
    wzesk's Avatar
    you need to build the swmodel class and model.file field wants a array[bye]. here is what I used:

    def submit(self):
    if self.session_id:
    model = self.assembly.SWModel()
    model.file = tuple(bytearray((stlreader.read()),"base64"))
    stlreader.close()
    loaded = self.shapelink.submitModel(self.session_id,model," ", self.application_id)
    return loaded
  4. wzesk -
    wzesk's Avatar
    I posted the shapeways model loader code: github.com/Wzesk/Rhino-to-Shapeways

    I would love any feedback or advice on tuning it up...