Jump to content

Going between GameObject<Model> and MeshFilter


Recommended Posts

Looking at a part that's defined by multiple MODEL {} entries for the meshes it uses, I'd like to be able to manipulate some of the models from within a plugin, with the models selected by the model's URI.

I can look up models in the GameDatabase with GameDatabase.Instance.GetModel(uri). I also can get the meshes in a part with part.FindModelComponents<MeshFilter>. However, meshFilter.name != model.name - the model.name is the URI + "(Clone)", but meshFilter.name is not, and it isn't necessarily only the filename of the model, either. I tried model.GetComponent<MeshFilter>() on a lark, and that returned null.

How do I figure out which GameObject (or URI, at least) correlates to which MeshFilter?

EDIT: Or, if there's not an accessible way to do that, is there a way to take the GameObject I get from the GameDatabase GetModel call, and turn it into a Mesh object that I can use?

Edited by MOARdV
Retitled & added more question
Link to comment
Share on other sites

Looking at a part that's defined by multiple MODEL {} entries for the meshes it uses, I'd like to be able to manipulate some of the models from within a plugin, with the models selected by the model's URI.

I can look up models in the GameDatabase with GameDatabase.Instance.GetModel(uri). I also can get the meshes in a part with part.FindModelComponents<MeshFilter>. However, meshFilter.name != model.name - the model.name is the URI + "(Clone)", but meshFilter.name is not, and it isn't necessarily only the filename of the model, either. I tried model.GetComponent<MeshFilter>() on a lark, and that returned null.

How do I figure out which GameObject (or URI, at least) correlates to which MeshFilter?

EDIT: Or, if there's not an accessible way to do that, is there a way to take the GameObject I get from the GameDatabase GetModel call, and turn it into a Mesh object that I can use?

What about the Mesh's name? MeshFilter simply contains the mesh, so I would think that the Mesh would contain the name.

Link to comment
Share on other sites

What about the Mesh's name? MeshFilter simply contains the mesh, so I would think that the Mesh would contain the name.

MeshFilter.mesh.name == "Instance" and MeshFilter.sharedMesh.name == "SharedMesh".

I did find a roundabout way (at least, roundabout to me, maybe this is sensible in Unity) - the transform names. part.transform's child transform "model" contains a bunch of children. Those transforms are named with the URIs of the models in question (with "(Clone)" tacked to the end). If I walk up the mesh's transforms, I reach the same transform name after a couple of steps. I don't know if this is the best way to go about it, but it seems to work. Now if I could figure out how to take a GameObject I got from GetModel and make a Mesh or MeshFilter out of it, I'd be set.

Link to comment
Share on other sites

Now if I could figure out how to take a GameObject I got from GetModel and make a Mesh or MeshFilter out of it, I'd be set.

Do you mean combine all of the meshes into a single MeshFilter on one GameObject? Many parts in the game are composed of many MeshFilters. You can find a particular portion by looking for GameObjects in the model hierarchy that have the name you're looking for

Edited by xEvilReeperx
clarity
Link to comment
Share on other sites

Do you mean combine all of the meshes into a single MeshFilter on one GameObject? Many parts in the game are composed of many MeshFilters. You can find a particular portion by looking for GameObjects in the model hierarchy that have the name you're looking for

No, what I want to do is something like this:

Pseudo-code:


var model = GameDatabase.Instance.GetModel(coolModel);
Mesh modelMesh = ???(model);

Then, I can go add a MeshFilter to the part to render this new part, or maybe go through the existing MeshFilters in the part and replace a Mesh with the new one.

Link to comment
Share on other sites

MeshFilter.mesh.name == "Instance" and MeshFilter.sharedMesh.name == "SharedMesh".

I did find a roundabout way (at least, roundabout to me, maybe this is sensible in Unity) - the transform names. part.transform's child transform "model" contains a bunch of children. Those transforms are named with the URIs of the models in question (with "(Clone)" tacked to the end). If I walk up the mesh's transforms, I reach the same transform name after a couple of steps. I don't know if this is the best way to go about it, but it seems to work. Now if I could figure out how to take a GameObject I got from GetModel and make a Mesh or MeshFilter out of it, I'd be set.

Actually, this makes a lot of sense. This would let KSP build the part out of all the meshes without moving or re-arranging them.

Link to comment
Share on other sites


var model = GameDatabase.Instance.GetModel(coolModel);
Mesh modelMesh = ???(model);

Perhaps this could be a start? I haven't actually tried it, though.


GameObject model = GameDatabase.Instance.GetModel(...);
Mesh mesh = model.GetComponent<MeshFilter>().sharedMesh;

Or, hmm, seeing that you talk about multiple meshes, perhaps there are child GameObjects that each have a MeshFilter.

Link to comment
Share on other sites

Perhaps this could be a start? I haven't actually tried it, though.


GameObject model = GameDatabase.Instance.GetModel(...);
Mesh mesh = model.GetComponent<MeshFilter>().sharedMesh;

It sounds like KSP wouldn't compose the multiple meshes (with different materials) into a single mesh. My bet is that each mesh has it's own GO (with its own meshFilter/meshRenderer/transform) and then instantiates the parent GO along with all the children. If you want one singular mesh, you are going to have to combine them yourself, and assign each mesh to a submesh within the "master" mesh (don't forget to assign the material array either).

Link to comment
Share on other sites

Perhaps this could be a start? I haven't actually tried it, though.


GameObject model = GameDatabase.Instance.GetModel(...);
Mesh mesh = model.GetComponent<MeshFilter>().sharedMesh;

Pretty sure I tried something similar -


GameObject model = GameDatabase.Instance.GetModel(...);
MeshFilter[] meshFilter = model.GetComponents<MeshFilter>();

And meshFilter.Length was 0 (or meshFilter was null - one of the two - don't remember exactly). In any case, it didn't work. Right now it's more of a backburner question for a stretch goal on the plugin I'm playing with.

Link to comment
Share on other sites

Again, try and see if the object has child GameObjects, and if those have MeshFilters?


GameObject model = GameDatabase.Instance.GetModel(...);
for (int i = 0; i < model.transform.childCount; i++) {
GameObject child = model.transform.GetChild(i).gameObject;
...
}

or just this:


GameObject model = GameDatabase.Instance.GetModel(...);
MeshFilter[] meshFilters = model.GetComponentsInChildren<MeshFilter>();

Link to comment
Share on other sites

If this questions is still relevant:

yes, there are several mesh filters and several gameobjects per part..

dozens in some cases..

you can allways use this code to get a good view over your gameobjects..

http://forum.kerbalspaceprogram.com/threads/94011-RESOLVED-Unity-animation-Play-Problem?p=1422965&viewfull=1#post1422965

(Thank's to xEvilReeperx for that snip)

I'm pretty sure it will also display usefull info if you use MODEL{}, wich I din't in the case below..

The output looks something like this:

  SCAurora (Aurora) has components:
[LOG 21:50:02.088] ...c: UnityEngine.Transform
[LOG 21:50:02.089] ...c: Vessel
[LOG 21:50:02.089] ...c: OrbitDriver
[LOG 21:50:02.089] ...c: OrbitRenderer
[LOG 21:50:02.089] ...c: FlightIntegrator
[LOG 21:50:02.090] ...c: Part
[LOG 21:50:02.090] ...c: PartResource
[LOG 21:50:02.090] ...c: ModuleCommand
[LOG 21:50:02.091] ...c: ModuleGenerator
[LOG 21:50:02.091] ...c: ModuleReactionWheel
[LOG 21:50:02.091] ...c: ModuleSAS
[LOG 21:50:02.091] ...c: LCARSMarkII.LCARSMarkII
[LOG 21:50:02.092] ...c: ModuleAnimateGeneric
[LOG 21:50:02.092] ...c: TextureAnimator.TextureAnimateGeneric
[LOG 21:50:02.092] ...c: ModuleTripLogger
[LOG 21:50:02.093] ---> model has components:
[LOG 21:50:02.093] ......c: UnityEngine.Transform
[LOG 21:50:02.093] ------> rotator has components:
[LOG 21:50:02.094] .........c: UnityEngine.Transform
[LOG 21:50:02.094] ---------> SS-Aurora has components:
[LOG 21:50:02.094] ............c: UnityEngine.Transform
[LOG 21:50:02.094] ............c: UnityEngine.Animation
[LOG 21:50:02.095] ------------> Origin_external has components:
[LOG 21:50:02.095] ...............c: UnityEngine.Transform
[LOG 21:50:02.095] ---------------> Chair_002 has components:
[LOG 21:50:02.096] ..................c: UnityEngine.Transform
[LOG 21:50:02.096] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.096] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.097] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.097] ---------------> ConvexCollider has components:
[LOG 21:50:02.097] ..................c: UnityEngine.Transform
[LOG 21:50:02.098] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.098] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.098] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.099] ---------------> ConvexCollider_001 has components:
[LOG 21:50:02.099] ..................c: UnityEngine.Transform
[LOG 21:50:02.100] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.100] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.100] ---------------> ConvexCollider_002 has components:
[LOG 21:50:02.101] ..................c: UnityEngine.Transform
[LOG 21:50:02.101] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.101] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.102] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.102] ---------------> ConvexCollider_003 has components:
[LOG 21:50:02.102] ..................c: UnityEngine.Transform
[LOG 21:50:02.103] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.103] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.103] ---------------> ConvexCollider_004 has components:
[LOG 21:50:02.104] ..................c: UnityEngine.Transform
[LOG 21:50:02.104] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.104] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.105] ---------------> ConvexCollider_005 has components:
[LOG 21:50:02.105] ..................c: UnityEngine.Transform
[LOG 21:50:02.105] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.106] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.106] ---------------> ConvexCollider_006 has components:
[LOG 21:50:02.106] ..................c: UnityEngine.Transform
[LOG 21:50:02.107] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.107] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.107] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.108] ---------------> ConvexCollider_007 has components:
[LOG 21:50:02.108] ..................c: UnityEngine.Transform
[LOG 21:50:02.109] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.109] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.109] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.110] ---------------> ConvexCollider_008 has components:
[LOG 21:50:02.110] ..................c: UnityEngine.Transform
[LOG 21:50:02.110] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.111] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.111] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.111] ---------------> ConvexCollider_009 has components:
[LOG 21:50:02.112] ..................c: UnityEngine.Transform
[LOG 21:50:02.112] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.112] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.113] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.113] ---------------> Cube has components:
[LOG 21:50:02.113] ..................c: UnityEngine.Transform
[LOG 21:50:02.114] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.114] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.114] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.115] ---------------> Cylinder_015 has components:
[LOG 21:50:02.115] ..................c: UnityEngine.Transform
[LOG 21:50:02.116] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.116] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.116] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.117] ---------------> Door1 has components:
[LOG 21:50:02.117] ..................c: UnityEngine.Transform
[LOG 21:50:02.117] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.118] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.118] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.118] ---------------> Door2 has components:
[LOG 21:50:02.119] ..................c: UnityEngine.Transform
[LOG 21:50:02.119] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.119] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.120] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.120] ---------------> ForceField has components:
[LOG 21:50:02.120] ..................c: UnityEngine.Transform
[LOG 21:50:02.121] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.121] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.121] ---------------> Hull has components:
[LOG 21:50:02.122] ..................c: UnityEngine.Transform
[LOG 21:50:02.122] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.122] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.123] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.123] ---------------> Mball_001 has components:
[LOG 21:50:02.123] ..................c: UnityEngine.Transform
[LOG 21:50:02.124] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.124] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.124] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.125] ..................c: UnityEngine.Light
[LOG 21:50:02.125] ---------------> Plane_002 has components:
[LOG 21:50:02.125] ..................c: UnityEngine.Transform
[LOG 21:50:02.126] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.126] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.126] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.127] ---------------> Plane_003 has components:
[LOG 21:50:02.127] ..................c: UnityEngine.Transform
[LOG 21:50:02.127] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.128] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.128] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.128] ---------------> Plane_004 has components:
[LOG 21:50:02.129] ..................c: UnityEngine.Transform
[LOG 21:50:02.129] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.130] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.130] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.130] ---------------> Replicator1 has components:
[LOG 21:50:02.131] ..................c: UnityEngine.Transform
[LOG 21:50:02.131] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.131] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.132] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.132] ---------------> Sphere_003 has components:
[LOG 21:50:02.132] ..................c: UnityEngine.Transform
[LOG 21:50:02.133] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.133] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.133] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.134] ---------------> Sphere_004 has components:
[LOG 21:50:02.134] ..................c: UnityEngine.Transform
[LOG 21:50:02.134] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.135] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.135] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.135] ---------------> Sphere_005 has components:
[LOG 21:50:02.136] ..................c: UnityEngine.Transform
[LOG 21:50:02.136] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.136] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.137] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.137] ---------------> Sphere_006 has components:
[LOG 21:50:02.137] ..................c: UnityEngine.Transform
[LOG 21:50:02.138] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.138] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.138] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.139] ---------------> Sphere_007 has components:
[LOG 21:50:02.139] ..................c: UnityEngine.Transform
[LOG 21:50:02.140] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.140] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.140] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.141] ---------------> Sphere_015 has components:
[LOG 21:50:02.141] ..................c: UnityEngine.Transform
[LOG 21:50:02.141] ..................c: UnityEngine.MeshCollider
[LOG 21:50:02.142] ..................c: UnityEngine.MeshFilter
[LOG 21:50:02.142] ..................c: UnityEngine.MeshRenderer
[LOG 21:50:02.142] ------------> Origin_internal has components:
[LOG 21:50:02.143] ...............c: UnityEngine.Transform
[LOG 21:50:02.143] ---------> WorkStationPoints has components:
[LOG 21:50:02.143] ............c: UnityEngine.Transform
[LOG 21:50:02.144] ------------> ConsolePoint01 has components:
[LOG 21:50:02.144] ...............c: UnityEngine.Transform
[LOG 21:50:02.144] ------------> ConsolePoint02 has components:
[LOG 21:50:02.145] ...............c: UnityEngine.Transform
[LOG 21:50:02.145] ------------> ConsolePoint03 has components:
[LOG 21:50:02.145] ...............c: UnityEngine.Transform
[LOG 21:50:02.146] ------------> GameObject has components:
[LOG 21:50:02.146] ...............c: UnityEngine.Transform
[LOG 21:50:02.146] ------------> ReactorManagementPoint has components:
[LOG 21:50:02.147] ...............c: UnityEngine.Transform
[LOG 21:50:02.147] ---------> HullLights has components:
[LOG 21:50:02.147] ............c: UnityEngine.Transform
[LOG 21:50:02.148] ------------> GameObject has components:
[LOG 21:50:02.148] ...............c: UnityEngine.Transform
[LOG 21:50:02.148] ...............c: UnityEngine.Light
[LOG 21:50:02.149] ------------> GameObject has components:
[LOG 21:50:02.149] ...............c: UnityEngine.Transform
[LOG 21:50:02.149] ...............c: UnityEngine.Light
[LOG 21:50:02.150] ------------> GameObject has components:
[LOG 21:50:02.150] ...............c: UnityEngine.Transform
[LOG 21:50:02.150] ...............c: UnityEngine.Light
[LOG 21:50:02.151] ------------> GameObject has components:
[LOG 21:50:02.151] ...............c: UnityEngine.Transform
[LOG 21:50:02.151] ...............c: UnityEngine.Light
[LOG 21:50:02.152] ------------> GameObject has components:
[LOG 21:50:02.152] ...............c: UnityEngine.Transform
[LOG 21:50:02.152] ...............c: UnityEngine.Light

hope that helps..

Link to comment
Share on other sites

Again, try and see if the object has child GameObjects, and if those have MeshFilters?

... snip

or just this:


GameObject model = GameDatabase.Instance.GetModel(...);
MeshFilter[] meshFilters = model.GetComponentsInChildren<MeshFilter>();

meshFilters.Length = 0.

Link to comment
Share on other sites

That's because GetComponentsInChildren only returns active components. Use the overload instead

GameObject model = GameDatabase.Instance.GetModel(...);
MeshFilter[] meshFilters = model.GetComponentsInChildren<MeshFilter>(true);

Ah! That's it - now meshFilters.Length = 1, which is expected for the models in question. Thank you.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...