__commandName__ = 'gridCheck' __commandDisplayName__ = 'Select off-grid worldspawn brushes' def execute(): def getBrushVerts(brushNode): # return the set of Vertex coordinates. Doesn't dedupe. verts = [] for i in range(brushNode.getNumFaces()): for v in brushNode.getFace(i).getWinding(): verts.append(v) return verts def onGrid (brushNode, gridsize): for v in getBrushVerts(brushNode): if v.vertex.x() % gridsize != 0: return False if v.vertex.y() % gridsize != 0: return False if v.vertex.z() % gridsize != 0: return False return True def requestGridSize(): # query the user for grid size to use # Test creating a new dialog dialog = GlobalDialogManager.createDialog("Find off-grid brushes") # Add a combo box, the options must be passed in the form of a StringVector options = StringVector() options.append("0.125") options.append("0.25") options.append("0.5") options.append("1") options.append("2") options.append("4") options.append("8") options.append("16") choice = dialog.addComboBox("Grid size", options) dialog.setElementValue(choice, "0.125") if dialog.run() == Dialog.OK: return float(dialog.getElementValue(choice)) else: raise Exception("User clicked cancel") class OffGridSelector(SceneNodeVisitor): # Traverse worldspawn brushes, and check all verts are on grid # If not, select the brush def __init__(self, gridsize): SceneNodeVisitor.__init__(self) self.gridsize = gridsize def pre(self, node): if node.isEntity(): classname = node.getEntity().getEntityClass().getAttribute('name').getValue() if classname=='world': return 1 # traverse children of world else: return 0 # but don't traverse entities other than world elif node.isBrush(): # Check verts are on-grid if not onGrid(node.getBrush(), self.gridsize): node.setSelected(1) return 1 # not an entity, return true to traverse children # execution code # Get grid size from user and allow them to cancel checker = OffGridSelector(requestGridSize()) # clear current selection GlobalSelectionSystem.setSelectedAll(0) GlobalSceneGraph.root().traverse(checker) # __executeCommand__ evaluates to true after DarkRadiant has successfully initialised if __executeCommand__: execute()