In the preferences tab, you can enable a post import script. This script, will be called after every import and it would be possible for the user to do additional action, such as adding hda's, transforms, rewiring nodes, etc.
The imported nodes, as well as the type of the asset that was imported, along with its screen name, are all stored in a dictionary called: hou.session.ODAssetLibImports
Sample Script (saved as postscript.py) It simply prints the created nodes during Import.
import hou
print ("We are now within the post script")
#we really only have one key, so lets grab it
key = list(hou.session.ODAssetLibImports)[0]
#get all the nodes that were created by the import
for node in hou.session.ODAssetLibImports[key]["nodes"]:
#do whatever you want with them, create connections, etc..This is up to the user.
print ("The last import created Node:", node, "at path", node.path())
Another Sample Script. It takes a vray material and inserts a usercolor node that gets multiplied with the diffuse texture through a texture color op node.
import hou
print ("We are now within the post script")
#we really only have one key, so lets grab it
key = list(hou.session.ODAssetLibImports)[0]
#We are going to define two nodes
vrayMTL = None
colorTex = None
for node in hou.session.ODAssetLibImports[key]["nodes"]:
#do whatever you want with them, create connections, etc..This is up to the user.
if node.name() == "color":
colorTex = node
if node.name() == "vrayMtl":
vrayMTL = node
if vrayMTL and colorTex:
mat = colorTex.parent()
uc = mat.createNode("VRayNodeTexUserColor")
co = mat.createNode("VRayNodeTexAColorOp")
co.setParms({"mode":"2"})
co.setInput(0, colorTex, 0)
co.setInput(2, uc, 0)
vrayMTL.setNamedInput("diffuse", co, 0)
mat.layoutChildren()
Another Example: Lets say you import a Substance .sbsar and wanted to convert it to a redshift material.
import hou
#import the shelftools module
from OD import shelftools
print ("We are now within the post script")
#we really only have one key, so lets grab it
key = list(hou.session.ODAssetLibImports)[0]
#We are going to define two nodes
for node in hou.session.ODAssetLibImports[key]["nodes"]:
#do whatever you want with them, create connections, etc..This is up to the user.
if node.type().name() == "labs::substance_material":
#allow Editing of the HDA
node.allowEditingOfContents()
#convert material to redshift
shelftools.convertMaterial([hou.node(node.path()+"/matnet1/principledshader1")], res=1)
# adjust the paths of the image due to the op: reference
for n in hou.node(node.path()+"/matnet1/principledshader1").children():
if n.type().name() == "redshift::TextureSampler":
n.parm("tex0").set(n.parm("tex0").rawValue().replace("../../", "../../../"))
break
The same example converting to an Octane Material.
import hou
#import the shelftools module
from OD import shelftools
print ("We are now within the post script")
#we really only have one key, so lets grab it
key = list(hou.session.ODAssetLibImports)[0]
#We are going to define two nodes
for node in hou.session.ODAssetLibImports[key]["nodes"]:
#do whatever you want with them, create connections, etc..This is up to the user.
if node.type().name() == "labs::substance_material":
#allow Editing of the HDA
node.allowEditingOfContents()
#convert material to redshift
shelftools.convertMaterial([hou.node(node.path()+"/matnet1/principledshader1")], res=0)
# adjust the paths of the image due to the op: reference
for n in hou.node(node.path()+"/matnet1/principledshader1").children():
if n.type().name() == "octane::NT_TEX_IMAGE":
n.parm("A_FILENAME").set(n.parm("A_FILENAME").rawValue().replace("../../", "../../../"))
break
Another example, lets say you add a megascan asset, but want to copy the entire megascan asset folder into a $job folder and reference from there.
import os, shutil
projectMegascanFolder = "$JOB/megascans"
key = list(hou.session.ODAssetLibImports)[0]
#only operate on megascans
if hou.session.ODAssetLibImports[key]["type"]=="mega":
fullPath = ""
dirSplit = key.replace("\\\\", "/").split("/")
#find original megascan dir, based on the "downloaded" folder
index = dirSplit.index("Downloaded")
#construct containing folder
subPath = "/".join(dirSplit[:index+3])
#create projectMegascane Folder if it doesnt exist
if not os.path.exists(hou.text.expandString(projectMegascanFolder)):
os.makedirs(hou.text.expandString(projectMegascanFolder))
if not os.path.exists(hou.text.expandString(projectMegascanFolder) + "/" + subPath.split("/")[-1]):
shutil.copytree(subPath, hou.text.expandString(projectMegascanFolder) + "/" + subPath.split("/")[-1])
#change the referencing parms to the new location
hou.hscript('opchange "' + subPath + '" "' + projectMegascanFolder.replace("$", "\\$") + "/" + subPath.split("/")[-1] + '"')+ projectMegascanFolder.replace("$", "\\$") + "/" + subPath.split("/")[-1] + '"')