【发布时间】:2018-01-09 10:09:54
【问题描述】:
我是 Sketchup 和 ruby 的新手,我使用过 java 和 c#,但这是第一次使用 ruby。
现在我有一个问题,我需要在一个 json 中序列化所有场景(场景层次结构、对象名称、对象材质和单个对象的位置)我该怎么做?
我已经为 unity3D (c#) 做了这个,没有问题。
我试过这个:
def main
avr_entities = Sketchup.active_model.entities # all objects
ambiens_dictionary = {}
ambiens_list = []
avr_entities.each do |root|
if root.is_a?(Sketchup::Group) || root.is_a?(Sketchup::ComponentInstance)
if root.name == ""
UI.messagebox("this is a group #{root.definition.name}")
if root.entities.count > 0
root.entities.each do |leaf|
if leaf.is_a?(Sketchup::Group) || leaf.is_a?(Sketchup::ComponentInstance)
UI.messagebox("this is a leaf #{leaf.definition.name}")
end
end
end
else
# UI.messagebox("this is a leaf #{root.name}")
end
end
end
end
【问题讨论】:
-
require 'json'; some_var.to_json -
SketchUp 实体/模型没有内置序列化为 JSON,因此您需要找出自己的方法。对于
Sketchup::Entity的每个子类,您需要确定要导出哪些属性以及如何表示它们。你如何做到这一点取决于任何使用它的需求。 -
hmm.. 这个问题是关于如何递归挖掘模型的每个子实例吗?因为我注意到你的代码只遍历根和第一级。
标签: json ruby serialization sketchup