material texture path change Python

material texture path change Python

lavicka.milan
Contributor Contributor
902 Views
2 Replies
Message 1 of 3

material texture path change Python

lavicka.milan
Contributor
Contributor

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")
        pass

2.) Can I make this path relative to my new creating folder? for example: "Materials\somematerialpng.png" ?

 

0 Likes
903 Views
2 Replies
Replies (2)
Message 2 of 3

BenoitE&A
Collaborator
Collaborator

Hi,

Yes you can make your link relative:

- to the path of your plug-in : 

System.Reflection.Assembly.GetExecutingAssembly().Location

- to the path of your document :

hostDocument.PathName

- to another path (fixed) : 

string myPath = "c:\\myPath\\";

 

 

 

 


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
Message 3 of 3

lavicka.milan
Contributor
Contributor

Hi thank you for the answer. I'll try it.

also is there a solution for my first question to rewrite the material image path. I tried simply declare variable from C# examples:

assetPathValue = texturePathFormat

but it doesn't work. 

 

0 Likes