0
$\begingroup$

I've been having issues trying to hide an object based off name (as a UI checkbox).

It nearly works, hiding an object is somewhat decent, not perfect as sometimes you need to recheck to work. But the main issue is i can't go back and unhide it.

if "bpy" in locals():
    import importlib
    if "MK8_export_paths" in locals():
        importlib.reload(MK8_export_paths)


import bpy
from bpy.types import Operator
from bpy.props import FloatVectorProperty
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector
from bl_operators.presets import AddPresetBase

from bpy.props import (StringProperty,
                       BoolProperty,
                       IntProperty,
                       FloatProperty,
                       EnumProperty,
                       PointerProperty,
                       )
from bpy.types import (Panel,
                       AddonPreferences,
                       PropertyGroup,
                       )


def common_update(self, context, origin):

    obj = context.object
    scene = context.scene

    for ob in scene.objects:
        if scene.my_prop:
            if ob.name.startswith("Glide"):
                ob.hide = True
            else:
                ob.unhide = True
    else:
            ob.hide = False         


class MySettings(PropertyGroup):

    bpy.types.Scene.my_prop = bpy.props.BoolProperty(update=lambda self, context: 
    common_update(self, context, 'my_bool_one'))



class PathSETTINGS(Panel):
    bl_idname = "OBJECT_PT_my_panel"
    bl_label = "My Panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "TOOLS"    
    bl_category = "NewTest"



    def draw(self, context):
        obj = context.object
        layout = self.layout
        scene = context.scene
        layout.label("First row")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row = layout.row()
        row.prop(scene, "my_prop", text="Hide Object")








# Registration


def register():
    bpy.utils.register_class(MySettings)
    bpy.utils.register_class(PathSETTINGS)
    bpy.types.Scene.my_prop = bpy.props.PointerProperty(type=MySettings)



def unregister():
    bpy.utils.unregister_class(MySettings)
    bpy.utils.unregister_class(PathSETTINGS)
    del bpy.types.Scene.my_prop




if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ How does common_update get called? $\endgroup$ – batFINGER Nov 22 '17 at 2:49
  • $\begingroup$ Oh yeah sorry i forgot the part with the boolean when i was copying parts of my script to the site, added it to the top of the code. $\endgroup$ – nathan miller Nov 22 '17 at 3:50
  • 1
    $\begingroup$ Shouldn't draw be indented to be a class method rather than a global function? $\endgroup$ – Duane Dibbley Nov 22 '17 at 4:03
  • $\begingroup$ It's apart of the "PathSETTINGS" class, just the indentation seems to have messed up a bit. $\endgroup$ – nathan miller Nov 22 '17 at 4:25
  • $\begingroup$ Suggest making script such that it can be run as a working example from blender text editor. For the formatting required on bse martkup, select all (ctrl-a) , tab (moves all one tab over), copy and paste here. Also can you explain the reason for update=lambda self, context: common_update(self, context, 'my_bool_one') instead of simply update=common_update and remove the never used origin parameter? $\endgroup$ – batFINGER Nov 22 '17 at 4:29
1
$\begingroup$

Feel free to try/modify this

bl_info = {
    "name": "Blank1",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 79, 0),
    "location": "View3D > Tools panel 'T' > Blank1 > Blank2",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "category": "",
    }

import bpy
import os
from bpy.types import Operator
from bpy.types import Panel

def unhide_objects(scene):
    """Unhide all objects"""
    for ob in scene.objects:
        ob.hide = False

def ToggleHide(self, context):
    scene = self
    unhide_objects(scene)
    if scene.TestStringProp:
        for ob in scene.objects:
            if ob.name.startswith(str(scene.TestStringProp)):
                ob.hide = True
            else:
                ob.hide = False
    if not scene.TestBool:
        unhide_objects(scene)

def initprop():
    bpy.types.Scene.TestBool = bpy.props.BoolProperty(
        name="TestBool", 
        description="", 
        default=False, 
        subtype='NONE', 
        update=ToggleHide)

    bpy.types.Scene.TestStringProp = bpy.props.StringProperty(
        name="TestStringProp", 
        description="", 
        default="", 
        subtype='NONE')

def delprop():
    del bpy.types.Scene.TestBool
    del bpy.types.Scene.TestStringProp

class VIEW3D_PT_Blank1_Blank2(Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Blank1"
    bl_context = "objectmode"
    bl_label = "Blank2"

    def draw(self, context):
        scene = context.scene
        layout = self.layout
        row = layout.row()
        col = layout.column(align=True)
        col.label(text="Columns")
        col.prop(scene, "TestBool")
        col.prop(scene, "TestStringProp")

def register():
    bpy.utils.register_class(VIEW3D_PT_Blank1_Blank2)
    initprop()

def unregister():
    bpy.utils.unregister_class(VIEW3D_PT_Blank1_Blank2)
    delprop()

if __name__ == "__main__":
    register()
$\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.