Beispiel #1
0
def main():
    doc.StartUndo()
    tag = op.GetLastTag()
    tags = op.GetTags()
    md = mo.GeGetMoData(op)
    cnt = md.GetCount()
    selection = mo.GeGetMoDataSelection(tag)
    prefix = "ms"
    sep = "_"
    x = 0
    for k in reversed(tags):
        if k.GetName().split("_")[0] == prefix:
            x = x + 1
    for i in reversed(xrange(0, cnt)):
        if selection.IsSelected(i) == True:
            t = c4d.BaseTag(1021338)
            t[c4d.ID_BASELIST_NAME] = prefix + sep + str(x)
            s = c4d.BaseSelect()
            s.Select(i)
            op.InsertTag(t)
            doc.AddUndo(c4d.UNDOTYPE_NEW, t)
            mo.GeSetMoDataSelection(t, s)
            x = x + 1
    tag.Remove()
    doc.AddUndo(c4d.UNDOTYPE_DELETE, tag)
    doc.EndUndo()
    c4d.EventAdd()
def MgSelTagsFromSelectedClones(obj):
    tag = obj.GetLastTag()  # Get object's last tag
    tags = obj.GetTags()  # Get object's tags
    md = mo.GeGetMoData(obj)  # Get object's MoGraph data
    cnt = md.GetCount()  # Get clone count
    selection = mo.GeGetMoDataSelection(
        tag)  # Get selection from MoGraph selection tag
    prefix = "ms"  # Prefix
    sep = "_"  # Separator
    x = 0  # Initialize iteration variable
    for k in tags:  # Loop through reversed list of tags
        if k.GetName().split("_")[0] == prefix:
            x = x + 1  # Increase iteration variable
    for i in range(0, cnt):  # Loop through reversed list of clones
        if selection.IsSelected(i) == True:
            t = c4d.BaseTag(1021338)  # Initialize MoGraph selection tag
            t[c4d.ID_BASELIST_NAME] = prefix + sep + str(x)  # Set tag name
            s = c4d.BaseSelect()  # Initialize BaseSelect
            s.Select(i)  # Select clone
            obj.InsertTag(t, obj.GetLastTag())  # Insert new tag to object
            doc.AddUndo(c4d.UNDOTYPE_NEW,
                        t)  # Add undo command for inserting new tag
            mo.GeSetMoDataSelection(t, s)  # Set MoGraph selection
            x = x + 1  # Increase iteration variable
    tag.Remove()  # Remove old tag
    doc.AddUndo(c4d.UNDOTYPE_DELETE, tag)  # Add undo command for removing tag
def SetMoGraphWeightMap(op):
    md = mo.GeGetMoData(op)
    if md is None: return False
    cnt = md.GetCount()
    warr = md.GetArray(c4d.MODATA_WEIGHT)
    moWeightMapTag = c4d.BaseTag(c4d.Tmgweight)
    doc.AddUndo(c4d.UNDOTYPE_NEW, moWeightMapTag)
    op.InsertTag(moWeightMapTag)
    mo.GeSetMoDataWeights(moWeightMapTag, warr)
Beispiel #4
0
def getclones():
    py = doc.SearchObject("ArrayMaker")
    md = mo.GeGetMoData(py)
    if md is None: return 1.0
    cnt = md.GetCount()
    marr = md.GetArray(c4d.MODATA_MATRIX)
    clone = md.GetArray(c4d.MODATA_CLONE)
    for i in range(0, cnt):
        objects.append(clone[i])
Beispiel #5
0
def main():
    md = mo.GeGetMoData(op)
    if md is None: return False
    marr=[]
    cnt = md.GetCount()
    for i in xrange(0,cnt):
        mat=setC4DMat([getBoxCoord(i,10,10,100),(1,0,0),(0,1,0),(0,0,1)])
        marr.append(mat)
    md.SetArray(c4d.MODATA_MATRIX, marr, True)
    return True
Beispiel #6
0
def main():
    doc.StartUndo()
    selo = doc.GetActiveObjects(0)

    if len(selo) > 0:
        positions = []
        for o in selo:
            # store positions

            md = mo.GeGetMoData(o)
            if md is None: return False
            cnt = md.GetCount()
            marr = md.GetArray(c4d.MODATA_MATRIX)
            for i in reversed(xrange(0, cnt)):
                positions.append(marr[i].off + o.GetAbsPos())
            o.DelBit(c4d.BIT_ACTIVE)

        # make splines
        matrixA = positions[:len(positions) / 2]
        matrixB = positions[len(positions) / 2:]

        for x in xrange(0, len(matrixA)):
            points = []
            c = 10
            spline = c4d.SplineObject(c, c4d.SPLINETYPE_LINEAR)
            a = matrixA[x]  # first point
            b = matrixB[x]  # last point

            # subd spline
            for k in xrange(0, c):
                t = u.RangeMap(k, 0, c - 1, 0, 1, True)
                point = u.MixVec(a, b, t)
                points.append(point)

            spline.SetAllPoints(points)

            select_points = spline.GetPointS()
            pc = len(spline.GetAllPoints())
            select_points.DeselectAll()
            select_points.Select(0)
            select_points.Select(pc - 1)
            spline.InsertTag(c4d.BaseTag(1018068))
            tag = spline.MakeTag(1018068)
            doc.InsertObject(spline)
            doc.AddUndo(c4d.UNDOTYPE_NEW, spline)

            spline.SetBit(c4d.BIT_ACTIVE)

            c4d.CallButton(tag, c4d.HAIR_SDYNAMICS_TAG_SET_FIXED)
            c4d.EventAdd()
            doc.EndUndo()
Beispiel #7
0
def main():
    md = mo.GeGetMoData(op)
    if md is None: return False

    cnt = md.GetCount()
    marr = md.GetArray(c4d.MODATA_MATRIX)
    fall = md.GetFalloffs()
    a = op[c4d.ID_USERDATA, 2]
    b = op[c4d.ID_USERDATA, 3]
    for i in reversed(xrange(0, cnt)):
        x, y, z = marr[i].off[0], marr[i].off[1], marr[i].off[2]
        y = x**2 / a**2 - (z**2) / b**2
        marr[i].off = c4d.Vector(x, y, z)
    md.SetArray(c4d.MODATA_MATRIX, marr, True)
    return True
Beispiel #8
0
def MgSelTagForEveryClone(obj):
    doc = c4d.documents.GetActiveDocument()  # Get active Cinema 4D document
    try:  # Try to execute following
        md = mo.GeGetMoData(obj)  # Get MoGraph data
        cnt = md.GetCount()  # Get clone count
        for i in xrange(0, cnt):  # Loop through clones
            tag = c4d.BaseTag(1021338)  # Initialize MoGraph selection tag
            tag[c4d.ID_BASELIST_NAME] = "ms_" + str(i)
            s = c4d.BaseSelect()  # Initialize BaseSelect
            obj.InsertTag(tag, obj.GetLastTag())  # Insert tag to object
            doc.AddUndo(c4d.UNDOTYPE_NEW,
                        tag)  # Add undo command for inserting tag
            s.Select(i)  # Select clone
            mo.GeSetMoDataSelection(tag, s)  # Set selection to tag
    except:  # If something went wrong
        pass  # Do nothing
Beispiel #9
0
def main():
    md = mo.GeGetMoData(op)
    if md==None: return
    cnt = md.GetCount()
    
    #matrix list of clones
    marr = md.GetArray(c4d.MODATA_MATRIX)
    
    #flag list of clones
    farr = md.GetArray(c4d.MODATA_FLAGS)
    
    hide = op[c4d.ID_USERDATA, 3]
    radius = op[c4d.ID_USERDATA, 1]
    iterations = op[c4d.ID_USERDATA, 2]
    seperation = (1.0 / float(iterations)) * 0.5
    
    ustart = (iterations-1) if hide==0 else (0)
    

    for u in xrange(ustart, iterations): 
        for i in xrange(0, cnt): #iterate over clones
            if ((farr[i]&(1<<0)) and (not (farr[i]&(1<<1)))): #Only if the clone is visible
                for o in xrange(i-1):
                    if ((farr[o]&(1<<0)) and (not (farr[o]&(1<<1))) and (marr[i].off - marr[o].off).GetLength() < radius):
                        if hide==0: #hide the clone
                            farr[i] &= ~(1<<0)
                            break #next clone
                        elif hide==1:
                            marr[i].off = marr[i].off +(marr[i].v3*radius)
                        elif hide==2:
                            delta = (marr[i].off-marr[o].off)*radius*seperation
                            delta.Normalize()
                            
                            marr[i].off +=delta
                            marr[o].off -=delta
                            
    #handle the modified flag list back to mograph
    md.SetArray(c4d.MODATA_FLAGS, farr, True)
    
    #handle the modified matrix list back to mograph
    md.SetArray(c4d.MODATA_MATRIX, marr, True)
Beispiel #10
0
def main():
    md = mo.GeGetMoData(op)
    if md is None: return False

    cnt = md.GetCount()
    marr = md.GetArray(c4d.MODATA_MATRIX)

    fall = md.GetFalloffs()

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP
    sock.bind((UDP_IP, UDP_PORT))
    data, addr = sock.recvfrom(1024)  # buffer size is 1024 bytes
    #print "received message:", data
    sock.close()

    fingerdictC4D = eval(data)  # convert to dictionary again from string
    fingers = ['Thumb', 'Index', 'Middle', 'Ring',
               'Pinky']  # list to sort the dictionary
    #get position y of each unicorn
    marrNew = []  # new vectors matrix
    for im, m in enumerate(marr):

        positions = m.off  #obtain position data
        xpos = positions[0]
        ypos = positions[1]
        ypos = fingerdictC4D[fingers[im]]
        zpos = positions[2]

        off = c4d.Vector(xpos, ypos, zpos)
        newMatrix = c4d.Matrix(off)
        marrNew.append(newMatrix)

        #set new position vector with new leap yposiiton
        #create new matrix
        #append matrix to empty list
    #set new y position for each clone

    setNewArr = md.SetArray(c4d.MODATA_MATRIX, marrNew,
                            False)  # set new cloner matrix
def main():

    md = mo.GeGetMoData(op)
    if md is None: return False

    # get total count on x,y,z direction from Cloner
    Rows=gen[c4d.MG_GRID_RESOLUTION]
    xRow=int(Rows[0])
    yRow=int(Rows[1])
    zRow=int(Rows[2])

    cnt = md.GetCount()

    # fetch color information and
    colorLs = md.GetArray(c4d.MODATA_COLOR)

    matrixLs=md.GetArray(c4d.MODATA_MATRIX)
    #flagLs = md.GetArray(c4d.MODATA_FLAGS)

    # get base color
    for i in range(0,xRow*yRow):

        # use color to decide how many object should show in certain bar
        ratio=c4d.utils.RGBToHSV(colorLs[i])[2]
        # use math.ceil keep floor show
        showCount=int(math.ceil(ratio*zRow))

        # make object unshow
        for j in range(0,zRow):
            if j > showCount:
                matrixLs[j*xRow*yRow+i].v1=c4d.Vector(0,0,0)
                matrixLs[j*xRow*yRow+i].v2=c4d.Vector(0,0,0)
                matrixLs[j*xRow*yRow+i].v3=c4d.Vector(0,0,0)
                #flagLs[j*xRow*yRow+i]=c4d.MOGENFLAG_DISABLE

    #md.SetArray(c4d.MODATA_FLAGS, flagLs, True)
    md.SetArray(c4d.MODATA_MATRIX, matrixLs, True)
    return True
def MoGraphSelectionFromRange(obj):
    #try:
    tag = obj.GetLastTag()  # Get last tag of object
    tags = obj.GetTags()  # Get object's tags
    md = mo.GeGetMoData(obj)  # Get MoGraph data
    cnt = md.GetCount()  # Get clone count
    prefix = "ms"  # Prefix for selection tag
    sep = "_"  # Separator for selection tag
    p = 0  # Initialize iteration variable
    userInput = g.InputDialog("Selected IDs for " + obj.GetName(),
                              "")  # User input dialog
    baseList = userInput.split(",")  # Split user input to list
    add = []  # Initialize empty list
    finalList = []  # Initialize empty list
    for x in baseList:  # Loop through list items
        rng = x.split("-")  # Split range value (e.g. 5-15)
        if len(rng) > 1:
            for i in xrange(int(rng[0]), int(rng[1]) + 1):
                add.append(i)
    fullList = baseList + add
    for f in fullList:
        if type(f) is int:
            finalList.append(f)
        if type(f) is not int:
            if f.find("-") is -1:
                finalList.append(f)
    for k in reversed(tags):  # Loop through tags
        if k.GetName().split("_")[0] == prefix:
            p = p + 1  # Increase iteration
    t = c4d.BaseTag(1021338)  # Initialize MoGraph Selection tag
    t[c4d.ID_BASELIST_NAME] = prefix + sep + str(p)  # Set tag name
    s = c4d.BaseSelect()  # Initialize BaseSelect
    for f in finalList:  # Loop through list
        s.Select(int(f))  # Select items
    obj.InsertTag(t)  # Insert tag to object
    doc.AddUndo(c4d.UNDOTYPE_NEW, t)  # Add undo command for inserting new tag
    mo.GeSetMoDataSelection(t, s)  # Set MoGraph selection
def main():
    md = mo.GeGetMoData(op)
    if not md: return False

    cnt = md.GetCount()
    scl = op[c4d.ID_USERDATA, 2]
    anim = op[c4d.ID_USERDATA, 3]
    mode = op[c4d.ID_USERDATA, 4]
    torus_R = op[c4d.ID_USERDATA, 6]
    torus_p = op[c4d.ID_USERDATA, 7]
    torus_q = op[c4d.ID_USERDATA, 8]
    liss_k = op[c4d.ID_USERDATA, 11]
    liss_l = op[c4d.ID_USERDATA, 12]
    speed = op[c4d.ID_USERDATA, 10]
    off_mode = op[c4d.ID_USERDATA, 5]
    offset = op[c4d.ID_USERDATA, 13]

    #matrix list of clones
    marr = md.GetArray(c4d.MODATA_MATRIX)

    pos = c4d.Vector()

    #extract the current time if the "Animate" attribute is enabled
    if anim:
        time = doc.GetTime().Get()
    else:
        time = 0

    #iter through all clones
    for i in xrange(0, cnt):
        if off_mode:
            itime = (offset / (100 * cnt)) * 2 * math.pi * i + (speed * time)
        else:
            itime = i + (speed * time)

        if mode == 1:  #Trefoil Knot A
            pos = c4d.Vector(
                scl * (2 + math.cos(3 * itime)) * math.cos(2 * itime),
                scl * (2 + math.cos(3 * itime)) * math.sin(2 * itime),
                scl * math.sin(3 * itime))
        elif mode == 2:  #Trefoil Knot B
            pos = c4d.Vector(
                -scl * math.cos(itime) - 0.2 * scl * math.cos(5 * itime) +
                1.5 * scl * math.sin(2 * itime),
                -1.5 * scl * math.cos(2 * itime) + scl * math.sin(itime) -
                0.2 * scl * math.sin(5 * itime), scl * math.cos(3 * itime))
        elif mode == 3:  #Achterknoten
            pos = c4d.Vector(
                scl * math.cos(itime) + scl * math.cos(3 * itime),
                0.6 * scl * math.sin(itime) + scl * math.sin(3 * itime),
                0.4 * scl * math.sin(3 * itime) - scl * math.sin(6 * itime))
        elif mode == 4:  #Torus
            pos = c4d.Vector((2 * scl + torus_R * math.cos(torus_p * itime)) *
                             math.cos(torus_q * itime),
                             (2 * scl + torus_R * math.cos(torus_p * itime)) *
                             math.sin(torus_q * itime),
                             torus_R * math.sin(torus_p * itime))
        elif mode == 5:  #Lissajous
            pos = c4d.Vector(scl * math.cos(liss_k.x * itime + liss_l.x),
                             scl * math.cos(liss_k.y * itime + liss_l.y),
                             scl * math.cos(liss_k.z * itime + liss_l.z))

        #set the position of the clone
        marr[i].off = pos

    #handle the modified matrix list back to mograph
    md.SetArray(c4d.MODATA_MATRIX, marr, True)
Beispiel #14
0
def main():

    coldata1 = "F:/ipcContentChapel001.csv"
    doc = c4d.documents.GetActiveDocument()
    ctime = doc.GetTime()  # Save current time

    # Get FPS and minimum + maximum frames
    fps = doc.GetFps()
    start = doc.GetMinTime().GetFrame(fps)
    end = doc.GetMaxTime().GetFrame(fps)

    op = doc.SearchObject('sunmatrix')  #search for object in c4d to store data
    op2 = doc.SearchObject(
        'skymatrix')  #search for object in c4d to store data
    op3 = doc.SearchObject('creepyman')
    op4 = doc.SearchObject('ledsSoloMorning')
    op5 = doc.SearchObject('ledsSoloEvening')

    with open(coldata1, 'wb') as f:

        #for frame in xrange(start,end+1):
        frame = 0
        #print c4d.BaseTime(frame,fps)
        while c4d.BaseTime(frame, fps) < doc.GetMaxTime():

            bt = c4d.BaseTime(frame, fps)  #current frame,frame rate
            c4d.documents.SetDocumentTime(doc, bt)

            #print "Document time : ", doc.GetTime().GetFrame(fps)

            c4d.GeSyncMessage(
                c4d.EVMSG_TIMECHANGED
            )  # make the timeline, timeslider etc. do an instant redraw.)
            c4d.EventAdd(c4d.EVENT_ANIMATE)
            md = mo.GeGetMoData(op)
            md2 = mo.GeGetMoData(op2)
            md3 = mo.GeGetMoData(op3)
            md4 = mo.GeGetMoData(op4)
            md5 = mo.GeGetMoData(op5)

            if md == None and md2 == None and md3 == None: return False

            cnt = md.GetCount()
            marr = md.GetArray(c4d.MODATA_CLONE)
            carr = md.GetArray(c4d.MODATA_COLOR)

            cnt2 = md2.GetCount()
            marr2 = md2.GetArray(c4d.MODATA_CLONE)
            carr2 = md2.GetArray(c4d.MODATA_COLOR)

            cnt3 = md3.GetCount()
            marr3 = md3.GetArray(c4d.MODATA_CLONE)
            carr3 = md3.GetArray(c4d.MODATA_COLOR)

            cnt4 = md4.GetCount()
            marr4 = md4.GetArray(c4d.MODATA_CLONE)
            carr4 = md4.GetArray(c4d.MODATA_COLOR)

            cnt5 = md5.GetCount()
            marr5 = md5.GetArray(c4d.MODATA_CLONE)
            carr5 = md5.GetArray(c4d.MODATA_COLOR)

            c4d.DrawViews()

            node_count = 0  #count node
            allcol = []
            for c, d, e, j, k in zip(carr, carr2, carr3, carr4, carr5):
                cx = c * 255  #multiply colour vals by 255 to get RGB
                r = int(cx[0])
                g = int(cx[1])
                b = int(cx[2])
                w = int((r + g + b) / 10)

                if (r >= 254):
                    r = 254
                if (g >= 254):
                    g = 254
                if (b >= 254):
                    b = 254
                if (w >= 254):
                    w = 254

                dx = d * 255  #multiply colour vals by 255 to get RGB
                r2 = int(dx[0])
                g2 = int(dx[1])
                b2 = int(dx[2])
                w2 = int((r2 + g2 + b2) / 10)

                if (r2 >= 254):
                    r2 = 254
                if (g2 >= 254):
                    g2 = 254
                if (b2 >= 254):
                    b2 = 254
                if (w2 >= 254):
                    w2 = 254

                ex = e * 255  #multiply colour vals by 255 to get RGB
                r3 = int(ex[0])
                g3 = int(ex[1])
                b3 = int(ex[2])
                w3 = int((r3 + g3 + b3) / 10)

                if (r3 >= 254):
                    r3 = 254
                if (g3 >= 254):
                    g3 = 25
                if (b3 >= 254):
                    b3 = 254
                if (w3 >= 254):
                    w3 = 254

                jx = j * 255  #multiply colour vals by 255 to get RGB
                r4 = int(jx[0])
                g4 = int(jx[1])
                b4 = int(jx[2])
                w4 = int((r4 + g4 + b4) / 10)

                if (r4 >= 254):
                    r4 = 254
                if (g4 >= 254):
                    g4 = 254
                if (b4 >= 254):
                    b4 = 254
                if (w4 >= 254):
                    w4 = 254

                kx = k * 255  #multiply colour vals by 255 to get RGB
                r5 = int(kx[0])
                g5 = int(kx[1])
                b5 = int(kx[2])
                w5 = int((r5 + g5 + b5) / 10)

                if (r5 >= 254):
                    r5 = 254
                if (g5 >= 254):
                    g5 = 254
                if (b5 >= 254):
                    b5 = 254
                if (w5 >= 254):
                    w5 = 254

                allcol.append(r)
                allcol.append(r2)
                allcol.append(r3)
                allcol.append(r4)
                allcol.append(r5)

                allcol.append(g)
                allcol.append(g2)
                allcol.append(g3)
                allcol.append(g4)
                allcol.append(g5)

                allcol.append(b)
                allcol.append(b2)
                allcol.append(b3)
                allcol.append(b4)
                allcol.append(b5)

                allcol.append(w)
                allcol.append(w2)
                allcol.append(w3)
                allcol.append(w4)
                allcol.append(w5)
                #append the led value for morning sun here
                #append the led value for the evening sun here

            newSplitList = [
                allcol[i:i + 1200] for i in xrange(0, len(allcol), 1200)
            ]  #60 pixels  x 4  (rgbw) = 240 values per frame

            for lst2 in newSplitList:
                for i, item in enumerate(lst2):
                    col_str = str(
                        item
                    )  #must be string in csv clamp stops negatives from occuring  export as int

                    #write string of col vals to csv
                    f.write(col_str)
                    if (i == len(lst2) - 1):
                        f.write('\n')
                    else:
                        f.write(',')  #separate values with comma in csv file

                    #see node and colour for each frame in c4d console

                    #print "Node ID : ",  node_count, " || Writing value : ", col255

                    node_count += 1

                #plus 2268

            #print "Frame " , frame

            #gen.Message(c4d.MSG_UPDATE)
            #op.Message(c4d.MSG_UPDATE)

            c4d.EventAdd(
                c4d.EVENT_ANIMATE
            )  #Adds a global event to Cinema 4D’s event queue. Results in a CoreMessage() message.

            frame += 1

    f.close()
Beispiel #15
0
def main():

    #------------ Basic MoGraph ---------------#

    md = mo.GeGetMoData(op)

    if md == None: return False

    cnt = md.GetCount()
    marr = md.GetArray(c4d.MODATA_MATRIX)
    fall = md.GetFalloffs()
    carr = md.GetArray(c4d.MODATA_COLOR)

    #--------- Colour Threshold -------------#

    colThres = []
    for i in reversed(xrange(0, cnt)):

        if carr[i] != c4d.Vector(
                0, 0, 0):  #if clones arent black add to list (colThres)
            l1 = i
            colThres.append(l1)

    #print "Number of Nnt black clones", len(colThres)

    #---- Update mapV list based upon input ----#

    mapV = []  #map value empty list

    for x in xrange(
            126
    ):  # Set list values to 0 (LED OFF) - There are 126 LEDs on sheild
        l = 0
        mapV.append(l)

    #---- Update mapV list based upon input ----#

    for x in xrange(len(
            colThres)):  # For the clones that aren't black set mapV to 1 (on)
        mapV[colThres[x]] = 1

    #---- Convert mapV to format for LED ----# #The sheild requires the formart to start with $$$F and end with "\r"
    #see mata for debug
    mapVup = ("".join(map(str, (mapV))))

    srt = '$$$F'
    end = "\r"

    #---- Send data via UDP ----#

    data = srt + mapVup + end
    #mata = "$$$F000000000000000000000111000011111110011111110111111111111101111111101111011000110011000110000000000000000000000000000000000000\r" #Debug
    #print "data =", data

    #------------- Send UDP -------------------#

    #UDP_IP = "localhost"
    UDP_IP = "192.168.0.19"  #Pi
    UDP_PORT = 2556

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((UDP_IP, UDP_PORT))
    sock.send(data)

    #-----------------------------------------#

    return True
def main():
    #get the data object from mograph
    md = mograph.GeGetMoData(op)
    if md == None: return

    #get an list of all matrices
    marr = md.GetArray(c4d.MODATA_MATRIX)

    #get the userdata values:
    #y-axis offset
    yOffset = op[c4d.ID_USERDATA, 1]
    #angle offset (0-360°)
    angleX = op[c4d.ID_USERDATA, 2]
    angleY = op[c4d.ID_USERDATA, 3]
    angleZ = op[c4d.ID_USERDATA, 4]
    #random threshold (0-1)
    randomX = op[c4d.ID_USERDATA, 5]
    randomY = op[c4d.ID_USERDATA, 6]
    randomZ = op[c4d.ID_USERDATA, 7]
    #link for an optional camera target
    cameraTarget = op[c4d.ID_USERDATA, 8]

    #degree2rad conversion constant (rad=degree*PI/180)
    modRad = math.pi / 180
    #always create a random instance so offsetting the seed won't affect other python scripts
    rnd = random.Random()
    #temp var to store a matrix
    tmpm = None

    #the utils package provides the lazy man's way to create matrices
    #...translation:
    opm = utils.MatrixMove(c4d.Vector(0, yOffset, 0))
    #...rotation
    orxm = c4d.utils.MatrixRotX(angleX * modRad)
    orym = c4d.utils.MatrixRotY(angleY * modRad)
    orzm = c4d.utils.MatrixRotZ(angleZ * modRad)

    #iterate over all matrices
    for i in xrange(0, len(marr)):

        #skip the first object
        if i > 0:
            """
            set the object's position to that of its predecessor and offset it by multiplying 
            with the translation matrix
            """
            marr[i] = tmpm * opm

        #on every second object...
        if i % 2:
            #offset the random seed
            rnd.seed(i)

            #if random < threshold apply the rotation offset
            if rnd.random() < randomX: marr[i] *= orxm
            if rnd.random() < randomY: marr[i] *= orym
            if rnd.random() < randomZ: marr[i] *= orzm

        #keep the resulting matrix for the following object
        tmpm = marr[i]

    #catch if the link field is empty
    if cameraTarget is not None:
        #set the linked object's matrix
        cameraTarget.SetMg(tmpm)

    #handle the modified matrix list back to mograph
    md.SetArray(c4d.MODATA_MATRIX, marr, True)
def main():

    coldata1 = "F:/contentIPC21.hex"
    doc = c4d.documents.GetActiveDocument ()
    ctime = doc.GetTime() # Save current time

    # Get FPS and minimum + maximum frames
    fps = doc.GetFps()
    start = doc.GetMinTime().GetFrame(fps)
    end  = doc.GetMaxTime().GetFrame(fps)

    op = doc.SearchObject('sunmatrix') #search for object in c4d to store data
    op2 = doc.SearchObject('skymatrix') #search for object in c4d to store data
    op3 = doc.SearchObject('creepyman')
    op4 = doc.SearchObject('ledsSoloMorning')
    op5 = doc.SearchObject('ledsSoloEvening')

    with open(coldata1, 'wb') as f:


        #for frame in xrange(start,end+1):
        frame = 0
        #print c4d.BaseTime(frame,fps)
        while c4d.BaseTime(frame,fps) < doc.GetMaxTime():


            bt = c4d.BaseTime (frame, fps) #current frame,frame rate
            c4d.documents.SetDocumentTime (doc, bt)

            #print "Document time : ", doc.GetTime().GetFrame(fps)

            c4d.GeSyncMessage(c4d.EVMSG_TIMECHANGED)# make the timeline, timeslider etc. do an instant redraw.)
            c4d.EventAdd(c4d.EVENT_ANIMATE)
            md = mo.GeGetMoData(op)
            md2 = mo.GeGetMoData(op2)
            md3 = mo.GeGetMoData(op3)
            md4 = mo.GeGetMoData(op4)
            md5 = mo.GeGetMoData(op5)
            if md==None and md2 == None and md3 == None and md4 == None and md5 == None: return False

            cnt = md.GetCount()
            marr = md.GetArray(c4d.MODATA_CLONE)
            carr = md.GetArray(c4d.MODATA_COLOR)

            cnt2 = md2.GetCount()
            marr2 = md2.GetArray(c4d.MODATA_CLONE)
            carr2 = md2.GetArray(c4d.MODATA_COLOR)

            cnt3 = md3.GetCount()
            marr3 = md3.GetArray(c4d.MODATA_CLONE)
            carr3 = md3.GetArray(c4d.MODATA_COLOR)

            cnt4 = md4.GetCount()
            marr4 = md4.GetArray(c4d.MODATA_CLONE)
            carr4 = md4.GetArray(c4d.MODATA_COLOR)

            cnt5 = md5.GetCount()
            marr5 = md5.GetArray(c4d.MODATA_CLONE)
            carr5 = md5.GetArray(c4d.MODATA_COLOR)

            c4d.DrawViews()

            node_count = 0 #count node
            allcol = []
            for c, d, e,j,k in zip(carr,carr2,carr3,carr4,carr5):
                cx = c * 255 #multiply colour vals by 255 to get RGB
                r = int(cx[0])
                g = int(cx[1])
                b = int(cx[2])
                w = int((r+g+b)/10)

                if(r >= 254):
                    r = 254
                if(g >= 254):
                    g = 254
                if(b >= 254):
                    b = 254
                if(w >= 254):
                   w = 254



                dx = d * 255 #multiply colour vals by 255 to get RGB
                r2 = int(dx[0])
                g2 = int(dx[1])
                b2 = int(dx[2])
                w2 = int((r2+g2+b2)/10)

                if(r2 >= 254):
                    r2 = 254
                if(g2 >= 254):
                    g2 = 254
                if(b2 >= 254):
                    b2 = 254
                if(w2 >= 254):
                   w2 = 254

                ex = e * 255 #multiply colour vals by 255 to get RGB
                r3 = int(ex[0])
                g3 = int(ex[1])
                b3 = int(ex[2])
                w3 = int((r3+g3+b3)/10)

                if(r3 >= 254):
                    r3 = 254
                if(g3 >= 254):
                    g3 = 25
                if(b3 >= 254):
                    b3 = 254
                if(w3 >= 254):
                   w3 = 254


                jx = j * 255 #multiply colour vals by 255 to get RGB
                r4 = int(jx[0])
                g4 = int(jx[1])
                b4 = int(jx[2])
                w4 = int((r4+g4+b4)/10)

                if(r4 >= 254):
                    r4 = 254
                if(g4 >= 254):
                    g4 = 254
                if(b4 >= 254):
                    b4 = 254
                if(w4 >= 254):
                   w4 = 254

                kx = k * 255 #multiply colour vals by 255 to get RGB
                r5 = int(kx[0])
                g5 = int(kx[1])
                b5 = int(kx[2])
                w5 = int((r5+g5+b5)/10)

                if(r5 >= 254):
                    r5 = 254
                if(g5 >= 254):
                    g5 = 254
                if(b5 >= 254):
                    b5 = 254
                if(w5 >= 254):
                   w5 = 254

                allcol.append(r)
                allcol.append(r2)
                allcol.append(r3)
                allcol.append(r4)
                allcol.append(r5)

                allcol.append(g)
                allcol.append(g2)
                allcol.append(g3)
                allcol.append(g4)
                allcol.append(g5)

                allcol.append(b)
                allcol.append(b2)
                allcol.append(b3)
                allcol.append(b4)
                allcol.append(b5)

                allcol.append(w)
                allcol.append(w2)
                allcol.append(w3)
                allcol.append(w4)
                allcol.append(w5)


            ipcByteArray = bytearray()
            ipcByteArray.append(0xFF)


            ipcByteArray.extend(bytearray(allcol))

            node_count += 1


            c4d.EventAdd(c4d.EVENT_ANIMATE) #Adds a global event to Cinema 4D’s event queue. Results in a CoreMessage() message.

            frame += 1
            f.write(ipcByteArray) # write after every frame
    f.close()