Creating bgeo files from selected objects

After importing FBX files, hip files are getting very large. To avoid saving all geometry for every scene it makes sense to save the data to disk. For 3-4 objects it’s no big deal to do this by hand, but when dealing with hundreds of objects, this is getting annoying. Here is a small snipped, that reads the current houdini selection and exports bgeo.sc files to $HIP/geo/`opname(“..”)`/`opname(“..”)`.bgeo.sc. This is really the basic approach without any versioning, it gives you a good starting point how to set this up.

import hou  
oSelection = hou.selectedItems()

if oSelection == 0:
    print "nothing is selected"
else:
    for oSelectedNode in oSelection:
        print oSelectedNode.name()
        c = oSelectedNode.children()
        for n in c:
            p = n.path()
            node = hou.node(p)
            type = hou.nodeFlag.Render
            flag = node.isGenericFlagSet(type)
            if flag == 1:
                oFlaggedNode = n
                #print (p + " Render Flag is " + str(flag))
        oNewROPNode = oSelectedNode.createNode("rop_geometry")
        oNewROPNode.setInput(0, oFlaggedNode )
        oNewROPNode.parm("sopoutput").set('$HIP/geo/`opname("..")`/`opname("..")`.bgeo.sc')
        oNewROPNode.render()
        oNewFileNode = oSelectedNode.createNode("file")
        oNewFileNode.parm("file").set('$HIP/geo/`opname("..")`/`opname("..")`.bgeo.sc')
        oNewFileNode.setDisplayFlag(True)
        oNewFileNode.setRenderFlag(True)
        c = oSelectedNode.children()
        for n in c:
            p = n.path()
            node = hou.node(p)
            type = hou.nodeFlag.Render
            flag = node.isGenericFlagSet(type)
            if flag == 0:
                n.destroy()