Author Topic: [python api] how to edit pixel processor?  (Read 1274 times)

I can't find information in the documentation. Could anyone help me

Here's a quick snippet that explains the process of adding a new node to an existing pixel processor.

Code: [Select]
import sd
import sdplugins

from sd import logger
from sd.api import sdproperty, sdbasetypes
from sd.api.sdvaluestring  import SDValueString

sd_log = logger.Logger()


class pixelProcessorTest(sdplugins.Plugin):
    sPluginDesc = sdplugins.PluginDesc(
        aLabel = 'pixel_processor_test',
        aTooltip = 'Add string function to all pixel processor nodes in graph.',
        aPluginLocation = sdplugins.PluginLocationMenu(sdplugins.MenuId.Scripts, '', 0),
        aIconFileAbsPath = '')

    def run(self, aContext) :
        """ Add a 'Hello' string function node to all pixel processor nodes in first graph. """
        pkg = aContext.getSDApplication().getPackageMgr().getUserPackages()[0]  # Target first package only.
        graph = pkg.getChildrenResources(False)[0]  # Target first graph only.
       
        # Query all nodes for the pixelprocessor node definition.
        # Docs: Substance Designer Python API 2018.3.1 documentation » Modules Definitions » sbs::compositing.
        #       ('sbs::compositing::pixelprocessor', 'perpixel')
        pp_nodes = []
        for node in graph.getNodes():
            if node.getDefinition().getId() == 'sbs::compositing::pixelprocessor':
                pp_nodes.append(node)
       
        # Iterate through all pixel processors and add string function.
        # Docs: Substance Designer Python API 2018.3.1 documentation » Modules Definitions
        if pp_nodes:
            for node in pp_nodes:
                pp_func_prp = node.getPropertyFromId('perpixel', sdproperty.SDPropertyCategory.Input)
                pp_graph = node.getPropertyGraph(pp_func_prp)  # Returns SDSBSFunctionGraph.
               
                # If pixel processor function graph doesn't exist, create it.
                if not pp_graph:
                    pp_graph = node.newPropertyGraph(pp_func_prp, 'SDSBSFunctionGraph')
               
                # Create and edit node.
                # Docs: Substance Designer Python API 2018.3.1 documentation » Modules Definitions » sbs::function.
                #       ('sbs::function::const_string')
                fnc_str_node = pp_graph.newNode('sbs::function::const_string')  # Returns SDSBSFunctionNode.

                fnc_str_node.setInputPropertyValueFromId('__constant__', SDValueString.sNew('Hello'))
                fnc_str_node.setPosition(sdbasetypes.float2(200, 0))

        else:
            sd_log.log('No pixel processors found in graph. Do nothing.', logger.LogLevel.Warning)
           
# Register module classes
sd.getContext().getSDApplication().registerModule(__name__)
Last Edit: April 26, 2019, 08:24:42 pm

Here's a quick snippet that explains the process of adding a new node to an existing pixel processor.

Code: [Select]
import sd
import sdplugins

from sd import logger
from sd.api import sdproperty, sdbasetypes
from sd.api.sdvaluestring  import SDValueString

sd_log = logger.Logger()


class pixelProcessorTest(sdplugins.Plugin):
    sPluginDesc = sdplugins.PluginDesc(
        aLabel = 'pixel_processor_test',
        aTooltip = 'Add string function to all pixel processor nodes in graph.',
        aPluginLocation = sdplugins.PluginLocationMenu(sdplugins.MenuId.Scripts, '', 0),
        aIconFileAbsPath = '')

    def run(self, aContext) :
        """ Add a 'Hello' string function node to all pixel processor nodes in first graph. """
        pkg = aContext.getSDApplication().getPackageMgr().getUserPackages()[0]  # Target first package only.
        graph = pkg.getChildrenResources(False)[0]  # Target first graph only.
       
        # Query all nodes for the pixelprocessor node definition.
        # Docs: Substance Designer Python API 2018.3.1 documentation » Modules Definitions » sbs::compositing.
        #       ('sbs::compositing::pixelprocessor', 'perpixel')
        pp_nodes = []
        for node in graph.getNodes():
            if node.getDefinition().getId() == 'sbs::compositing::pixelprocessor':
                pp_nodes.append(node)
       
        # Iterate through all pixel processors and add string function.
        # Docs: Substance Designer Python API 2018.3.1 documentation » Modules Definitions
        if pp_nodes:
            for node in pp_nodes:
                pp_func_prp = node.getPropertyFromId('perpixel', sdproperty.SDPropertyCategory.Input)
                pp_graph = node.getPropertyGraph(pp_func_prp)  # Returns SDSBSFunctionGraph.
               
                # If pixel processor function graph doesn't exist, create it.
                if not pp_graph:
                    pp_graph = node.newPropertyGraph(pp_func_prp, 'SDSBSFunctionGraph')
               
                # Create and edit node.
                # Docs: Substance Designer Python API 2018.3.1 documentation » Modules Definitions » sbs::function.
                #       ('sbs::function::const_string')
                fnc_str_node = pp_graph.newNode('sbs::function::const_string')  # Returns SDSBSFunctionNode.

                fnc_str_node.setInputPropertyValueFromId('__constant__', SDValueString.sNew('Hello'))
                fnc_str_node.setPosition(sdbasetypes.float2(200, 0))

        else:
            sd_log.log('No pixel processors found in graph. Do nothing.', logger.LogLevel.Warning)
           
# Register module classes
sd.getContext().getSDApplication().registerModule(__name__)


thanks ,that really helps.   BTW, Why not put this info into documentation?