Jump to content

Programmatic access to MODEL nodes?


Recommended Posts

Hi folks,

I've been trying my hand at plugin programming this weekend, and I've mostly been able to make progress by studying API documentation and the source of other plugins, but I'm having no luck figuring out how a partmodule can get access to its part's MODEL nodes, or to whatever Unity objects those get turned into when KSP parses cfg files. Does anyone know what I might be missing or where I should be looking for it?

Link to comment
Share on other sites

Update after much experimentation, with some information about the component hierarchy I wish I'd seen documented somewhere:

part.GetComponents<Component>() basically seems to be the highest-level way to explore all of a part's inner workings and returns an array of various stuff, including a Transform which is the only thing that's interesting to the present purpose.

part.GetComponent<Transform>() is one of the elements of that array, and still isn't very interesting. It just has another Transform called "model" as a child.

"model" is part.GetComponent<Transform>().GetChild(0), but part has another method that lets us skip all that; we can also get "model" via part.FindModelComponent<Transform>().

"model" is interesting because its direct children correspond to MODEL nodes from the cfg. Transforms don't seem to offer up an array of their children, but we can call Transform.childCount() to find out how many there are and then Transform.GetChild(i) to access the i'th child. Transform parent-child relationships aren't the same as object-component relationships; "model" is its own component, so that part.FindModelComponent<Transform>().FindModelComponents<Transform>() just returns an array with only "model" in it, not the array of its children that I initially hoped for.

The Transforms corresponding to the MODEL nodes each have children corresponding to the hierarchy of meshes within each model.

If we call part.FindModelComponents<Transform>(), the result is the entire model hierarchy flattened into a list. It seems to follow the order in which the MODEL nodes are declared in the cfg.

part.FindModelTransform(s) gets a Transform by name, but the name for each MODEL node has "(Clone)" appended to it, so if your MODEL node had "model = YourMod/model" then you'd get its associated Transform by part.FindModelTransform("YourMod/model(Clone)"). You can also directly use the name of a mesh within a model. part.FindModelTransforms(s) gets all the Transforms that share a name, which might be useful when you're reusing a model repeatedly. We can also call Transform.FindChild(s) to look one up by name from the transform instead of from the part; again, Transform doesn't seem to be very friendly to retrieving an array of it children.

Link to comment
Share on other sites

You could try parsing the .cfg directly

part.partInfo.partConfig

You could also explore using "part.GetComponent<type>" if you know what you're looking for

That's null now except in the loading scene. ConfigNodes are not serializable anymore.

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...