async def calc_batch(): global currentXPosition global currentYPosition global xTransformVelocity global yTransformVelocity # Generate 180 frame requests (Number can be changed. Smaller is actually generally better.) ret = [] for i in range(180): # Apply velocity to the position for the current frame. currentXPosition += xTransformVelocity currentYPosition += yTransformVelocity # If edge is reached, generate a new velocity and reverse direction. newVelocity = random.randrange(velocityMin * 100, velocityMax * 100) / 100 if is_positive(xTransformVelocity) and currentXPosition >= (obsXResolution - sceneItemWidth): xTransformVelocity = -newVelocity elif not is_positive(xTransformVelocity) and currentXPosition <= 0: xTransformVelocity = newVelocity if is_positive(yTransformVelocity) and currentYPosition >= (obsYResolution - sceneItemHeight): yTransformVelocity = -newVelocity elif not is_positive(yTransformVelocity) and currentYPosition <= 0: yTransformVelocity = newVelocity # Generate the requests for the current frame sceneItemTransform = {} sceneItemTransform['positionX'] = currentXPosition sceneItemTransform['positionY'] = currentYPosition ret.append(simpleobsws.Request('SetSceneItemTransform', {'sceneName': dvdStageSceneName, 'sceneItemId': sceneItemId, 'sceneItemTransform': sceneItemTransform})) ret.append(simpleobsws.Request('Sleep', {'sleepFrames': 1})) return ret
async def request_callback(request, emit): if not ws or not ws.is_identified(): return fail_response('obs-websocket is not connected.') authOk, comment = validate_request(request) if not authOk: return fail_response(comment) requestType = request.match_info.get('requestType') if not requestType: return fail_response('Your path is missing a request type.') requestData = await get_json(request) req = simpleobsws.Request(requestType, requestData) logging.info('Performing request for request type `{}` | Emit: {} | Client IP: {}'.format(requestType, emit, request.remote)) logging.debug('Request data:\n{}'.format(requestData)) if emit: await ws.emit(req) return web.json_response({'result': True}) try: ret = await ws.call(req) except simpleobsws.MessageTimeout: return fail_response('The obs-websocket request timed out.') responseData = {'result': True, 'requestResult': response_to_object(ret)} return web.json_response(responseData)
async def make_request(): await ws.connect() # Make the connection to obs-websocket await ws.wait_until_identified( ) # Wait for the identification handshake to complete request = simpleobsws.Request('GetVersion') # Build a Request object ret = await ws.call(request) # Perform the request if ret.ok(): # Check if the request succeeded print("Request succeeded! Response data: {}".format(ret.responseData)) await ws.disconnect() # Disconnect from the websocket server cleanly
async def make_request(): await ws.connect() # Make the connection to obs-websocket await ws.wait_until_identified( ) # Wait for the identification handshake to complete requests = [] requests.append(simpleobsws.Request( 'GetVersion')) # Build a Request object, then append it to the batch requests.append(simpleobsws.Request( 'GetStats')) # Build another request object, and append it ret = await ws.call_batch(requests, halt_on_failure=False ) # Perform the request batch for result in ret: if ret.ok(): # Check if the request succeeded print("Request succeeded! Response data: {}".format( ret.responseData)) await ws.disconnect() # Disconnect from the websocket server cleanly
async def init(): await ws.connect() await ws.wait_until_identified() logging.info('Connected and identified.') global obsXResolution global obsYResolution req = simpleobsws.Request('GetVideoSettings') ret = await ws.call(req) if not ret.ok(): logging.error('Failed to fetch OBS info!') return False obsXResolution = ret.responseData['baseWidth'] obsYResolution = ret.responseData['baseHeight'] global sceneItemId req = simpleobsws.Request('GetSceneItemId', {'sceneName': dvdStageSceneName, 'sourceName': dvdStageSourceName}) ret = await ws.call(req) if not ret.ok(): logging.error('Failed to fetch scene item ID!') return False sceneItemId = ret.responseData['sceneItemId'] global sceneItemWidth global sceneItemHeight req = simpleobsws.Request('GetSceneItemTransform', {'sceneName': dvdStageSceneName, 'sceneItemId': sceneItemId}) ret = await ws.call(req) if not ret.ok(): logging.error('Failed to fetch scene item transform!') return False cropLeft = ret.responseData['sceneItemTransform']['cropLeft'] cropRight = ret.responseData['sceneItemTransform']['cropRight'] cropTop = ret.responseData['sceneItemTransform']['cropTop'] cropBottom = ret.responseData['sceneItemTransform']['cropBottom'] # Funky math. In order to get the true width and height of the scene item, we need to take the sum of the source dimensions and the crop values, *then* apply the scale value, as scale is applied last in OBS. sceneItemWidth = (ret.responseData['sceneItemTransform']['sourceWidth'] - cropLeft - cropRight) * ret.responseData['sceneItemTransform']['scaleX'] sceneItemHeight = (ret.responseData['sceneItemTransform']['sourceHeight'] - cropTop - cropBottom) * ret.responseData['sceneItemTransform']['scaleY'] return True
async def update_loop(): # First, initialize the scene item to the corner of the screen. req = simpleobsws.Request('SetSceneItemTransform', {'sceneName': dvdStageSceneName, 'sceneItemId': sceneItemId, 'sceneItemTransform': {'positionX': 0, 'positionY': 0}}) await ws.call(req) # Call a batch, wait for it to finish, then call a new one infinitely. while True: # Generate a new batch of per-frame requests. requests = await calc_batch() # Send it, wait for response responses = await ws.call_batch(requests, halt_on_failure = True, execution_type = simpleobsws.RequestBatchExecutionType.SerialFrame) # Stop sending new requests if they start failing. if len(requests) != len(responses): logging.warning('Received {} responses for {} requests!'.format(len(responses), len(requests))) break