oliviano
9/15/2018 - 10:28 AM

python

Cool GitHub Repo:

https://github.com/aturtur/cinema4d-scripts

Material Reflection:

When importing from Rhino, we always get shiny reflective material, this script iterate through all the selected material and sets the reflection channel to 0% ( lazy code, it's done by index, not by name)

import c4d
from c4d import gui

def main():
    # Get the active document
    doc = c4d.documents.GetActiveDocument()
    
    # Get the active material in the Material Manager
    selected_materials = doc.GetActiveMaterials()
    
    if selected_materials is None:
        gui.MessageDialog("Please select a material.")
        return
    
    # Accessing the material properties
    #color = selected_material[c4d.MATERIAL_COLOR_COLOR]  # Get the current color
    #print("Current Color:", color)
    
    # Modify the color (for example, setting it to red)
    #selected_material[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector(1, 0, 0)
    for material in selected_materials:
        
        cntLayer = material.GetReflectionLayerCount() # get the number of all reflection layers
    
        for i in range(0, cntLayer) :  # loop through all layers
            layer = material.GetReflectionLayerIndex(i) # process current layer
        
            # Sets the Strength layer index 1 to 0%
            if i == 1:
                material[layer.GetDataID() + c4d.REFLECTION_LAYER_TRANS_BRIGHTNESS] = 0 # Here, the DataID is the 
    
        # Update the material
        material.Message(c4d.MSG_UPDATE)
    # Refresh Cinema 4D to reflect changes
    c4d.EventAdd()

if __name__ == "__main__":
    main()

MoGraph Matrix Tips:

# Get the MoData from the Cloner object
md = mo.GeGetMoData(cloner)
# Get the arrays for position, scale, rotation and color
clones_matrix = md.GetArray(c4d.MODATA_MATRIX)
clones_colors = md.GetArray(c4d.MODATA_COLOR)
  
  for i in range(md.GetCount()):
  
    position = clones_matrix[i].off # position.x etc
    color = clones_colors[i] # color.x etc
    myrot = c4d.utils.MatrixToHPB(clones_matrix[i],9) #INT 9 is rotation order , see SDK for more  ( HPB is 9, XYZ is 5 )
    myRx = c4d.utils.RadToDeg(myrot.x) # matrix to eulers 
    myRy = c4d.utils.RadToDeg(myrot.y) 
    myRz = c4d.utils.RadToDeg(myrot.z)

Mograph to UDP:

https://aturtur.com/mograph-to-launchpad/

import c4d
import socket
from c4d.modules import mograph as mo
def initSocket():
    global s
    global server
    host = '127.0.0.1' # Host address
    port = 5001 # Port
    server = ('127.0.0.1', 5006) # Server address and server port
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Initialize socket
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Re-use socket
    s.bind((host, port)) # Bind
    return s, server
initSocket() # Create a socket
def main():
    md = mo.GeGetMoData(op) # Get MoData
    if md is None: return False # If no MoData return false
    cnt = md.GetCount() # Get clone count
    carr = md.GetArray(c4d.MODATA_COLOR) # Get color array
    message = "" # Initialize message string
    row = 0 # Initialize row integer
    for i in xrange(0, cnt): # Loop through clones
        if i != 0: # If not the first index
            if i % 8 == 0:
                row = row + 8 # Launchpad pad numbering
        color = carr[i] # Get clone's color
        index = (i+row) # Calculate pitch
        message += str(index) + "-" + str(color.x) + "," # Write message
    s.sendto(message, server) # Send message
    return True # Everything is fine

MISC

if you dont get any help from here, there's a python for c4d slack run by Andy Needham

bencares [10:50 AM] Thanks @gernge that sounds great. How can I join that?

Billy Chitkin [11:57 AM] i'd search him up on twitter i think his handle is @imcalledandy

Andy Needham Display name imcalledandy Timezone 11:28 AM local time Email andy@imcalledandy.com

Note to self, find back the script collection that as got some good camera related scripts