“”"
How can i change material pattern and color.?
“”"
you can modify this code from dynamo to clean pyRevit python
import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
def tolist(obj1):
if hasattr(obj1,"__iter__"): return obj1
else: return [obj1]
def ConvertColor(e):
return Autodesk.Revit.DB.Color(e.Red, e.Green, e.Blue)
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
version=int(app.VersionNumber)
materials = tolist(UnwrapElement(IN[0]))
renders=tolist(IN[1])
shadingColors=tolist(IN[2])
transparencies=tolist(IN[3])
surfaceForegroundPatterns=tolist(UnwrapElement(IN[4]))
surfaceForegroundPatternColors=tolist(IN[5])
surfaceBackgroundPatterns=tolist(UnwrapElement(IN[6]))
surfaceBackgroundPatternColors=tolist(IN[7])
cutForegroundPatterns=tolist(UnwrapElement(IN[8]))
cutForegroundPatternColors=tolist(IN[9])
cutBackgroundPatterns=tolist(UnwrapElement(IN[10]))
cutBackgroundPatternColors=tolist(IN[11])
tar = FillPatternTarget.Drafting
fillPattern = FilteredElementCollector(doc).OfClass(Autodesk.Revit.DB.FillPatternElement)
# "Start" the transaction
TransactionManager.Instance.EnsureInTransaction(doc)
for material,render,shadingColor,transparency,surfaceForegroundPattern,surfaceForegroundPatternColor,surfaceBackgroundPattern, surfaceBackgroundPatternColor,cutForegroundPattern,cutForegroundPatternColor,cutBackgroundPattern,cutBackgroundPatternColor in zip(materials,renders,shadingColors,transparencies,surfaceForegroundPatterns,surfaceForegroundPatternColors,surfaceBackgroundPatterns,surfaceBackgroundPatternColors,cutForegroundPatterns,cutForegroundPatternColors,cutBackgroundPatterns,cutBackgroundPatternColors):
if version < 2019 :
if render == None:
pass
else:
material.UseRenderAppearanceForShading=render
if shadingColor == None:
pass
else:
material.Color = Color(shadingColor.Red, shadingColor.Green, shadingColor.Blue)
if transparency == None:
pass
else:
material.Transparency = transparency
if surfaceForegroundPattern == None:
pass
else:
if surfaceForegroundPattern.GetType().ToString() == "System.String" :
spId=[elem for elem in fillPattern if elem.Name.lower() == surfaceForegroundPattern.lower()][0].Id
material.SurfacePatternId = spId
else:
material.SurfacePatternId = surfaceForegroundPattern.Id
if surfaceForegroundPatternColor == None:
pass
else:
material.SurfacePatternColor = Color(surfaceForegroundPatternColor.Red, surfaceForegroundPatternColor.Green, surfaceForegroundPatternColor.Blue)
if surfaceBackgroundPattern == None:
pass
else:
if surfaceBackgroundPattern.GetType().ToString() == "System.String" :
material.SurfacePatternId = FillPatternElement.GetFillPatternElementByName(doc, FillPatternTarget.Drafting,surfaceBackgroundPattern).Id
else:
material.SurfacePatternId = surfaceBackgroundPattern.Id
if surfaceBackgroundPatternColor == None:
pass
else:
material.SurfacePatternColor = Color(surfaceBackgroundPatternColor.Red, surfaceBackgroundPatternColor.Green, surfaceBackgroundPatternColor.Blue)
if cutForegroundPattern == None:
pass
else:
if cutForegroundPattern.GetType().ToString() == "System.String" :
material.CutPatternId = FillPatternElement.GetFillPatternElementByName(doc, FillPatternTarget.Drafting,cutForegroundPattern).Id
else:
material.CutPatternId = cutForegroundPattern.Id
if cutForegroundPatternColor == None:
pass
else:
material.CutPatternColor = Color(cutForegroundPatternColor.Red, cutForegroundPatternColor.Green, cutForegroundPatternColor.Blue)
if cutBackgroundPattern == None:
pass
else:
if cutBackgroundPattern.GetType().ToString() == "System.String" :
material.CutPatternId = FillPatternElement.GetFillPatternElementByName(doc, FillPatternTarget.Drafting,cutBackgroundPattern).Id
else:
material.CutPatternId = cutBackgroundPattern.Id
if cutBackgroundPatternColor == None:
pass
else:
material.CutPatternColor = Color(cutBackgroundPatternColor.Red, cutBackgroundPatternColor.Green, cutBackgroundPatternColor.Blue)
else:
if render == None:
pass
else:
material.UseRenderAppearanceForShading=render
if shadingColor == None:
pass
else:
material.Color = Color(shadingColor.Red, shadingColor.Green, shadingColor.Blue)
if transparency == None:
pass
else:
material.Transparency = transparency
if surfaceForegroundPattern == None:
pass
else:
if surfaceForegroundPattern.GetType().ToString() == "System.String" :
spId=[elem for elem in fillPattern if elem.Name.lower() == surfaceForegroundPattern.lower()][0].Id
material.SurfaceForegroundPatternId= spId
else:
material.SurfaceForegroundPatternId = surfaceForegroundPattern.Id
if surfaceForegroundPatternColor == None:
pass
else:
material.SurfaceForegroundPatternColor = Color(surfaceForegroundPatternColor.Red, surfaceForegroundPatternColor.Green, surfaceForegroundPatternColor.Blue)
if surfaceBackgroundPattern == None:
pass
else:
if surfaceBackgroundPattern.GetType().ToString() == "System.String" :
material.SurfaceBackgroundPatternId = FillPatternElement.GetFillPatternElementByName(doc, FillPatternTarget.Drafting,surfaceBackgroundPattern).Id
else:
material.SurfaceBackgroundPatternId = surfaceBackgroundPattern.Id
if surfaceBackgroundPatternColor == None:
pass
else:
material.SurfaceBackgroundPatternColor = Color(surfaceBackgroundPatternColor.Red, surfaceBackgroundPatternColor.Green, surfaceBackgroundPatternColor.Blue)
if cutForegroundPattern == None:
pass
else:
if cutForegroundPattern.GetType().ToString() == "System.String" :
material.CutForegroundPatternId = FillPatternElement.GetFillPatternElementByName(doc, FillPatternTarget.Drafting,cutForegroundPattern).Id
else:
material.CutForegroundPatternId = cutForegroundPattern.Id
if cutForegroundPatternColor == None:
pass
else:
material.CutForegroundPatternColor = Color(cutForegroundPatternColor.Red, cutForegroundPatternColor.Green, cutForegroundPatternColor.Blue)
if cutBackgroundPattern == None:
pass
else:
if cutBackgroundPattern.GetType().ToString() == "System.String" :
material.CutBackgroundPatternId = FillPatternElement.GetFillPatternElementByName(doc, FillPatternTarget.Drafting,cutBackgroundPattern).Id
else:
material.CutBackgroundPatternId = cutBackgroundPattern.Id
if cutBackgroundPatternColor == None:
pass
else:
material.CutBackgroundPatternColor = Color(cutBackgroundPatternColor.Red, cutBackgroundPatternColor.Green, cutBackgroundPatternColor.Blue)
# "End" the transaction
TransactionManager.Instance.TransactionTaskDone()
if isinstance(IN[0], list): OUT = materials
else: OUT = materials[0]
you can set your material as you like.
1 Like