Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
how to get scene node parent?

ssatguru
Registered User
Quote
2023-01-03 01:09:20

I see we have a "ccbGetChildSceneNode()".
I do not see something like ""ccbGetParentSceneNode()".
or
something like
cbGetSceneNodeProperty(s, "Parent");

any idea how we can get this ?


okeoke
Registered User
Quote
2023-01-03 10:25:47

Hi ssatguru,

I don't think there is a direct way of doing that. You probably, should compose the structure of you project the other way somehow.

If there is no other way, I would write a recursive search function like https://stackoverflow.com/a/9133680.
The algorithm would be:
1. Pass node to the function (root node on 1st iteration);
2. Check if the root node has your child node - if yes return the node, if no:
3. Call the function for every child node.

I'm also not sure how to match the nodes, since they can have the same names. I can, probably, come up with the exact implementation tonight, if you don't have another answer before that.


okeoke
Registered User
Quote
2023-01-03 23:00:40

function getNodeParent(nodeToFind) {
function findNode(nodeToFind, curNode, parent) {
var nodeToFindName = ccbGetSceneNodeProperty(nodeToFind, 'Name');
var curNodeName = ccbGetSceneNodeProperty(curNode, 'Name');
if (nodeToFindName === curNodeName) {
return parent;
} else {
var result = null;
for (var i = 0; result === null && i < ccbGetSceneNodeChildCount(curNode); i++) {
var child = ccbGetChildSceneNode(curNode, i);
result = findNode(nodeToFind, child, curNode);
}
return result;
}
}

var root = ccbGetRootSceneNode();
var result = null;
for (var i = 0; result === null && i < ccbGetSceneNodeChildCount(root); i++) {
var child = ccbGetChildSceneNode(root, i);
result = findNode(nodeToFind, child, root);
}

return result;
}


Sorry, not that elegant solution, but it kinda works for me. The biggest issue is that it compares nodes by names, so all your nodes should have the unique ones for the function to work. It would be really nice to get nodes IDs somehow, but I don't think it's possible.


ssatguru
Registered User
Quote
2023-01-04 01:10:05

@okeoke
Thanks for looking into this and providing a nice solution.
You are right about non uniqueness of name.. A unique id would have helped.

Would use your solution for now

Simplest solution would be to update cbGetSceneNodeProperty() to provide parent option
example.

cbGetSceneNodeProperty(s, "Parent");


@niko, @just_in_case any thoughts ?


just_in_case
Moderator
Quote
2023-01-04 10:36:56

maybe, can extend the API to get the parent node as well. Will look into this.


ssatguru
Registered User
Quote
2023-01-05 00:13:05

Nice.

Also how do we check if two nodes provided by the API are the same?
We can compare their names but names may not be unique.


just_in_case
Moderator
Quote
2023-01-06 11:28:33

@ssatguru, just added the API command to my extended JS API for getting the parent node. You can also compare using the ID of a node if you don't wanna compare using Name.

hope that helps :)


ssatguru
Registered User
Quote
2023-01-07 00:34:37

@just_in_case
Looking forward to your extended JS API.

About ID. How do I get the ID of a node?


just_in_case
Moderator
Quote
2023-01-07 12:24:34

I am not sure but I think you can use the command
 ccbGetSceneNodeProperty(scenenode,"ID")
that way you might get the ID of the CC object.


hadoken
Guest
Quote
2023-01-07 16:32:02

"ID" (or anything similar like "Id" or "id", etc.) seems not implemented as javascript accessible node property yet - no solution this way ...



ssatguru
Registered User
Quote
2023-01-07 20:45:20

Like @hadoken, I didn't find that either.
Maybe another addition to your extended API ?


just_in_case
Moderator
Quote
2023-01-08 16:53:16

You guys right, there is already code for this in the C++ source, there is probably something buggy in there or something is missing. Will look into this.


ssatguru
Registered User
Quote
2023-01-08 18:30:07

I also see this in the CopperLicht javascript source code and documentation
see https://www.ambiera.com/copperli...

What makes me wonder though is the description.
Both the Id and Name description says
completely freely usable by the user.


does that mean then that "Id" like "Name" maynot be unique?

If that is the case then maybe we need a function like
ccbSceneNodesEqual(node1,node2)

which returns true or false if equal or not equal.


okeoke
Registered User
Quote
2023-01-09 10:03:38

Would it be easier to just define a unique name for every node? You can just have a global index variable and add up it everytime you clone/create a new entity. Then you add it's value to a node name.
Or you can generate a random string and set it as a node name as well. The simplest way to do this in js I know is something like:
Math.random().toString(36).slice(2, 10);



ssatguru
Registered User
Quote
2023-01-10 00:53:14

@okeoke
Yes it would be easier, as long as I own that project. I can then follow some naming convention which would ensure that the names are unique. But what if I am developing an extension or a plugin meant to be used by others in their projects. I cannot force them to follow some such naming convention.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internat?onal" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact