Example #1
0
def main(filePath):
    readOptions = mfr.Options()
    readOptions.filePath = filePath
    proj = mfr.readRZML(readOptions)
    for cam in proj.cameras:
        mmCam = converter.convertCamera(cam, cdo.softwareType.mm)
        tdeCam = converter.convertCamera(cam, cdo.softwareType.tde)
        assert tdeCam != None
        assert tdeCam.name == cam.name
        assert tdeCam.index == cam.index
        assert tdeCam.filmAspectRatio == cam.filmAspectRatio
        assert tdeCam.width == cam.width
        assert tdeCam.height == cam.height
        assert tdeCam.imageAspectRatio == cam.imageAspectRatio
        assert isinstance(tdeCam.distortion, cdo.KeyframeData)
        assert isinstance(tdeCam.focalLength, cdo.KeyframeData)
        assert isinstance(cam.sequences, list)
        assert isinstance(tdeCam.sequences, list)
        assert tdeCam.sequences == cam.sequences
        assert tdeCam.distortion.getValue(0) != None
        assert tdeCam.focalLength.getValue(0) != None
        assert len(tdeCam.distortion.getTimeValues()) > 0
        assert len(tdeCam.focalLength.getTimeValues()) > 0

        # Test distortion converter.
        distortion = converter.convertDistortion(cam, cdo.softwareType.tde)
        distortion = converter.convertDistortion(cam, cdo.softwareType.mm)

        # Test unit conversion, on float values and KeyframeData objects
        # (static and not static).
        assert converter.convertValue(1.0, cdo.units.cm, cdo.units.mm) == 10.0
        assert converter.convertValue(1.0, cdo.units.mm, cdo.units.cm) == 0.1
        assert converter.convertValue(1.0, cdo.units.cm, cdo.units.cm) == 1.0
        assert converter.convertValue(1.0, cdo.units.mm, cdo.units.mm) == 1.0
        keyData = cdo.KeyframeData(static=True, initialValue=1.0)
        keyDataNew = converter.convertValue(keyData, cdo.units.mm,
                                            cdo.units.mm)
        assert keyDataNew.getValue(0) == 1.0
        keyDataNew = converter.convertValue(keyData, cdo.units.cm,
                                            cdo.units.mm)
        assert keyDataNew.getValue(0) == 10.0
        keyDataNew = converter.convertValue(keyData, cdo.units.mm,
                                            cdo.units.cm)
        assert keyDataNew.getValue(0) == 0.1
        keyData = cdo.KeyframeData()
        keyData.setValue(1.0, 2)
        keyData.setValue(2.0, 3)
        keyDataNew = converter.convertValue(keyData, cdo.units.mm,
                                            cdo.units.mm)
        assert keyDataNew.getValue(2) == 1.0
        keyDataNew = converter.convertValue(keyData, cdo.units.cm,
                                            cdo.units.mm)
        assert keyDataNew.getValue(2) == 10.0
        keyDataNew = converter.convertValue(keyData, cdo.units.mm,
                                            cdo.units.cm)
        assert keyDataNew.getValue(2) == 0.1

    return True
def convertDistortion(cam, outType):
    inValue = cam.distortion
    outValue = None
    inType = cam._software

    if inType == cdo.softwareType.mm and \
         outType == cdo.softwareType.tde:

        outValue = cdo.KeyframeData()
        frameRange = (0, 0, 24)
        if (cam.sequences != None) and (len(cam.sequences) > 0):
            frameRange = cam.sequences[0].frameRange
        if (frameRange[1] - frameRange[0]) > 0:
            for i in range(frameRange[0], frameRange[1]):
                dst = cam.distortion.getValue(i)
                fl = cam.focal.getValue(i)
                k = mmToTdeConvertDistortion(dst, fl, cam.pixelAspectRatio,
                                             cam.filmbackWidth,
                                             cam.filmbackHeight, cam.width,
                                             cam.height)
                outValue.setValue(k, i)
        else:
            outValue.setValue(0.0, 0)
        outValue.simplifyData()

    return outValue
Example #3
0
def getCameras(dom, options):
    cams = list()
    cinfs = dom.getElementsByTagName("CINF")
    for node in cinfs:
        attrs = getAttrs(node)

        # create camera object and fill in information.
        cam = cdo.MMCameraData()

        cam.name = getKey(attrs, 'n', 'str')
        cam.index = getKey(attrs, 'i', 'int')

        cam.width = getKey(attrs, 'sw', 'int', default=512)
        cam.height = getKey(attrs, 'sh', 'int', default=512)
        cam.imageAspectRatio = float(cam.width) / float(cam.height)

        cam.filmbackHeight = getKey(attrs, 'fbh', 'float', default=24.0)
        cam.pixelAspectRatio = getKey(attrs, 'a', 'float', default=1.0)
        cam.filmbackWidth = float(cam.filmbackHeight * cam.imageAspectRatio)
        cam.filmAspectRatio = float(cam.filmbackWidth / cam.filmbackHeight)

        cam.lensCentreX = getKey(attrs, 'ppx', 'float', default=0.5)
        cam.lensCentreY = getKey(attrs, 'ppy', 'float', default=0.5)

        cam.focalLength = cdo.KeyframeData(static=True)
        cam.focalLength.setValue(30.0, 0)
        assert cam.focalLength.length == 1

        cam.focal = cdo.KeyframeData(static=True)
        cam.focal.setValue(1550.0, 0)
        assert cam.focal.length == 1

        cam.distortion = cdo.KeyframeData(static=True)
        cam.distortion.setValue(0.0, 0)
        assert cam.distortion.length == 1

        cams.append(cam)
    return cams
def convertValue(inValue, inUnit, outUnit):
    assert (isinstance(inValue, int) or isinstance(inValue, float)
            or isinstance(inValue, cdo.KeyframeData))
    assert isinstance(inUnit, str)
    assert isinstance(outUnit, str)
    outValue = None

    # if we are not needing to convert, simply return.
    if inUnit == outUnit:
        outValue = inValue
        return outValue

    # get the conversion factor.
    conversionFactor = 1.0
    if ((inUnit == cdo.units.mm) and (outUnit == cdo.units.cm)):
        conversionFactor = 0.1
    elif ((inUnit == cdo.units.cm) and (outUnit == cdo.units.mm)):
        conversionFactor = 10.0

    # convert the value, taking "KeyframeData" into account.
    if isinstance(inValue, cdo.KeyframeData):
        outValue = cdo.KeyframeData()
        if inValue.static:
            value = inValue.getValue(0)
            assert inValue.length == 1
            outValue.setValue(value * conversionFactor, 0)
        else:
            inKeyValues = inValue.getKeyValues()
            for kv in inKeyValues:
                key = kv[0]
                value = kv[1]
                outValue.setValue(value * conversionFactor, key)
    else:
        outValue = inValue * conversionFactor

    assert outValue != None
    return outValue
Example #5
0
def getShots(dom, cams, options):
    timeList = cdo.parseTimeString(options.time)
    seqDataList = list()
    shotTag = dom.getElementsByTagName("SHOT")
    # constant used in the loop.
    piTwoRad = math.pi / 360.0
    for node in shotTag:
        shotAttrs = getAttrs(node)

        shot = cdo.MMSequenceData()

        # Get cam index from shot, loop over cams
        # and get the camera that this shot links to.
        shotCam = None
        camIndex = getKey(shotAttrs, 'ci', 'int', default=1)
        if camIndex != 0:
            for cam in cams:
                if cam.index == camIndex:
                    shotCam = cam
                    shot.cameraIndex = int(camIndex)
        shot.cameraName = str(cam.name)
        assert shotCam != None

        # put shot data into shot object.
        shot.name = str(getKey(shotAttrs, 'n', 'str'))
        shot.index = str(getKey(shotAttrs, 'i', 'str'))

        shot.width = int(getKey(shotAttrs, 'w', 'int'))
        shot.height = int(getKey(shotAttrs, 'h', 'int'))
        shot.imageAspectRatio = float(shot.width) / float(shot.height)

        shotCam.focalLength = cdo.KeyframeData()
        shotCam.focal = cdo.KeyframeData()
        shotCam.distortion = cdo.KeyframeData()

        # # initialise list of sequences for camera.
        # shotCam.sequences = list()

        # loop over all frames in the shot.
        halfFbWidth = 0.5 * shotCam.filmbackWidth
        for childNode in node.childNodes:
            if childNode.nodeName == 'IPLN':
                imgAttrs = getAttrs(childNode)
                shot.imagePath = getKey(imgAttrs, 'img', 'path')
            elif childNode.nodeName == 'TRNG':
                trngAttrs = getAttrs(childNode)
                trim = getKey(trngAttrs, 't', 'int')
                length = getKey(trngAttrs, 'd', 'int')
                startFrame = int()
                endFrame = int()
                startFrame = trim
                if length != None:
                    endFrame = trim + (length - 1)
                fps = getKey(trngAttrs, 'f', 'float')
                shot.frameRange = (startFrame, endFrame, fps)
            elif childNode.nodeName == 'CFRM':
                frameAttrs = getAttrs(childNode)

                frame = getKey(frameAttrs, 't', 'int')
                if frame == None:
                    frame = int(0)
                assert frame >= 0

                # ensure the frame is valid.
                # if cdo.isFrameInTimeList(frame, timeList):

                fovx = getKey(frameAttrs, 'fovx', 'float')
                pixelRatio = getKey(frameAttrs,
                                    'pr',
                                    'float',
                                    default=float(1))
                dst = getKey(frameAttrs, 'rd', 'float', default=0.0)
                fovy = float(fovx) / float(pixelRatio)

                # calculate focal length (from the FOV) and the "focal" attribute.
                assert isinstance(shotCam.filmbackWidth, float)
                focalLength = (halfFbWidth) / (math.tan(piTwoRad * fovx))
                focal = (focalLength *
                         float(shot.width)) / shotCam.filmbackWidth

                shotCam.focal.setValue(focal, frame)
                shotCam.focalLength.setValue(focalLength, frame)
                shotCam.distortion.setValue(dst, frame)

        shotCam.focalLength.simplifyData()
        shotCam.focal.simplifyData()
        shotCam.distortion.simplifyData()

        shotCam.sequences.append(shot)

        seqDataList.append(shot)

    assert isinstance(shotCam.sequences, list)
    return seqDataList
def main(filePath):
    assert cdo.floatIsEqual(float(), float())
    assert cdo.floatIsEqual(float(), sys.float_info.epsilon * 10000.0)
    assert cdo.floatIsEqual(float(), sys.float_info.epsilon * 99999.9)
    assert cdo.floatIsEqual(float(), sys.float_info.epsilon * 19999.9)

    # Test KeyframeData, static and initial
    keyData = cdo.KeyframeData(static=True, initialValue=1.0)
    assert keyData.getValue(0) == 1.0
    assert keyData.static == True
    assert len(keyData.getTimeValues()) == 1

    # Test KeyframeData, static
    keyData = cdo.KeyframeData(static=True)
    assert keyData.getValue(0) == None
    assert keyData.static == True
    assert len(keyData.getTimeValues()) == 1

    # Test KeyframeData, initial value
    keyData = cdo.KeyframeData(initialValue=1.0)
    assert keyData.getValue(0) == 1.0
    assert keyData.static == False
    assert len(keyData.getTimeValues()) == 1

    # Test KeyframeData, consistent variables, when simplifying data.
    keyData = cdo.KeyframeData(static=False)
    assert keyData.length == 0
    keyData.setValue(1.0, 0)
    assert keyData.length == 1
    assert keyData.startFrame == 0
    assert keyData.endFrame == 0
    keyData.setValue(1.0, -1)  # negative time value.
    keyData.setValue(1.0, 10)
    assert keyData.length == 3
    ok = keyData.simplifyData()
    assert ok == True
    assert keyData.static == True
    assert keyData.length == 1
    assert keyData.startFrame == 0
    assert keyData.endFrame == 0
    ok = keyData.simplifyData()
    assert ok == True

    # Test KeyframeData, consistent variables, when animated keyframes.
    keyData = cdo.KeyframeData(static=False)
    keyData.setValue(1.0, 0)
    keyData.setValue(1.0, 1)
    keyData.setValue(1.0, 10)
    keyData.setValue(0.0, 20)
    assert keyData.static == False
    ok = keyData.simplifyData()
    assert ok == True
    assert keyData.static == False
    assert keyData.length == 4
    assert keyData.startFrame == 0
    assert keyData.endFrame == 20
    ok = keyData.simplifyData()
    assert ok == True

    # Test KeyframeData, duplicate frame number/value pairs, overwriting keys.
    keyData = cdo.KeyframeData()
    keyData.setValue(1.0, 0)
    keyData.setValue(1.0, 0)
    keyData.setValue(1.0, 0)
    assert keyData.length == 1
    values = keyData.getValues()
    assert len(values) == 1
    ok = keyData.simplifyData()
    assert ok == True
    values = keyData.getValues()
    assert len(values) == 1

    # Test getClosestFrame
    value = {
        '10': 0.0,
        '12': 0.0,
        '13': 0.0,
        '15': 0.0,
        '45': 0.0,
        '44': 0.0,
        '110': 0.0
    }
    assert cdo.getClosestFrame(10, value) == 10
    assert cdo.getClosestFrame(0, value) == 10
    assert cdo.getClosestFrame(50, value) == 45
    assert cdo.getClosestFrame(2000, value) == 110
    assert cdo.getClosestFrame(14, value) == 13 or 15
    assert cdo.getClosestFrame(28, value) == 15
    assert cdo.getClosestFrame(29, value) == 15
    assert cdo.getClosestFrame(30, value) == 44
    assert cdo.getClosestFrame(31, value) == 44
    assert cdo.getClosestFrame(32, value) == 44
    assert cdo.getClosestFrame(43, value) == 44
    assert cdo.getClosestFrame(30, value) == 44
    assert cdo.getClosestFrame(-1, value) == 10

    return True
def main(cam, options):
    filePath = options.filePath
    assert filePath != None
    outDir = options.outDir
    timeList = cdo.parseTimeString(options.time)

    outFilePath = cdo.createOutFileName(filePath, cam.name,
                                        cdo.exportDesc.tdeRawText, 'txt',
                                        outDir)

    outFileName = p.split(outFilePath)[1]
    msg = "Writing 3DE Raw Text file to '%s'."
    print(msg % outFileName)
    f = open(outFilePath, "w")
    if not f.closed:
        # write camera name
        f.write('Camera Name: ' + str(cam.name) + "\n")

        fl = 3.0  # 30mm
        if isinstance(cam.focalLength, cdo.KeyframeData):
            fl = cam.focalLength.getValue(0)
            assert fl != None

        # write lens data
        f.write("Focal Length (%s): %.4f\n" % (cam._units, fl))
        f.write("Filmback Width (%s): %.4f\n" %
                (cam._units, cam.filmbackWidth))
        f.write("Filmback Height (%s): %.4f\n" %
                (cam._units, cam.filmbackHeight))
        f.write("Film Aspect Ratio: %.4f\n" % cam.filmAspectRatio)
        f.write("Lens Centre X (%s): %.4f\n" % (cam._units, 0.0))
        f.write("Lens Centre Y (%s): %.4f\n" % (cam._units, 0.0))
        f.write("Pixel Aspect Ratio: %.4f\n\n" % cam.pixelAspectRatio)

        # write dynamic distortion
        if cam.distortion.static:
            f.write("Static Distortion\n\n")
        else:
            f.write("Animated Distortion\n\n")

        # write the distortion value out
        distValue = 0.0
        if isinstance(cam.distortion, cdo.KeyframeData):
            distValue = cam.distortion.getValue(0)
            assert distValue != None
        f.write("%s\n" % (distPara))
        f.write("%.15f\n" % (distValue))
        writeCurve(f, cam.distortion, timeList)
        f.write("\n")

        # write out all other distortion default values
        for para in defParas:
            f.write("%s\n" % (para))
            d = 0.0
            if para == 'Anamorphic Squeeze':
                d = 1.0
            f.write("%.15f\n" % (d))
            keys = cdo.KeyframeData(static=True, initialValue=d)
            writeCurve(f, keys, timeList)
            f.write("\n")

        f.close()
    return True