使用 Python 为 Valve QC 文件生成代码

Generating code for Valve QC file using Python

在手工编写这些文件大约一年后,我正在寻找一种生成 QC files 的方法。我尝试过对列表中的命令进行硬编码,然后将命令及其值作为文本写出,但这感觉非常蛮力,而且我认为必须有一种更优雅的方式来填充文件。如果重要的话,我的最终目标是将其变成搅拌机的附加组件。 编辑:添加示例 output/QC 文件

$modelname "AnrachyReigns\Jack the ripper.mdl"
$maxverts "65000"

$model "Jack" "genericmale.dmx"{
 eyeball "eye_right" "bip_head" -1.66 0.53 66.89 "eyeball_r" 1 4 "iris_unused" 2.9
 eyeball "eye_left" "bip_head" -1.66 0.53 66.89 "eyeball_l" 1 4 "iris_unused" 2.9
 mouth 0 "mouth" "bip_head" 0 1 0
flexcontroller eyes range -65 65 eyes_updown
flexconttroller eye range -65 65 eyes_rightleft
}

$bodygroup "Right Arm"{
 studio "Gattling.dmx"
 blank
}

$bodygroup "Left arm"{
 studio "HandCannon.dmx"
 blank
}

$mostlyopaque

$attacement "eyes" "bip_head" 1 1 1 rotate 0 -80 -90
$attachment "mouth" "bip_head" 0 0 0 rotate 0 0 0

$cdmaterials "models\GameModels\AnarachyReigns\Jack\"

$sequence "Jack" "genericmale.dmx

正如我在评论中提到的,您可以为这些对象想出一个 tree-like API。

例如,

import json
import sys


class QCObject:
    def __init__(self, tag, parameters=(), children=()):
        self.tag = tag
        self.parameters = list(parameters)
        self.children = list(children)

    def add(self, child):
        self.children.append(child)

    def serialize(self, stream, depth=-1):
        indent = "  " * depth
        if self.tag:
            header_line = [self.tag]
            # Here's hoping JSON serialization is close enough to QC
            header_line.extend([json.dumps(val) for val in self.parameters])
            if self.children:
                header_line.append("{")
            print(indent + " ".join(header_line), file=stream)
        for child in self.children:
            child.serialize(stream, depth=depth + 1)
        if self.tag and self.children:
            print(indent + "}", file=stream)


root = QCObject(None)
root.add(QCObject("$modelname", ["Jack.mdl"]))
root.add(QCObject("$maxverts", ["65000"]))

jack_model = QCObject(
    "$model",
    ["Jack" "genericmale.dmx"],
    children=[
        QCObject(
            "eyeball", ["eye_right", "bip_head", -1.66, 0.53, 66.89, "eyeball_r", 1, 4, "iris_unused", 2.9]
        ),
    ],
)

root.add(jack_model)

root.add(
    QCObject("$bodygroup", ["Right Arm"], children=[QCObject("studio", ["Gattling.dmx"]), QCObject("blank"),])
)

root.serialize(sys.stdout)

产出

$modelname "Jack.mdl"
$maxverts "65000"
$model "Jackgenericmale.dmx" {
  eyeball "eye_right" "bip_head" -1.66 0.53 66.89 "eyeball_r" 1 4 "iris_unused" 2.9
}
$bodygroup "Right Arm" {
  studio "Gattling.dmx"
  blank
}

具有自动缩进和无管理字符串处理。