Skip to content

Commit

Permalink
more work on output texture baking: make the node options internal set
Browse files Browse the repository at this point in the history
so that we can catch if bake textures is turned on, and bake the textures without
recooking, if the asset has already been cooked once, and then dirty the output
texture path so that the new texture is actually displayed
  • Loading branch information
juliasfx committed Aug 21, 2019
1 parent eeeab25 commit 69cbd9b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 17 deletions.
8 changes: 5 additions & 3 deletions Asset.C
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,16 @@ void
Asset::computeMaterial(
const MPlug& plug,
MDataBlock& data,
AssetNodeOptions::AccessorDataBlock &options,
bool bakeTextures,
bool &needToSyncOutputs
)
{
MStatus status;

MPlug materialsPlug = plug.child(AssetNode::outputMaterials);
size_t numElements = materialsPlug.numElements();
if(numElements == 0)
return;
if(myMaterials.size() < numElements)
{
myMaterials.reserve(numElements);
Expand All @@ -958,7 +960,7 @@ Asset::computeMaterial(
MPlug materialPlug = materialsPlug.elementByLogicalIndex(i);
MDataHandle materialHandle = data.outputValue(materialPlug);

myMaterials[i]->compute(myTime, materialPlug, data, materialHandle, options.bakeOutputTextures());
myMaterials[i]->compute(myTime, materialPlug, data, materialHandle, bakeTextures);
}
}

Expand Down Expand Up @@ -1144,7 +1146,7 @@ Asset::compute(
needToSyncOutputs);

computeMaterial(plug, data,
options,
options.bakeOutputTextures(),
needToSyncOutputs);

return stat;
Expand Down
12 changes: 6 additions & 6 deletions Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Asset {
AssetNodeOptions::AccessorDataBlock &options,
bool &needToSyncOutputs
);
void computeMaterial(
const MPlug& plug,
MDataBlock& data,
bool bakeTextures,
bool &needToSyncOutputs
);

void fillParmNameCache();
void getParmValues(
Expand Down Expand Up @@ -91,12 +97,6 @@ class Asset {
AssetNodeOptions::AccessorDataBlock &options,
bool &needToSyncOutputs
);
void computeMaterial(
const MPlug& plug,
MDataBlock& data,
AssetNodeOptions::AccessorDataBlock &options,
bool &needToSyncOutputs
);

private:
typedef std::vector<OutputObject*> OutputObjects;
Expand Down
40 changes: 40 additions & 0 deletions AssetNode.C
Original file line number Diff line number Diff line change
Expand Up @@ -1386,10 +1386,18 @@ AssetNode::setDependentsDirty(const MPlug& plugBeingDirtied,
bool isTime = plugBeingDirtied == inTime;
bool isInput = Util::isPlugBelow(plugBeingDirtied, AssetNode::input);
bool isParameter = false;
bool isTextureOpt = false;
{
MFnDependencyNode assetNodeFn(thisMObject());
MObject parmAttrObj = assetNodeFn.attribute(Util::getParmAttrPrefix(), &status);
isParameter = Util::isPlugBelow(plugBeingDirtied, parmAttrObj);

MPlug optionPlug = assetNodeFn.findPlug("bakeOutputTextures", true);
if(plugBeingDirtied == optionPlug ) {
isTextureOpt = true;
}


}
bool isMaterialPath = plugBeingDirtied == outputMaterialPath;

Expand Down Expand Up @@ -1449,6 +1457,11 @@ AssetNode::setDependentsDirty(const MPlug& plugBeingDirtied,
myNeedToMarshalInput = true;
}

// if bakeOutputTextures was toggled and the texture was rebaked
// the texture file path may have changed
if(isTextureOpt) {
affectedPlugs.append(MPlug(thisMObject(),AssetNode::outputMaterialTexturePath));
}
// Changing time or parameters will dirty the output
if(isTime || isInput || isParameter)
{
Expand Down Expand Up @@ -1788,6 +1801,33 @@ AssetNode::setInternalValueInContext(
return false;
}

// if we've turned on the bakeTextures option, we may need to actually
// bake the textures, since even if the files exist from before and the
// output file name isn't dirty, the actual texture may be out of date
// although if the fileTexture node doesn't exist at all it will
// still need syncing
MPlug optionPlug = assetNodeFn.findPlug("bakeOutputTextures", true);
if(plugBeingSet == optionPlug ) {
int bakeTexture = dataHandle.asBool();
if(bakeTexture) {
// recompute output textures
MPlug outputPlug(thisMObject(), AssetNode::output);
MDataBlock data = forceCache();

// note that we need to be careful accessing the data block here
// the new value for the plug being set has not updated the data block yet
// other data not related to that plug should be OK though
bool needToSyncOutputs;
myAsset->computeMaterial(
outputPlug,
data,
bakeTexture,
needToSyncOutputs
);

}
}

#if MAYA_API_VERSION >= 201800
return MPxTransform::setInternalValue(plugBeingSet, dataHandle);
#else
Expand Down
1 change: 1 addition & 0 deletions NodeOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class NodeOptionDefinition<Size>::Option<Index, bool>
MFnNumericData::kBoolean,
defaultValue
);
nAttr.setInternal(true);
}
};

Expand Down
41 changes: 33 additions & 8 deletions OutputMaterial.C
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
OutputMaterial::OutputMaterial(HAPI_NodeId assetId) :
myAssetId(assetId),
myNodeId(-1),
myMaterialLastCookCount(0)
myMaterialLastCookCount(0),
myBakeTexture(0)
{
}

Expand Down Expand Up @@ -58,8 +59,9 @@ OutputMaterial::compute(
&materialInfo));

if(myNodeInfo.totalCookCount > myMaterialLastCookCount
|| materialInfo.hasChanged)
|| materialInfo.hasChanged || bakeTexture != myBakeTexture)
{
myBakeTexture = bakeTexture;
std::vector<HAPI_ParmInfo> parms(myNodeInfo.parmCount);
HAPI_GetParameters(
Util::theHAPISession.get(),
Expand Down Expand Up @@ -169,13 +171,13 @@ OutputMaterial::compute(
}

int destinationFilePathSH = 0;
if(canRenderTexture)
{
MString destinationFolderPath;
MGlobal::executeCommand("workspace -expandName "
"`workspace -q -fileRuleEntry sourceImages`;",
destinationFolderPath);
MString destinationFolderPath;
MGlobal::executeCommand("workspace -expandName "
"`workspace -q -fileRuleEntry sourceImages`;",
destinationFolderPath);

if(canRenderTexture && bakeTexture)
{
// this could fail if the image planes don't exist
hapiResult = HAPI_ExtractImageToFile(
Util::theHAPISession.get(),
Expand All @@ -196,6 +198,29 @@ OutputMaterial::compute(
DISPLAY_ERROR_HAPI_STATUS_CALL();
}
}

if(hasTextureSource && !bakeTexture) {
// if baking is off but the expected texture file exists
// keep using it
hapiResult = HAPI_GetImageFilePath(
Util::theHAPISession.get(),
myNodeId,
HAPI_PNG_FORMAT_NAME,
"C A",
destinationFolderPath.asChar(),
NULL,
texturePathSHParmIndex,
&destinationFilePathSH
);

MString texturePath = Util::HAPIString(destinationFilePathSH);
MString cmd = "filetest -e \"" + texturePath + "\"";
int fileExists;
MGlobal::executeCommand(cmd, fileExists);
if(!fileExists)
destinationFilePathSH = 0;

}

if(destinationFilePathSH > 0)
{
Expand Down
1 change: 1 addition & 0 deletions OutputMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class OutputMaterial
HAPI_NodeId myNodeId;
HAPI_NodeInfo myNodeInfo;
int myMaterialLastCookCount;
bool myBakeTexture;
};

#endif

0 comments on commit 69cbd9b

Please sign in to comment.