2828using System . Collections . Generic ;
2929using System . Linq ;
3030using UnityEngine ;
31- using UnityEngine . UIElements ;
3231
3332#if UNITY_SPLINES_INSTALLED
3433
@@ -127,7 +126,7 @@ public override bool CreateInputNodeWithDataUpload(HEU_SessionBase session, HAPI
127126 }
128127
129128 // Get spline data from the input object
130- HEU_InputDataSplineContainer inputSplines = GenerateSplineDataFromGameObject ( inputObject ) ;
129+ HEU_InputDataSplines inputSplines = GenerateSplineDataFromGameObject ( inputObject ) ;
131130 if ( inputSplines == null || inputSplines . _inputSplines == null || inputSplines . _inputSplines . Count ( ) == 0 )
132131 {
133132 HEU_Logger . LogError ( "No valid splines found on input objects." ) ;
@@ -145,7 +144,7 @@ public override bool CreateInputNodeWithDataUpload(HEU_SessionBase session, HAPI
145144 inputNodeID = newNodeID ;
146145
147146 HEU_InputDataSpline inputSpline = inputSplines . _inputSplines [ 0 ] ;
148- if ( ! UploadData ( session , inputNodeID , inputSpline , Matrix4x4 . identity ) )
147+ if ( ! UploadData ( session , inputNodeID , inputSpline ) )
149148 {
150149 if ( ! session . CookNode ( inputNodeID , false ) )
151150 {
@@ -156,16 +155,21 @@ public override bool CreateInputNodeWithDataUpload(HEU_SessionBase session, HAPI
156155 return false ;
157156 }
158157
159- // The spline is made up of branching sub-splines.
160- // Create an input node for each branching spline and object-merge it to the root spline.
158+ if ( ! session . CookNode ( inputNodeID , false ) )
159+ {
160+ HEU_Logger . LogError ( "New input node failed to cook!" ) ;
161+ return false ;
162+ }
163+
161164 bool createMergeNode = inputSplines . _inputSplines . Count ( ) > 1 ;
162165 if ( ! createMergeNode )
163166 return true ;
164167
168+ // Create merge node to merge branching splines
165169 HAPI_NodeId mergeNodeId = HEU_Defines . HEU_INVALID_NODE_ID ;
166170 HAPI_NodeId parentId = HEU_HAPIUtility . GetParentNodeID ( session , inputNodeID ) ;
167171
168- if ( ! session . CreateNode ( parentId , "merge" , null , false , out mergeNodeId ) )
172+ if ( ! session . CreateNode ( parentId , "merge" , "rajat-merge" , false , out mergeNodeId ) )
169173 {
170174 HEU_Logger . LogErrorFormat ( "Unable to create merge SOP node for connecting input assets." ) ;
171175 return false ;
@@ -183,20 +187,21 @@ public override bool CreateInputNodeWithDataUpload(HEU_SessionBase session, HAPI
183187 }
184188 inputNodeID = mergeNodeId ;
185189
186- Matrix4x4 localToWorld = inputSplines . _transform . localToWorldMatrix ;
187190 HAPI_NodeId branchNodeID ;
191+ string branchName = inputObject . name ;
188192 HEU_InputDataSpline branchSpline ;
193+ int inputNodeIndex = 1 ;
189194 for ( int i = 1 ; i < inputSplines . _inputSplines . Count ( ) ; i ++ )
190195 {
191- session . CreateInputCurveNode ( out branchNodeID , inputObject . name + "_" + i ) ;
196+ session . CreateInputCurveNode ( out branchNodeID , branchName + "_" + i ) ;
192197 if ( branchNodeID == HEU_Defines . HEU_INVALID_NODE_ID || ! HEU_HAPIUtility . IsNodeValidInHoudini ( session , branchNodeID ) )
193198 {
194199 HEU_Logger . LogError ( "Failed to create new input cruve node in Houdini session!" ) ;
195200 return false ;
196201 }
197202
198203 branchSpline = inputSplines . _inputSplines [ i ] ;
199- if ( ! UploadData ( session , branchNodeID , branchSpline , localToWorld ) )
204+ if ( ! UploadData ( session , branchNodeID , branchSpline , true ) )
200205 {
201206 if ( ! session . CookNode ( branchNodeID , false ) )
202207 {
@@ -211,6 +216,7 @@ public override bool CreateInputNodeWithDataUpload(HEU_SessionBase session, HAPI
211216 HEU_Logger . LogErrorFormat ( "Unable to connect to input node!" ) ;
212217 return false ;
213218 }
219+ inputNodeIndex ++ ;
214220 }
215221
216222 if ( ! session . CookNode ( inputNodeID , false ) )
@@ -228,6 +234,7 @@ public override bool CreateInputNodeWithDataUpload(HEU_SessionBase session, HAPI
228234 public class HEU_InputDataSpline
229235 {
230236 public Spline _spline ;
237+ public Transform _transform ;
231238 public bool _closed ;
232239 public int _count ;
233240 public float _length ;
@@ -237,10 +244,9 @@ public class HEU_InputDataSpline
237244 /// <summary>
238245 /// Contains input geometry for multiple splines.
239246 /// </summary>
240- public class HEU_InputDataSplineContainer : HEU_InputData
247+ public class HEU_InputDataSplines : HEU_InputData
241248 {
242249 public List < HEU_InputDataSpline > _inputSplines = new List < HEU_InputDataSpline > ( ) ;
243- public Transform _transform ;
244250 }
245251
246252 /// <summary>
@@ -249,26 +255,25 @@ public class HEU_InputDataSplineContainer : HEU_InputData
249255 /// </summary>
250256 /// <param name="inputObject">GameObject containing a Spline component</param>
251257 /// <returns>A valid input data strcuture containing spline data</returns>
252- public HEU_InputDataSplineContainer GenerateSplineDataFromGameObject ( GameObject inputObject )
258+ public HEU_InputDataSplines GenerateSplineDataFromGameObject ( GameObject inputObject )
253259 {
254260 SplineContainer splineContainer = inputObject . GetComponent < SplineContainer > ( ) ;
255261 IReadOnlyList < Spline > splines = splineContainer . Splines ;
256262
257- HEU_InputDataSplineContainer splineContainerData = new HEU_InputDataSplineContainer ( ) ;
263+ HEU_InputDataSplines splineData = new HEU_InputDataSplines ( ) ;
258264 foreach ( Spline spline in splines )
259265 {
260- HEU_InputDataSpline splineData = new HEU_InputDataSpline ( ) ;
261- splineData . _spline = spline ;
262- splineData . _closed = spline . Closed ;
263- splineData . _count = spline . Count ;
264- splineData . _length = spline . GetLength ( ) ;
265- splineData . _knots = spline . Knots . ToArray < BezierKnot > ( ) ;
266-
267- splineContainerData . _inputSplines . Add ( splineData ) ;
266+ HEU_InputDataSpline inputSpline = new HEU_InputDataSpline ( ) ;
267+ inputSpline . _spline = spline ;
268+ inputSpline . _transform = inputObject . transform ;
269+ inputSpline . _closed = spline . Closed ;
270+ inputSpline . _count = spline . Count ;
271+ inputSpline . _length = spline . GetLength ( ) ;
272+ inputSpline . _knots = spline . Knots . ToArray < BezierKnot > ( ) ;
273+
274+ splineData . _inputSplines . Add ( inputSpline ) ;
268275 }
269- splineContainerData . _transform = inputObject . transform ;
270-
271- return splineContainerData ;
276+ return splineData ;
272277 }
273278
274279 /// <summary>
@@ -278,14 +283,16 @@ public HEU_InputDataSplineContainer GenerateSplineDataFromGameObject(GameObject
278283 /// <param name="inputNodeID">ID of the input node</param>
279284 /// <param name="inputData">Container of the mesh geometry</param>
280285 /// <returns>True if successfully uploaded data</returns>
281- public bool UploadData ( HEU_SessionBase session , HAPI_NodeId inputNodeID , HEU_InputDataSpline inputSpline , Matrix4x4 localToWorld )
286+ public bool UploadData ( HEU_SessionBase session , HAPI_NodeId inputNodeID , HEU_InputDataSpline inputSpline , bool toWorld = false )
282287 {
283288 // Set the input curve info of the newly created input curve
284289 HAPI_InputCurveInfo inputCurveInfo = new HAPI_InputCurveInfo ( ) ;
285290 inputCurveInfo . curveType = HAPI_CurveType . HAPI_CURVETYPE_BEZIER ;
286- inputCurveInfo . order = 4 ;
291+ inputCurveInfo . order = 4 ; // Recommended default
287292 inputCurveInfo . closed = inputSpline . _closed ;
288293 inputCurveInfo . reverse = false ;
294+
295+ // Curve always goes through the specified points
289296 inputCurveInfo . inputMethod = HAPI_InputCurveMethod . HAPI_CURVEMETHOD_BREAKPOINTS ;
290297 inputCurveInfo . breakpointParameterization = HAPI_InputCurveParameterization . HAPI_CURVEPARAMETERIZATION_UNIFORM ;
291298 if ( ! session . SetInputCurveInfo ( inputNodeID , 0 , ref inputCurveInfo ) )
@@ -294,16 +301,18 @@ public bool UploadData(HEU_SessionBase session, HAPI_NodeId inputNodeID, HEU_Inp
294301 return false ;
295302 }
296303
297- // Calculate the number of refined points we want
304+ // Calculate the number of refined point we want
298305 int numControlPoints = inputSpline . _knots . Count ( ) ;
299306 float splineLength = inputSpline . _length ;
300- float splineResolution = settings != null ? settings . SamplingResolution : 0.0f ;
307+ float splineResolution = settings != null ? settings . SamplingResolution : 0.5f ;
308+
301309 int numRefinedSplinePoints = splineResolution > 0.0f ? Mathf . CeilToInt ( splineLength / splineResolution ) + 1 : numControlPoints ;
310+ Matrix4x4 localToWorld = inputSpline . _transform . localToWorldMatrix ;
302311
303312 float [ ] posArr ;
304313 float [ ] rotArr ;
305314 float [ ] scaleArr ;
306- if ( numRefinedSplinePoints <= numControlPoints )
315+ if ( numRefinedSplinePoints < numControlPoints )
307316 {
308317 // There's not enough refined points, so we'll use the control points instead
309318 posArr = new float [ numControlPoints * 3 ] ;
@@ -312,10 +321,7 @@ public bool UploadData(HEU_SessionBase session, HAPI_NodeId inputNodeID, HEU_Inp
312321 for ( int i = 0 ; i < numControlPoints ; i ++ )
313322 {
314323 BezierKnot knot = inputSpline . _knots [ i ] ;
315-
316- // For branching sub-splines, apply local transform on vertices to get the merged spline
317- float3 pos = localToWorld . MultiplyPoint ( knot . Position ) ;
318-
324+ float3 pos = toWorld ? localToWorld . MultiplyPoint ( knot . Position ) : knot . Position ;
319325 HEU_HAPIUtility . ConvertPositionUnityToHoudini ( pos , out posArr [ i * 3 + 0 ] , out posArr [ i * 3 + 1 ] , out posArr [ i * 3 + 2 ] ) ;
320326 HEU_HAPIUtility . ConvertRotationUnityToHoudini ( knot . Rotation , out rotArr [ i * 4 + 0 ] , out rotArr [ i * 4 + 1 ] , out rotArr [ i * 4 + 2 ] , out rotArr [ i * 4 + 3 ] ) ;
321327 }
@@ -330,32 +336,22 @@ public bool UploadData(HEU_SessionBase session, HAPI_NodeId inputNodeID, HEU_Inp
330336 for ( int i = 0 ; i < numRefinedSplinePoints ; i ++ )
331337 {
332338 float3 pos = SplineUtility . EvaluatePosition < Spline > ( inputSpline . _spline , currentDistance / splineLength ) ;
333-
334- // For branching sub-splines, apply local transform on vertices to get the merged spline
335- pos = localToWorld . MultiplyPoint ( pos ) ;
336-
339+ if ( toWorld )
340+ {
341+ pos = localToWorld . MultiplyPoint ( pos ) ;
342+ }
337343 HEU_HAPIUtility . ConvertPositionUnityToHoudini ( pos , out posArr [ i * 3 + 0 ] , out posArr [ i * 3 + 1 ] , out posArr [ i * 3 + 2 ] ) ;
344+
338345 currentDistance += splineResolution ;
339346 }
340347 }
341348
342- bool hasRotations = rotArr . Length == posArr . Length ;
343- bool hasScales = scaleArr . Length == posArr . Length ;
344- bool hapi_result = false ;
345- if ( ! hasRotations && ! hasScales )
346- {
347- hapi_result = session . SetInputCurvePositions ( inputNodeID , 0 , posArr , 0 , posArr . Length ) ;
348- }
349- else
350- {
351- hapi_result = session . SetInputCurvePositionsRotationsScales (
352- inputNodeID , 0 ,
353- posArr , 0 , posArr . Length ,
354- rotArr , 0 , rotArr . Length ,
355- scaleArr , 0 , 0
356- ) ;
357- }
358-
349+ bool hapi_result = session . SetInputCurvePositionsRotationsScales (
350+ inputNodeID , 0 ,
351+ posArr , 0 , posArr . Length ,
352+ rotArr , 0 , rotArr . Length ,
353+ scaleArr , 0 , 0
354+ ) ;
359355 if ( ! hapi_result )
360356 {
361357 HEU_Logger . LogError ( "Failed to set input curve positions." ) ;
0 commit comments