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
def visible(context, object, value=True, **kargs): """ Set an object's (or bone's object's) visibility to the specified value """ scn = context.scene if not Is.object(object): if isinstance(object, bpy.types.Collection): found = False def loop(root, tree=list()): nonlocal found if root.collection == object: return True for child in root.children: if loop(child, tree): found = True tree.append(child) break if found: return tree view_layer = kargs.get('view_layer', False) if not view_layer: object.hide_viewport = not value if value or view_layer: # Only enables the collection for the view layer once tree = loop(context.view_layer.layer_collection) for col in tree: if (col.exclude == value) and (col.name == object.name): # When a collection is enabled in the view layer, # all of its child collections are as well. col.exclude = not value if value and col.collection.hide_viewport: col.collection.hide_viewport = False elif Is.posebone(object): return Set.visible(context, object.id_data, value) elif Is.bone(object) or Is.editbone(object): return Set.visible(context, Get.rig(context, object), value) else: assert None, ( "Set.visible() does not work with the specified item", object, ) return Set.in_scene(context, object) is_visible = Is.visible(context, object) object_visible = not object.hide_viewport # if Is.visible(context, object) is value: # return visible while (Is.visible(context, object) is not value): "If object isn't in the desired visiblity, loop until it is" if (object.hide_viewport is value): object.hide_viewport = not value continue is_visible = object_visible view = None for collection in object.users_collection: view = context.view_layer.layer_collection.children.get( collection.name) if not view: # collection isn't in scene or whatever continue if view.hide_viewport is value: view.hide_viewport = not value break if view is None: assert None, ( "Set.visible(): Object[", object, "] \nis hidden from viewport and I don't know how to change it" ) # collection.hide_viewport = value break return is_visible
def edit_func(self, context, bone): rig = Get.rig(context, bone.id_data) ebones = rig.data.edit_bones (do_mch, do_start_end, do_in_out) = self.do() def get_disconnected_parent(bone, first_loop=True): if ((bone is None) or (not bone.parent)): if first_loop: return else: return bone elif Is.connected(bone): # Keep going up the chain until it finds a disconnected bone return get_disconnected_parent(bone.parent, False) else: return bone.parent def reset(bone, edit_bone): attributes = [ 'head', 'head_radius', 'tail', 'tail_radius', 'roll', 'matrix', 'layers', 'bbone_x', 'bbone_z', ] for atr in attributes: if hasattr(bone, atr): setattr(bone, atr, getattr(edit_bone, atr)) def edit(ebone, bbone_xz=1.0): reset(ebone, bone) ebone.bbone_x *= bbone_xz ebone.bbone_z *= bbone_xz ebone.use_deform = False ebone.inherit_scale = 'NONE' ebone.hide = True def edit_mch(ebone): edit(ebone, 1.25) ebone.parent = bone.parent ebone.inherit_scale = bone.inherit_scale # ebone.use_connect = bone.use_connect # bone.use_connect = False # bone.parent = ebone # for cbone in bone.children: # cbone.parent = ebone def edit_start(ebone): edit(ebone, 2.5) if do_mch: ebone.parent = bone_mch else: if Is.connected(bone): ebone.parent = ebones.get( get_name(bone.parent, 'bbone_end')) if ebone.parent: self.hide_bones[rig].append(ebone.name) ebone.hide = True else: ebone.parent = bone.parent if not do_in_out: cbone = ebones.get(get_name(bone, 'bbone_in')) if cbone: cbone.parent = ebone for cbone in bone.children_recursive: if (bone.head != cbone.tail): continue cbone_end = ebones.get(get_name(cbone, 'bbone_end')) if cbone_end: cbone_end.parent = ebone self.hide_bones[rig].append(cbone_end.name) cbone_end.hide = True ebone.tail = utils.lerp(ebone.head, ebone.tail, 0.1) def edit_head(ebone): edit(ebone, 0.5) ebone.parent = bone_start ebone.tail = utils.lerp(ebone.head, ebone.tail, 0.1) ebone.translate(ebone.head - ebone.tail) bone.bbone_custom_handle_start = ebone self.hide_bones[rig].append(ebone.name) def edit_end(ebone): edit(ebone, 2.5) if do_mch: ebone.parent = bone_mch else: for tbone in bone.parent_recursive: if (tbone.head != bone.tail): continue tobone_name = get_name(tbone, 'bbone_start') tobone = ebones.get(tobone_name) if tobone or ((tbone, rig) in self.selected): self.hide_bones[rig].append(ebone.name) ebone.hide = True if tobone: ebone.parent = tobone else: self.delayed_parenting.append( ebones, ebone.name, tobone_name) else: ebone.parent = tbone break else: ebone.parent = get_disconnected_parent(bone) if not do_in_out: cbone = ebones.get(get_name(bone, 'bbone_out')) if cbone: cbone.parent = ebone for cbone in bone.children: if Is.connected(cbone): cbone_start = ebones.get(get_name(cbone, 'bbone_start')) if cbone_start: cbone_start.parent = ebone self.hide_bones[rig].append(cbone_start.name) cbone_start.hide = True ebone.head = utils.lerp(ebone.head, ebone.tail, 0.9) ebone.translate(ebone.tail - ebone.head) bone.bbone_custom_handle_end = ebone def edit_in(ebone): edit(ebone, 2.0) if do_start_end: ebone.parent = bone_start else: ebone.parent = ebones.get(get_name(bone, 'bbone_start'), bone) (head, tail) = (ebone.head.copy(), ebone.tail.copy()) ebone.head = utils.lerp(head, tail, 0.1) ebone.tail = utils.lerp(head, tail, 0.2) def edit_out(ebone): edit(ebone, 2.0) if do_start_end: ebone.parent = bone_end else: ebone.parent = ebones.get(get_name(bone, 'bbone_end'), bone) (head, tail) = (ebone.head.copy(), ebone.tail.copy()) ebone.tail = utils.lerp(head, tail, 0.8) ebone.head = utils.lerp(head, tail, 0.9) ebone.align_roll(-bone.z_axis) # This bone is reversed, so the the roll needs to be flipped if (do_in_out and (not (do_mch or do_start_end)) and (bone.bbone_segments < 2)): # parenting to the bone will cause dependency loop, with drivers # if the bone isn't using bbones if not (ebones.get(get_name(bone, 'bbone_start'), ebones.get(get_name(bone, 'bbone_end')))): if self.warning: self.warning = ( f"{bone.name} does not have Bendy Bone Segments;" " this will cause a dependency cycle-loop with its drivers/controllers" ) else: self.warning = ( f"{self.warnings + 1} bones don't have Bendy Bone Segments;" " this will cause a dependency cycle-loop with their drivers/controllers" ) self.warnings += 1 if do_start_end: bone.bbone_handle_type_start = bone.bbone_handle_type_end = 'ABSOLUTE' args = dict(context=context, armature=rig, overwrite=True) if do_mch: bone_mch = New.bone(**args, name=get_name(bone, 'bbone'), edit=edit_mch) if do_start_end: bone_start = New.bone(**args, name=get_name(bone, 'bbone_start'), edit=edit_start) bone_end = New.bone(**args, name=get_name(bone, 'bbone_end'), edit=edit_end) bone_head = New.bone(**args, name=get_name(bone, 'bbone_head'), edit=edit_head) if do_in_out: bone_in = New.bone(**args, name=get_name(bone, 'bbone_in'), edit=edit_in) bone_out = New.bone(**args, name=get_name(bone, 'bbone_out'), edit=edit_out)