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

问题描述

经过大约一年的手工编写这些文件,我正在寻找一种生成QC files方法。我只是尝试对列表中的命令进行硬编码,然后将命令及其值写为文本,但是这种感觉非常残酷,而且我认为必须有一种更为优雅的方法来填充文件。如果有关系,我的最终目标就是将其变成搅拌机的附加组件。 编辑:添加示例输出/ 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

解决方法

正如我在评论中提到的那样,您可以为这些对象提供类似树的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
}

具有自动缩进功能,无管理字符串修饰。