3
$\begingroup$

I have an armature with multiple bones and a different object connected to each bone enter image description here

when I hide my bone I want the object that is connected to it to hide as well

How do I achieve this? maybe via driver?

$\endgroup$
5
  • $\begingroup$ Is that for animation? $\endgroup$
    – Crantisz
    Jun 22 at 9:37
  • $\begingroup$ You can't except maybe with a script $\endgroup$
    – moonboots
    Jun 22 at 9:52
  • $\begingroup$ you could do it with a driver, but then you have to adapt every driver manually...so really tedious $\endgroup$
    – Chris
    Jun 22 at 10:17
  • $\begingroup$ @Crantisz Yes it's for animation $\endgroup$
    – cakelover
    Jun 22 at 10:35
  • $\begingroup$ @Chris all I want is the object to hide when bone hides, is that possible with a driver? $\endgroup$
    – cakelover
    Jun 22 at 10:35

3 Answers 3

3
$\begingroup$

yes you can, but it works "not fine":

enter image description here

right click on your bones "TV" -> copy as new driver

then right click on your objects "TV" and click "paste driver".

Then it "kind of" works.

The disadvantage: if you are in pose mode and press H you have to change to object/edit mode so that the object hides too....

To make it work in rendering as well, copy paste the driver to the object camera Icon.

$\endgroup$
10
  • $\begingroup$ does it work for render viewport animation? $\endgroup$
    – cakelover
    Jun 22 at 10:48
  • $\begingroup$ yes, if you change object/edit mode before rendering...as i said, it is pretty weird. I honestly don't know whether it is a bug...for me it looks like one. $\endgroup$
    – Chris
    Jun 22 at 10:49
  • $\begingroup$ I did as you did but when I hide the bone the object does not hide with it $\endgroup$
    – cakelover
    Jun 22 at 10:52
  • 1
    $\begingroup$ aah you have to switch it like 2 time, yea it's weird but as long as it works in rendering it should be fine $\endgroup$
    – cakelover
    Jun 22 at 10:54
  • 1
    $\begingroup$ ah ha! I found a way to make it work in rendering as well! instead of copy pasting from bone TV icon copy paste the driver to the object camera Icon, add that to your answer and I'll mark it as solved $\endgroup$
    – cakelover
    Jun 22 at 11:03
2
$\begingroup$

Good to know the more simple answer works, but I also wrote a custom operator to accomplish this task, It works in pose and edit_armature mode, but does not yet have an "unhide" method that works in reverse. I will leave this to you to reverse engineer how to accomplish.

It only works if the object is parented to the bone via Parent> Bone not Parent > With Automatic Weights or any of the other ones, but I figure you know this.

You can just run the script in the text editor, call it from the search menu and then set a hotkey for it there.

import bpy


class ToggleHiddenObjectBone(bpy.types.Operator):
    bl_idname = "object.toggle_hide_bone_object"
    bl_label = "Toggle Hide on Parented Bones and Objects"
    bl_options = {"REGISTER", "UNDO"}
    

    @classmethod
    def poll(cls, context):
        return context.active_bone is not None and context.mode in {"POSE", "EDIT_ARMATURE"}
    
    def invoke(self, context, event):
        self.mode = context.mode
        self.parent_bones = dict(zip(
            [obj.parent_bone for obj in bpy.data.objects],
            [obj.name for obj in bpy.data.objects],
            ))
        
        self.bone = context.active_bone
        self.armature = [
            arm for arm in bpy.data.armatures[:] 
            if self.bone.basename in 
            [bone.basename for bone in arm.bones[:]
            ]][0]
        try:
            self.obj_name = self.parent_bones[self.bone.name]
        except KeyError:
            self.report({"INFO"}, "Bone not found in object parent bones, make sure you set 'Parent > Bone'")
            return {"CANCELLED"}
        if not self.obj_name:
            self.report({"INFO"}, "Active bone has no child object.")
            return {"CANCELLED"}
        else:
            return self.execute(context)
            
        
    def execute(self, context):
        obj = bpy.data.objects[self.obj_name]
        if self.mode == "POSE":
            bpy.ops.pose.hide()
        else:
            bpy.ops.armature.hide()
        bpy.ops.object.mode_set(mode="OBJECT")
        bpy.ops.object.select_all(action="DESELECT")
        context.view_layer.objects.active = obj
        obj.select_set(True)        
        bpy.ops.object.hide_view_set() 
        context.view_layer.objects.active = bpy.data.objects[self.armature.name]
        print(context.view_layer.objects.active)
        try:
            bpy.ops.object.mode_set(mode=self.mode)
        except TypeError:
            bpy.ops.object.mode_set(mode="EDIT")
    
        return {'FINISHED'}




# Register and add to the "object" menu (required to also use F3 search "Simple Object Operator" for quick access).
def register():
    bpy.utils.register_class(ToggleHiddenObjectBone)
   
def unregister():
    bpy.utils.unregister_class(ToggleHiddenObjectBone)

if __name__ == "__main__":
    register()

$\endgroup$
0
1
$\begingroup$

Using this answer

you can achieve the same process via scripting, just in case you want to automate the process

import bpy
from bpy import context

def add_hide_driver(
        source, target, prop, dataPath,
        index = -1, negative = False, func = ''):

    if index != -1:
        d = source.driver_add( prop, index ).driver
    else:
        d = source.driver_add( prop ).driver

    v = d.variables.new()
    v.name                 = prop
    v.targets[0].id_type   = 'ARMATURE'
    v.targets[0].id        = target
    v.targets[0].data_path = dataPath

    d.expression = func + "(" + v.name + ")" if func else v.name
    d.expression = d.expression if not negative else "-1 * " + d.expression

armature=bpy.data.armatures["Armature"]

for bone in armature.bones:
    target_obj=bpy.data.objects[bone.name]
    add_hide_driver(target_obj,bone.id_data, 'hide_render', 'bones["'+bone.name+'"].hide')

This will achieve the same thing as copying driver from TV to Camera as mentioned in this answer

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.