How to translate object by using it's name

api

(SRF Studio) #1

Hi,

I am successfully using a piece of code to show/hide objects by their names:

var nodeMyOBJ;
var objMyOBJ = 'wolf2geo';
api.getNodeMap( function ( err, nodes ) {
		if ( !err ) {
		for ( var prop in nodes ) {
			if ( nodes.hasOwnProperty( prop ) ) {
				if ( nodes[ prop ].name === objMyOBJ ) {
				nodeMyOBJ = nodes[prop].instanceID;
				api.hide( nodeMyOBJ );
				}}}}});

When I try to use this code with translate instead of show/hide it’s not working:( I’m guessing that this code is fetching the Group ID and not MatrixTransform? But how can I modify it to fetch MatrixTransform ID?


…sorry for the meme, I couldn’t help it, I just feel sooo stupid :no_mouth:


(Shaderbytes) #2

you could of coarse use my api utility :slight_smile: ( shameless plug )

It permits all object actions via object name , it also allows you to retrieve objects by type , but most important it defaults to matrix transform so if you dont stipulate which type it is it will use the matrix transform of that object name if it exists.

Also there is abstractions for things like translate , which allow you so set a direction vector and amount to transverse along that vector.

you can see docs here with code pen examples as well. there is a section for object translate as well:


(SRF Studio) #3

Thank you for replying,

I did use your utility in one of my free games and it worked / it’s working great, thank you for creating it :+1:

I would still be using it but I couldn’t get that addEventListener for ‘Click’ to function and it was fundamental for my new project to be able to click on objects (materials in this case). I assumed that you didn’t implement that functionality because I remembered you said somewhere that your utility is not 100% compatible with all official API options.

This code that I’m using now (for hide/show) I found in one of your replies and it worked. If it’s my mistake about the ‘Click’ listener I could go back & try again with Utility? Or maybe there is a way to combine both official API & your Utility in a single script?:no_mouth:


(Shaderbytes) #4

i have a click abstraction that is super easy to use , i still need to do the last two pages of the docs but you can view the code pen of the “camera lookat” section and you can see the click in action :wink:

Of coarse i do store the actual official api object within my tool so you can call any function of the official api via that variable it is named, surprise surprise “api” so instance.api.whatever()


(SRF Studio) #5

…damn it, I knew it was my fault :no_mouth:

It’s evident that I’m not much of programmer but this is really stupid. I will try again with using the Utility. I downloaded, I think, the newest version of Utility 10 or 12 days ago… I will have a look.


(Shaderbytes) #6

in a nut shell :

defined the listening function :

function objectClicked(event){

}

add a listener :

sketchfabAPIUtilityInstance.addEventListener(sketchfabAPIUtilityInstance.EVENT_CLICK,objectClicked);

the event object argument of the listener function has two extra properties compared to the regular api :

“parentGroup”

“parentMatrixTransform”

so you can use these in any conditionals inside the listener to know what matrix transform or group was clicked


(SRF Studio) #7

Thank you, I will try it again, hopefully I can pull it off with my skill levels.

These are the main reference pages?
https://www.shaderbytes.co.za/sketchfab/development/api/utility/index.html?state=content-page|CameraLookAt

And the newest Utility is on GitHub? I’ll see if I downloaded the newest when I was testing.


(Shaderbytes) #8

Well that is the url to the camera lookat section, the click page is still coming , i just mentioned this page because it has a code pen example where i am using the click event of the utility

yes this is now the one place all the docs for the utility tool and will be kept up to date with the latest version on github. Good luck , feel free to give a shout is you are having issues.

Its still very fresh , I only did this the other day but the tool i use myself for client work all the time , so it is actively maintained and has grown and changed plenty over the last two years or so


(SRF Studio) #9

First I wanna say that your utility docs are incredible, very well written and very helpful. But the examples in the Code Pen are the best :+1: Finally I can see and experiment with the code that’s actually implemented in the most efficient way.

There is one thing that is troubling me, when I last used utility I had no problems with node-names of animated models. This time the names are valid only when the model is static.

The names that were functioning through official api (for show/hide) are not functioning through utility. They are still showing up but, instead of them, utility is registering models with ‘undefined’ and then number, I suppose the ID.

I have been using ‘Camera LookAt’ example to see what is being registered when I click on animated models and ‘Scene Object Visibility’ example to see if regular names are being registered at all. They are.

This is the link to my game model -3ec89af998134521842ca3c2c84f7cdb- All animated models have ‘geo’ for ‘geometry’ behind the name. Would be great if you could take a look. Maybe there is something I’m doing wrong in naming the models armature/geometry in Blender?

///// aditional edit

I’ve gone through our last posts regarding the utility, it seems that it had similar problems with rigged-models identification a year ago but you did something and it was successfully resolved. This is what you wrote the problem was:

‘I cache matrixtransform nodes as priority. On static models this was a safe route to take to handle sub geometry objects…’ & ‘…but this doesn’t seem to hold fast for rigged models. The matrix transform nodes on your model don’t have children , hence why hiding them does nothing currently.’

I thought it could be helpful if the problem is similar.


(Shaderbytes) #10

for rigged models there will be a group named the same as the matrix transform , the geometry is in the group not the matrix transform, if you look in the docs you will see an example for static and rigged models hide and show.

So basically when you call hide on a rigged model you have to stipulate to use the group storage for lookup… check the code pen eg:

sketchfabAPIUtilityInstance.toggleNodeVisibility(key,null,sketchfabAPIUtilityInstance.nodeTypeGroup);

you can use toggle or directly set the visibility by notice im passing the preferred group to the function

hth


(Shaderbytes) #11

if you put your model id in the code pen for rigged models it will work just fine :

All the groups with geometry will be listed and you can hide/show them :wink:


(SRF Studio) #12

This worked perfectly, thanx.

I also changed ‘event.parentMatrixTransform.name’ to ‘event.parentGroup.name’ in ‘objectClicked(event)’ of the ‘Camera LookAt’ example and it’s also working great!

///////// EDIT

I just seen your new post with Code Pen, I will check it out, you probably did something better with code than me :slight_smile:


(Shaderbytes) #13

glad you got it sorted :wink:


(SRF Studio) #14

I have just one more question, I realize I already took too much of your time, I managed to move my models with your ‘Scene Object Translate’ example with tweaked distance, but I really need the ability to position the model to specific location. Similar to annotation concept, with duration: 0? Can the ‘direction’ field be used for that?


(Shaderbytes) #15

mmm no you will have to use the official API for that I think, but it gives me the idea to add a function to the utility “setPosition” , if you give me a moment i will add it in… brb


(Shaderbytes) #16

sorry my internet was down for a while , ok i updated the utility to have a setPosition function. I will add a docs page and codepen soon for it, the code is updated on github so you can grab it , i didnt test it though so cross fingers :wink:

 this.setPosition = function (key, position, duration, easing, callback)
//use it like this 
var arrayOfPositionvalues = [1,0,0];// just an example of the array for position
sketchfabAPIUtilityInstance.setPosition ("object_name_here",arrayOfPositionvalues,0);

(SRF Studio) #17

I’m sorry for the delay in replying, it was past 3am where I’m from.

I downloaded new utility from GitHub and used the code in your post but, sadly, it didn’t work :frowning_face:

Maybe it was a fault on my end, this is the code I used (it worked with your standard translate):

  var arrayOfPositionvalues = [-1.3,0.2,1];  
  function objectClicked(event){   
    if(event.parentMatrixTransform !== null && event.parentMatrixTransform !== undefined){
       currentObjectKeyMT =  event.parentMatrixTransform.name;
       sketchfabAPIUtilityInstance.setPosition (currentObjectKeyMT,arrayOfPositionvalues,0);
    }

I also wanted you to know what I’m trying to achieve, so that it doesn’t seem random or overly stupid. I’m going to use SketchFab (iFrame it) in my new game together with main engine (Construct 2). In order to make the 3D part more game-like I wanted to have a main character ‘movable’ or somehow a part of 3D exploration. Since SketchFab allows only one animation to be active I gave up on conventional approach but I wanted to do following:

When player clicks on object that’s suppose to be interactive, camera will go to that object and player models (one with ‘walk-in’ and one with ‘idle’ animation) will be moved behind the camera to allow model to ‘walk in’.

Something like this:

Because the walk-in animation is only 2 sec (and looping through the main animation sequence with all other models) I can wait for interval to make it visible. So the whole process (clicking, camera movement and animation play) should take about 4-5 seconds. After the walk-in is over I than hide ‘walk-in’ and show ‘idle’ loop.

This could be done with multiple player-models on designated places but I wanted that to be the last resort since it’s not practical at all :slight_smile: Moving and showing/hiding two models would be much more efficient then having show/hide on 12 same models.


(Shaderbytes) #18

dont know what you are doing wrong but here is a snippet i tested today and it is fine :


(Shaderbytes) #19

I did fix up a small error but it would have worked regardless of this change ( to be specific the code was not allowing zero values for duration based on some javascript shorthand i used, before this current change 0 would have just become 1 ) Anyway I tested it with duration 1 and duration zero now and both are working.


(SRF Studio) #20

The problem was completely my fault: I downloaded the last version of utility from GitHub but failed to modify script in header :no_mouth:

I realized it when I duplicated the entire (JS body) code from your example (that worked with my level & position in your example) and realized that it’s still not working.

When I updated the script path everything started to function as it should :+1:

Thank you so much, the possibility of 15 identical models being shown/hidden instead of just one being moved around was problematic at best :smile: