""" Script DAT Callbacks me - this DAT scriptOp - the OP which is cooking """ # press 'Setup Parameters' in the OP to call this function to re-create the # parameters. def onSetupParameters(scriptOp): """ Called to setup custom parameters for the Script DAT. """ page = scriptOp.appendCustomPage('Custom') page.appendFloat('Valuea', label='Value A') page.appendFloat('Valueb', label='Value B') return def onPulse(par): """ Called when a custom pulse parameter is pushed. """ return def onCook(scriptOp): """ Called when the Script DAT needs to cook. """ scriptOp.clear() scriptOp.appendRow(['id', 'P(X)', 'P(Y)', 'P(Z)']) # Find the constant OP relative to this script const_op = op('constant1') camera_y_offset = 0 camera_y_far=0 if const_op: target_chan = const_op.chan('camera_distance_y') if target_chan is not None: camera_y_offset = target_chan.eval() camera_y_far = const_op.chan('camera_distance_y_far').eval() const_select=op('SelectId') select_id=-1 if const_select: select_chan = const_select.chan('id') if select_chan is not None: select_id = int(select_chan.eval()) # Check if there is an input connected if len(scriptOp.inputs) == 0: return inputDat = scriptOp.inputs[0] if select_id >= 0: # Find the row with the matching ID in the input table for i in range(1, inputDat.numRows): try: row_id = int(inputDat[i, 0]) if row_id == select_id: px_val = float(inputDat[i, 1] or 0) py_val = float(inputDat[i, 2] or 0) - camera_y_offset pz_val = float(inputDat[i, 3] or 0) scriptOp.appendRow([row_id, px_val, py_val, pz_val]) return except (ValueError, TypeError): continue return else: # append with average of rows 1 to 4 (index 1 to 4 inclusive if they exist) ax = 0.0 ay = 0.0 az = 0.0 rows_processed = 0 # Process rows 1 to 4 (index 1 to 4 inclusive if they exist) for i in range(1, min(5, inputDat.numRows)): try: # Safely convert cell values to floats # Assuming col 1=X, 2=Y, 3=Z based on your previous logic px_val = float(inputDat[i, 1] or 0) py_val = float(inputDat[i, 2] or 0) pz_val = float(inputDat[i, 3] or 0) ax += px_val ay += py_val az += pz_val rows_processed += 1 except (ValueError, TypeError): # Skip rows that don't contain valid numbers continue if rows_processed > 0: ax /= rows_processed ay /= rows_processed az /= rows_processed scriptOp.appendRow(['average', ax, ay - camera_y_far, az]) return def onGetCookLevel(scriptOp): return CookLevel.AUTOMATIC