コード例 #1
0
def upload_resolutions(files, data):
    preferences = bpy.context.preferences.addons['blenderkit'].preferences

    upload_data = {
        "token": preferences.api_key,
        "id": data['asset_data']['id']
    }

    uploaded = upload_bg.upload_files(upload_data, files)

    if uploaded:
        bg_blender.progress('upload finished successfully')
    else:
        bg_blender.progress('upload failed.')
コード例 #2
0
    def run(self):
        # utils.pprint(upload_data)
        self.upload_data['parameters'] = utils.dict_to_params(
            self.upload_data['parameters'])  # weird array conversion only for upload, not for tooltips.

        script_path = os.path.dirname(os.path.realpath(__file__))

        # first upload metadata to server, so it can be saved inside the current file
        url = paths.get_api_url() + 'assets/'

        headers = utils.get_headers(self.upload_data['token'])

        # self.upload_data['license'] = 'ovejajojo'
        json_metadata = self.upload_data  # json.dumps(self.upload_data, ensure_ascii=False).encode('utf8')

        # tasks_queue.add_task((ui.add_report, ('Posting metadata',)))
        self.send_message('Posting metadata')
        if self.export_data['assetBaseId'] == '':
            try:
                r = rerequests.post(url, json=json_metadata, headers=headers, verify=True,
                                    immediate=True)  # files = files,

                # tasks_queue.add_task((ui.add_report, ('uploaded metadata',)))
                utils.p(r.text)
                self.send_message('uploaded metadata')

            except requests.exceptions.RequestException as e:
                print(e)
                self.end_upload(e)
                return {'CANCELLED'}

        else:
            url += self.export_data['id'] + '/'
            try:
                if 'MAINFILE' in self.upload_set:
                    json_metadata["verificationStatus"] = "uploading"
                r = rerequests.patch(url, json=json_metadata, headers=headers, verify=True,
                                     immediate=True)  # files = files,
                self.send_message('uploaded metadata')

                # tasks_queue.add_task((ui.add_report, ('uploaded metadata',)))
                # parse the request
                # print('uploaded metadata')
                print(r.text)
            except requests.exceptions.RequestException as e:
                print(e)
                self.end_upload(e)
                return {'CANCELLED'}

        if self.stopped():
            self.end_upload('Upload cancelled by user')
            return
        # props.upload_state = 'step 1'
        if self.upload_set == ['METADATA']:
            self.end_upload('Metadata posted successfully')
            return {'FINISHED'}
        try:
            rj = r.json()
            utils.pprint(rj)
            # if r.status_code not in (200, 201):
            #     if r.status_code == 401:
            #         ###ui.add_report(r.detail, 5, colors.RED)
            #     return {'CANCELLED'}
            # if props.asset_base_id == '':
            #     props.asset_base_id = rj['assetBaseId']
            #     props.id = rj['id']
            if self.export_data['assetBaseId'] == '':
                self.export_data['assetBaseId'] = rj['assetBaseId']
                self.export_data['id'] = rj['id']
                # here we need to send asset ID's back into UI to be written in asset data.
                estring = f"{self.export_data['eval_path']}.blenderkit.asset_base_id = '{rj['assetBaseId']}'"
                tasks_queue.add_task((exec, (estring,)))
                estring = f"{self.export_data['eval_path']}.blenderkit.id = '{rj['id']}'"
                tasks_queue.add_task((exec, (estring,)))
                # after that, the user's file needs to be saved to save the

            self.upload_data['assetBaseId'] = self.export_data['assetBaseId']
            self.upload_data['id'] = self.export_data['id']

            # props.uploading = True

            if 'MAINFILE' in self.upload_set:
                if self.upload_data['assetType'] == 'hdr':
                    fpath = self.export_data['hdr_filepath']
                else:
                    fpath = os.path.join(self.export_data['temp_dir'], self.upload_data['assetBaseId'] + '.blend')

                    clean_file_path = paths.get_clean_filepath()

                    data = {
                        'export_data': self.export_data,
                        'upload_data': self.upload_data,
                        'debug_value': self.export_data['debug_value'],
                        'upload_set': self.upload_set,
                    }
                    datafile = os.path.join(self.export_data['temp_dir'], BLENDERKIT_EXPORT_DATA_FILE)

                    with open(datafile, 'w') as s:
                        json.dump(data, s)

                    # non waiting method - not useful here..
                    # proc = subprocess.Popen([
                    #     binary_path,
                    #     "--background",
                    #     "-noaudio",
                    #     clean_file_path,
                    #     "--python", os.path.join(script_path, "upload_bg.py"),
                    #     "--", datafile  # ,filepath, tempdir
                    # ], bufsize=5000, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
                    # tasks_queue.add_task((ui.add_report, ('preparing scene - running blender instance',)))
                    self.send_message('preparing scene - running blender instance')

                    proc = subprocess.run([
                        self.export_data['binary_path'],
                        "--background",
                        "-noaudio",
                        clean_file_path,
                        "--python", os.path.join(script_path, "upload_bg.py"),
                        "--", datafile
                    ], bufsize=1, stdout=sys.stdout, stdin=subprocess.PIPE, creationflags=utils.get_process_flags())

            if self.stopped():
                self.end_upload('Upload stopped by user')
                return

            files = []
            if 'THUMBNAIL' in self.upload_set:
                files.append({
                    "type": "thumbnail",
                    "index": 0,
                    "file_path": self.export_data["thumbnail_path"]
                })
            if 'MAINFILE' in self.upload_set:
                files.append({
                    "type": "blend",
                    "index": 0,
                    "file_path": fpath
                })

            self.send_message('Uploading files')

            uploaded = upload_bg.upload_files(self.upload_data, files)

            if uploaded:
                # mark on server as uploaded
                if 'MAINFILE' in self.upload_set:
                    confirm_data = {
                        "verificationStatus": "uploaded"
                    }

                    url = paths.get_api_url() + 'assets/'

                    headers = utils.get_headers(self.upload_data['token'])

                    url += self.upload_data["id"] + '/'

                    r = rerequests.patch(url, json=confirm_data, headers=headers, verify=True)  # files = files,

                self.end_upload('Upload finished successfully')
            else:
                self.end_upload('Upload failed')
        except Exception as e:
            self.end_upload(e)
            print(e)
            return {'CANCELLED'}