<commit_before>"""Various utilities relating to the OCIOCDLTransform node
"""

import nuke
import nukescripts
import PyOpenColorIO as OCIO
import xml.etree.ElementTree as ET


def _node_to_cdltransform(node):
    """From an OCIOCDLTransform node, returns a PyOpenColorIO
    CDLTransform object, which could be used to write XML
    """

    # Color_Knob.value returns single float if control is not
    # expanded, so use value(index=...) to always get three values
    slope = [node['slope'].value(x) for x in range(3)]
    offset = [node['offset'].value(x) for x in range(3)]
    power = [node['power'].value(x) for x in range(3)]
    sat = node['saturation'].value()
    cccid = node['cccid'].value()

    cdl = OCIO.CDLTransform()
    cdl.setSlope(slope)
    cdl.setOffset(offset)
    cdl.setPower(power)
    cdl.setSat(sat)
    cdl.setID(cccid)

    return cdl


def _cdltransform_to_node(cdl, node):
    """From an XML string, populates the parameters on an
    OCIOCDLTransform node
    """

    # Treat "node" as a dictionary of knobs, as the "node" argument could be
    # a the return value of PythonPanel.knob(), as in SelectCCCIDPanel

    node['slope'].setValue(cdl.getSlope())
    node['offset'].setValue(cdl.getOffset())
    node['power'].setValue(cdl.getPower())
    node['saturation'].setValue(cdl.getSat())
    node['cccid'].setValue(cdl.getID())


def _xml_to_cdltransforms(xml):
    """Given some XML as a string, returns a list of CDLTransform
    objects for each ColorCorrection (returns a one-item list for a
    .cc file)
    """

    tree = ET.fromstring(xml)

    # Strip away xmlns
    for elem in tree.getiterator():
        if elem.tag.startswith("{"):
             elem.tag = elem.tag.partition("}")[2]

    filetype = tree.tag

    if filetype == "ColorCorrection":
        ccxml = ET.tostring(tree)
        cdl = OCIO.CDLTransform()
        cdl.setXML(ccxml)
        return [cdl]

    elif filetype == "ColorCorrectionCollection":
        allcdl = []
        for cc in tree.getchildren():
            ccxml = ET.tostring(cc)
            cdl = OCIO.CDLTransform()
            cdl.setXML(ccxml)
            allcdl.append(cdl)
        return allcdl

    else:
        raise RuntimeError(
            "The supplied file did not have the correct root element, expected"
            " 'ColorCorrection' or 'ColorCorrectionCollection', got %r" % (filetype))


def _cdltransforms_to_xml(allcc):
    """Given a list of CDLTransform objects, returns an XML string
    """

    root = ET.Element("ColorCorrectionCollection")
    root.attrib['xmlns'] = 'urn:ASC:CDL:v1.2'

    for cc in allcc:
        cur = ET.fromstring(cc.getXML())<commit_msg>nuke cdl import bugfix. ccc files may contain children other than ColorCorrection<commit_after>"""Various utilities relating to the OCIOCDLTransform node
"""

import nuke
import nukescripts
import PyOpenColorIO as OCIO
import xml.etree.ElementTree as ET


def _node_to_cdltransform(node):
    """From an OCIOCDLTransform node, returns a PyOpenColorIO
    CDLTransform object, which could be used to write XML
    """

    # Color_Knob.value returns single float if control is not
    # expanded, so use value(index=...) to always get three values
    slope = [node['slope'].value(x) for x in range(3)]
    offset = [node['offset'].value(x) for x in range(3)]
    power = [node['power'].value(x) for x in range(3)]
    sat = node['saturation'].value()
    cccid = node['cccid'].value()

    cdl = OCIO.CDLTransform()
    cdl.setSlope(slope)
    cdl.setOffset(offset)
    cdl.setPower(power)
    cdl.setSat(sat)
    cdl.setID(cccid)

    return cdl


def _cdltransform_to_node(cdl, node):
    """From an XML string, populates the parameters on an
    OCIOCDLTransform node
    """

    # Treat "node" as a dictionary of knobs, as the "node" argument could be
    # a the return value of PythonPanel.knob(), as in SelectCCCIDPanel

    node['slope'].setValue(cdl.getSlope())
    node['offset'].setValue(cdl.getOffset())
    node['power'].setValue(cdl.getPower())
    node['saturation'].setValue(cdl.getSat())
    node['cccid'].setValue(cdl.getID())


def _xml_to_cdltransforms(xml):
    """Given some XML as a string, returns a list of CDLTransform
    objects for each ColorCorrection (returns a one-item list for a
    .cc file)
    """

    tree = ET.fromstring(xml)

    # Strip away xmlns
    for elem in tree.getiterator():
        if elem.tag.startswith("{"):
             elem.tag = elem.tag.partition("}")[2]

    filetype = tree.tag

    if filetype == "ColorCorrection":
        ccxml = ET.tostring(tree)
        cdl = OCIO.CDLTransform()
        cdl.setXML(ccxml)
        return [cdl]

    elif filetype == "ColorCorrectionCollection":
        allcdl = []
        for cc in tree.getchildren():
            if cc.tag != "ColorCorrection": continue
            ccxml = ET.tostring(cc)
            cdl = OCIO.CDLTransform()
            cdl.setXML(ccxml)
            allcdl.append(cdl)
        return allcdl

    else:
        raise RuntimeError(
            "The supplied file did not have the correct root element, expected"
            " 'ColorCorrection' or 'ColorCorrectionCollection', got %r" % (filetype))


def _cdltransforms_to_xml(allcc):
    """Given a list of CDLTransform objects, returns an XML string
    """

    root = ET.Element("ColorCorrectionCollection")
    root.attrib['xmlns'] = 'urn:ASC:CDL:v1.2'

    for cc in allcc:
        cur = ET.fromstring(cc.getXML())
{'fraction_non_alphanumeric': 0.1170973062122045, 'fraction_numerical': 0.0021990104452996153, 'mean_word_length': 2.9522085445329473, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5801205', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1743, 'n_words': 482}