我正在尝试编写一个将input.xml
的某些属性复制到output.xml
的脚本
input.xml:
<i3d>
<Scene>
<TransformGroup>
<TransformGroup name="birch_stage2" translation="50.9362 113.806 -465.889" rotation="180 1.4175 180" scale="1.5 1.5 1.5" clipDistance="599" nodeId="13499" lodDistance="0 120">
<Shape shapeId="1" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538" angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13500" materialIds="611" castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="2" name="LOD0attachments" clipDistance="500" nodeId="13501" materialIds="801" castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="3" name="LOD1" clipDistance="1500" nodeId="13502" materialIds="103" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="birch_stage4" translation="96.7489 119.845 -469.445" scale="0.3 0.3 0.3" clipDistance="2000" nodeId="13519" lodDistance="0 165">
<Shape shapeId="10" name="LOD0" static="true" collisionMask="16789538" angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13520" materialIds="611" castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="11" name="LOD0attachments" clipDistance="500" nodeId="13521" materialIds="801" castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="12" name="LOD1" clipDistance="1500" nodeId="13522" materialIds="90" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="birch_stage2" translation="134.159 123.536 -472.109" rotation="180 -72.8097 180" scale="0.5 0.5 0.5" clipDistance="599" nodeId="13523" lodDistance="0 120">
<Shape shapeId="1" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538" angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13524" materialIds="611" castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="2" name="LOD0attachments" clipDistance="500" nodeId="13525" materialIds="801" castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="3" name="LOD1" clipDistance="1500" nodeId="13526" materialIds="103" castsShadows="true"/>
</TransformGroup>
</TransformGroup>
</Scene>
</i3d>
output.xml:
<i3d>
<Scene>
<TransformGroup>
<TransformGroup name="birch_stage2" translation="00.00 00.00 00.00" rotation="0 0 0" scale="0 0 0" clipDistance="599" nodeId="00000" lodDistance="0 120">
<Shape shapeId="1" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538" angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="00000" materialIds="611" castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="2" name="LOD0attachments" clipDistance="500" nodeId="00000" materialIds="801" castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="3" name="LOD1" clipDistance="1500" nodeId="00000" materialIds="103" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="birch_stage4" translation="00.00 00.00 00.00" scale="0 0 0" clipDistance="2000" nodeId="00000" lodDistance="0 165">
<Shape shapeId="10" name="LOD0" static="true" collisionMask="16789538" angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="00000" materialIds="611" castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="11" name="LOD0attachments" clipDistance="500" nodeId="00000" materialIds="801" castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="12" name="LOD1" clipDistance="1500" nodeId="00000" materialIds="90" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="birch_stage2" translation="00.00 00.00 00.00" rotation="0 00.00 0" scale="0 0 0" clipDistance="599" nodeId="00000" lodDistance="0 120">
<Shape shapeId="1" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538" angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="00000" materialIds="611" castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="2" name="LOD0attachments" clipDistance="500" nodeId="00000" materialIds="801" castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="3" name="LOD1" clipDistance="1500" nodeId="00000" materialIds="103" castsShadows="true"/>
</TransformGroup>
</TransformGroup>
</Scene>
</i3d>
我需要从包含属性translation
的{{1}}个元素中复制属性rotation
scale
nodeId
和TransformGroup
并将其粘贴到{{ 1}}来更新这些属性。在TransformGroup下的name="birch_stage2"
中,我只需要output.xml
。 (当前提到的所有属性都是一堆零,需要从Shapes
更新为nodeId
)
我可以获取一个脚本来打印这些属性,但是更新不会显示在input.xml
中。这就是我做的:
output.xml
那么我如何使其将这些属性写入output.xml
?
我希望您拥有所有这些属性。
答案 0 :(得分:0)
以下是基于XML简化版的有效代码。该代码查找具有所请求名称属性的TransformGroup [s],并根据所请求属性创建新的简化TransformGroup元素。它将新的xml转储到控制台以及文件中。
<i3d>
<Scene>
<TransformGroup name="birch_stage2" translation="50.9362 113.806 -465.889" rotation="180 1.4175 180"
scale="1.5 1.5 1.5" clipDistance="599" nodeId="13499" lodDistance="0 120">
<Shape shapeId="10" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538"
angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13500"
materialIds="611" castsShadows="true" receiveShadows="true" splitType="4"
splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="11" name="LOD0attachments" clipDistance="500" nodeId="13501" materialIds="801"
castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="12" name="LOD1" clipDistance="1500" nodeId="13502" materialIds="103" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="birch_stage4" translation="96.7489 119.845 -469.445" scale="0.3 0.3 0.3"
clipDistance="2000" nodeId="13519" lodDistance="0 165">
<Shape shapeId="1" name="LOD0" static="true" collisionMask="16789538" angularDamping="0.15"
rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13520" materialIds="611"
castsShadows="true" receiveShadows="true" splitType="4" splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="2" name="LOD0attachments" clipDistance="500" nodeId="13521" materialIds="801"
castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="3" name="LOD1" clipDistance="1500" nodeId="13522" materialIds="90" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="birch_stage2" translation="134.159 123.536 -472.109" rotation="180 -72.8097 180"
scale="0.5 0.5 0.5" clipDistance="599" nodeId="13523" lodDistance="0 120">
<Shape shapeId="10" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538"
angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13524"
materialIds="611" castsShadows="true" receiveShadows="true" splitType="4"
splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="11" name="LOD0attachments" clipDistance="500" nodeId="13525" materialIds="801"
castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="12" name="LOD1" clipDistance="1500" nodeId="13526" materialIds="103" castsShadows="true"/>
</TransformGroup>
<TransformGroup name="not_birch_stage2" translation="134.159 123.536 -472.109" rotation="180 -72.8097 180"
scale="0.5 0.5 0.5" clipDistance="599" nodeId="13523" lodDistance="0 120">
<Shape shapeId="10" name="LOD0" translation="2.27374e-15 0 0" static="true" collisionMask="16789538"
angularDamping="0.15" rollingFriction="0.27" density="0.65" clipDistance="500" nodeId="13524"
materialIds="611" castsShadows="true" receiveShadows="true" splitType="4"
splitUvs="0 0 1 0.064 1.65">
<Shape shapeId="11" name="LOD0attachments" clipDistance="500" nodeId="13525" materialIds="801"
castsShadows="true" receiveShadows="true"/>
</Shape>
<Shape shapeId="12" name="LOD1" clipDistance="1500" nodeId="13526" materialIds="103" castsShadows="true"/>
</TransformGroup>
</Scene>
代码
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
id3 = Element('i3d')
scene = SubElement(id3, 'Scene')
tree = ET.parse('input.xml')
root = tree.getroot()
for s3birch in root.findall("./Scene/TransformGroup[@name='birch_stage2']"):
transform_group = SubElement(scene, 'TransformGroup')
for attr in ['translation', 'rotation', 'scale', 'nodeId']:
transform_group.set(attr, s3birch.get(attr))
print(prettify(id3))
tree = ET.ElementTree(id3)
tree.write("output.xml")
输出
<?xml version="1.0" ?>
<i3d>
<Scene>
<TransformGroup nodeId="13499" rotation="180 1.4175 180" scale="1.5 1.5 1.5" translation="50.9362 113.806 -465.889"/>
<TransformGroup nodeId="13523" rotation="180 -72.8097 180" scale="0.5 0.5 0.5" translation="134.159 123.536 -472.109"/>
</Scene>
</i3d>
答案 1 :(得分:0)
我找到了解决方案。我使用列表存储值并将它们写入输出文件:
var distance = rangeSquared;
var direction = (hit.point - startPos).normalized;
endPos = startPos + direction * distance;