private void cacheNumInstances() { var obj_idx = prAsset.findObjectByNodeId(prObjectId); HAPI_ObjectInfo object_info = prAsset.prObjects[obj_idx]; // Get Detail info. HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId); if (geo_info.partCount == 0) { return; } HAPI_PartInfo part_info = HoudiniHost.getPartInfo(geo_info.nodeId, 0); if (prAsset.prEnableLogging) { Debug.Log("Instancer #" + prObjectId + " (" + object_info.name + "): " + "points: " + part_info.pointCount); } if (part_info.pointCount > 65000) { throw new HoudiniError("Point count (" + part_info.pointCount + ") above limit (" + 65000 + ")!"); } myNumInstances = part_info.pointCount; }
private void getInstanceAndNameAttrs(out int[] instance_attr, out int[] name_attr) { instance_attr = new int[0]; name_attr = new int[0]; HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId); HAPI_AttributeInfo instance_attr_info = new HAPI_AttributeInfo("instance"); Utility.getAttribute( geo_info.nodeId, 0, "instance", ref instance_attr_info, ref instance_attr, HoudiniHost.getAttributeStringData); if (!instance_attr_info.exists) { return; } if (instance_attr_info.exists && instance_attr_info.owner != HAPI_AttributeOwner.HAPI_ATTROWNER_POINT) { throw new HoudiniErrorIgnorable("I only understand instance as point attributes!"); } if (instance_attr_info.exists && instance_attr.Length != myNumInstances) { throw new HoudiniError( "Unexpected instance_hint array length found for asset: " + prAsset.prAssetId + "!"); } HAPI_AttributeInfo name_attr_info = new HAPI_AttributeInfo("name"); Utility.getAttribute( geo_info.nodeId, 0, "name", ref name_attr_info, ref name_attr, HoudiniHost.getAttributeStringData); if (name_attr_info.exists && name_attr_info.owner != HAPI_AttributeOwner.HAPI_ATTROWNER_POINT) { throw new HoudiniErrorIgnorable("I only understand name as point attributes!"); } if (name_attr_info.exists && name_attr.Length != myNumInstances) { throw new HoudiniError( "Unexpected name array length found for asset: " + prAsset.prAssetId + "!"); } }
public bool refresh(bool reload_asset, HAPI_ObjectInfo object_info) { bool needs_recook = false; if (reload_asset) { for (int i = 0; i < myGeos.Count; ++i) { if (myGeos[i]) { HoudiniAssetUtility.destroyGameObject(myGeos[i]); } } myGeos.Clear(); } if (reload_asset || object_info.haveGeosChanged) { // TODO: Add back support for templated geos and curve SOPs. HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId); object_info.geoCount = 1; // Add new geos as needed. while (myGeos.Count < object_info.geoCount) { myGeos.Add(createGeo(geo_info.nodeId)); } // Remove stale geos. while (myGeos.Count > object_info.geoCount) { HoudiniAssetUtility.destroyGameObject(myGeos[object_info.geoCount]); myGeos.RemoveAt(object_info.geoCount); } // Refresh all geos. for (int i = 0; i < myGeos.Count; ++i) { needs_recook |= myGeos[i].GetComponent <HoudiniGeoControl>().refresh(reload_asset); } } return(needs_recook); }
protected override bool buildCreateObjects(bool reload_asset, ref HoudiniProgressBar progress_bar) { try { prCurve.syncPointsWithParm(); HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prCurve.prControl.prAssetId); HAPI_NodeInfo geo_node_info = HoudiniHost.getNodeInfo(geo_info.nodeId); prCurve.createObject(geo_node_info.parentId, geo_info.nodeId); HoudiniHost.repaint(); } catch (HoudiniError) { // Per-object errors are not re-thrown so that the rest of the asset has a chance to load. //Debug.LogWarning( error.ToString() ); } return(false); }
public void instanceObjects(HoudiniProgressBar progress_bar) { try { destroyChildren(); HAPI_ObjectInfo object_info = HoudiniHost.getObjectInfo(prObjectId); // Get Detail info. HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId); if (geo_info.partCount == 0) { return; } cacheNumInstances(); HAPI_Transform[] instance_transforms = new HAPI_Transform[myNumInstances]; Utility.getArray2Id( geo_info.nodeId, HAPI_RSTOrder.HAPI_SRT, HoudiniHost.getInstanceTransforms, instance_transforms, myNumInstances); // Get scale point attributes. HAPI_AttributeInfo scale_attr_info = new HAPI_AttributeInfo("scale"); float[] scale_attr = new float[0]; Utility.getAttribute( geo_info.nodeId, 0, "scale", ref scale_attr_info, ref scale_attr, HoudiniHost.getAttributeFloatData); if (scale_attr_info.exists && scale_attr_info.owner != HAPI_AttributeOwner.HAPI_ATTROWNER_POINT) { throw new HoudiniErrorIgnorable("I only understand scale as point attributes!"); } if (scale_attr_info.exists && scale_attr.Length != myNumInstances * 3) { throw new HoudiniError( "Unexpected scale array length found for asset: " + prAsset.prAssetId + "!\n" + "Expected length of: " + myNumInstances * 3 + " but given: " + scale_attr.Length); } HAPI_AttributeInfo script_attr_info = new HAPI_AttributeInfo("Unity_Script"); int[] script_attr = new int[0]; Utility.getAttribute( geo_info.nodeId, 0, "Unity_Script", ref script_attr_info, ref script_attr, HoudiniHost.getAttributeStringData); if (script_attr_info.exists && script_attr_info.owner != HAPI_AttributeOwner.HAPI_ATTROWNER_POINT) { throw new HoudiniErrorIgnorable("I only understand Unity_Script as point attributes!"); } if (script_attr_info.exists && script_attr.Length != myNumInstances) { throw new HoudiniError("Unexpected Unity_Script array length found for asset: " + prAsset.prAssetId + "!"); } int[] instance_attr = null; int[] name_attr = null; getInstanceAndNameAttrs(out instance_attr, out name_attr); progress_bar.prTotal = myNumInstances; List <int> exclusion_list = new List <int>(); instanceOverriddenObjects(myNumInstances, exclusion_list); List <string> unique_instantiated_names = new List <string>(); HoudiniInstancerPersistentData persistent_data = prPersistentData; if (persistent_data.variationChoice.Count != myNumInstances) { if (myNumInstances > persistent_data.variationChoice.Count) { int difference = myNumInstances - persistent_data.variationChoice.Count; for (int ii = 0; ii < difference; ii++) { persistent_data.variationChoice.Add(-1); } } else { int difference = persistent_data.variationChoice.Count - myNumInstances; persistent_data.variationChoice.RemoveRange( persistent_data.variationChoice.Count - difference, difference); } } bool liveTransformPropagationSetting = false; bool syncAssetTransformSetting = false; bool enableCooking = true; for (int i = 0; i < myNumInstances; ++i) { if (exclusion_list.Contains(i)) { continue; } GameObject obj_to_instance = null; if (instance_attr.Length > 0 || name_attr.Length > 0) { if (name_attr.Length > 0) { string obj_name = HoudiniHost.getString(name_attr[i]); int object_index = prAsset.findObjectByName(obj_name); if (object_index >= 0) { obj_to_instance = prAsset.prGameObjects[object_index]; } else { obj_to_instance = prAsset.findPartByName(obj_name, true); } if (obj_to_instance == null) { obj_to_instance = GameObject.Find(obj_name); } } else { string instanceObjectPath = HoudiniHost.getString(instance_attr[i]); string [] pathItems = instanceObjectPath.Split('/'); string instanceObjectName = pathItems[pathItems.Length - 1]; int objectIndex = prAsset.findObjectByName(instanceObjectName); if (objectIndex >= 0) { obj_to_instance = prAsset.prGameObjects[objectIndex]; } else { obj_to_instance = GameObject.Find(instanceObjectName); } } if (obj_to_instance != null) { HoudiniAsset hapi_asset = obj_to_instance.GetComponent <HoudiniAsset>(); if (hapi_asset != null) { liveTransformPropagationSetting = hapi_asset.prTransformChangeTriggersCooks; syncAssetTransformSetting = hapi_asset.prPushUnityTransformToHoudini; enableCooking = hapi_asset.prEnableCooking; hapi_asset.prTransformChangeTriggersCooks = false; hapi_asset.prPushUnityTransformToHoudini = false; hapi_asset.prEnableCooking = false; } } } else if (object_info.objectToInstanceId >= 0) { int object_to_instance_idx = prAsset.findObjectByNodeId(object_info.objectToInstanceId); if (object_to_instance_idx >= 0) { obj_to_instance = prAsset.prGameObjects[object_to_instance_idx]; } } if (obj_to_instance != null) { // Set progress bar information. progress_bar.prCurrentValue = i; progress_bar.prMessage = "Instancing: " + obj_to_instance.name + " (" + i + " of " + myNumInstances + ")"; progress_bar.displayProgressBar(); if (!unique_instantiated_names.Contains(obj_to_instance.name)) { unique_instantiated_names.Add(obj_to_instance.name); } Vector3 pos = new Vector3(); // Apply object transforms. // // Axis and Rotation conversions: // Note that Houdini's X axis points in the opposite direction that Unity's does. Also, Houdini's // rotation is right handed, whereas Unity is left handed. To account for this, we need to invert // the x coordinate of the translation, and do the same for the rotations (except for the x rotation, // which doesn't need to be flipped because the change in handedness AND direction of the left x axis // causes a double negative - yeah, I know). pos[0] = -instance_transforms[i].position[0]; pos[1] = instance_transforms[i].position[1]; pos[2] = instance_transforms[i].position[2]; Quaternion quat = new Quaternion(instance_transforms[i].rotationQuaternion[0], instance_transforms[i].rotationQuaternion[1], instance_transforms[i].rotationQuaternion[2], instance_transforms[i].rotationQuaternion[3]); Vector3 euler = quat.eulerAngles; euler.y = -euler.y; euler.z = -euler.z; Vector3 scale = new Vector3(instance_transforms[i].scale[0], instance_transforms[i].scale[1], instance_transforms[i].scale[2]); Matrix4x4 local_mat = new Matrix4x4(); local_mat.SetTRS(pos, Quaternion.Euler(euler), scale); // TODO: Now this *should* be the transform.localToWorldMatrix // but for some reason, after a scene load, we pick up compensating // factors in the local transform that cancel out the transform on the // asset. For now just use the asset's transform as the parent matrix. Matrix4x4 parent_mat = prAsset.transform.localToWorldMatrix; Matrix4x4 global_mat = parent_mat * local_mat; euler = HoudiniAssetUtility.getQuaternion(global_mat).eulerAngles; pos = HoudiniAssetUtility.getPosition(global_mat); scale = HoudiniAssetUtility.getScale(global_mat); //mat.SetTRS( pos, string script_to_attach = ""; if (script_attr_info.exists) { script_to_attach = HoudiniHost.getString(script_attr[i]); } instanceObject(obj_to_instance, pos, euler, i, scale_attr_info.exists, scale, script_attr_info.exists, script_to_attach); HoudiniAsset hapi_asset = obj_to_instance.GetComponent <HoudiniAsset>(); if (hapi_asset != null) { hapi_asset.prTransformChangeTriggersCooks = liveTransformPropagationSetting; hapi_asset.prPushUnityTransformToHoudini = syncAssetTransformSetting; hapi_asset.prEnableCooking = enableCooking; } } } updateUniqueInstantiatedNames(unique_instantiated_names); } catch (HoudiniError error) { Debug.LogWarning(error.ToString()); return; } }
public void bakeAnimation(float curr_time, GameObject parent_object) { try { HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId); HAPI_Transform[] instance_transforms = new HAPI_Transform[myNumInstances]; Utility.getArray2Id( geo_info.nodeId, HAPI_RSTOrder.HAPI_SRT, HoudiniHost.getInstanceTransforms, instance_transforms, myNumInstances); Matrix4x4 parent_xform_inverse = Matrix4x4.identity; if (parent_object != null) { parent_xform_inverse = parent_object.transform.localToWorldMatrix.inverse; } for (int ii = 0; ii < myNumInstances; ++ii) { Vector3 pos = new Vector3(); // Apply object transforms. // // Axis and Rotation conversions: // Note that Houdini's X axis points in the opposite direction that Unity's does. Also, Houdini's // rotation is right handed, whereas Unity is left handed. To account for this, we need to invert // the x coordinate of the translation, and do the same for the rotations (except for the x rotation, // which doesn't need to be flipped because the change in handedness AND direction of the left x axis // causes a double negative - yeah, I know). pos[0] = -instance_transforms[ii].position[0]; pos[1] = instance_transforms[ii].position[1]; pos[2] = instance_transforms[ii].position[2]; Quaternion quat = new Quaternion(instance_transforms[ii].rotationQuaternion[0], instance_transforms[ii].rotationQuaternion[1], instance_transforms[ii].rotationQuaternion[2], instance_transforms[ii].rotationQuaternion[3]); Vector3 euler = quat.eulerAngles; euler.y = -euler.y; euler.z = -euler.z; quat = Quaternion.Euler(euler); Vector3 scale = new Vector3(instance_transforms[ii].scale[0], instance_transforms[ii].scale[1], instance_transforms[ii].scale[2]); if (parent_object != null) { Matrix4x4 world_mat = Matrix4x4.identity; world_mat.SetTRS(pos, quat, scale); Matrix4x4 local_mat = parent_xform_inverse * world_mat; quat = HoudiniAssetUtility.getQuaternion(local_mat); scale = HoudiniAssetUtility.getScale(local_mat); pos = HoudiniAssetUtility.getPosition(local_mat); } HoudiniCurvesCollection curves = myCurvesCollection[ii]; HoudiniAssetUtility.addKeyToCurve(curr_time, pos[0], curves.tx); HoudiniAssetUtility.addKeyToCurve(curr_time, pos[1], curves.ty); HoudiniAssetUtility.addKeyToCurve(curr_time, pos[2], curves.tz); HoudiniAssetUtility.addKeyToCurve(curr_time, quat.x, curves.qx); HoudiniAssetUtility.addKeyToCurve(curr_time, quat.y, curves.qy); HoudiniAssetUtility.addKeyToCurve(curr_time, quat.z, curves.qz); HoudiniAssetUtility.addKeyToCurve(curr_time, quat.w, curves.qw); HoudiniAssetUtility.addKeyToCurve(curr_time, scale.x, curves.sx); HoudiniAssetUtility.addKeyToCurve(curr_time, scale.y, curves.sy); HoudiniAssetUtility.addKeyToCurve(curr_time, scale.z, curves.sz); } } catch (HoudiniError error) { Debug.LogWarning(error.ToString()); return; } }
public bool refresh(bool reload_asset, HAPI_ObjectInfo object_info) { bool needs_recook = false; if (reload_asset) { for (int i = 0; i < myGeos.Count; ++i) { if (myGeos[i]) { HoudiniAssetUtility.destroyGameObject(myGeos[i]); } } myGeos.Clear(); } if (reload_asset || object_info.haveGeosChanged) { // Get the GeoInfos of the display geo node HAPI_GeoInfo display_geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId); int GeoCount = object_info.geoCount = 1; // Add new geos as needed. while (myGeos.Count < GeoCount) { myGeos.Add(createGeo(display_geo_info.nodeId)); } int node_id = object_info.nodeId; if (prAsset.prNodeInfo.type == HAPI_NodeType.HAPI_NODETYPE_SOP) { node_id = display_geo_info.nodeId; } // Look for editable nodes inside the network/the object const bool recursive = true; int[] editable_networks = HoudiniHost.getChildNodeList( node_id, (int)HAPI_NodeType.HAPI_NODETYPE_SOP, (int)HAPI_NodeFlags.HAPI_NODEFLAGS_EDITABLE, recursive); // Add the editable nodes to it for (int n = 0; n < editable_networks.Length; n++) { // The editable node has to be cooked first HoudiniHost.cookNode(editable_networks[n]); HAPI_GeoInfo editGeoInfo = HoudiniHost.getGeoInfo(editable_networks[n]); myGeos.Add(createGeo(editGeoInfo.nodeId)); GeoCount++; } // Remove stale geos. while (myGeos.Count > GeoCount) { HoudiniAssetUtility.destroyGameObject(myGeos[GeoCount]); myGeos.RemoveAt(GeoCount); } // Refresh all geos. for (int i = 0; i < myGeos.Count; ++i) { needs_recook |= myGeos[i].GetComponent <HoudiniGeoControl>().refresh(reload_asset); } } return(needs_recook); }