Sorry, I had forgotten all about this.

 

What I had planned worked out pretty well.  Bear in mind this code is
not optimized in any way.  I tend to write looser, more self commenting
code.  Especially when I'm prototyping things, like I was here.

 

Anyway this is what I came up with.  Fire it from onKnobChanged and it
essentially determines if a new element was created in the list.  Do
what you want from there.

 

import nuke.rotopaint as rp

 

def getStrokes(layer):

    strokes = []

    for element in layer:

        if isinstance(element, nuke.rotopaint.Layer):

            strokes.extend(getStrokes(element))

        elif isinstance(element, nuke.rotopaint.Stroke):

            strokes.append(element)

    return strokes

 

def updateElements():

    knob = nuke.thisKnob()

    node = nuke.thisNode()

    if knob.name() == 'curves':

        strokes = getStrokes(knob.rootLayer)

        elements = ''

        lenOldElements = 0

        for stroke in strokes:

            elements = elements + stroke.name + ','

        elements = elements[:-1]

        oldElements  = node['elements'].value().split(',')

        if oldElements[0] == '':

            lenOldElements = 0

        else:

            lenOldElements = len(oldElements)

        print lenOldElements

        if lenOldElements >= len(strokes):

            print 'No new strokes'

            node['elements'].setValue(elements)

            return

        else:

            for stroke in strokes:

                if stroke.name not in oldElements:

                    print 'New Stroke: ' + stroke.name

 

 

From: [email protected]
[mailto:[email protected]] On Behalf Of John
Vanderbeck
Sent: Tuesday, June 28, 2011 11:00 AM
To: Nuke Python discussion
Subject: RE: [Nuke-python] Roto shape "onCreation" callback entry?

 

What you should be able to do, and what I'm actually in the middle of
doing, is essentially save a list of the elements in the RotoKnob, and
then when the knobChanged fires, look to see if the list of elements has
any new entries in it.

 

This is actually something I'm working on right now, so I'll let you
know if it works or not :)

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Ean
Carr
Sent: Tuesday, June 28, 2011 5:12 AM
To: Nuke Python discussion
Subject: Re: [Nuke-python] Roto shape "onCreation" callback entry?

 

Hi David,

Welcome to the list.

If you don't mind using the older Bezier node, you can get what you want
by setting the gl_color with an onUserCreate callback. Of course, you'll
have to use 1 Bezier for each shape and put up with the other
limitations in that node.

In a menu.py:

def randomBezierGLColorCB(minlum=.7, maxlum=1):
    tn = nuke.thisNode()
    r = random.random() * minlum + (maxlum - minlum)
    g = random.random() * minlum + (maxlum - minlum)
    b = random.random() * minlum + (maxlum - minlum)
    hexv = int('%02x%02x%02x%02x' % (r*255,g*255,b*255,255),16)
    tn['gl_color'].setValue(hexv)

nuke.addOnUserCreate(randomBezierGLColorCB, nodeClass='Bezier')

I suppose you could do the same thing with Roto/RotoPaint nodes, but it
seems wasteful to use these heavier nodes for only 1 shape each just to
get the different colors for each shape.

-Ean

On Tue, Jun 28, 2011 at 4:58 AM, Nathan Rusch <[email protected]>
wrote:

There's no callback for onShapeCreation (you can find a list of the
existing callbacks in the User Guide). The only callback that would
currently come anywhere close to handling the kind of behavior you're
after would be knobChanged, and it won't be able get you what you want.

You could get as far as grabbing the correct knob, but until you
actually finish the shape, its attribute object doesn't exist (or isn't
editable), so you run into a dependency problem. Also, you have no way
of knowing which shape was just created if you have any kind of layer
hierarchy in your RotoPaint node and aren't creating shapes at the top
layer. Finally, the knobChanged callback only lets you access very
general information about the knob (knob, node, group context, etc.), so
you would have no way of knowing whether the number of shapes in the
RotoPaint node had actually changed, or if someone was just editing
another shape (without saving its size to a custom knob every time a new
shape was added).

Hope this answers your question.

-Nathan

-----Original Message----- From: Melazoma
Sent: Monday, June 27, 2011 7:43 PM
To: [email protected]
Subject: [Nuke-python] Roto shape "onCreation" callback entry?



Hi all,

First time posting here.

I'm trying to insert a piece of code that assigns a random color to a
roto shape upon its creation, giving each shape a unique-ish color
automatically.
I'm still not that familiar with Nuke python, and I'm having a hard
time finding a callback entry for something like "onShapeCreation" in
the rotopaint module.
I'm also at a loss with where to register this callback (if it
exists). Should it be done in an init script (i.e. init.py or menu.py)
or somewhere in Nuke's UI?

Any pointers are greatly apprecited.

This is the code I'm trying to run at each roto shape creation:
----------
knob = nuke.toNode('Roto1')['curves']
shape = knob.toElement('Bezier1')
# perhaps grab the newly created shape object without referencing it
by name? Is that possible?
attrs = shape.getAttributes()

randR = nuke.expression('random()')
randG = nuke.expression('random()')
randB = nuke.expression('random()')
attrs.set('r', randR)
attrs.set('g', randG)
attrs.set('b', randB)

# refresh viewer and color knob
----------

Thanks in advance.
Best,
-David
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python 
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

 

_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to