Heres what I was putzing around with today. Wanted to make a more robust
context-aware node creation function. I'm no python expert so suggestions
and criticisms are welcome. I would like to find a better detection method
for 3d nodes, since the 'render_mode' knob is not in all 3d nodes, like
Lights.


## context aware node function
def contextAwareCreateNode(node2d=None, node3d=None, nodeDeep=None):
    nodes = nuke.selectedNodes()
    try:
        if nodeDeep and selectedNodesDeep(nodes):
            return nuke.createNode(nodeDeep)
        elif node3d and selectedNodes3d(nodes):
            return nuke.createNode(node3d)
        else:
            raise ValueError
    except:
        return nuke.createNode(node2d)

## Returns true if all selected nodes are Deep nodes
def selectedNodesDeep(nodes):
    if not nodes:
        nodes = nuke.selectedNodes()
    for n in nodes:
        try:
            n.deepSampleCount(0,0)
            ret = True
        except(ValueError):
             ret = False
             break
    return ret

## Returns true if all selected nodes are 3D nodes
def selectedNodes3d(nodes):
    if not nodes:
        nodes = nuke.selectedNodes()
    for n in nodes:
        try:
            ## Need to find better detection method for 3D nodes, this
method doesnt include Lights
            n['render_mode'].getValue()
            ret = True
        except(ValueError):
             ret = False
             break
    return ret

nuke.menu('Nodes').addCommand('Transform/Transform',
'contextAwareCreateNode(node2d=\'Transform\', node3d=\'TransformGeo\',
nodeDeep=\'DeepTransform\')', 't', icon="2D.png")
nuke.menu('Nodes').addCommand('Merge/Merge','contextAwareCreateNode(node2d=\'Merge\',
node3d=\'Scene\', nodeDeep=\'DeepMerge\')', 'm', icon="Merge.png")
nuke.menu('Nodes').addCommand('Color/ColorCorrect',
'contextAwareCreateNode(node2d=\'ColorCorrect\',
nodeDeep=\'DeepColorCorrect\')', 'c', icon="ColorCorrect.png")
nuke.menu('Nodes').addCommand('Image/Write',
'contextAwareCreateNode(node2d=\'Write\', nodeDeep=\'DeepWrite\')', 'w',
icon="Write.png")






On Fri, Jun 13, 2014 at 3:00 PM, Anthony Kramer <anthony.kra...@gmail.com>
wrote:

> Good point...
>
> For my particular purposes, I do want to know if the output of the
> selected node has deep data, which DeepToImage and DeepToPoints do not. The
> deepSampleCount method actually returns False for those nodes as well.
>
>
> On Fri, Jun 13, 2014 at 2:54 PM, Nathan Rusch <nathan_ru...@hotmail.com>
> wrote:
>
>>   Be careful with with the deepSampleCount approach, as it will require
>> computation of image data if it isn’t already available.
>>
>> The canSetInput test isn’t a bad idea and would work most of the time,
>> but what about DeepToImage or DeepToPoints?
>>
>> -Nathan
>>
>>
>>  *From:* Anthony Kramer <anthony.kra...@gmail.com>
>> *Sent:* Friday, June 13, 2014 2:47 PM
>> *To:* Nuke Python discussion <nuke-python@support.thefoundry.co.uk>
>> *Subject:* Re: [Nuke-python] Identify deep node with python
>>
>>  Nice, that seems to work, thanks!
>>
>>
>> On Fri, Jun 13, 2014 at 2:42 PM, Jose Fernandez de Castro <
>> pixelcowbo...@gmail.com> wrote:
>>
>>> Another hacky way (not thoroughly tested though) could be to do the
>>> following:
>>> try:
>>>     nuke.selectedNode().deepSampleCount(0,0)
>>>     print("Deep")
>>> except(ValueError):
>>>      print("Not deep")
>>>
>>> On Fri, Jun 13, 2014 at 2:26 PM, Anthony Kramer
>>> <anthony.kra...@gmail.com> wrote:
>>> > Its hacky but heres what I came up with. It tests whether all the
>>> node(s)
>>> > you have selected can connect to a DeepTransform node, which presumably
>>> > means they are deep nodes:
>>> >
>>> >
>>> > nodes = nuke.selectedNodes()
>>> >
>>> > nukescripts.clear_selection_recursive()
>>> >
>>> > test = nuke.createNode('DeepTransform')
>>> >
>>> > for n in nodes:
>>> >
>>> >      if not test.canSetInput(0, n):
>>> >
>>> >      ret = False
>>> >
>>> >      break
>>> >
>>> > else:
>>> >
>>> >      ret = True
>>> >
>>> > nuke.delete(test)
>>> >
>>> > print ret
>>> >
>>> >
>>> >
>>> > If anyones got a better idea, I'd love to hear it.
>>> >
>>> >
>>> >
>>> >
>>> > On Fri, Jun 13, 2014 at 12:24 PM, Nathan Rusch <
>>> nathan_ru...@hotmail.com>
>>> > wrote:
>>> >>
>>> >> Off the top of my head, I don’t think there’s anything incriminating
>>> about
>>> >> deep nodes other than the fact that their class names start with
>>> "Deep" (for
>>> >> now, at least).
>>> >>
>>> >> -Nathan
>>> >>
>>> >>
>>> >> From: Anthony Kramer
>>> >> Sent: Friday, June 13, 2014 11:36 AM
>>> >> To: Nuke Python discussion
>>> >> Subject: [Nuke-python] Identify deep node with python
>>> >>
>>> >> Looking for a way with python to identify if the selected node(s) as
>>> deep
>>> >> nodes.
>>> >>
>>> >> -ak
>>> >>
>>> >> ________________________________
>>> >> _______________________________________________
>>> >> Nuke-python mailing list
>>> >> Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
>>> >> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>> >>
>>> >>
>>> >> _______________________________________________
>>> >> Nuke-python mailing list
>>> >> Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
>>> >> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>> >>
>>> >
>>> >
>>> > _______________________________________________
>>> > Nuke-python mailing list
>>> > Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
>>> > http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>> >
>>>
>>>
>>>
>>> --
>>> Jose Fernandez de Castro
>>>  _______________________________________________
>>> Nuke-python mailing list
>>> Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
>>> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>>
>>
>>
>> ------------------------------
>> _______________________________________________
>> Nuke-python mailing list
>> Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
>> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>
>>
>> _______________________________________________
>> Nuke-python mailing list
>> Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
>> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>
>>
>
_______________________________________________
Nuke-python mailing list
Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to