def UpdateMaterial(): global Polygon global shader _mat = doc.SearchMaterial("OBJ Sequence Material") _tags = [t for t in Polygon.GetTags() if t.GetType() == c4d.Ttexture] _tag = None for t in _tags: if t.GetName() == "OBJ Sequence Texture Tag": _tag = t break if not _mat: _mat = c4d.Material() _mat.SetName("OBJ Sequence Material") _mat.SetChannelState(c4d.CHANNEL_COLOR, False) _mat.SetChannelState(c4d.CHANNEL_LUMINANCE, True) _mat[c4d.MATERIAL_LUMINANCE_SHADER] = shader _mat.InsertShader(shader) doc.InsertMaterial(_mat) if not _tag: _tag = c4d.TextureTag() _tag.SetMaterial(_mat) _tag.SetName("OBJ Sequence Texture Tag") Polygon.InsertTag(_tag)
def checkName(obj, matList): objName = obj.GetName() matNames = [] for mat in matList: mName = mat.GetName() matNames.append(mName) if objName in matNames: # print "Lets apply some tags!" tags = obj.GetTags() for tag in tags: Tagtype = tag.GetTypeName() if Tagtype == "UVW": print "found a UVW tag" # check if there is already a material with that name # if not then apply the material # check if an ojbect has a UVW tag # if the object has a UVW tag -> aplly UVW projection # else apply Cubic projection texTag = c4d.TextureTag() texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) else: print "Texture Not Found!"
def ApplyMaterial(obj, material, tagName, overwrite=False): """ Attaches a C4D material to a C4D object, by creating a new TextureTag. Args: obj: The object to attach the material to. material: The material to attach to the object. [optional] tagName (str): The name of the TextureTag. [optional] overwrite: If a textureTag is already present, should this function add a new textureTag, or use the existing one? Returns: True on success """ if obj==None: return False if material==None: return False if tagName==None: return False if overwrite == True: tags = obj.GetTags() for t in tags: if t.GetType()== c4d.Ttexture: t.SetMaterial(material) return t _tag = c4d.TextureTag() _tag.SetMaterial(material) _tag.SetName(tagName) obj.InsertTag(_tag) return _tag
def addTexTag(obj, layer, mat): textag = c4d.TextureTag() textag.SetMaterial(mat) textag[c4d.ID_BASELIST_NAME] = mat[c4d.ID_BASELIST_NAME] textag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW textag[c4d.ID_LAYER_LINK] = layer obj.InsertTag(textag) return textag
def importVox(path): #parse file with open(path, 'rb') as f: #check filetype bytes = f.read(4) file_id = struct.unpack(">4s", bytes) if file_id[0] == 'VOX ': #init material list matlist = [] BaseCube = c4d.BaseObject(c4d.Ocube) BaseCube[c4d.PRIM_CUBE_DOFILLET] = True BaseCube[c4d.PRIM_CUBE_FRAD] = 8 BaseCube[c4d.PRIM_CUBE_SUBF] = 1 doc.InsertObject(BaseCube) #skip header f.seek(56) #read number of voxels, stuct.unpack parses binary data to variables bytes = f.read(4) numvoxels = struct.unpack('<I', bytes) #iterate through voxels for x in range(0, numvoxels[0]): #read voxels, ( each voxel : 1 byte x 4 : x, y, z, colorIndex ) x numVoxels bytes = f.read(4) voxel = struct.unpack('<bbbB', bytes) #generate Cube and set position, change to 'Oinstance' for instances MyCube = c4d.BaseObject(c4d.Oinstance) MyCube[c4d.INSTANCEOBJECT_RENDERINSTANCE] = True MyCube[c4d.INSTANCEOBJECT_LINK] = BaseCube MyCube.SetAbsPos( c4d.Vector(-voxel[1] * 200, voxel[2] * 200, voxel[0] * 200)) #update material list, generate new material only if it isn't in the list yet matid = voxel[3] if matid not in matlist: matlist.append(matid) myMat = c4d.BaseMaterial(c4d.Mmaterial) myMat.SetName(str(matid)) myMat[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector( random.random(), random.random(), random.random()) doc.InsertMaterial(myMat) #assign material to voxel and insert everything into the scene mat = doc.SearchMaterial(str(matid)) textag = c4d.TextureTag() textag.SetMaterial(mat) MyCube.InsertTag(textag) doc.InsertObject(MyCube) else: gui.MessageDialog('Not a .VOX file')
def addTexTag(obj, layer, mat, projection, selection): textag = c4d.TextureTag() textag.SetMaterial(mat) textag[c4d.ID_BASELIST_NAME] = mat[c4d.ID_BASELIST_NAME] textag[c4d.TEXTURETAG_PROJECTION] = projection textag[c4d.TEXTURETAG_RESTRICTION] = selection textag[c4d.ID_LAYER_LINK] = layer obj.InsertTag(textag) return textag
def main(): ext = '.json' path = f'/Users/donzeo/SWITCHdrive/PYTHON/Requests_ESRI_REST_API/cartes/*{ext}' res = c4d.BaseObject(c4d.Onull) res.SetName('CARTES') origin = doc[CONTAINER_ORIGIN] for fn in sorted(glob(path)): with open(fn) as f: name = os.path.basename(fn)[:-len(ext)] fn_img = fn.replace(ext, '.png') mat = creer_mat(fn_img, name) doc.InsertMaterial(mat) data = json.load(f) xmax = data['extent']['xmax'] xmin = data['extent']['xmin'] ymax = data['extent']['ymax'] ymin = data['extent']['ymin'] centre = c4d.Vector((xmax + xmin) / 2, 0, (ymin + ymax) / 2) if not origin: doc[CONTAINER_ORIGIN] = centre origin = centre larg = xmax - xmin haut = ymax - ymin plan = c4d.BaseObject(c4d.Oplane) plan[c4d.PRIM_PLANE_WIDTH] = larg plan[c4d.PRIM_PLANE_HEIGHT] = haut plan[c4d.PRIM_PLANE_SUBW] = 0 plan[c4d.PRIM_PLANE_SUBH] = 0 plan.SetAbsPos(centre - origin) plan.SetName(name) plan.InsertUnderLast(res) tag = c4d.TextureTag() tag.SetMaterial(mat) tag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW plan.InsertTag(tag) doc.InsertObject(res) c4d.EventAdd()
def checkName(obj, matList): objName = obj.GetName() matNames = [] texTag = c4d.TextureTag() for mat in matList: mName = mat.GetName() matNames.append(mName) # check if the object has a matching material if objName in matNames: # collect the tags from the object in a list tags = obj.GetTags() # print tags # if the object has a tex tag and the names match - DO NOTING if obj.GetTag(c4d.Ttexture).GetMaterial().GetName() == objName: print "This object already has a material applied, matching the object name." # if the object has a tex tag and NOT a UVW tag, AND the names DON'T match - APPLY TAG AFTER CURRENT TAG with CUBIC Projection elif obj.GetTag(c4d.Ttexture) and not obj.GetTag( c4d.Tuvw) in tags and not obj.GetTag().GetMaterial().GetName( ) == objName: print "We have already applied a TEXTURE tag here." # if the object has a tex tag and a UVW tag and the names elif obj.GetTag(c4d.Ttexture) and obj.GetTag( c4d.Tuvw ) in tags and not obj.GetTag().GetMaterial().GetName() == objName: print "We have alreaqdy applied both a TEXTURE and UVW tag here." elif obj.GetTag(c4d.Tuvw) and not obj.GetTag(c4d.Ttexture) in tags: print "We have only a UVW tag here" texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) else: print "TEXTURE tag with name matching the object's has been applied!" texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag) else: print "Texture Not Found!"
def placeTag(obj, objName, matList, matNames, proj): texTag = c4d.TextureTag() setMAT = matList[matNames.index(objName)] if proj == 'cubic': texTag.SetMaterial(setMAT) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # move existing material tags to last tag moveExistingTags(obj) elif proj == 'uvw': texTag.SetMaterial(setMAT) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # move existing material tags to last tag moveExistingTags(obj)
def checkName(obj, matList): objName = obj.GetName() matNames = [] texTag = c4d.TextureTag() for mat in matList: mName = mat.GetName() matNames.append(mName) # check if the object has a matching material if objName in matNames: # collect the tags from the object in a list tags = obj.GetTags() # print tags # this is WRONG.. we're checking every tag # looping through each objects set of tags, checking them one by one for tag in tags: # set up a variable for the typename Tagtype = tag.GetTypeName() # check if there is already a material with that name if Tagtype == "Texture" and tag.GetMaterial().GetName() == objName: print "Texture with correct name already applied!" elif Tagtype == "UVW": # check if an ojbect has a UVW tag # if the object has a UVW tag -> apply UVW projection texTag.SetMaterial(matList[matNames.index(objName)]) texTag[ c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) else: # if not then apply the material with Cubic projection texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d. TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag) else: print "Texture Not Found!"
def checkName(obj, matList): objName = obj.GetName() matNames = [] texTag = c4d.TextureTag() for mat in matList: mName = mat.GetName() matNames.append(mName) # collect the tags from the object in a list tags = obj.GetTags() # check if the object has a matching material try: if objName in matNames: placeTags(obj, tags, objName, texTag, matList, matNames) else: print "Texture Not Found!" except: raise StopIteration
def checkName(obj, matNames): objName = obj.GetName() # print objName # compare each object with the list of materials if objName in matNames: print "Lets apply some tags!" # check if there is already a material with that name # if not then apply the material # check if an ojbect has a UVW tag # if the object has a UVW tag -> aplly UVW projection # else apply Cubic projection mat = matNames.index(objName) texTag = c4d.TextureTag() texTag.SetMaterial(mat) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) else: print "Texture Not Found!"
def checkName(obj, matList): objName = obj.GetName() matNames = [] texTag = c4d.TextureTag() for mat in matList: mName = mat.GetName() matNames.append(mName) # check if the object has a matching material if objName in matNames: # collect the tags from the object in a list tags = obj.GetTags() try: # if the object has a tex tag and the names match - DO NOTING if obj.GetTag(c4d.Ttexture) in tags and obj.GetTag(c4d.Ttexture).GetMaterial().GetName() == objName: print "This object already has a material applied, matching the object name." # IF TEHRE ARE TWO STACKED MATERIALS ONE OF WHICH MATCH THE NAME THEN IGNORE # if the object has a TEX tag and a UVW tag, BUT the names DON'T match: elif obj.GetTag(c4d.Ttexture) and obj.GetTag(c4d.Tuvw) in tags and obj.GetTag(c4d.Ttexture).GetMaterial().GetName() != objName: print "We have already applied both a TEXTURE and UVW tag here." # APPLY TAG AFTER CURRENT TAG with UVW Projection texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag, obj.GetTag(c4d.Ttexture)) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # if the object has ONLY a TEX tag, AND the names DON'T match elif obj.GetTag(c4d.Ttexture) and not obj.GetTag(c4d.Tuvw) in tags and obj.GetTag(c4d.Ttexture).GetMaterial().GetName() != objName: print "We have alreaqdy applied TEXTURE tag here." # APPLY TEX tag AFTER CURRENT tag with CUBIC Projection texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag, obj.GetTag(c4d.Ttexture)) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # if the object has ONLY a UVW tag AND the names DON'T match elif obj.GetTag(c4d.Tuvw) and not obj.GetTag(c4d.Ttexture) in tags: print "We have only a UVW tag here" # APPLY TEX tag with UVW projectoin texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) else: # if the object has NONE tags then apply print "TEXTURE tag with name matching the object's has been applied!" texTag.SetMaterial(matList[matNames.index(objName)]) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) except: raise StopIteration else: print "Texture Not Found!"
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 placeTags(obj, objName, matList, matNames): tags = obj.GetTags() texTag = c4d.TextureTag() objTEX = obj.GetTag(c4d.Ttexture) objUVW = obj.GetTag(c4d.Tuvw) setMAT = matList[matNames.index(objName)] # this function is called on every object that with matching material # the tags list has been created in the checkName function # if the object has a tex tag and the names match - DO NOTING if objTEX in tags and objTEX.GetMaterial().GetName() == objName: print "This object already has a material applied, matching the object name." # IF TEHRE ARE TWO STACKED MATERIALS ONE OF WHICH MATCH THE NAME THEN IGNORE elif objTEX in tags and objTEX.GetMaterial().GetName() != objName and objTEX.GetMaterial().GetName() == objName: print "This object seems to already have two TEX tags applied." # if the object has a TEX tag and a UVW tag, BUT the names DON'T match: elif objTEX and objUVW in tags and objTEX.GetMaterial().GetName() != objName: # if objTEX.GetMaterial().GetName() == objName: # print "We have already applied both a TEXTURE and UVW tag here." # break print "We found a UVW tag and have STACKED a TEXTURE." # APPLY TAG AFTER CURRENT TAG with UVW Projection texTag.SetMaterial(setMAT) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag, objTEX) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # if the object has ONLY a TEX tag, AND the names DON'T match elif objTEX and not objUVW in tags and objTEX.GetMaterial().GetName() != objName: # if tag.GetMaterial().GetName() == objName: # print "We have already applied TEXTURE tag here." # break print "We have STACKED a TEXTURE tag here." # APPLY TEX tag AFTER CURRENT tag with CUBIC Projection texTag.SetMaterial(setMAT) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag, objTEX) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # if the object has ONLY a UVW tag AND the names DON'T match elif objUVW and not objTEX in tags: print "We have only a UVW tag here. A TEXTURE tag has been applied!" # APPLY TEX tag with UVW projectoin texTag.SetMaterial(setMAT) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW obj.InsertTag(texTag) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # move existing material tags to last tag moveExistingTags(obj) else: # if the object has NONE tags then apply print "TEXTURE tag with name matching the object's has been applied!" texTag.SetMaterial(setMAT) texTag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_CUBIC obj.InsertTag(texTag) doc.AddUndo(c4d.UNDOTYPE_NEW, texTag) # move existing material tags to last tag moveExistingTags(obj)