Attaching nParticle cache using python


#1

Hello everybody! I am working on a scripting tool to cache all simulations in a scene in a GUI tool. However, when working out the code, I am running into issues attaching a cache to my nParticle, or nCloth. The first part of code I have works, and it exports a cache to directory of my choosing. The problem I am having is getting the cache to attach via python code to nParticle in the scene. I could really use some help with this, since it has me perplexed. If there is a better way of doing this I would definitely be interested. Thanks! I have this code:

import maya.cmds as cmds
#variables
fileName_ = “cacheFile”
start_ = 1
end_ = 100
c = “cacheFile”

dir_=“C:/Users/basic_02a”
#This actually creates a cache
cacheFiles = cmds.cacheFile(f=‘shapeCache’, st=start_, et=end_, dir=dir_, fm=‘OneFilePerFrame’, doubleToFloat=1, cnd=“nParticleShape1”, sch=1, cf=‘mcx’)
#Found this in python command reference under cacheFile, it is needed?
switch = maya.mel.eval(‘createHistorySwitch(“nParticleShape1”,false)’)
#Suppose to attach cache, but doesn’t add it to where it needs to be
cacheNode = cmds.cacheFile(f=cacheFiles[0], cnd=‘nParticleShape1’, ia=’%s.inp[0]’ % switch ,attachFile=True)


#2

Hey there! I was looking for an answer to the same question. I know this is pretty late, but what I did was this:

import pymel.core as pm
ncloth_shape = pm.PyNode(‘nClothShape1’)
ncache_name = ‘ncache_01’
dir=‘C:\Users\YOURNAME\Desktop
cache’

cache_file = pm.cacheFile(cachableNode=ncloth_shape, cacheFormat=‘mcx’, format=‘OneFilePerFrame’, fileName=‘ncache_name’, directory=dir, startTime=0, endTime=100, channelName=ncloth_shape, inAttr=’%s.positions’ % ncloth_shape)
cache_node = pm.PyNode(’%sCache1’ % ncache_name)
pm.connectAttr("%s.inRange" % cache_node, “%s.playFromCache” % ncloth_shape)

Hopefully this helps someone else looking for answers!