Message 1 of 3
material texture path change Python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
hello,
i am trying to copy all texture pictures, used in project, to one folder and than change the path to this folder.
everything works fine until changing the path.
I read block: https://thebuildingcoder.typepad.com/blog/2019/04/set-material-texture-path-in-editscope.html but I just starting to learn Python and I can't read much from C#
import clr
import rpw
import sys
import os
import ntpath
from shutil import copy
clr.AddReference("RevitApi")
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import TaskDialog
from rpw.ui.forms import Console
doc = __revit__.ActiveUIDocument.Document
#Save file:
try:
folder = rpw.ui.forms.select_folder()
filePath = folder + '\\'+ doc.Title + '.rvt'
opt = SaveAsOptions()
opt.OverwriteExistingFile = True
#doc.SaveAs(filePath, opt)
except:
sys.exit()
#name of file from path
def fileName(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def newfolder(name, folder):
name = folder+"\\"+name
newfolder.path = name
if not os.path.exists(name):
os.makedirs(name)
#Create New Folder
newfolder("Materials",folder)
#select material
materials = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Materials).\
WhereElementIsNotElementType().ToElements()
for material in materials:
appearanceAssetId = material.AppearanceAssetId
assetElem = doc.GetElement(appearanceAssetId)
try:
with Transaction(doc, "Change jpeg path") as t:
t.Start()
with Visual.AppearanceAssetEditScope(assetElem.Document) as editScope:
editableAsset = editScope.Start(assetElem.Id)
assetProperty = editableAsset.FindByName("generic_diffuse")
connectedAsset = assetProperty.GetConnectedProperty(0)
if connectedAsset.Name == "UnifiedBitmapSchema":
assetPath = connectedAsset["unifiedbitmap_Bitmap"]
assetPathValue = assetPath.Value
#Copy material to new folder
copy(assetPathValue,newfolder.path)
texturePath = "{}\\{}"
texturePathFormat = texturePath.format(folder,fileName(assetPathValue))
#NOT WORKING
assetPathValue = texturePathFormat
editScope.Commit(True)
t.Commit()
except:
print("passing")
pass2.) Can I make this path relative to my new creating folder? for example: "Materials\somematerialpng.png" ?