File: test_targets.py

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (65 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Find any entities with "target" spawnargs that don't point to a valid entity in the map.
Print results to the console, plus show a dialog and offer to select any entities that have 
missing targets.

# 3718
Proposal: RJFerret
Script: SteveL 
"""

__commandName__ = 'test_targets' # should not contain spaces
__commandDisplayName__ = 'Test for Missing Targets' # should not contain spaces


def execute():
    g_targets = {} # entityname : [targets]
    g_missing = {} # entityname : [missingtargets]

    import darkradiant as dr

    class TargetFinder(dr.SceneNodeVisitor):
        def pre(self, node):
            if node.isEntity():
                n = node.getEntity()
                name = n.getKeyValue('name')
                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
    walker = TargetFinder()
    GlobalSceneGraph.root().traverse(walker)
    # Find any targets that don't exist, and count all targets
    entities = g_targets.keys()
    targetcount = 0
    for ent in entities:
        targetcount += len(g_targets[ent])
        missing = []
        for targ in g_targets[ent]:
            if targ not in entities:
                missing.append(targ)
        if missing:
            g_missing[ent] = missing
    # generate report
    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, dr.Dialog.CONFIRM).run()
    else:
        msg += 'Missing targets:\n'
        for ent in g_missing.keys():
            for targ in g_missing[ent]:
                msg += '%s -> %s\n' % (ent, targ)
        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, dr.Dialog.ASK).run()
        if response == dr.Dialog.YES:
            class Selector(dr.SceneNodeVisitor):
                def pre(self, node):
                    if node.isEntity() and node.getEntity().getKeyValue('name') in g_missing.keys():
                        node.setSelected(True)
                    return 1
            GlobalSceneGraph.root().traverse(Selector())

if __executeCommand__:
    execute()