模型方法
以下为模型常用的相关方法,是为了在场景中精准的找到模型,从而对模型进行一系列操作。
getObjectByProperty
依据对象的属性值来查找场景中的特定对象。该方法定义于 Object3D 类,所以场景里的任何对象都能使用它。
js
TO.scene.getObjectByProperty(name, value, recursive);| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
name | 查找的属性名称 | string | 是 | 无 |
value | 该属性所对应的值 | string | 是 | 无 |
recursive | 为 true,就会递归查找所有的子对象; 为 false,则只会在直接子对象中进行查找 | boolean | 否 | true |
实际使用案例
已导入ABB机械臂为例,具体代码请参考 导入模型章节。
- 通过 name 属性查找对象
js
let model = TO.scene.getObjectByProperty("name", "WS30-1"); //找到名称为“WS30-1”的模型对象
console.log(model);- 通过自定义属性查找对象
js
let model = TO.scene.getObjectByProperty("userData.by", "ThingOrigin"); //找到userData.by 为 ThingOrigin的模型对象
console.log(model);getModelCenter
场景中获取模型中心点位置。
js
TO.tool.getModelCenter(model);| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
model | 模型 | any | 是 | 无 |
实际使用案例
已导入ABB机械臂为例,具体代码请参考 导入模型章节。
js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let center = TO.tool.getModelCenter(model);//获取模型中心点
console.log(center);getModelInfo
获取模型信息。
js
TO.tool.getModelInfo(model);| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
model | 模型 | any | 是 | 无 |
实际使用案例
已导入ABB机械臂为例,具体代码请参考 导入模型章节。
js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let modelInfo = TO.tool.getModelInfo(model);//获取模型信息
console.log(modelInfo);getChildrenInfo
获取模型的子模型信息。
js
TO.tool.getChildrenInfo(model);| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
model | 模型 | any | 是 | 无 |
实际使用案例
已导入ABB机械臂为例,具体代码请参考 导入模型章节。
js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let child = TO.tool.getChildrenInfo(model);//获取模型的子模型信息
console.log(child);getWorldPosition
获取模型世界坐标位置。
js
TO.tool.getWorldPosition(model);| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
model | 模型 | any | 是 | 无 |
实际使用案例
已导入ABB机械臂为例,具体代码请参考 导入模型章节。
js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let position = TO.tool.getWorldPosition(model);//获取模型世界坐标位置
console.log(position);