Author Topic: Questions and requests about the Python API  (Read 16269 times)

Hi,

Is there a way to open a graph or function in the UI using the plugin Python API? I cannot find this functionality in the API currently.

I would need to access the comment (SDGraphObjectComment) associated to a graph node (SDNode), I have been kooking at the code of SDGraphObjectComment.sNewAsChild and it seems this is using internal bindings, I am not sure we are able to get access to the comment from an SDNode. Is this possible and if not, can this be added?

It doesn't seem the Python API is currently handling the loading/saving of presets (not for MDL but regular graphs and sbsar), can this be added?

Thanks for your help!

I'm not entirely sure what you're looking for here in terms of functionality, but here's some answers based on my interpretation of your questions:

Quote
Is there a way to open a graph or function in the UI using the plugin Python API? I cannot find this functionality in the API currently.
If you're referring to opening a package similar to File > Open, you can use the following...
Code: [Select]
sdpackagemgr.loadUserPackage()

Quote
...I am not sure we are able to get access to the comment from an SDNode
A comment is a GUI element belonging to the graph, not the node itself.
If you're trying to get the comments belonging to a graph instance, then you can try the following...
Code: [Select]
from sd.api.sdgraphobjectcomment import SDGraphObjectComment

pkg = aContext.getSDApplication().getPackageMgr().getUserPackages()[0]
graph = pkg.getChildrenResources(False)[0]
       
objs = graph.getGraphObjects()
for obj in objs:
    if isinstance(obj, SDGraphObjectComment):
        print(obj.getDescription())

Quote
It doesn't seem the Python API is currently handling the loading/saving of presets (not for MDL but regular graphs and sbsar), can this be added?
This does appear to be missing and would be nice to have.

Thanks for your answers!

Regarding "open a graph or function in the UI", there is apparently no such functionality currently. I meant to open a container in the UI in the same way you would do when double-clicking on a function in the Explorer view or a graph or using CTRL+E to enter a node.

Regarding comments, they can actually be associated to nodes, you can see this by creating a node, right-clicking on it to assign it a comment, then when you move the node in the view, the comment also moves with it. So there is a logical link between the two, however this link doesn't seem to be accessible in the API currently.

Last Edit: October 15, 2019, 05:08:14 pm

Quote
Regarding "open a graph or function in the UI", there is apparently no such functionality currently. I meant to open a container in the UI in the same way you would do when double-clicking on a function in the Explorer view or a graph or using CTRL+E to enter a node.
You're right, there doesn't appear to be a way to open an instance in the view.

This seems more of a 2 step GUI related operation: open reference first (sdpackagemgr.loadUserPackage()), then load reference in graph view (possible GUI op?).
I don't have access to the latest API currently, but have you looked into the new QT GUI related API features introduced in > 2019.1.0?

Quote
Regarding comments, they can actually be associated to nodes, you can see this by creating a node, right-clicking on it to assign it a comment, then when you move the node in the view, the comment also moves with it. So there is a logical link between the two, however this link doesn't seem to be accessible in the API currently.
Ah, I wasn't aware of this but I did look into it and it is possible, just not from the node object.
Although the comment created in this manner still belongs to the active graph, it adds an additional attribute creating a dependency to the node.
The dependency appears to be strictly one way* (comment --> node), so my guess is that it would be impossible to get "comment" via sdnode.

However, you can still use the example I provided above and perform a check to see if a certain node is a parent to the comment, with a small addition:
Code: [Select]
from sd.api.sdgraphobjectcomment import SDGraphObjectComment

pkg = aContext.getSDApplication().getPackageMgr().getUserPackages()[0]
graph = pkg.getChildrenResources(False)[0]
       
objs = graph.getGraphObjects()
for obj in objs:
    if isinstance(obj, SDGraphObjectComment):
        if obj.getParent():
            parent_node = obj.getParent()
            print(parent_node.getDefinition().getLabel())

Now if you're looking to create a comment and parent it to a node, use SdGraphObjectComment.sNewAsChild().



* If you're wondering how I know this: The sbs file is pretty much an xml file; you can open it up in an editor and try to cross reference the compNode and GUIObjects.

A parented comment creates a GUIDependency attribute with a pointer to the node's UUID.
However, if you try to cross-reference the GUIObject UUID for the comment, with the compNode it's parented to, the node stores no such data.
Therefore, it's a one way relationship stored on the GUIObject itself.
Last Edit: October 15, 2019, 06:26:49 pm

I don't have access to the latest API currently, but have you looked into the new QT GUI related API features introduced in > 2019.1.0?

Yes I have been looking into the new Qt features but there doesn't seem to be an method to open a container in the UI currently.

Quote
Ah, I wasn't aware of this but I did look into it and it is possible, just not from the node object.
Although the comment created in this manner still belongs to the active graph, it adds an additional attribute creating a dependency to the node.

Ok! So indeed there seems to be a way to indirectly get the comment from a node, however this will be quite inefficient in terms of parsing. Thanks a lot, I will explore this option maybe using some caching mechanism.

Quote

* If you're wondering how I know this: The sbs file is pretty much an xml file; you can open it up in an editor and try to cross reference the compNode and GUIObjects.


Indeed, this is good to know, thanks!