コード例 #1
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'}
コード例 #2
0
def start_upload(self, context, asset_type, as_new, metadata_only):
    '''start upload process, by processing data'''
    props = utils.get_upload_props()
    storage_quota_ok = check_storage_quota(props)
    if not storage_quota_ok:
        self.report({'ERROR_INVALID_INPUT'}, props.report)
        return {'CANCELLED'}

    location = get_upload_location(props)
    props.upload_state = 'preparing upload'

    auto_fix(asset_type = asset_type)

    # do this for fixing long tags in some upload cases
    props.tags = props.tags[:]


    props.name = props.name.strip()
    # TODO  move this to separate function
    # check for missing metadata
    if asset_type == 'MODEL':
        get_missing_data_model(props)
    if asset_type == 'SCENE':
        get_missing_data_scene(props)
    elif asset_type == 'MATERIAL':
        get_missing_data_material(props)
    elif asset_type == 'BRUSH':
        get_missing_data_brush(props)

    if props.report != '':
        self.report({'ERROR_INVALID_INPUT'}, props.report)
        return {'CANCELLED'}

    if as_new:
        props.asset_base_id = ''
        props.id = ''
    export_data, upload_data, eval_path_computing, eval_path_state, eval_path, props = get_upload_data(self, context,
                                                                                                       asset_type)
    # utils.pprint(upload_data)
    upload_data['parameters'] = params_to_dict(
        upload_data['parameters'])  # weird array conversion only for upload, not for tooltips.

    binary_path = bpy.app.binary_path
    script_path = os.path.dirname(os.path.realpath(__file__))
    basename, ext = os.path.splitext(bpy.data.filepath)
    # if not basename:
    #     basename = os.path.join(basename, "temp")
    if not ext:
        ext = ".blend"
    tempdir = tempfile.mkdtemp()
    source_filepath = os.path.join(tempdir, "export_blenderkit" + ext)
    clean_file_path = paths.get_clean_filepath()
    data = {
        'clean_file_path': clean_file_path,
        'source_filepath': source_filepath,
        'temp_dir': tempdir,
        'export_data': export_data,
        'upload_data': upload_data,
        'debug_value': bpy.app.debug_value,
    }
    datafile = os.path.join(tempdir, BLENDERKIT_EXPORT_DATA_FILE)

    # check if thumbnail exists:

    if not os.path.exists(export_data["thumbnail_path"]):
        props.upload_state = 'Thumbnail not found'
        props.uploading = False
        return {'CANCELLED'}

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

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

    # upload_data['license'] = 'ovejajojo'
    json_metadata = upload_data  # json.dumps(upload_data, ensure_ascii=False).encode('utf8')
    global reports
    if props.asset_base_id == '':
        try:
            r = requests.post(url, json=json_metadata, headers=headers, verify=True)  # files = files,
            props.upload_state = 'uploaded metadata'
            utils.p(r.text)
        except requests.exceptions.RequestException as e:
            print(e)
            props.upload_state = str(e)
            props.uploading = False
            return {'CANCELLED'}

    else:
        url += props.id + '/'
        try:
            if not metadata_only:
                json_metadata["verificationStatus"] = "uploading"
            r = requests.put(url, json=json_metadata, headers=headers, verify=True)  # files = files,
            props.upload_state = 'uploaded metadata'
            # parse the reqest
            # print('uploaded metadata')
            # print(r.text)
        except requests.exceptions.RequestException as e:
            print(e)
            props.upload_state = str(e)
            props.uploading = False
            return {'CANCELLED'}

    # props.upload_state = 'step 1'
    if metadata_only:
        props.uploading = False

        return {'FINISHED'}
    try:
        rj = r.json()
        if props.asset_base_id == '':
            props.asset_base_id = rj['assetBaseId']
            props.id = rj['id']

        upload_data['assetBaseId'] = props.asset_base_id
        upload_data['id'] = props.id

        bpy.ops.wm.save_mainfile()
        # fa
        props.uploading = True
        # save a copy of actual scene but don't interfere with the users models
        bpy.ops.wm.save_as_mainfile(filepath=source_filepath, compress=False, copy=True)

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

        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)

        bg_blender.add_bg_process(eval_path_computing=eval_path_computing, eval_path_state=eval_path_state,
                                  eval_path=eval_path, process_type='UPLOAD', process=proc, location=location)

    except Exception as e:
        props.upload_state = str(e)
        props.uploading = False
        return {'CANCELLED'}

    return {'FINISHED'}
コード例 #3
0
ファイル: upload.py プロジェクト: sambler/myblenderaddons
def start_upload(self, context, asset_type, as_new, metadata_only):
    props = utils.get_upload_props()
    location = get_upload_location(props)
    props.upload_state = 'preparing upload'
    # do this for fixing long tags in some upload cases
    props.tags = props.tags[:]

    props.name = props.name.strip()
    # TODO  move this to separate function
    # check for missing metadata
    if asset_type == 'MODEL':
        get_missing_data_model(props)
    if asset_type == 'SCENE':
        get_missing_data_scene(props)
    elif asset_type == 'MATERIAL':
        get_missing_data_material(props)
    elif asset_type == 'BRUSH':
        get_missing_data_brush(props)

    if props.report != '':
        self.report({'ERROR_INVALID_INPUT'}, props.report)
        return {'CANCELLED'}
    if as_new:
        props.asset_base_id = ''
        props.id = ''
    export_data, upload_data, eval_path_computing, eval_path_state, eval_path, props = get_upload_data(self, context,
                                                                                                       asset_type)
    # utils.pprint(upload_data)
    upload_data['parameters'] = params_to_dict(
        upload_data['parameters'])  # weird array conversion only for upload, not for tooltips.

    binary_path = bpy.app.binary_path
    script_path = os.path.dirname(os.path.realpath(__file__))
    basename, ext = os.path.splitext(bpy.data.filepath)
    # if not basename:
    #     basename = os.path.join(basename, "temp")
    if not ext:
        ext = ".blend"
    tempdir = tempfile.mkdtemp()
    source_filepath = os.path.join(tempdir, "export_blenderkit" + ext)
    clean_file_path = paths.get_clean_filepath()
    data = {
        'clean_file_path': clean_file_path,
        'source_filepath': source_filepath,
        'temp_dir': tempdir,
        'export_data': export_data,
        'upload_data': upload_data,
        'debug_value': bpy.app.debug_value,
    }
    datafile = os.path.join(tempdir, BLENDERKIT_EXPORT_DATA_FILE)

    # check if thumbnail exists:

    if not os.path.exists(export_data["thumbnail_path"]):
        props.upload_state = 'Thumbnail not found'
        props.uploading = False
        return {'CANCELLED'}

    # first upload metadata to server, so it can be saved inside the current file
    url = paths.get_bkit_url() + 'assets/'
    headers = {"accept": "application/json", "Authorization": "Bearer %s" % upload_data['token']}
    # upload_data['license'] = 'ovejajojo'
    json_metadata = upload_data  # json.dumps(upload_data, ensure_ascii=False).encode('utf8')
    global reports
    if props.asset_base_id == '':
        try:
            r = requests.post(url, json=json_metadata, headers=headers, verify=True)  # files = files,
            props.upload_state = 'uploaded metadata'
        except requests.exceptions.RequestException as e:
            print(e)
            props.upload_state = str(e)
            props.uploading = False
            return {'CANCELLED'}

    else:
        url += props.id + '/'
        try:
            if not metadata_only:
                json_metadata["verificationStatus"] = "uploading"
            r = requests.put(url, json=json_metadata, headers=headers, verify=True)  # files = files,
            props.upload_state = 'uploaded metadata'
            # parse the reqest
            # print('uploaded metadata')
            # print(r.text)
        except requests.exceptions.RequestException as e:
            print(e)
            props.upload_state = str(e)
            props.uploading = False
            return {'CANCELLED'}

    # props.upload_state = 'step 1'
    if metadata_only:
        props.uploading = False

        return {'FINISHED'}
    try:
        rj = r.json()
        if props.asset_base_id == '':
            props.asset_base_id = rj['assetBaseId']
            props.id = rj['id']

        upload_data['assetBaseId'] = props.asset_base_id
        upload_data['id'] = props.id

        bpy.ops.wm.save_mainfile()
        # fa
        props.uploading = True
        # save a copy of actual scene but don't interfere with the users models
        bpy.ops.wm.save_as_mainfile(filepath=source_filepath, compress=False, copy=True)

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

        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)

        bg_blender.add_bg_process(eval_path_computing=eval_path_computing, eval_path_state=eval_path_state,
                                  eval_path=eval_path, process_type='UPLOAD', process=proc, location=location)

    except Exception as e:
        props.upload_state = str(e)
        props.uploading = False
        return {'CANCELLED'}

    return {'FINISHED'}