top of page

PFTrack Support Community

Public·44 Users

Macro Scripts: PySide2 Example Script

What does it do?

This is an example PySide2 script. It allows you to select any node in your project from the popup window.


Note: This script will only work with PFTrack Enterprise 24.08.22 and onwards


Download


Script

import pfpym


import PySide2

from PySide2 import QtWidgets, QtCore

from PySide2.QtWidgets import QApplication, QDialog, QPushButton, QGridLayout, QVBoxLayout, QLabel

from PySide2.QtCore import QSignalMapper


def pfMacroName():

return 'PySide2 Dialog'



class Form(QDialog):


def __init__(self, tree, parent = None):

super(Form, self).__init__(parent)

self.setWindowTitle("PySide2 Example")


self.tree = tree


vl = QVBoxLayout()

vl.addWidget(QLabel("Jump to a node by clicking on a button"))

gl = QGridLayout()

vl.addLayout(gl)


mapper = QSignalMapper(self)

mapper.mapped[str].connect(self.button_clicked)


# create a button for each node in the tree

row = 0

col = 0

for i in range(self.tree.getNumNodes()):


name = tree.getNode(i).getName()


# create the button and store its name with the signal mapper

b = QPushButton(name)

b.clicked.connect(mapper.map)

mapper.setMapping(b, name)


# add to the grid layout

gl.addWidget(b, row, col)


col += 1

if col == 4 :

col = 0

row += 1


if row == 0 and col == 0 :

l = QLabel("Error: No nodes in the tree. Create some nodes and try again")

gl.addWidget(l, 1, 0)

self.setLayout(vl)


@QtCore.Slot(str)

def button_clicked(self, name):

# find the node in the tree

for i in range(self.tree.getNumNodes()):


if self.tree.getNode(i).getName() == name :


print("Activating node " + name)

n = self.tree.getNode(i)


# fetch the node position

pos = n.getPosition()

# move to the centre of the viewport

self.tree.setPosition(pos[0], pos[1])


# select the node and activate the editor

self.tree.activateEditor(n)


# uncomment to close the dialog as soon as the editor is activated

#self.accept();


break;



def main(tree):


# QApplication is already built, so just exec the dialog and

# wait for it to run inside the application's event loop

form = Form(tree)

form.exec()


# cleanp

form.deleteLater()

67 Views
bottom of page