kahuna031
Feb. 22, 2017 14:04:38
New to Unity but not to Houdini. I want to create a fracture setup and get the pieces to get their respective collision geo. Naming them “piece0”, “piece0_collision_geo” etc doesn't work. It results in a object list as in the attachment, all collision geo merged to one piece and without any connection to their piece.
What's the correct way to do this?
Thanks!
damian
Feb. 24, 2017 11:37:21
You'll have to rename the pieces themselves to have the “_rendered_collision_geo” postfix in their name to in order to have the collision component be on the same GameObject as the part. There is no way currently in Unity to associate a collision geo with a different visible mesh on the same GameObject.
kahuna031
Feb. 24, 2017 12:13:47
I see.
I guess it's possible to hook up collision/render meshes by scripts?
damian
Feb. 24, 2017 15:38:51
I guess. But do the collision meshes really need to be different than the visible piece meshes? Why can't the piece mesh also be the collision mesh?
kahuna031
Feb. 24, 2017 15:41:12
I aim to have much higher resloution on the render geo than the collision.
damian
Feb. 24, 2017 16:50:06
Hmm, ya, that makes sense. Maybe submit an RFE to
support@sidefx.com?
kahuna031
Feb. 25, 2017 02:33:38
Going to do some tests with scripts and obj networks first to understand the problem better.
kahuna031
March 1, 2017 16:41:14
Ready to use script for this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class importHoudiniRBD : MonoBehaviour {
static void attachCollider(Transform renderXform, Transform colliderXform){
GameObject colObj = colliderXform.gameObject;
GameObject renderObj = renderXform.gameObject;
//print("Attaching collider: " + colObj.name);
MeshCollider collider= colObj.AddComponent<MeshCollider>(); //add collider
collider.convex = true;
colliderXform.SetParent(renderXform);
MeshRenderer colRend = colliderXform.GetComponent<MeshRenderer>();
colRend.enabled = false;
//check if allready processed
if (null==renderObj.GetComponent<Rigidbody>())
{
renderObj.AddComponent<Rigidbody>();
}
}
static int getChildIndexByName(Transform parent, string name, int numChildren)
{
int i =-1;
for (int ii = 0; ii < numChildren; ii++)
{
string currentName = parent.GetChild(ii).name;
//print(nameSplit[0]);
if (currentName.Contains(name+'_'))//'_' and a suffix int is added to the Houdini name
{
return ii; //found the correct i for current piece
}
}
return i;
}
public float test = 0;
// Adds a sphere collider to all children that doesn't have a collider
void Start()
{
//print("init" + this.gameObject.name);
//print("Components: " + this.gameObject.GetComponents(typeof(GameObject)));
Transform houObjNode = this.gameObject.transform;
int nChildren = houObjNode.childCount; //keep track of number of children as this number gets obsolete as we parent objects
print (nChildren);
int i = 0;
int j = 0;
int k = 0;
//List<string> pieces = new List<string>();
List<string> colliders = new List<string>();
//get all pieces to attach
foreach (Transform childXform in houObjNode)
{
//if (j > houObjNode.childCount) { continue; } //safety break
if (childXform.name.Contains("Collider")) { colliders.Add(childXform.name); }
//else { pieces.Add(childXform.name); }
}
foreach (string colliderName in colliders)
{
k++;
if (k > 1000) { break;} //safety break
i = -1;
j = -1;
string[] nameSplit = colliderName.Split(new string[] { "Collider" }, System.StringSplitOptions.None);
string renderObjName = nameSplit[0];
nameSplit = colliderName.Split(new string[] { "Collider" }, System.StringSplitOptions.None);
//get transform indices
i = getChildIndexByName(houObjNode, renderObjName, nChildren);
//check if we found render piece
if (i < 0)
{
Debug.LogError("Couldn't find render mesh: " + renderObjName);
//print("i:" + i +", j:" + j);
break;
}
j = getChildIndexByName(houObjNode, colliderName.Split('_')[0], nChildren);
if (j < 0) {
Debug.LogError("Can't process " + colliderName);
}
else {
Transform renderXform = houObjNode.GetChild(i);
Transform colXform = houObjNode.GetChild(j);
attachCollider(renderXform, colXform);
//print("Render object: " + renderXform);
nChildren--; //reduce loop size
}
}
}
}
damian
March 7, 2017 17:35:17
Nice. I'll see if we can incorporate this into the plugin proper, but no promises.
kahuna031
March 8, 2017 14:39:07
Please do. I'm updating the script above though, learned that parts can have multiple collision meshes so I'm changing the parenting order to allow that.