def main(): # Checks if active object is valid if op is None: return # Checks if active object is a polygon object if not op.CheckType(c4d.Opolygon): return # Retrieves the vertex color tag on the polygon object pointCount = op.GetPointCount() vertexColor = op.GetTag(c4d.Tvertexcolor) if not vertexColor: # Creates vertex color tag if it does not exist vertexColor = c4d.VertexColorTag(pointCount) op.InsertTag(vertexColor) # Creates linear field and adds it to the active document linearField = c4d.BaseObject(c4d.Flinear) doc.InsertObject(linearField) # Creates random field and adds it to the active document randomField = c4d.BaseObject(c4d.Frandom) doc.InsertObject(randomField) # Creates layer for linear field and sets link linearFieldLayer = mograph.FieldLayer(c4d.FLfield) linearFieldLayer.SetLinkedObject(linearField) # Creates layer for random field and sets link randomFieldLayer = mograph.FieldLayer(c4d.FLfield) randomFieldLayer.SetLinkedObject(randomField) # Creates a field list fields = c4d.FieldList() # Adds layers to the field list fields.InsertLayer(linearFieldLayer) fields.InsertLayer(randomFieldLayer) # Prepares field input with the points to sample input = mograph.FieldInput(op.GetAllPoints(), pointCount) # Sample all the points of the polygon object output = fields.SampleListSimple(op, input, c4d.FIELDSAMPLE_FLAG_VALUE) # Writes field output values to the vertex color data writeData = vertexColor.GetDataAddressW() for pointIndex in xrange(pointCount): vertexColor.SetColor(writeData, None, None, pointIndex, c4d.Vector(output._value[pointIndex])) # Removes fields from the document linearField.Remove() randomField.Remove() # Updates Cinema 4D c4d.EventAdd()
def main(): # Checks if selected object is valid if op is None: raise ValueError("op is none, please select one object.") if op[c4d.FIELDS] is None or not isinstance(op[c4d.FIELDS], c4d.FieldList): raise ValueError("op does not have a field falloff parameter.") # Creates a FieldList into the memory fieldList = c4d.FieldList() if fieldList is None: raise MemoryError("Failed to create a FieldList.") # Creates a field layer formula formulaLayer = c4d.modules.mograph.FieldLayer(c4d.FLformula) if formulaLayer is None: raise MemoryError("Failed to create a Field Layer Formula.") # Inserts the field layer formula into the FieldList fieldList.InsertLayer(formulaLayer) # Creates a mask for the formula field if not formulaLayer.AddMask(): raise RuntimeError("Failed to create a mask") # Creates a field layer formula to be used as mask formulaMaskLayer = c4d.modules.mograph.FieldLayer(c4d.FLformula) if formulaMaskLayer is None: raise MemoryError("Failed to create a Field Layer Formula.") # Defines the string of the formula layer that will be used as mask formulaMaskLayer[c4d.FORMULAFIELD_STRING] = "u * id / count" # Retrieves the GeListHead (root) of the the mask for the formulaLayer rootMaskFormulaLayer = formulaLayer.GetMaskHead() if rootMaskFormulaLayer is None: raise RuntimeError("There is no mask for the passed Field Layer.") # Inserts the formula layer under the mask head so it will operate as a mask. formulaMaskLayer.InsertUnder(rootMaskFormulaLayer) # Defines the FieldList used by the object to the one we generated op[c4d.FIELDS] = fieldList # Pushes an update event to Cinema 4D c4d.EventAdd()
def main(): obj = c4d.BaseObject(1025800) obj.SetName("Quantize") doc.InsertObject(obj) doc.SetSelection(obj) field = c4d.FieldList() solid = mo.FieldLayer(c4d.FLsolid) field.InsertLayer(solid) obj[c4d.FIELDS] = field rootGroup = c4d.GetCustomDataTypeDefault(c4d.DTYPE_GROUP) rootGroup[c4d.DESC_NAME] = "Parameters" rootGroup[c4d.DESC_SHORT_NAME] = "Parameters" rootGroup[c4d.DESC_TITLEBAR] = True rootGroup[c4d.DESC_GUIOPEN] = True rootGroup[c4d.DESC_COLUMNS] = 2 rootGroup[c4d.DESC_PARENTGROUP] = c4d.DescID() root = obj.AddUserData(rootGroup) nameVec = ["Position step", "Scale step", "Angle step"] unitVec = [c4d.DESC_UNIT_METER, c4d.DESC_UNIT_REAL, c4d.DESC_UNIT_DEGREE] stepVec = [c4d.Vector(1), c4d.Vector(0.1), c4d.Vector(pi / 180)] minVec = [c4d.Vector(0.1), c4d.Vector(0.1), c4d.Vector(pi / 180)] defVec = [c4d.Vector(20), c4d.Vector(0.5), c4d.Vector(pi / 2)] nameBool = "On" defBool = True for i in xrange(3): dataAddVector(obj, nameVec[i], root, unitVec[i], stepVec[i], minVec[i], defVec[i]) dataAddBool(obj, nameBool, root, defBool) dataAddBool(obj, "Unify parameters", root, False) obj[c4d.OEPYTHON_MODE] = 1 obj[c4d. OEPYTHON_STRING] = "import c4d\nfrom c4d.modules import mograph as mo\nfrom c4d import utils\n\ndef quantize(num, step):\n return step*(num//step)\n\ndef vectorQuantize(vec, stepParams):\n vecX = quantize(vec.x, stepParams.x)\n vecY = quantize(vec.y, stepParams.y)\n vecZ = quantize(vec.z, stepParams.z)\n return c4d.Vector(vecX, vecY, vecZ)\n\ndef main():\n\n unify = op[c4d.ID_USERDATA,8]\n posStep = op[c4d.ID_USERDATA,2]\n sclStep = op[c4d.ID_USERDATA,4]\n angStep = op[c4d.ID_USERDATA,6]\n posOn = op[c4d.ID_USERDATA,3]\n sclOn = op[c4d.ID_USERDATA,5]\n angOn = op[c4d.ID_USERDATA,7]\n\n md = mo.GeGetMoData(op)\n if md is None: return 1.0\n\n cnt = md.GetCount()\n marr = md.GetArray(c4d.MODATA_MATRIX)\n\n if unify == True:\n posStep = c4d.Vector(posStep.x)\n sclStep = c4d.Vector(sclStep.x)\n angStep = c4d.Vector(angStep.x)\n\n for i in xrange(cnt):\n m = marr[i]\n\n if posOn == True:\n pos = m.off\n m.off = vectorQuantize(pos, posStep)\n\n if sclOn == True:\n scl = c4d.Vector( m.v1.GetLength(),\n m.v2.GetLength(),\n m.v3.GetLength())\n scl = vectorQuantize(scl, sclStep)\n m.v1 = m.v1.GetNormalized()*scl.x\n m.v2 = m.v2.GetNormalized()*scl.y\n m.v3 = m.v3.GetNormalized()*scl.z\n\n if angOn == True:\n ang = utils.MatrixToHPB(m)\n ang = vectorQuantize(ang, angStep)\n pos = m.off\n scl = c4d.Vector( m.v1.GetLength(),\n m.v2.GetLength(),\n m.v3.GetLength())\n\n m = utils.HPBToMatrix(ang)\n\n m.off = pos\n m.v1 = m.v1.GetNormalized() * scl.x\n m.v2 = m.v2.GetNormalized() * scl.y\n m.v3 = m.v3.GetNormalized() * scl.z\n\n marr[i] = m\n\n md.SetArray(c4d.MODATA_MATRIX, marr, True)" ""
def main(): # Checks if active object is valid if op is None: raise ValueError("op is none, please select one object.") # Checks if active object is a polygon object if not op.CheckType(c4d.Opolygon): raise TypeError("Selected object is not a polygon object.") # Retrieves the vertex color tag on the polygon object pointCount = op.GetPointCount() vertexColor = op.GetTag(c4d.Tvertexcolor) if not vertexColor: # Creates vertex color tag if it does not exist vertexColor = c4d.VertexColorTag(pointCount) if vertexColor is None: raise MemoryError("Failed to create a vertex color tag.") op.InsertTag(vertexColor) if vertexColor is None: RuntimeError("Failed to retrieve or create a vertex color tag.") # Creates linear field and adds it to the active document linearField = c4d.BaseObject(c4d.Flinear) if linearField is None: raise MemoryError("Failed to create a linear field.") doc.InsertObject(linearField) # Creates random field and adds it to the active document randomField = c4d.BaseObject(c4d.Frandom) if randomField is None: raise MemoryError("Failed to create a random field.") doc.InsertObject(randomField) # Creates layer for linear field and sets link linearFieldLayer = c4d.modules.mograph.FieldLayer(c4d.FLfield) if linearFieldLayer is None: raise MemoryError("Failed to create a field layer for linear field.") linearFieldLayer.SetLinkedObject(linearField) # Creates layer for random field and sets link randomFieldLayer = c4d.modules.mograph.FieldLayer(c4d.FLfield) if randomFieldLayer is None: raise MemoryError("Failed to create a field layer for random field.") randomFieldLayer.SetLinkedObject(randomField) # Creates a field list fields = c4d.FieldList() if fields is None: raise MemoryError("Failed to create a FieldList.") # Adds layers to the field list fields.InsertLayer(linearFieldLayer) fields.InsertLayer(randomFieldLayer) # Prepares field input with the points to sample inputField = c4d.modules.mograph.FieldInput(op.GetAllPoints(), pointCount) # Sample all the points of the polygon object output = fields.SampleListSimple(op, inputField, c4d.FIELDSAMPLE_FLAG_VALUE) # Writes field output values to the vertex color data writeData = vertexColor.GetDataAddressW() for pointIndex in range(pointCount): vertexColor.SetColor(writeData, None, None, pointIndex, c4d.Vector(output._value[pointIndex])) # Removes fields from the document linearField.Remove() randomField.Remove() # Pushes an update event to Cinema 4D c4d.EventAdd()