|
| 1 | +function Set-OBS3DFilter |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Sets an OBS 3D Filter. |
| 6 | + .DESCRIPTION |
| 7 | + Adds or Changes a 3D Filter on an OBS Input. |
| 8 | +
|
| 9 | + This requires the [3D Effect](https://github.com/exeldro/obs-3d-effect). |
| 10 | + #> |
| 11 | + [inherit(Command={ |
| 12 | + Import-Module ..\..\obs-powershell.psd1 -Global |
| 13 | + "Add-OBSSourceFilter" |
| 14 | + }, Dynamic, Abstract, ExcludeParameter='FilterKind','FilterSettings')] |
| 15 | + [Alias('Add-OBS3DFilter')] |
| 16 | + param( |
| 17 | + # The Field of View |
| 18 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 19 | + [ComponentModel.DefaultBindingProperty("fov")] |
| 20 | + [double] |
| 21 | + $FieldOfView, |
| 22 | + |
| 23 | + # The Rotation along the X-axis |
| 24 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 25 | + [ComponentModel.DefaultBindingProperty("rot_x")] |
| 26 | + [double] |
| 27 | + $RotationX, |
| 28 | + |
| 29 | + # The Rotation along the Y-axis |
| 30 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 31 | + [ComponentModel.DefaultBindingProperty("rot_y")] |
| 32 | + [double] |
| 33 | + $RotationY, |
| 34 | + |
| 35 | + # The Rotation along the Z-axis |
| 36 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 37 | + [ComponentModel.DefaultBindingProperty("rot_z")] |
| 38 | + [double] |
| 39 | + $RotationZ, |
| 40 | + |
| 41 | + # The Position along the X-axis |
| 42 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 43 | + [ComponentModel.DefaultBindingProperty("pos_x")] |
| 44 | + [double] |
| 45 | + $PositionX, |
| 46 | + |
| 47 | + # The Position along the Y-axis |
| 48 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 49 | + [ComponentModel.DefaultBindingProperty("pos_y")] |
| 50 | + [double] |
| 51 | + $PositionY, |
| 52 | + |
| 53 | + # The Position along the Z-axis |
| 54 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 55 | + [ComponentModel.DefaultBindingProperty("pos_z")] |
| 56 | + [double] |
| 57 | + $PositionZ, |
| 58 | + |
| 59 | + # The scale of the source along the X-axis |
| 60 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 61 | + [ComponentModel.DefaultBindingProperty("scale_x")] |
| 62 | + [double] |
| 63 | + $ScaleX, |
| 64 | + |
| 65 | + # The scale of the source along the Y-axis |
| 66 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 67 | + [ComponentModel.DefaultBindingProperty("scale_y")] |
| 68 | + [double] |
| 69 | + $ScaleY, |
| 70 | + |
| 71 | + # If set, will remove a filter if one already exists. |
| 72 | + # If this is not provided and the filter already exists, the settings of the filter will be changed. |
| 73 | + [switch] |
| 74 | + $Force |
| 75 | + ) |
| 76 | + |
| 77 | + process { |
| 78 | + $myParameters = [Ordered]@{} + $PSBoundParameters |
| 79 | + |
| 80 | + if (-not $myParameters["FilterName"]) { |
| 81 | + $filterName = $myParameters["FilterName"] = "3Band3D" |
| 82 | + } |
| 83 | + |
| 84 | + $myParameterData = [Ordered]@{} |
| 85 | + foreach ($parameter in $MyInvocation.MyCommand.Parameters.Values) { |
| 86 | + |
| 87 | + $bindToPropertyName = $null |
| 88 | + |
| 89 | + foreach ($attribute in $parameter.Attributes) { |
| 90 | + if ($attribute -is [ComponentModel.DefaultBindingPropertyAttribute]) { |
| 91 | + $bindToPropertyName = $attribute.Name |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (-not $bindToPropertyName) { continue } |
| 97 | + if ($myParameters.Contains($parameter.Name)) { |
| 98 | + $myParameterData[$bindToPropertyName] = $myParameters[$parameter.Name] |
| 99 | + if ($myParameters[$parameter.Name] -is [switch]) { |
| 100 | + $myParameterData[$bindToPropertyName] = $parameter.Name -as [bool] |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + $addSplat = @{ |
| 106 | + filterName = $myParameters["FilterName"] |
| 107 | + SourceName = $myParameters["SourceName"] |
| 108 | + filterKind = "3d_effect_filter" |
| 109 | + filterSettings = $myParameterData |
| 110 | + NoResponse = $myParameters["NoResponse"] |
| 111 | + } |
| 112 | + |
| 113 | + if ($MyParameters["PassThru"]) { |
| 114 | + $addSplat.Passthru = $MyParameters["PassThru"] |
| 115 | + if ($MyInvocation.InvocationName -like 'Add-*') { |
| 116 | + Add-OBSSourceFilter @addSplat |
| 117 | + } else { |
| 118 | + $addSplat.Remove('FilterKind') |
| 119 | + Set-OBSSourceFilterSettings @addSplat |
| 120 | + } |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + # Add the input. |
| 125 | + $outputAddedResult = Add-OBSSourceFilter @addSplat *>&1 |
| 126 | + |
| 127 | + |
| 128 | + # If we got back an error |
| 129 | + if ($outputAddedResult -is [Management.Automation.ErrorRecord]) { |
| 130 | + # and that error was saying the source already exists, |
| 131 | + if ($outputAddedResult.TargetObject.d.requestStatus.code -eq 601) { |
| 132 | + # then check if we use the -Force. |
| 133 | + if ($Force) { # If we do, remove the input |
| 134 | + Remove-OBSSourceFilter -FilterName $addSplat.FilterName -SourceName $addSplat.SourceName |
| 135 | + # and re-add our result. |
| 136 | + $outputAddedResult = Add-OBSInput @addSplat *>&1 |
| 137 | + } else { |
| 138 | + # Otherwise, get the existing filter. |
| 139 | + $existingFilter = Get-OBSSourceFilter -SourceName $addSplat.SourceName -FilterName $addSplat.FilterName |
| 140 | + # then apply the settings |
| 141 | + $existingFilter.Set($addSplat.filterSettings) |
| 142 | + # and output them |
| 143 | + $existingFilter |
| 144 | + # (don't forget to null the result, so we don't show this error) |
| 145 | + $outputAddedResult = $null |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + # If the output was still an error |
| 150 | + if ($outputAddedResult -is [Management.Automation.ErrorRecord]) { |
| 151 | + # use $psCmdlet.WriteError so that it shows the error correctly. |
| 152 | + $psCmdlet.WriteError($outputAddedResult) |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + # Otherwise, if we had a result |
| 157 | + elseif ($outputAddedResult) { |
| 158 | + # Otherwise, get the input from the filters. |
| 159 | + Get-OBSSourceFilter -SourceName $addSplat.SourceName -FilterName $addSplat.FilterName |
| 160 | + |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments