Exemple #1
0
def queue_worker():
    #bk_logger.debug('timer queue worker')
    time_step = 2.0
    q = get_queue()

    back_to_queue = []  #delayed events
    stashed = {}
    # first round we get all tasks that are supposed to be stashed and run only once (only_last option)
    # stashing finds tasks with the property only_last and same command and executes only the last one.
    while not q.empty():
        # print('queue while 1')

        task = q.get()
        if task.only_last:
            #this now makes the keys not only by task, but also first argument.
            # by now stashing is only used for ratings, where the first argument is url.
            # This enables fast rating of multiple assets while allowing larger delay for uploading of ratings.
            # this avoids a duplicate request error on the server
            stashed[str(task.command) + str(task.arguments[0])] = task
        else:
            back_to_queue.append(task)
    if len(stashed.keys()) > 1:
        bk_logger.debug('task queue stashed task:' + str(stashed))
    #return tasks to que except for stashed
    for task in back_to_queue:
        q.put(task)
    #return stashed tasks to queue
    for k in stashed.keys():
        q.put(stashed[k])
    #second round, execute or put back waiting tasks.
    back_to_queue = []
    while not q.empty():
        # print('window manager', bpy.context.window_manager)
        task = q.get()

        if task.wait > 0:
            task.wait -= time_step
            back_to_queue.append(task)
        else:
            bk_logger.debug('task queue task:' + str(task.command) +
                            str(task.arguments))
            try:
                if task.fake_context:
                    fc = utils.get_fake_context(
                        bpy.context, area_type=task.fake_context_area)
                    task.command(fc, *task.arguments)
                else:
                    task.command(*task.arguments)
            except Exception as e:
                bk_logger.error('task queue failed task:' + str(task.command) +
                                str(task.arguments) + str(e))
                # bk_logger.exception('Got exception on main handler')
                # raise
        # print('queue while 2')
    for task in back_to_queue:
        q.put(task)
    return 2.0
def queue_worker():
    time_step = 2.0
    q = get_queue()

    back_to_queue = []  #delayed events
    stashed = {}
    # first round we get all tasks that are supposed to be stashed and run only once (only_last option)
    # stashing finds tasks with the property only_last and same command and executes only the last one.
    while not q.empty():
        task = q.get()
        if task.only_last:
            stashed[task.command] = task
        else:
            back_to_queue.append(task)
    #return tasks to que except for stashed
    for task in back_to_queue:
        q.put(task)
    #return stashed tasks to queue
    for k in stashed.keys():
        q.put(stashed[k])
    #second round, execute or put back waiting tasks.
    back_to_queue = []
    while not q.empty():
        # print('window manager', bpy.context.window_manager)
        task = q.get()

        if task.wait > 0:
            task.wait -= time_step
            back_to_queue.append(task)
        else:
            utils.p('as a task:   ')
            utils.p(task.command, task.arguments)
            try:
                if task.fake_context:
                    fc = utils.get_fake_context(
                        bpy.context, area_type=task.fake_context_area)
                    task.command(fc, *task.arguments)
                else:
                    task.command(*task.arguments)
            except Exception as e:
                utils.p('task failed:')
                print(e)
    for task in back_to_queue:
        q.put(task)
    return 2.0
def append_objects(file_name,
                   obnames=[],
                   location=(0, 0, 0),
                   link=False,
                   **kwargs):
    '''append objects into scene individually'''
    #simplified version of append
    if kwargs.get('name'):
        # by now used for appending into scene
        scene = bpy.context.scene
        sel = utils.selection_get()
        bpy.ops.object.select_all(action='DESELECT')

        path = file_name + "\\Collection\\"
        object_name = kwargs.get('name')
        fc = utils.get_fake_context(bpy.context, area_type='VIEW_3D')
        bpy.ops.wm.append(fc, filename=object_name, directory=path)

        return_obs = []
        for ob in bpy.context.scene.objects:
            if ob.select_get():
                return_obs.append(ob)
                if not ob.parent:
                    main_object = ob
                    ob.location = location

        if kwargs.get('rotation'):
            main_object.rotation_euler = kwargs['rotation']

        if kwargs.get('parent') is not None:
            main_object.parent = bpy.data.objects[kwargs['parent']]
            main_object.matrix_world.translation = location

        bpy.ops.object.select_all(action='DESELECT')
        utils.selection_set(sel)

        return main_object, return_obs
    #this is used for uploads:
    with bpy.data.libraries.load(file_name, link=link,
                                 relative=True) as (data_from, data_to):
        sobs = []
        # for col in data_from.collections:
        #     if col == kwargs.get('name'):
        for ob in data_from.objects:
            if ob in obnames or obnames == []:
                sobs.append(ob)
        data_to.objects = sobs
        # data_to.objects = data_from.objects#[name for name in data_from.objects if name.startswith("house")]

    # link them to scene
    scene = bpy.context.scene
    sel = utils.selection_get()
    bpy.ops.object.select_all(action='DESELECT')

    return_obs = [
    ]  # this might not be needed, but better be sure to rewrite the list.
    main_object = None
    hidden_objects = []
    #
    for obj in data_to.objects:
        if obj is not None:
            # if obj.name not in scene.objects:
            scene.collection.objects.link(obj)
            if obj.parent is None:
                obj.location = location
                main_object = obj
            obj.select_set(True)
            # we need to unhide object so make_local op can use those too.
            if link == True:
                if obj.hide_viewport:
                    hidden_objects.append(obj)
                    obj.hide_viewport = False
            return_obs.append(obj)

    # Only after all objects are in scene! Otherwise gets broken relationships
    if link == True:
        bpy.ops.object.make_local(type='SELECT_OBJECT')
        for ob in hidden_objects:
            ob.hide_viewport = True

    if kwargs.get('rotation') is not None:
        main_object.rotation_euler = kwargs['rotation']

    if kwargs.get('parent') is not None:
        main_object.parent = bpy.data.objects[kwargs['parent']]
        main_object.matrix_world.translation = location

    bpy.ops.object.select_all(action='DESELECT')

    utils.selection_set(sel)

    return main_object, return_obs