Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Last active May 4, 2016 14:19
Show Gist options
  • Save tokejepsen/b7478884cfbb0d6d126f071d86034c5d to your computer and use it in GitHub Desktop.
Save tokejepsen/b7478884cfbb0d6d126f071d86034c5d to your computer and use it in GitHub Desktop.
Maya: Pointcache to Transforms
"""
This script will try to convert a point cache to transform animation.
Create a set called "driven" with the transforms to get animated,
and create a set called "driver" with the single pointcache.
Tolerance is for how far the sample points should be from each other.
"""
import re
import os
import pymel.core
import pymel.core as pm
import maya.mel
import maya.cmds as mc
def ctr_dist(objA, objB):
Ax, Ay, Az = objA.getPosition(space="world")
Bx, By, Bz = objB.getPosition(space="world")
return ((Ax-Bx)**2 + (Ay-By)**2 + (Az-Bz)**2)**0.5
driven = pm.PyNode('driven').members()
driver = pm.PyNode('driver').members()[0]
tolerance = 10
x = len(driven)
window = mc.window(t="Progress Bar")
mc.columnLayout()
progressControl = mc.progressBar(maxValue=x, width=300)
mc.showWindow(window)
dir_path = os.path.dirname(mc.file(sceneName=True, query=True))
dir_path = os.path.join(dir_path, 'cache')
if not os.path.exists(dir_path):
os.makedirs(dir_path)
for node in driven:
copy = pymel.core.duplicate(node)[0]
pymel.core.select(copy)
pm.polyMultiLayoutUV(lm=1, sc=1, rbf=2, fr=1, ps=0.05, l=2, psc=2, su=1,
sv=1, ou=0, ov=0)
pymel.core.select([copy, driver])
maya.mel.eval("CreateWrap;")
verts = []
points = copy.getPoints()
for p in points:
if len(verts) == 3:
break
for v in verts:
if ctr_dist(copy.vtx[points.index(p)], v) < tolerance:
break
else:
verts.append(copy.vtx[points.index(p)])
grps = []
for vert in verts:
grp = pymel.core.general.group(empty=True)
grps.append(grp)
[u, v] = vert.getUV()
pop = pymel.core.pointOnPolyConstraint(vert, grp)
for attr in pop.listAttr():
if attr.name().endswith('U0'):
attr.set(u)
if attr.name().endswith('V0'):
attr.set(v)
grp = pymel.core.general.group(empty=True)
pc = pymel.core.pointConstraint(grps[0], grps[1], grp)
ac = pymel.core.aimConstraint(grps[1], grp, worldUpObject=grps[2],
worldUpType="object")
start = pymel.core.playbackOptions(query=True, animationStartTime=True)
end = pymel.core.playbackOptions(query=True, animationEndTime=True)
pymel.core.bakeResults(grp, time=(start, end))
pymel.core.delete([pc, ac, grps, copy])
pymel.core.parent(node, grp)
pymel.core.rename(grp, node.name() + '_grp')
file_path = os.path.join(dir_path, re.sub('[^\w\-_\. ]', '_', node.name()))
file_path += '.abc'
file_path = file_path.replace('\\', '/')
cmd = '-frameRange %s %s' % (start, end)
cmd += ' -stripNamespaces'
cmd += ' -uvWrite -worldSpace -wholeFrameGeo '
cmd += '-eulerFilter '
cmd += '-writeVisibility -root %s -file "%s"' % (node.longName(),
file_path)
pymel.core.AbcExport(j=cmd)
i = driven.index(node)
progressInc = mc.progressBar(progressControl, edit=True, pr=i+1)
if (mc.window(window, exists=True)):
mc.deleteUI(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment