|
| 1 | +""" An example script that uses the API to instantiate two HDAs. The first HDA |
| 2 | +will be used as an input to the second HDA. For the second HDA we set 2 inputs: |
| 3 | +an asset input (the first instantiated HDA) and a curve input (a helix). The |
| 4 | +inputs are set during post instantiation (before the first cook). After the |
| 5 | +first cook and output creation (post processing) the input structure is fetched |
| 6 | +and logged. |
| 7 | +
|
| 8 | +""" |
| 9 | +import math |
| 10 | + |
| 11 | +import unreal |
| 12 | + |
| 13 | +_g_wrapper1 = None |
| 14 | +_g_wrapper2 = None |
| 15 | + |
| 16 | + |
| 17 | +def get_copy_curve_hda_path(): |
| 18 | + return '/HoudiniEngine/Examples/hda/copy_to_curve_1_0.copy_to_curve_1_0' |
| 19 | + |
| 20 | + |
| 21 | +def get_copy_curve_hda(): |
| 22 | + return unreal.load_object(None, get_copy_curve_hda_path()) |
| 23 | + |
| 24 | + |
| 25 | +def get_pig_head_hda_path(): |
| 26 | + return '/HoudiniEngine/Examples/hda/pig_head_subdivider_v01.pig_head_subdivider_v01' |
| 27 | + |
| 28 | + |
| 29 | +def get_pig_head_hda(): |
| 30 | + return unreal.load_object(None, get_pig_head_hda_path()) |
| 31 | + |
| 32 | + |
| 33 | +def configure_inputs(in_wrapper): |
| 34 | + print('configure_inputs') |
| 35 | + |
| 36 | + # Unbind from the delegate |
| 37 | + in_wrapper.on_post_instantiation_delegate.remove_callable(configure_inputs) |
| 38 | + |
| 39 | + # Create a geo input |
| 40 | + asset_input = in_wrapper.create_empty_input(unreal.HoudiniPublicAPIAssetInput) |
| 41 | + # Set the input objects/assets for this input |
| 42 | + # asset_input.set_input_objects((_g_wrapper1.get_houdini_asset_actor().houdini_asset_component, )) |
| 43 | + asset_input.set_input_objects((_g_wrapper1, )) |
| 44 | + # copy the input data to the HDA as node input 0 |
| 45 | + in_wrapper.set_input_at_index(0, asset_input) |
| 46 | + # We can now discard the API input object |
| 47 | + asset_input = None |
| 48 | + |
| 49 | + # Create a curve input |
| 50 | + curve_input = in_wrapper.create_empty_input(unreal.HoudiniPublicAPICurveInput) |
| 51 | + # Create a curve wrapper/helper |
| 52 | + curve_object = unreal.HoudiniPublicAPICurveInputObject(curve_input) |
| 53 | + # Make it a Nurbs curve |
| 54 | + curve_object.set_curve_type(unreal.HoudiniPublicAPICurveType.NURBS) |
| 55 | + # Set the points of the curve, for this example we create a helix |
| 56 | + # consisting of 100 points |
| 57 | + curve_points = [] |
| 58 | + for i in range(10): |
| 59 | + t = i / 10.0 * math.pi * 2.0 |
| 60 | + x = 100.0 * math.cos(t) |
| 61 | + y = 100.0 * math.sin(t) |
| 62 | + z = i * 10.0 |
| 63 | + curve_points.append(unreal.Transform([x, y, z], [0, 0, 0], [1, 1, 1])) |
| 64 | + curve_object.set_curve_points(curve_points) |
| 65 | + # Set the curve wrapper as an input object |
| 66 | + curve_input.set_input_objects((curve_object, )) |
| 67 | + # Copy the input data to the HDA as node input 1 |
| 68 | + in_wrapper.set_input_at_index(1, curve_input) |
| 69 | + # We can now discard the API input object |
| 70 | + curve_input = None |
| 71 | + |
| 72 | + |
| 73 | +def print_api_input(in_input): |
| 74 | + print('\t\tInput type: {0}'.format(in_input.__class__)) |
| 75 | + print('\t\tbKeepWorldTransform: {0}'.format(in_input.keep_world_transform)) |
| 76 | + print('\t\tbImportAsReference: {0}'.format(in_input.import_as_reference)) |
| 77 | + if isinstance(in_input, unreal.HoudiniPublicAPICurveInput): |
| 78 | + print('\t\tbCookOnCurveChanged: {0}'.format(in_input.cook_on_curve_changed)) |
| 79 | + print('\t\tbAddRotAndScaleAttributesOnCurves: {0}'.format(in_input.add_rot_and_scale_attributes_on_curves)) |
| 80 | + |
| 81 | + input_objects = in_input.get_input_objects() |
| 82 | + if not input_objects: |
| 83 | + print('\t\tEmpty input!') |
| 84 | + else: |
| 85 | + print('\t\tNumber of objects in input: {0}'.format(len(input_objects))) |
| 86 | + for idx, input_object in enumerate(input_objects): |
| 87 | + print('\t\t\tInput object #{0}: {1}'.format(idx, input_object)) |
| 88 | + if hasattr(in_input, 'get_object_transform_offset'): |
| 89 | + print('\t\t\tObject Transform Offset: {0}'.format(in_input.get_object_transform_offset(input_object))) |
| 90 | + if isinstance(input_object, unreal.HoudiniPublicAPICurveInputObject): |
| 91 | + print('\t\t\tbClosed: {0}'.format(input_object.is_closed())) |
| 92 | + print('\t\t\tCurveMethod: {0}'.format(input_object.get_curve_method())) |
| 93 | + print('\t\t\tCurveType: {0}'.format(input_object.get_curve_type())) |
| 94 | + print('\t\t\tReversed: {0}'.format(input_object.is_reversed())) |
| 95 | + print('\t\t\tCurvePoints: {0}'.format(input_object.get_curve_points())) |
| 96 | + |
| 97 | + |
| 98 | +def print_inputs(in_wrapper): |
| 99 | + print('print_inputs') |
| 100 | + |
| 101 | + # Unbind from the delegate |
| 102 | + in_wrapper.on_post_processing_delegate.remove_callable(print_inputs) |
| 103 | + |
| 104 | + # Fetch inputs, iterate over it and log |
| 105 | + node_inputs = in_wrapper.get_inputs_at_indices() |
| 106 | + parm_inputs = in_wrapper.get_input_parameters() |
| 107 | + |
| 108 | + if not node_inputs: |
| 109 | + print('No node inputs found!') |
| 110 | + else: |
| 111 | + print('Number of node inputs: {0}'.format(len(node_inputs))) |
| 112 | + for input_index, input_wrapper in node_inputs.items(): |
| 113 | + print('\tInput index: {0}'.format(input_index)) |
| 114 | + print_api_input(input_wrapper) |
| 115 | + |
| 116 | + if not parm_inputs: |
| 117 | + print('No parameter inputs found!') |
| 118 | + else: |
| 119 | + print('Number of parameter inputs: {0}'.format(len(parm_inputs))) |
| 120 | + for parm_name, input_wrapper in parm_inputs.items(): |
| 121 | + print('\tInput parameter name: {0}'.format(parm_name)) |
| 122 | + print_api_input(input_wrapper) |
| 123 | + |
| 124 | + |
| 125 | +def run(): |
| 126 | + # get the API singleton |
| 127 | + api = unreal.HoudiniPublicAPIBlueprintLib.get_api() |
| 128 | + |
| 129 | + global _g_wrapper1, _g_wrapper2 |
| 130 | + # instantiate the input HDA with auto-cook enabled |
| 131 | + _g_wrapper1 = api.instantiate_asset(get_pig_head_hda(), unreal.Transform()) |
| 132 | + |
| 133 | + # instantiate the copy curve HDA |
| 134 | + _g_wrapper2 = api.instantiate_asset(get_copy_curve_hda(), unreal.Transform()) |
| 135 | + |
| 136 | + # Configure inputs on_post_instantiation, after instantiation, but before first cook |
| 137 | + _g_wrapper2.on_post_instantiation_delegate.add_callable(configure_inputs) |
| 138 | + # Print the input state after the cook and output creation. |
| 139 | + _g_wrapper2.on_post_processing_delegate.add_callable(print_inputs) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + run() |
0 commit comments