Skip to content

Instantly share code, notes, and snippets.

@robertlugg
Created October 31, 2018 07:20
Show Gist options
  • Save robertlugg/a5161200998092ddf69cb7393f7efcc0 to your computer and use it in GitHub Desktop.
Save robertlugg/a5161200998092ddf69cb7393f7efcc0 to your computer and use it in GitHub Desktop.
Lens Studio scene object hierarchy
// -----JS CODE-----
function showProps(obj, pre) {
pre = pre || ''
var result = '';
result += pre + "Properties\n";
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
result += pre + ' .' + i + ' = ' + obj[i] + '\n';
}
}
result += pre + "Inherited Properties\n";
for (var i in obj) {
if (!obj.hasOwnProperty(i)) {
result += pre + ' .' + i + ' = ' + obj[i] + '\n';
}
}
return result;
}
function showComponent(component, pre) {
pre = pre || ''
var out = '';
out += pre + component.getTypeName() + "\n";
out += showProps(component, pre + ' ');
return out;
}
function showSceneObject(obj, pre) {
pre = pre || ''
var out = '';
out += pre + obj.name + " (" + obj.getTypeName() + ")\n";
out += showProps(obj, pre + ' ')
var componentCount = obj.getComponentCount('');
if (componentCount) {
out += pre + " Components:\n"
for(var i=0; i < componentCount; i++) {
var thisComponent = obj.getComponentByIndex('', i);
out += showComponent(thisComponent, pre + ' ');
}
}
var childCount = obj.getChildrenCount();
if (childCount) {
out += pre + " Children:\n"
for(var i=0; i < childCount; i++) {
var thisChild = obj.getChild(i);
out += showSceneObject(thisChild, pre + ' ');
}
}
return out;
}
function showScene(scene) {
var out = '---\n';
out += "core version: " + global.getCoreVersion().toString() + '\n';
var childCount = scene.getRootObjectsCount()
for(var i = 0; i < childCount; i++) {
var obj = scene.getRootObject(i);
out += showSceneObject(obj, '');
}
return out
}
print(showScene(scene))
@positlabs
Copy link

This is super useful! I have come back to it several times so I decided to make a version that behaves a little differently. It always prints the result, and exposes the functions globally so they can be called from other scripts.

https://gist.github.com/positlabs/13a4420a20f38715053af218c625c08a

@robertlugg
Copy link
Author

I'm glad that this is useful. I'd encourage anyone reading this message to use @positlabs ' gist instead...better than mine in several ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment