While recently testing out Ziva, I ran into the problem of running out of memory during a (long) simulation/ cache, so after searching the forum I found this thread and it pointed me in the proper direction!
I'm still new to Ziva and donβt have tons of time to dedicate to it, and I'm sure there are already better ways of doing this, but I figured I'd post this here just in case someone else just starting out might find it useful.
After setting up the file and getting ready to do the full sim, I modified the wrapper in the thread to cache out each frame to disk. By default, itβll look at the playback frame range, but you can tell it what range to do. Change the path line to something your machine can see locally. You can also cancel the sim by ESC.
import maya.cmds as cmds
import maya.mel as mel
import os
def ziva_exportCache(path=os.path.normpath("E:/ziva_muscle_sims/20200929/sim2"), # change the path to something local to you...
start=cmds.playbackOptions(q=True, min=True),
end=cmds.playbackOptions(q=True, max=True)):
gMainProgressBar = mel.eval('$tmp = $gMainProgressBar')
if not isinstance(start, int): # just in case "range" complains...
start=int(start)
if not isinstance(end, int):
end=int(end)
numFrames = end-start
cmds.progressBar( gMainProgressBar,
edit=True,
beginProgress=True,
isInterruptable=True,
status='Simming',
maxValue=numFrames )
cmds.zCache(clear=True)
for i in range(start, end):
if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True ) :
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
break
cmds.progressBar(gMainProgressBar, edit=True, step=1)
frame=i
cmds.currentTime(frame)
cacheFile=os.path.normpath(os.path.join(path, 'out.%04i.zCache' % frame))
try:
cmds.zCache(save=cacheFile, ts=float(frame), te=float(frame))
print("Saved cache file for frame :: {} :: {}".format(frame, cacheFile))
except:
cmds.warning("Could not save cache for frame :: {}".format(frame))
try:
cmds.zCache(clear=True, te=float(frame-1))
except:
cmds.warning("Could not clear cache before frame :: {}".format(frame-1))
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
# ###########################
# # execute the next line to sim/run
# ziva_exportCache()
Then after the sim, I would reload (frame by frame) and export the thing as an Alembic cache via the Alembic Export Selection UI.
"""
Load in a sequential cache and export selected as Alembic (using the Alembic Export UI)
"""
import maya.cmds as cmds
import os
def perFrameAbcExport():
path = os.path.normpath("E:/ziva_muscle_sims/20200929/sim2") # !!! change the path to something local to you...
solverNode = 'zSolver1' # !!! you may need to change the solver name.
cmds.setAttr('{}.enable'.format(solverNode), 0)
cmds.zCache(clear=True)
curFrame=int(cmds.currentTime(q=True))
setSolver=0
# load 2 frames just incase the frame update happens pre/ post
for i in range( 0, 2 ):
cFile = os.path.normpath(os.path.join(path, 'out.%04i.zCache' % (curFrame+i)))
if os.path.isfile(cFile):
setSolver=1
cmds.zCache(load=cFile)
cmds.setAttr('{}.enable'.format(solverNode), setSolver)
cmds.currentTime(curFrame)
print("Frame :: {}".format(curFrame))
# turn off the solver to start...
cmds.setAttr('zSolver1.enable', 0) # !!! you may need to change the solver name.
# ###########################
# # "execute" this function as a global command before exporting...it won't "run" yet....
# # then add this command into the alembic export UI
# perFrameAbcExport()
First execute this to make it a "global" function in Maya (it won't do anything unless you specifically call it). Then using the export UI, set whatever options you want (typically just the frame range) and add the call into the PerFrameCallbackPython: perFrameAbcExport()
I also just added a safety net in the PostJobCallback (don't forget to change the name of the solver if needed!):
cmds.zCache(clear=True);cmds.setAttr('zSolver1.enable', 0);
When you hit the Export selection button in the UI, it'll bake out the geom to the specified location/ file.
Again, I'm sure there's better ways of doing this (and I'd bet most don't want/ need to export a per frame cache--it just allowed me to reEdit as/ if desired with my long animation test). But if it helps at least point someone in the right direction...great!!! (...Hopefully I copied the lines up properly!)
Oh, and if you just wanted to load in the cache files to view them here's a quick wrapper (again, change the paths and solver name to taste):
"""
Load in a sequential cache
"""
# load in some cache files
import maya.cmds as cmds
import os
def ziva_loadCache(path = os.path.normpath("E:/ziva_muscle_sims/20200929/sim2"), # change the path to something local to you...
start=int(cmds.playbackOptions(q=True, min=True)),
end=int(cmds.playbackOptions(q=True, max=True))):
solverNode = 'zSolver1' # !!! you may need to change the solver name.
cmds.setAttr('{}.enable'.format(solverNode), 0)
for i in range(start, end):
cFile = os.path.normpath(os.path.join(path, 'out.%04i.zCache' % (i)))
if os.path.isfile(cFile):
cmds.zCache(load=cFile)
print("Loading zCache frame :: {} :: {}".format(i, cFile))
cmds.currentTime(i)
cmds.setAttr('{}.enable'.format(solverNode), 1)
print("Done loading ziva caches files")
# ###########################
# # execute the next line to run
# ziva_loadCache