def main(): objs = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0) mypoly = c4d.BaseObject(c4d.Opolygon) #Create an empty polygon object mypoly.SetName("points") mypoly.ResizeObject(len(objs), 0) #New number of points, New number of polygons vrottag = c4d.VertexColorTag(mypoly.GetPointCount()) vrottag.SetName("rot_vertex_map") vscaletag = c4d.VertexColorTag(mypoly.GetPointCount()) vscaletag.SetName("scale_vertex_map") rotdata = vrottag.GetDataAddressW() scaledata = vscaletag.GetDataAddressW() i = 0 for obj in objs: pos = obj.GetAbsPos() rot = obj.GetAbsRot() scale = obj.GetAbsScale() mypoly.SetPoint(i, c4d.Vector(pos.x, pos.y, pos.z)) c4d.VertexColorTag.SetColor(rotdata, None, None, i, c4d.Vector(rot.x, rot.y, rot.z)) c4d.VertexColorTag.SetColor(scaledata, None, None, i, c4d.Vector(scale.x, scale.y, scale.z)) i = i + 1 #mypoly.SetPolygon(0, c4d.CPolygon(0, 1, 2,3) ) #The Polygon's index, Polygon's points mypoly.InsertTag(vrottag) mypoly.InsertTag(vscaletag) doc.InsertObject(mypoly, None, None) mypoly.Message(c4d.MSG_UPDATE) c4d.EventAdd()
def parse_vertex_colors(index, c4d_mesh): colors = [] color_key = 'COLOR_{}'.format(index) colortag = None if color_key in prim.attributes: colors = BinaryData.get_data_from_accessor( gltf, prim.attributes[color_key]) if colors: nb_verts = len(verts) colortag = c4d.VertexColorTag(nb_verts) colortag.SetPerPointMode(True) colortag.SetName(color_key) vtx_color_data = colortag.GetDataAddressW() has_alpha = len(colors[0]) > 3 for i in range(nb_verts): c4d.VertexColorTag.SetPoint( vtx_color_data, None, None, i, c4d.Vector4d(colors[i][0], colors[i][1], colors[i][2], colors[i][3] if has_alpha else 1.0)) c4d_mesh.InsertTag(colortag) self.has_vertex_colors = True return colortag
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 UpdateVertexColors(_Polygon, Colors): #global Polygon #VERTEX TAG FOR THE BEHIND-THE-SCENES POLYGON OBJECT if not _Polygon.GetTag(c4d.Tvertexcolor): colorTag = c4d.VertexColorTag(_Polygon.GetPointCount()) _Polygon.InsertTag(colorTag) else: colorTag = _Polygon.GetTag(c4d.Tvertexcolor) data = c4d.VertexColorTag.GetDataAddressW(colorTag) #VERTEX TAG FOR THE PYTHON GENERATOR OBJECT if not op.GetTag(c4d.Tvertexcolor): opTag = colorTag.GetClone() op.InsertTag(opTag) else: opTag = op.GetTag(c4d.Tvertexcolor) data2 = c4d.VertexColorTag.GetDataAddressW(opTag) for i in range(_Polygon.GetPointCount()): if i < len(Colors): r = Colors[i][0] g = Colors[i][1] b = Colors[i][2] else: r = (_Polygon.GetPoint(i)[0] - minX) / (maxX - minX) g = (_Polygon.GetPoint(i)[1] - minY) / (maxY - minY) b = (_Polygon.GetPoint(i)[2] - minZ) / (maxZ - minZ) col = c4d.Vector(r, g, b) c4d.VertexColorTag.SetColor(data, None, None, i, col) c4d.VertexColorTag.SetColor(data2, None, None, i, col) shader[c4d.SLA_DIRTY_VMAP_OBJECT] = colorTag
def UpdateVertexColors(Colors): global Polygon if not Polygon.GetTag(c4d.Tvertexcolor): colorTag = c4d.VertexColorTag(Polygon.GetPointCount()) Polygon.InsertTag(colorTag) else: colorTag = Polygon.GetTag(c4d.Tvertexcolor) data = c4d.VertexColorTag.GetDataAddressW(colorTag) for i in range(Polygon.GetPointCount()): if i < len(Colors): col = Colors[i] else: r = (Polygon.GetPoint(i)[0] - minX) / (maxX - minX) g = (Polygon.GetPoint(i)[1] - minY) / (maxY - minY) b = (Polygon.GetPoint(i)[2] - minZ) / (maxZ - minZ) col = c4d.Vector(r, g, b) c4d.VertexColorTag.SetColor(data, None, None, i, col) shader[c4d.SLA_DIRTY_VMAP_OBJECT] = colorTag
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()