Author Topic: Trying to understand this  (Read 14473 times)

Why am i getting an error when i do this, why do i need a positional argument and where? :

Code: [Select]
NodeSpawn = SDGraph.newNode(sdDefinitionId = "NormalMaker")
Error:
[ERR][5]Traceback (most recent call last):
[ERR][6]  File "<string>", line 13, in NewBuild
[ERR][7]TypeError
[ERR][8]:
[ERR][9]newNode() missing 1 required positional argument: 'self'

-----
Function:
newNode(sdDefinitionId)
Create a new node

Parameters
sdDefinitionId (string) – The identifier of the SDDefinition of the node to create

Return type
SDNode

How are SDGraph and NormalMaker defined? Can you post the relevant code preceding this?


SDGraph is treated as an instance class; the only reason it could be warning you of a positional argument is if the constuctor wasn't called and the class never initialized.

Hard to tell without looking at the rest of the code; however, it won't work if you're attempting the following:
Code: [Select]
from sd.api.sdgraph import SDGraph
node = SDGraph.newNode(sdDefinitionId='normal')

Here's a quick snippet of the proper way of doing this:
Code: [Select]
pkg_mgr = aContext.getSDApplication().getPackageMgr()
pkg = pkg_mgr.getUserPackages()[0]
graph = pkg.getChildrenResources(False)[0]

node = graph.newNode(sdDefinitionId='sbs::compositing::normal’)  # Docs: ../resources/documentation/pythonapi/html/pythonapi/modules/sbs_compositing.html



You can also check out the .../resources/python/samples directory under your Designer installation.

Code: [Select]
import sd
from PySide2 import QtWidgets
from sd.api.sdgraph import SDGraph


# Get the application and the UI Manager.
app = sd.getContext().getSDApplication()
uiMgr = app.getQtForPythonUIMgr()

# Function that will be called when our menu item is selected.
def NewBuild():
print("Hello")


NodeSpawn = SDGraph.newNode(sdDefinitionId = "NormalBuild")
pass

#Create a Menu toolbar
find = uiMgr.findMenuFromObjectName(objectName = "Tool_Normal_Build")

print(find)

if find == None:
   menu = uiMgr.newMenu(menuTitle = "Normal Tool", objectName = "Tool_Normal_Build")
   act = QtWidgets.QAction("New",menu)
   act.triggered.connect(NewBuild)
   menu.addAction(act)

else:
uiMgr.deleteMenu(objectName = "Tool_Normal_Build")

i just want to add a node when i click on the action tab

How are SDGraph and NormalMaker defined? Can you post the relevant code preceding this?


SDGraph is treated as an instance class; the only reason it could be warning you of a positional argument is if the constuctor wasn't called and the class never initialized.

Hard to tell without looking at the rest of the code; however, it won't work if you're attempting the following:
Code: [Select]
from sd.api.sdgraph import SDGraph
node = SDGraph.newNode(sdDefinitionId='normal')

Here's a quick snippet of the proper way of doing this:
Code: [Select]
pkg_mgr = aContext.getSDApplication().getPackageMgr()
pkg = pkg_mgr.getUserPackages()[0]
graph = pkg.getChildrenResources(False)[0]

node = graph.newNode(sdDefinitionId='sbs::compositing::normal’)  # Docs: ../resources/documentation/pythonapi/html/pythonapi/modules/sbs_compositing.html



You can also check out the .../resources/python/samples directory under your Designer installation.

yea i jant to add a node when i click on the action tab it makes under the newMenu.

You need to do something along the lines of this:
Code: [Select]
def NewBuild(graph):
    NodeSpawn = graph.newNode(sdDefinitionId = 'sbs::compositing::normal')


if find == None:
    pkg = app.getPackageMgr().getUserPackages()[0]
    graph = pkg.getChildrenResources(False)[0]

    menu = uiMgr.newMenu(menuTitle = "Normal Tool", objectName = "Tool_Normal_Build")
    act = QtWidgets.QAction("New",menu)
    act.triggered.connect(lambda: NewBuild(graph))
    menu.addAction(act)

You need to do something along the lines of this:
Code: [Select]
def NewBuild(graph):
    NodeSpawn = graph.newNode(sdDefinitionId = 'sbs::compositing::normal')


if find == None:
    pkg = app.getPackageMgr().getUserPackages()[0]
    graph = pkg.getChildrenResources(False)[0]

    menu = uiMgr.newMenu(menuTitle = "Normal Tool", objectName = "Tool_Normal_Build")
    act = QtWidgets.QAction("New",menu)
    act.triggered.connect(lambda: NewBuild(graph))
    menu.addAction(act)

it worked but where you find what lambda is and sbs::compositing::normal. Documentations dont really help on how SD method are read

Lambda is the pythonic way of calling anonymous functions.
Refer back to my original post for sbs::compositing::normal. It includes a link to the docs which defines the sdDefinitionID for comp nodes.