From a506ed3c5af8b697503baeac851e9a909433e974 Mon Sep 17 00:00:00 2001 From: codereader Date: Fri, 21 Jul 2017 10:29:09 +0200 Subject: [PATCH] Fixup all command scripts - namespace handling --- install/scripts/commands/ase_export.py | 14 +++++++------ install/scripts/commands/ase_export_blend.py | 14 +++++++------ .../commands/check_for_invalid_visportals.py | 6 ++++-- .../commands/convert_to_ase_and_replace.py | 20 +++++++++++-------- install/scripts/commands/example.py | 5 +++++ install/scripts/commands/patchsplitter.py | 5 ++++- .../commands/select_all_models_of_type.py | 6 ++++-- install/scripts/commands/test_targets.py | 13 +++++++----- 8 files changed, 53 insertions(+), 30 deletions(-) diff --git a/install/scripts/commands/ase_export.py b/install/scripts/commands/ase_export.py index 4572ddb0ce..9bc953ece4 100644 --- a/install/scripts/commands/ase_export.py +++ b/install/scripts/commands/ase_export.py @@ -28,13 +28,15 @@ def execute(): author = "Richard Bartlett, some additions by greebo and tels" version = "0.9" + import darkradiant as dr + # Check if we have a valid selection selectionInfo = GlobalSelectionSystem.getSelectionInfo() # Don't allow empty selections or selected components only if selectionInfo.totalCount == 0 or selectionInfo.totalCount == selectionInfo.componentCount: - errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', Dialog.ERROR) + errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', dr.Dialog.ERROR) errMsg.run() return @@ -117,7 +119,7 @@ def processPrimitive(scenenode): return # Traversor class to visit child primitives of entities - class nodeVisitor(SceneNodeVisitor): + class nodeVisitor(dr.SceneNodeVisitor): def pre(self, scenenode): # Brush? if scenenode.isBrush(): @@ -129,7 +131,7 @@ def pre(self, scenenode): # Traverse all child nodes, regardless of type return 1 - class dataCollector(SelectionVisitor): + class dataCollector(dr.SelectionVisitor): def visit(self, scenenode): if scenenode.isBrush() or scenenode.isPatch(): @@ -161,7 +163,7 @@ def visit(self, scenenode): exportCaulkHandle = dialog.addCheckbox("Export caulked faces") dialog.setElementValue(exportCaulkHandle, GlobalRegistry.get('user/scripts/aseExport/exportcaulk')) - if dialog.run() == Dialog.OK: + if dialog.run() == dr.Dialog.OK: fullpath = dialog.getElementValue(pathHandle) + '/' + dialog.getElementValue(fileHandle) if not fullpath.endswith('.ase'): fullpath = fullpath + '.ase' @@ -175,8 +177,8 @@ def visit(self, scenenode): try: file = open(fullpath, 'r') file.close() - prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', Dialog.ASK) - if prompt.run() == Dialog.YES: + prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', dr.Dialog.ASK) + if prompt.run() == dr.Dialog.YES: overwrite = True else: overwrite = False diff --git a/install/scripts/commands/ase_export_blend.py b/install/scripts/commands/ase_export_blend.py index e6a3230da3..269e37d94a 100644 --- a/install/scripts/commands/ase_export_blend.py +++ b/install/scripts/commands/ase_export_blend.py @@ -28,13 +28,15 @@ def execute(): author = "Richard Bartlett, some additions by greebo and tels" version = "0.71" + import darkradiant as dr + # Check if we have a valid selection #import time selectionInfo = GlobalSelectionSystem.getSelectionInfo() # Don't allow empty selections or selected components only if selectionInfo.totalCount == 0 or selectionInfo.totalCount == selectionInfo.componentCount: - errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', Dialog.ERROR) + errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', dr.Dialog.ERROR) errMsg.run() return @@ -192,7 +194,7 @@ def processPrimitive(scenenode): return # Traversor class to visit child primitives of entities - class nodeVisitor(SceneNodeVisitor): + class nodeVisitor(dr.SceneNodeVisitor): def pre(self, scenenode): # Brush? if scenenode.isBrush(): @@ -204,7 +206,7 @@ def pre(self, scenenode): # Traverse all child nodes, regardless of type return 1 - class dataCollector(SelectionVisitor): + class dataCollector(dr.SelectionVisitor): def visit(self, scenenode): if scenenode.isBrush() or scenenode.isPatch(): @@ -238,7 +240,7 @@ def visit(self, scenenode): # Add a Spin Button for blending and one for random height #blendHeightHandle = dialog.addSpinButton("Level of blending in percent",0,1,0.05,2) #blendRandomnessHandle = dialog.addSpinButton("Randomness",0,1,0.05,2) - if dialog.run() == Dialog.OK: + if dialog.run() == dr.Dialog.OK: fullpath = dialog.getElementValue(pathHandle) + '/' + dialog.getElementValue(fileHandle) if not fullpath.endswith('.ase'): fullpath = fullpath + '.ase' @@ -252,8 +254,8 @@ def visit(self, scenenode): try: file = open(fullpath, 'r') file.close() - prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', Dialog.ASK) - if prompt.run() == Dialog.YES: + prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', dr.Dialog.ASK) + if prompt.run() == dr.Dialog.YES: overwrite = True else: overwrite = False diff --git a/install/scripts/commands/check_for_invalid_visportals.py b/install/scripts/commands/check_for_invalid_visportals.py index 0bcd56bbaf..60ed2a5869 100644 --- a/install/scripts/commands/check_for_invalid_visportals.py +++ b/install/scripts/commands/check_for_invalid_visportals.py @@ -32,7 +32,9 @@ class VisportalResults(object): results = VisportalResults() visportalShader = 'textures/editor/visportal' - class VisportalChecker(SceneNodeVisitor): + import darkradiant as dr + + class VisportalChecker(dr.SceneNodeVisitor): def pre(self, node): if node.isBrush(): brush = node.getBrush() @@ -73,7 +75,7 @@ def pre(self, node): msg += 'The problematic visportals have been highlighted.' - GlobalDialogManager.createMessageBox('Visportal Test Results', msg, Dialog.CONFIRM).run() + GlobalDialogManager.createMessageBox('Visportal Test Results', msg, dr.Dialog.CONFIRM).run() if __executeCommand__: execute() diff --git a/install/scripts/commands/convert_to_ase_and_replace.py b/install/scripts/commands/convert_to_ase_and_replace.py index 8a236449b0..bd76ba88a0 100644 --- a/install/scripts/commands/convert_to_ase_and_replace.py +++ b/install/scripts/commands/convert_to_ase_and_replace.py @@ -28,24 +28,26 @@ def execute(): author = "Richard Bartlett, some additions by greebo" version = "0.7" + import darkradiant as dr + # Check if we have a valid selection selectionInfo = GlobalSelectionSystem.getSelectionInfo() # Don't allow empty selections or selected components only if selectionInfo.totalCount == 0 or selectionInfo.totalCount == selectionInfo.componentCount: - errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', Dialog.ERROR) + errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', dr.Dialog.ERROR) errMsg.run() return if selectionInfo.entityCount != 1: - errMsg = GlobalDialogManager.createMessageBox('Wrong selection', 'Please select exactly one func_static.', Dialog.ERROR) + errMsg = GlobalDialogManager.createMessageBox('Wrong selection', 'Please select exactly one func_static.', dr.Dialog.ERROR) errMsg.run() return if not GlobalRegistry.get('user/scripts/aseExport/initialUserWarning') == '1': GlobalRegistry.set('user/scripts/aseExport/initialUserWarning', '1') - warningMsg = GlobalDialogManager.createMessageBox('Careful', 'This option will export the selected func_static to ASE and replace your entity with the newly created model.\nThe currently selected func_static will be DELETED. You can undo this operation, but know what you''re doing!', Dialog.WARNING) + warningMsg = GlobalDialogManager.createMessageBox('Careful', 'This option will export the selected func_static to ASE and replace your entity with the newly created model.\nThe currently selected func_static will be DELETED. You can undo this operation, but know what you''re doing!', dr.Dialog.WARNING) warningMsg.run() shaderlist = [] @@ -117,7 +119,7 @@ def processPrimitive(scenenode): return # Traversor class to visit child primitives of entities - class nodeVisitor(SceneNodeVisitor): + class nodeVisitor(dr.SceneNodeVisitor): def pre(self, scenenode): # Brush? if scenenode.isBrush(): @@ -129,7 +131,8 @@ def pre(self, scenenode): # Traverse all child nodes, regardless of type return 1 - class dataCollector(SelectionVisitor): + class dataCollector(dr.SelectionVisitor): + from darkradiant import Vector3 fs_origin = Vector3(0,0,0) fs = 0 @@ -138,6 +141,7 @@ def visit(self, scenenode): processPrimitive(scenenode) elif scenenode.isEntity(): import re + from darkradiant import Vector3 # greebo: Found an entity, this could be a func_static or similar # Traverse children of this entity using a new walker @@ -175,7 +179,7 @@ def visit(self, scenenode): pathHandle = dialog.addPathEntry("Save path:", True) dialog.setElementValue(pathHandle, GlobalRegistry.get('user/scripts/aseExport/recentPath')) - if dialog.run() == Dialog.OK: + if dialog.run() == dr.Dialog.OK: fullpath = dialog.getElementValue(pathHandle) + '/' + dialog.getElementValue(fileHandle) if not fullpath.endswith('.ase'): fullpath = fullpath + '.ase' @@ -187,8 +191,8 @@ def visit(self, scenenode): try: file = open(fullpath, 'r') file.close() - prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', Dialog.ASK) - if prompt.run() == Dialog.YES: + prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', dr.Dialog.ASK) + if prompt.run() == dr.Dialog.YES: overwrite = True else: overwrite = False diff --git a/install/scripts/commands/example.py b/install/scripts/commands/example.py index 8b6d930472..8b31f13dbf 100644 --- a/install/scripts/commands/example.py +++ b/install/scripts/commands/example.py @@ -7,6 +7,11 @@ def execute(): shader = GlobalShaderSystem.getShaderForName('bc_rat') print(shader.getName()) + # the "Global*" variables like GlobalShaderSystem are already exposed to this scripts + # The rest needs to imported from the darkradiant module and referred to by the prefix "dr" + import darkradiant as dr + GlobalDialogManager.createMessageBox('Example Box', str(e), dr.Dialog.ERROR).run() + # __executeCommand__ evaluates to true after DarkRadiant has successfully initialised if __executeCommand__: execute() diff --git a/install/scripts/commands/patchsplitter.py b/install/scripts/commands/patchsplitter.py index 78c52ddfaa..d2b69b70e3 100644 --- a/install/scripts/commands/patchsplitter.py +++ b/install/scripts/commands/patchsplitter.py @@ -12,6 +12,7 @@ class TooFewVerts(Exception): pass class Vert: """Holds coords for one patch control vertex.""" def __init__(self, vertex, texcoord): + from darkradiant import Vector3, Vector2 self.vertex = Vector3(vertex) self.texcoord = Vector2(texcoord) @@ -252,8 +253,10 @@ def getSelectedVertsLine(patch, patchdata): patch.setSelected(True) newpatch.setSelected(True) +import darkradiant as dr + if __executeCommand__: try: execute() except Exception as e: - GlobalDialogManager.createMessageBox('Patch Splitter', str(e), Dialog.ERROR).run() + GlobalDialogManager.createMessageBox('Patch Splitter', str(e), dr.Dialog.ERROR).run() diff --git a/install/scripts/commands/select_all_models_of_type.py b/install/scripts/commands/select_all_models_of_type.py index 4b405fc037..390b348b9f 100644 --- a/install/scripts/commands/select_all_models_of_type.py +++ b/install/scripts/commands/select_all_models_of_type.py @@ -8,7 +8,9 @@ def execute(): # Collect all currently selected models selectedModelNames = {} - class Walker(SelectionVisitor) : + import darkradiant as dr + + class Walker(dr.SelectionVisitor) : def visit(self, node): # Try to "cast" the node to an entity entity = node.getEntity() @@ -23,7 +25,7 @@ def visit(self, node): print('Unique models currently selected: ' + str(len(selectedModelNames))) # Now traverse the scenegraph, selecting all of the same names - class SceneWalker(SceneNodeVisitor) : + class SceneWalker(dr.SceneNodeVisitor) : def pre(self, node): # Try to "cast" the node to an entity diff --git a/install/scripts/commands/test_targets.py b/install/scripts/commands/test_targets.py index bc24618ec9..5bb5062137 100644 --- a/install/scripts/commands/test_targets.py +++ b/install/scripts/commands/test_targets.py @@ -14,12 +14,15 @@ def execute(): g_targets = {} # entityname : [targets] g_missing = {} # entityname : [missingtargets] - class TargetFinder(SceneNodeVisitor): + + import darkradiant as dr + + class TargetFinder(dr.SceneNodeVisitor): def pre(self, node): if node.isEntity(): n = node.getEntity() name = n.getKeyValue('name') - targs = [t.second for t in n.getKeyValuePairs('target')] + targs = [t[1] for t in n.getKeyValuePairs('target')] g_targets[name] = targs return 1 # Instantiate a new walker object and get list of entities/targets @@ -40,7 +43,7 @@ def pre(self, node): msg = '%d entities found with %d targets' % (len(entities), targetcount) + '\n\n' if not g_missing: msg += 'No missing targets found' - GlobalDialogManager.createMessageBox('Missing targets', msg, Dialog.CONFIRM).run() + GlobalDialogManager.createMessageBox('Missing targets', msg, dr.Dialog.CONFIRM).run() else: msg += 'Missing targets:\n' for ent in g_missing.keys(): @@ -49,8 +52,8 @@ def pre(self, node): print(msg) # output to console msg += "\nThe list of missing targets has been printed to the console." msg += "\n\nDo you want to select all entities with missing targets?" - response = GlobalDialogManager.createMessageBox('Missing targets', msg, Dialog.ASK).run() - if response == Dialog.YES: + response = GlobalDialogManager.createMessageBox('Missing targets', msg, dr.Dialog.ASK).run() + if response == dr.Dialog.YES: class Selector(SceneNodeVisitor): def pre(self, node): if node.isEntity() and node.getEntity().getKeyValue('name') in g_missing.keys():