def GenerateMeshFromMatrices(_matrices, _size=1, _quad=True): # QUAD MESH if _quad: verticesPerFace = 4 baseVertices = CreateRectangleVertices(_size) # TRIANGLE MESH else: verticesPerFace = 3 baseVertices = CreateTriangleVertices(_size) # Setup List Counts matrixCount = len(_matrices) faceCount = matrixCount vertexCount = faceCount * verticesPerFace # Setup Mesh Lists mesh = c4d.PolygonObject(vertexCount, faceCount) vertices = [None] * vertexCount faces = [None] * faceCount # Iterate through the matrices and transform the vertices accordingly for i, m in enumerate(_matrices): cnt = i * verticesPerFace if _quad: vertices[cnt:cnt + 4] = TransformVertices(baseVertices, m) faces[i] = c4d.CPolygon(cnt + 0, cnt + 1, cnt + 2, cnt + 3) else: vertices[cnt:cnt + 3] = TransformVertices(baseVertices, m) faces[i] = c4d.CPolygon(cnt + 0, cnt + 1, cnt + 2) # Translate the matrix of the mesh object to the centroid of the vertices centroid = Centroid(vertices) mesh.SetAbsPos(centroid) # Translate the vertices back to their original positions vertices = [v - centroid for v in vertices] # Set the mesh vertices mesh.SetAllPoints(vertices) # Set the mesh faces for i, f in enumerate(faces): mesh.SetPolygon(i, f) # Update the object mesh.Message(c4d.MSG_UPDATE) return mesh
def UpdateFaces(_Polygon, Faces): #global Polygon for i, face in enumerate(Faces): if len(face) > 2: A = face[0] - 1 B = face[1] - 1 C = face[2] - 1 if len(face) == 3: _Polygon.SetPolygon(i, c4d.CPolygon( A, B, C)) #The Polygon's index, Polygon's points elif len(face) == 4: D = face[3] - 1 _Polygon.SetPolygon(i, c4d.CPolygon(A, B, C, D))
def multipatch(self, shp): pts = [ c4d.Vector(x, z, y) - self.centre for (x, y), z in zip(shp.points, shp.z) ] nb_pts = len(pts) polys = [] id_pt = 0 n = 0 for p1, p2 in zip(shp.parts[:-1], shp.parts[1:]): nb_cotes = p2 - p1 #Triangles if nb_cotes == 3: poly = c4d.CPolygon(id_pt + n, id_pt + n + 1, id_pt + n + 2) polys.append(poly) #Quadrangles elif nb_cotes == 4: poly = c4d.CPolygon(id_pt + n, id_pt + n + 1, id_pt + n + 2, id_pt + n + 3) polys.append(poly) #Autres else: print 'attention ngons!' pass n += nb_cotes res = c4d.PolygonObject(nb_pts, len(polys)) #TODO : tag phong ! res.SetAllPoints(pts) for i, poly in enumerate(polys): res.SetPolygon(i, poly) res.Message(c4d.MSG_UPDATE) res.InsertUnder(self.geoms)
def polygonise(obj, nb_rows, nb_cols): id_poly = 0 id_pt = 0 for r in xrange(nb_rows): for c in xrange(nb_cols): if c < (nb_cols - 1) and r < (nb_rows - 1): try: obj.SetPolygon( id_poly, c4d.CPolygon(id_pt, id_pt + nb_cols, id_pt + 1 + nb_cols, id_pt + 1)) except: print id_poly, '->', (id_pt, id_pt + nb_cols, id_pt + 1 + nb_cols, id_pt + 1) id_poly += 1 id_pt += 1
def main(): pos_0 = c4d.Vector(-100, 0, -100) pos_1 = c4d.Vector(-100, 0, 100) pos_2 = c4d.Vector(100, 0, 100) pos_3 = c4d.Vector(100, 0, -100) pos = [pos_0, pos_1, pos_2, pos_3] mypoly = c4d.BaseObject(c4d.Opolygon) # Create an empty polygon object mypoly.ResizeObject(4, 1) # New number of points, New number of polygons for i in range(0, 4): mypoly.SetPoint(i, pos[i]) mypoly.SetPolygon(0, c4d.CPolygon(0, 1, 2, 3)) # The Polygon's index, Polygon's points doc.InsertObject(mypoly, None, None) mypoly.Message(c4d.MSG_UPDATE) c4d.EventAdd()
def readRig(self, received): if self.workerThread.TestBreak(): self.sock.close() return 0 reciv = received[8] + received[9] + received[10] + received[11] readerCnt = 11 blockCount = struct.unpack('I', reciv) quadCnt = 0 quads = [] while quadCnt < blockCount[0]: quadCnt += 1 reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose1 = struct.unpack('I', reciv)[0] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose2 = struct.unpack('I', reciv)[0] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose3 = struct.unpack('I', reciv)[0] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose4 = struct.unpack('I', reciv)[0] quads.append(pose4) quads.append(pose3) quads.append(pose2) quads.append(pose1) if self.workerThread.TestBreak(): self.sock.close() return 0 reciv = received[readerCnt + 1] + received[readerCnt + 2] + received[ readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 blockCount = struct.unpack('I', reciv) quadCnt = 0 tris = [] while quadCnt < blockCount[0]: quadCnt += 1 reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose1 = struct.unpack('I', reciv)[0] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose2 = struct.unpack('I', reciv)[0] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pose3 = struct.unpack('I', reciv)[0] tris.append(pose3) tris.append(pose2) tris.append(pose1) if self.workerThread.TestBreak(): self.sock.close() return 0 reciv = received[readerCnt + 1] + received[readerCnt + 2] + received[ readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 blockCount = struct.unpack('I', reciv) pointCnt = 0 points = [] while pointCnt < blockCount[0]: pointCnt += 1 reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pointX = struct.unpack('f', reciv)[0] #print pointX reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pointY = struct.unpack('f', reciv)[0] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 pointZ = struct.unpack('f', reciv)[0] newPoint = c4d.Vector(pointX, pointY, pointZ * -1) points.append(newPoint) if self.workerThread.TestBreak(): self.sock.close() return 0 reciv = received[readerCnt + 1] + received[readerCnt + 2] readerCnt += 2 blockCount = struct.unpack('H', reciv)[0] poseCnt = 0 shapes = [] while poseCnt < blockCount: poseCnt += 1 poseCnt2 = 0 reciv = received[readerCnt + 1] + received[readerCnt + 2] readerCnt += 2 nameLength = float(struct.unpack('H', reciv)[0]) nameCnt = 0 name = "" while nameCnt < nameLength: nameCnt += 1 name += received[readerCnt + 1] readerCnt += 1 #print name if self.workerThread.TestBreak(): self.sock.close() return 0 reciv = received[readerCnt + 1] + received[readerCnt + 2] readerCnt += 2 blockCount = struct.unpack('H', reciv)[0] #print "POSES = "+str(blockCount) poseCnt = 0 while poseCnt < blockCount: poseCnt += 1 poseCnt2 = 0 shape = [] reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 length = float(struct.unpack('I', reciv)[0]) #print length while poseCnt2 < (len(points)): poseCnt2 += 1 reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 posepointX = float(struct.unpack('f', reciv)[0]) reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 posepointY = float(struct.unpack('f', reciv)[0]) reciv = received[readerCnt + 1] + received[ readerCnt + 2] + received[readerCnt + 3] + received[readerCnt + 4] readerCnt += 4 posepointZ = float(struct.unpack('f', reciv)[0]) shape.append( c4d.Vector(posepointX, posepointY, posepointZ * -1)) shapes.append(shape) #print "quads = "+str(quads) #print "quads = "+str(len(quads)) #print "tris = "+str(tris) #print "tris = "+str(len(tris)) #print "points = "+str(points) #print "points = "+str(len(points)) #print "shapes = "+str(shapes) #print "shapes = "+str(len(shapes)) if self.workerThread.TestBreak(): self.sock.close() return 0 mypoly = c4d.BaseObject(c4d.Opolygon) #Create an empty polygon object mypoly.ResizeObject( len(points), len(quads) + len(tris)) #New number of points, New number of polygons mypoly.SetAllPoints(points) quadCnt = 0 quadCnt2 = 0 while quadCnt < len(quads): mypoly.SetPolygon( quadCnt2, c4d.CPolygon(quads[quadCnt], quads[quadCnt + 1], quads[quadCnt + 2], quads[quadCnt + 3])) #The Polygon's index, Polygon's points quadCnt2 += 1 quadCnt += 4 if self.workerThread.TestBreak(): self.sock.close() return 0 quadCnt = 0 while quadCnt < len(tris): mypoly.SetPolygon( quadCnt2, c4d.CPolygon(quads[quadCnt], quads[quadCnt + 1], quads[quadCnt + 2], quads[quadCnt + 2])) #The Polygon's index, Polygon's points quadCnt2 += 1 quadCnt += 3 if self.workerThread.TestBreak(): self.sock.close() return 0 self.exchangeData.newRiggPoses = [] shapeCnt = 0 while shapeCnt < len(shapes): newPoly = mypoly.GetClone() #print shapes[shapeCnt] newPoly.SetAllPoints(shapes[shapeCnt]) self.exchangeData.newRiggPoses.append(newPoly) shapeCnt += 1 if self.workerThread.TestBreak(): self.sock.close() return 0 self.exchangeData.newRigg = mypoly c4d.SpecialEventAdd(ids.EVT_NEWRIGG)
def main(): thisobj = doc.SearchObject("mafgrapher") try: textag = thisobj.GetTag(c4d.TextureTag) except: pass #stuff to read from Userdata l = thisobj[c4d.ID_USERDATA,2] h = thisobj[c4d.ID_USERDATA,3] A = thisobj[c4d.ID_USERDATA,5] func = thisobj[c4d.ID_USERDATA,8] maxval = A #frame nu frameno = 0 scalefactor = thisobj[c4d.ID_USERDATA,6] phase = 0 panx = thisobj[c4d.ID_USERDATA,13] pany = thisobj[c4d.ID_USERDATA,14] lSegments = int(thisobj[c4d.ID_USERDATA,10]) hSegments = int(thisobj[c4d.ID_USERDATA,11]) #point spacing lSpace = l /(lSegments - 1) hSpace = h /(hSegments - 1) #midpoint lhalfSegment = lSegments * 0.5 points = [] for z in xrange(hSegments): for x in xrange (lSegments): p = c4d.Vector(0.0) p.x = lSpace * ( x) p.z = hSpace * z try: p.y = A*currentfunc(p.x*scalefactor,p.z*scalefactor,panx,pany,func) #point position setting shit happens here if(p.y > maxval): maxval = p.y else: pass except: p.y = 1/A points.append(p) pol = c4d.BaseObject(c4d.Opolygon) ptCnt = lSegments * hSegments polyCnt = (lSegments - 1 )* (hSegments - 1) #tag material stretching try: TextureScaler(thisobj,maxval/1.9) except: pass pol.ResizeObject(ptCnt, polyCnt) pol.SetAllPoints(points) polyIndex = 0 for x in xrange(lSegments - 1): for y in xrange (hSegments - 1): xmove = x + y * lSegments ymove = x + y * lSegments + lSegments cpol = c4d.CPolygon( xmove ,ymove ,ymove + 1 ,xmove + 1 ) pol.SetPolygon(polyIndex , cpol) polyIndex += 1 # Updates the polygon object pol.Message(c4d.MSG_UPDATE) # Done return pol
def convert_primitive(self, prim, gltf, materials): # Helper functions def float2bytes(f): int_value = int(math.fabs(f * 32000.0)) high_byte = int(int_value / 256) low_byte = int_value - 256 * high_byte if f < 0: high_byte = 255 - high_byte low_byte = 255 - low_byte return (low_byte, high_byte) # Normals tag. (Contains 12 WORDs per polygon, enumerated like the following: ax,ay,az,bx,by,bz,cx,cy,cz,dx,dy,dz. # The value is the Real value of the normal vector component multiplied by 32000.0.) def set_normals(normal_tag, polygon, normal_a, normal_b, normal_c, normal_d): normal_list = [normal_a, normal_b, normal_c, normal_d] normal_buffer = normal_tag.GetLowlevelDataAddressW() vector_size = 6 component_size = 2 for v in range(4): normal = normal_list[v] component = [normal.x, normal.y, normal.z] for c in range(3): low_byte, high_byte = float2bytes(component[c]) normal_buffer[normal_tag.GetDataSize() * polygon + v * vector_size + c * component_size + 0] = chr(low_byte) normal_buffer[normal_tag.GetDataSize() * polygon + v * vector_size + c * component_size + 1] = chr(high_byte) def parse_normals(): normal = [] if 'NORMAL' in prim.attributes: normal = BinaryData.get_data_from_accessor( gltf, prim.attributes['NORMAL']) if normal: normaltag = c4d.NormalTag(nb_poly) for polyidx in range(nb_poly): poly = c4d_mesh.GetPolygon(polyidx) normal_a = self.switch_handedness_v3( self.list_to_vec3(normal[poly.a])) normal_b = self.switch_handedness_v3( self.list_to_vec3(normal[poly.b])) normal_c = self.switch_handedness_v3( self.list_to_vec3(normal[poly.c])) normal_d = c4d.Vector(0.0, 0.0, 0.0) set_normals(normaltag, polyidx, normal_a, normal_b, normal_c, normal_d) c4d_mesh.InsertTag(normaltag) # A Phong tag is needed to make C4D use the Normal Tag (seems to be done for Collada) phong = c4d.BaseTag(5612) c4d_mesh.InsertTag(phong) def parse_texcoords(index, c4d_mesh): texcoord_key = 'TEXCOORD_{}'.format(index) if texcoord_key in prim.attributes: uvs = BinaryData.get_data_from_accessor( gltf, prim.attributes[texcoord_key]) if uvs: uvtag = c4d.UVWTag(nb_poly) uvtag.SetName(texcoord_key) for i in range(0, nb_poly): poly = c4d_mesh.GetPolygon(i) aa = (uvs[poly.a][0], uvs[poly.a][1], 0.0) bb = (uvs[poly.b][0], uvs[poly.b][1], 0.0) cc = (uvs[poly.c][0], uvs[poly.c][1], 0.0) uvtag.SetSlow(i, aa, bb, cc, (0.0, 0.0, 0.0)) c4d_mesh.InsertTag(uvtag) 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 parse_tangents(): tangent = [] if 'TANGENT' in prim.attributes: tangent = BinaryData.get_data_from_accessor( gltf, prim.attributes['TANGENT']) if tangent: tangentTag = c4d.TangentTag(nb_poly) for polyidx in range(0, nb_poly): poly = c4d_mesh.GetPolygon(polyidx) normal_a = self.switch_handedness_v3( self.list_to_vec3(tangent[poly.a])) normal_b = self.switch_handedness_v3( self.list_to_vec3(tangent[poly.b])) normal_c = self.switch_handedness_v3( self.list_to_vec3(tangent[poly.c])) normal_d = c4d.Vector(0.0, 0.0, 0.0) set_normals(tangentTag, polyidx, normal_a, normal_b, normal_c, normal_d) c4d_mesh.InsertTag(tangentTag) vertex = BinaryData.get_data_from_accessor(gltf, prim.attributes['POSITION']) nb_vertices = len(vertex) # Vertices are stored under the form # [(1.0, 0.0, 0.0), (0.0, 0.0, 0.0) ...] verts = [] for i in range(len(vertex)): vect = c4d.Vector(vertex[i][0], vertex[i][1], vertex[i][2]) verts.append(self.switch_handedness_v3(vect)) indices = BinaryData.get_data_from_accessor(gltf, prim.indices) nb_poly = len(indices) / 3 c4d_mesh = c4d.PolygonObject(nb_vertices, nb_poly) c4d_mesh.SetAllPoints(verts) # Indices are stored like [(0,), (1,), (2,)] current_poly = 0 for i in range(0, len(indices), 3): poly = c4d.CPolygon( indices[i + 2][0], indices[i + 1][0], indices[i][0]) # indice list is like [(0,), (1,), (2,)] c4d_mesh.SetPolygon(current_poly, poly) current_poly += 1 parse_normals() # TANGENTS (Commented for now, "Tag not in sync" error popup in c4d) # parse_tangents() for texcoord_index in range(10): parse_texcoords(texcoord_index, c4d_mesh) mat = materials[prim.material] # Only parse COLORS_0 colortag = parse_vertex_colors(0, c4d_mesh) self.make_vertex_colors_layer(mat, colortag) # Enable vertex colors for material if not gltf.data.materials[prim.material].double_sided: mat.SetParameter(c4d.TEXTURETAG_SIDE, c4d.SIDE_FRONT, c4d.DESCFLAGS_SET_NONE) mattag = c4d.TextureTag() mattag.SetParameter(c4d.TEXTURETAG_MATERIAL, mat, c4d.DESCFLAGS_SET_NONE) mattag.SetParameter(c4d.TEXTURETAG_PROJECTION, c4d.TEXTURETAG_PROJECTION_UVW, c4d.DESCFLAGS_GET_NONE) c4d_mesh.InsertTag(mattag) c4d_mesh.SetDirty(c4d.DIRTYFLAGS_ALL) return c4d_mesh
def main(): class polys: def __init__(self, ind, polygon): self.Id = ind self.polyst = polygon self.triangle = self.polyst.IsTriangle() obj = doc.GetActiveObject() all_polys = obj.GetAllPolygons() select = obj.GetPolygonS().GetAll(obj.GetPolygonCount()) select_list = [] for i in range(obj.GetPolygonCount()): ### make select polys list a = polys(i, all_polys[i]) if select[i] == 1: select_list.append(a) for i in range(len(select_list)): ### print select polys print select_list[i].Id, " ", select_list[i].polyst new_list = [] for i in range(len(select_list)): polycount = obj.GetPolygonCount() pointcount = obj.GetPointCount() if select_list[i].triangle == 0: n = [] n.append(obj.GetPoint(select_list[i].polyst.a)) n.append(obj.GetPoint(select_list[i].polyst.b)) n.append(obj.GetPoint(select_list[i].polyst.c)) n.append(obj.GetPoint(select_list[i].polyst.d)) obj.ResizeObject(pointcount + 4, polycount + 1) index = [] for k in range(len(n)): obj.SetPoint(pointcount + k, n[k]) index.append(pointcount + k) poly = c4d.CPolygon(index[0], index[1], index[2], index[3]) obj.SetPolygon(polycount, poly) obj.Message(c4d.MSG_UPDATE) else: n = [] n.append(obj.GetPoint(select_list[i].polyst.a)) n.append(obj.GetPoint(select_list[i].polyst.b)) n.append(obj.GetPoint(select_list[i].polyst.c)) obj.ResizeObject(pointcount + 3, polycount + 1) index = [] for k in range(len(n)): obj.SetPoint(pointcount + k, n[k]) index.append(pointcount + k) poly = c4d.CPolygon(index[0], index[1], index[2]) obj.SetPolygon(polycount, poly) obj.Message(c4d.MSG_UPDATE) new_list.append(polycount) print new_list select = obj.GetPolygonS() select.DeselectAll() for i in range(len(new_list)): select.Select(new_list[i]) settings = c4d.BaseContainer() settings[c4d.MDATA_OPTIMIZE_TOLERANCE] = 0.01 settings[c4d.MDATA_OPTIMIZE_POINTS] = True settings[c4d.MDATA_OPTIMIZE_POLYGONS] = True settings[c4d.MDATA_OPTIMIZE_UNUSEDPOINTS] = True utils.SendModelingCommand(command=c4d.MCOMMAND_OPTIMIZE, list=[op], mode=c4d.MODELINGCOMMANDMODE_POLYGONSELECTION, bc=settings, doc=doc)