5
$\begingroup$

Update:

Let me ask a slightly different way, since I may be over-complicating things.

If working from the UI there's an option to do this:

enter image description here

You can Shift-Click to hide parent and children objects. How do you accomplish this programmatically? The only way I've found so far just hides the main object:

row.prop(wall, "hide_viewport")

Original Question

I'm working on an addon that's essentially a kitchen designer, so the base objects that need tracking are walls. Each wall has objects snap to it and become the parent object for everything underneath.

The walls need the ability to be hidden, along with all of the associated child objects. This requires a function for a little processing. If all of the walls could be defined ahead of time, I know how to accomplish this (create the class functions and register them, etc).

However walls are added as needed, with a simple loop that tracks and displays them:

    row.label(text="Walls")

    if object.expanded_walls:
        row = box.row()
        for wall in objs:
            row = box.row()
            row.prop(wall, "name")
        --> row.##boolean_flag(script_that_hides, arg_with_wall_id)

This list is updated dynamically in the display, so displaying any wall property is easy.

But, I can't figure out how to put a simple boolean flag with each line item capable of calling a function data from objects in the same row. Any way to do that?

$\endgroup$

2 Answers 2

5
$\begingroup$

Boolean with update

enter image description here

  • A hide_children boolean property is added to all objects.
  • If the property is updated all children are hidden, plus their hide_children is set, propagating the hide down the hierarchy.

2.8x Test script, with property drawn on text editor footer.

import bpy
from bpy.props import BoolProperty


def hide_children(self, context):
    self.hide_set(self.hide_children)
    for o in self.children:
        o.hide_children = self.hide_children


bpy.types.Object.hide_children = BoolProperty(update=hide_children)


def draw(self, context):
    layout = self.layout
    ob = context.object
    layout.prop(ob, "hide_children")


bpy.types.TEXT_HT_footer.append(draw)

Similarly to only toggle children

def hide_children(self, context):
    for o in self.children:
        o.hide_set(self.hide_children)
        o.hide_children = self.hide_children

These are object properties, to layout for all objects in scene

for o in scene.objects:
    layout.prop(o, "hide_children")

Note: For 2.8x would be very tempted to do this with collections, and their hierarchy instead of objects.

$\endgroup$
2
  • $\begingroup$ Is it possible to pass this function an object that isn't currently the active object? For instance: "row.operator('view3d.hide_children', text= "Text") sits inside a panel as a button. When pressed I would like to send it the object info contained in that row, but it doesn't appear these can take other params. Either way, looks like you are correct about reorganizing according to Collections... Seems to be much easier to control if everything is laid out that way instead. $\endgroup$
    – Sam Vimes
    Commented Aug 2, 2019 at 16:55
  • $\begingroup$ See latest edit. $\endgroup$
    – batFINGER
    Commented Aug 3, 2019 at 7:45
0
$\begingroup$

Only for first level children:

# hide the parent object
bpy.data.objects["parent"].hide_viewport = True

# get the list of children
list_of_children = bpy.data.objects["parent"].children

# hide the children
for obj in list_of_children:
    obj.hide_viewport = True

Now for all level children:

def recursive_child (list):

    children_list = []    
    for obj in list:
        if obj.children:
            print (obj.children)
            children_list.append(obj.children)

        obj.hide_viewport = True  # hide the child
#        obj.hide_viewport = False  # use this if you want to show the child
    print (">>children_list: " + str(children_list))
    if children_list:
        for child in children_list:
            print ("going deeper")
            recursive_child (child)

recursive_child(bpy.data.objects["parent"].children)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .