create or duplicate asset by C#

   193   2   2
User Avatar
Member
2 posts
Joined: Nov. 2016
Offline
hello,

i'm trying the new plugin (v2 beta), and i want to duplicate many copy of my asset (a building for a little town)and change for each copy some parameters.

so i want to do it by C#. but i can't find a way to do it.
(i've done it before with the plugin V1 but all is so different…)

is it possible to have a (very)simple example of creating (or duplicating) an asset by C# ?


thank's a lot.


phil
User Avatar
Staff
141 posts
Joined: May 2017
Offline
Very simple actually. Just call HEU_HoudiniAsset:: DuplicateAsset() which should return a new GameObject that is a duplicate of the asset. This is what the Duplicate Asset button does.

Here is an example of duplicating a selected asset via C#:
[MenuItem("Test/TestDuplicate")]
public static void TestDuplicate()
{
	GameObject rootGO = Selection.activeGameObject;
	if(rootGO == null)
	{
		Debug.LogWarning("Nothing selected. You need to select an asset!");
		return;
	}

	// The selected gameobject is the asset's root gameobject.
	// Get the root component (HEU_HoudiniAssetRoot) which will allow us to get the asset component (HEU_HoudiniAsset) itself.
	HoudiniEngineUnity.HEU_HoudiniAssetRoot root = rootGO.GetComponent<HoudiniEngineUnity.HEU_HoudiniAssetRoot>();
	if (root != null)
	{
		HoudiniEngineUnity.HEU_HoudiniAsset asset = root._houdiniAsset;

		// This will return a duplicated asset as a gameobject
		asset.DuplicateAsset();
	}
}

Note that I just fixed a regression with the DuplicateAsset function that will be in tomorrow's build (H16.5.502). The duplicated asset wasn't cooking.

The reasons for the explicit duplicate method as opposed to using Unity's built-in duplicate or copy/paste mechanism are that aside from the lack of a consistent way to capture copy/paste or duplicate events in the Unity, we also need to create a duplicate asset in the background Houdini session that connects to the Unity's duplicate instance, and we're using ScriptableObjects internally in the asset, which do not get duplicated via the built-in way but rather just have their references copied.
Edited by seelan - June 12, 2018 09:45:40
User Avatar
Member
2 posts
Joined: Nov. 2016
Offline
thank's a lot.

all is fine for this part now.
  • Quick Links