def execute(self, context):
        active = Get.active(context)
        active_select = Is.selected(active)
        selected = Get.selected(context)
        is_pose = bool(context.mode == 'POSE')
        arg = dict(only_selected=bool(selected))

        if is_pose:
            clear = bpy.ops.pose.paths_clear

            # Operator only runs on active rig, so repeat for all in pose mode
            if selected:
                obs = {b.id_data for b in selected}
            else:
                obs = {ob for ob in Get.objects(context) if ob.mode == 'POSE'}

            for ob in obs:
                Set.active(context, ob)
                clear(**arg)
            else:
                Set.active(context, active)
                Set.select(active, active_select)
        elif bpy.ops.object.paths_clear.poll(context.copy()):
            bpy.ops.object.paths_clear(**arg)

        return {'FINISHED'}
Beispiel #2
0
def active(context, target):
    """Set target as active scene object or bone"""

    objects = Get.objects(context)

    # Remember previous active
    previous = Get.active(context)
    selected = Is.selected(target)

    # Set the active
    if Is.object(target):
        obj = target
    elif Is.posebone(target):
        obj = target.id_data
        obj.data.bones.active = obj.data.bones.get(target.name)
    elif isinstance(target, bpy.types.Armature):
        obj = Get.rig(context, target)
    elif Is.bone(target):
        obj = Get.rig(context, target)
        bones = target.id_data.bones
        bones.active = bones.get(target.name)
    elif Is.editbone(target):
        obj = Get.rig(context, target)

        if obj: in_edit = (obj.mode == 'EDIT')
        else: in_edit = (context.mode == 'EDIT_ARMATURE')

        if in_edit:
            bones = target.id_data.edit_bones
            bones.active = bones.get(target.name)
    elif target is None:
        obj = None
        # debug("Set.active() has None as the target")
    else:
        assert None, ("Set.active() can't use the provided target", target)

    if (target and Is.selected(target) != selected):
        # When setting a bone as active in a rig, it gets selected as well.
        Set.select(target, selected)
    objects.active = obj

    return previous
Beispiel #3
0
def selected_edit_bones(context, src=None):
    "Always return edit bones as list, never as None"

    if src:
        bones = src.data.edit_bones
        selected = [
            b for b in bones if Is.selected(b) and Is.visible(context, b)
        ]
    else:
        selected = Get.as_list(context, 'selected_editable_bones')

    return selected
Beispiel #4
0
def selected_pose_bones(context,
                        src=None,
                        force: "not needed, todelete" = False):
    "Always return pose bones as list, never as None"

    if src:
        selected = [
            b for b in src.pose.bones
            if Is.selected(b) and Is.visible(context, b)
        ]
    # elif not force and context.mode not in ('POSE', 'PAINT_WEIGHT'):
    # selected = []
    else:
        selected = Get.as_list(context, 'selected_pose_bones')

    return selected
Beispiel #5
0
def get_selected_keys_and_extents():
    context = bpy.context
    pbones = context.selected_pose_bones
    if pbones is None:
        pbones = []
    curve_datas = []
    selected = []
    objects = []
    bones = []
    fcurves = []

    try:
        only_selected = context.space_data.dopesheet.show_only_selected
        show_hidden = context.space_data.dopesheet.show_hidden
    except:
        only_selected = True
        show_hidden = False

    def add_obj(obj):
        if show_hidden is False and not Is.visible(context, obj):
            return None
        if obj not in selected:
            selected.append(obj)

    def add_bone(b):
        if only_selected and not b.bone.select:
            return None
        add_obj(b.id_data)
        bones.append(b)

    for obj in Get.objects(context):
        if show_hidden is False and not Is.visible(context, obj):
            continue

        # Add object and bones
        if not (only_selected and not Is.selected(obj)):
            add_obj(obj)
        if obj.pose is not None:
            for (name, pbone) in obj.pose.bones.items():
                if any((only_selected is False, Is.selected(obj), pbone in pbones,)):
                    add_bone(pbone)

    # Add fcurves from objects
    for obj in selected:
        anim = obj.animation_data
        if anim and anim.action:
            fcurves.extend([(obj, fc) for fc in anim.action.fcurves])

    # Scan fcurves for keyframes
    for obj, curve in fcurves:
        if curve.hide or curve.lock or not curve.select:
            continue
        first_co = None
        points = None
        last_co = None
        path = curve.data_path

        # Read path to get target's name
        if (path.startswith('pose.bones')):
            # btype =   'BONE'
            # bpath =   path.split('"]', 1)[1]      ## Transforms and custom prop
            # if (bpath.startswith('.')):       ## constraints?
                # bpath =   bpath.split('.', 1)[1]
            bname = (path.split('["', 1)[1].split('"]', 1)[0])
            bone = obj.pose.bones.get(bname)
        elif (path.startswith('bones')):  # data.bones
            # btype = 'BONE'
            # bpath = path.split('"].', 1)[1]
            bname = (path.split('["', 1)[1].split('"]', 1)[0])
            bone = obj.bones.get(bname)
        else:
            # btype = 'OBJECT'
            # bpath = path
            bname = obj.name
            bone = obj

        if (bone is None and curve.is_valid is True) or (bone is not None and bone != obj and bone not in bones):
            # Bone not selected
            continue

        keyframes_referenced = []
        keyframes_data = []
        for keyframe in curve.keyframe_points:
            if keyframe.select_control_point:
                if first_co is None:
                    first_co = keyframe.co
                else:
                    last_co = keyframe.co
                keyframes_referenced.append(keyframe)
                keyframes_data.append({
                    'co': deepcopy(keyframe.co),
                    'handle_left': deepcopy(keyframe.handle_left),
                    'handle_right': deepcopy(keyframe.handle_right)
                })  # needs to be all three data points!
        if last_co is not None:
            curve_datas.append([keyframes_referenced, first_co, last_co, keyframes_data, curve])
    return curve_datas
Beispiel #6
0
def mode(context, mode, target=None, keep_visiblity=True):
    """
    Set the context.mode for an object (or bone's rig)
    """
    if not target:
        bpy.ops.object.mode_set(mode=mode)
        return context.mode == mode

    target = target.id_data
    # I can't think of a situation where I care to use
    # a bone/etc instead of the object

    # objects = context.selected_objects
    # for obj in objects:
    # select(obj, False)

    if Is.linked(target) and mode not in ('OBJECT', 'POSE'):
        return False

    class active_item:
        mode = target.mode
        is_visible = Set.visible(context, target, True)

    if mode != target.mode:
        modes = dict()
        selected = list()
        objects = list()

        # Find the visible objects of the same type as the target object
        # Remember their modes and selection, then deselect them
        for obj in Get.in_view(context):
            if obj == target or obj.type != target.type or not Is.visible(
                    context, obj):
                continue
            if obj.mode not in modes:
                modes[obj.mode] = list()
            modes[obj.mode].append(obj)

            if Is.selected(obj):
                selected.append(obj)
            Set.select(obj, False)
            objects.append(obj)

        # Remember the target's selected status
        pselect = Is.selected(target)

        # Set the mode for the target object
        previous = Set.active(context, target)
        Set.select(target, False)
        bpy.ops.object.mode_set(mode=mode)

        # Since the operator switches all objects to the specified mode
        # Go through the objects and manually set them back their previous mode
        for pmode in modes:
            for obj in modes[pmode]:
                Set.select(obj, True)
            Set.active(context, obj)
            bpy.ops.object.mode_set(mode=pmode)
            Set.active(context, None)
            for obj in modes[pmode]:
                Set.select(obj, False)

        # Re-select previously selected objects
        for obj in selected:
            Set.select(obj, True)

        # reselect target if it was selected
        Set.select(target, pselect)

        # Set the active object back to the original
        if previous is not None:
            Set.active(context, previous)
        else:
            Set.active(context, target)

    if (keep_visiblity):
        Set.visible(context, target, active_item.is_visible)

    # for obj in objects:
    # select(obj, True)

    return (target.mode == mode)