pythonpython-requestsautodesk-forgeautodesk-realitycapture

Forge Reality Capture: Specified photoscene ID doesn't exist


I'm using python to upload some .jpg images to my created photoscene but I'm constantly getting this error.

{'Usage': '0.48637413978577', 'Resource': '/file', 'Error': {'code': '19', 'msg': "Specified Photoscene ID doesn't exist in the database"}}

This is my code, photoscene creation works great, I get the photoscene id and copy that as a string to store it as "sceneId"


formData = {'Content-Type': 'multipart/form-data', 'Authorization': 'Bearer eyXXXX'}

sceneId = "l5w----etc-etc------qQ"

# This bit is so I can use tkinter to choose my images
application_window = tk.Tk()
application_window.withdraw()
answer = filedialog.askopenfilenames(parent=application_window,
                                    initialdir=os.getcwd(),
                                    title="Please select one or more files:",
                                    filetypes=[("Image files", ".jpg .jpeg")])
if answer != "":
    files = {
        "photosceneid":(None, sceneId),
        "type":(None, "image")
    }
    n=-1
    for a in answer:
        n = n+1
        a = a.replace("/", "//")
        files["file[{x}]".format(x=n)] = (a, open(a,"rb"))
    # This bit adds keys and values to the dictionary as "file[0]": ("path//to//image//", open("path//to//image//","rb"))

    r = requests.post("https://developer.api.autodesk.com/photo-to-3d/v1/file",headers=formData,files=files).json()
    print(r)

I'm following the snips from the official api reference

curl -v 'https://developer.api.autodesk.com/photo-to-3d/v1/file' \
  -X 'POST' \
  -H 'Authorization: Bearer eyjhbGCIOIjIuzI1NiISimtpZCI6...' \
  -F "photosceneid=hcYJcrnHUsNSPII9glhVe8lRF6lFXs4NHzGqJ3zdWMU" \
  -F "type=image" \
  -F "file[0]=@c:/sample_data/_MG_9026.jpg" \
  -F "file[1]=@c:/sample_data/_MG_9027.jpg"

Thanks for reading and for the help!


Solution

  • The problem is that you are sending the photosceneid data as a file. In the cURL snip from official api

    curl -v 'https://developer.api.autodesk.com/photo-to-3d/v1/file' \
    -X 'POST' \
    -H 'Authorization: Bearer eyjhbGCIOIjIuzI1NiISimtpZCI6...' \
    -F "photosceneid=hcYJcrnHUsNSPII9glhVe8lRF6lFXs4NHzGqJ3zdWMU" \
    ...
    

    -F flag means form (in in case of cURL) and that's not necessary a file.

    Thus, you'll have to send the photosceneid and the type as data, instead of file:

    from requests_toolbelt import MultipartEncoder
    import requests
    
    url = "https://developer.api.autodesk.com/photo-to-3d/v1/file"
    
    payload = MultipartEncoder(
       fields={'photosceneid': MY_PHOTOSCENE,
               'type': 'image',
               'file[0]': ("DSC_5428.JPG", open('./DSC_5428.JPG', 'rb'), 'image/jpg')
            }
      )
    
    
    headers = {
     'Content-Type': payload.content_type,
     'Authorization': TOKEN
    }
    
    
    req = requests.request("POST",
                          url,
                          headers=headers,
                          data=payload
                         )