我正在尝试创建一个脚本,该脚本将选择对象属性“便利性”等于“学校”的所有对象。有些人可以帮我确定以下代码有什么问题吗?
from scripting import *
# get a CityEngine instance
ce = CE()
def selectByAttribute(attr, value):
objects = ce.getObjectsFrom(ce.scene)
selection = []
for item in objects:
attribute = ce.getAttribute(item, attr)
if attribute == value:
selection.append(item)
ce.setSelection(selection)
if __name__ == '__main__':
selectByAttribute("/ce/geometry/amenity", "school")
For object attributes you don’t need any prefix just write selectByAttribute(“amenity”, “school”) and your script should work.
I suggest two other small improvements to your script:
- Add @noUIupdate before your function. This makes sure the UI will be refreshed only after the function is run through. Depending on the size of the scene and what your function does this can speed up the run significantly
- In your case you don’t need to query all objects from the scene, you can limit it to shapes only if you write objects = ce.getObjectsFrom(ce.scene, ce.isShape)
对于对象属性,您不需要任何前缀,只需编写 selectByAttribute(“便利设施”、“学校”),您的脚本应该可以工作。
我建议对脚本进行另外两个小改进:
- 在函数之前添加@noUIupdate。这可确保仅在函数运行后刷新 UI。根据场景的大小和函数的作用,这可以显着加快运行速度
- 在您的情况下,您不需要查询场景中的所有对象,仅当您编写对象 = ce.getObjectsFrom(ce.scene, ce.isShape) 时,您才能将其限制为形状
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。