Created
November 12, 2015 19:55
-
-
Save paulwinex/421cf838c0d2def9a55f to your computer and use it in GitHub Desktop.
Export selected poly object to .abc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymel.core import * | |
import maya.OpenMaya as om | |
from imath import * | |
from alembic.Abc import * | |
import alembic | |
import math | |
def get_mesh_data(): | |
selection = om.MSelectionList() | |
om.MGlobal.getActiveSelectionList( selection ) | |
iter = om.MItSelectionList ( selection, om.MFn.kTransform ) | |
dagPath = om.MDagPath() | |
iter.getDagPath( dagPath ) | |
dagNode = om.MFnDagNode(dagPath) | |
child = dagNode.child(0) | |
meshFn = om.MFnMesh(child) | |
# points | |
pointArray = om.MFloatPointArray() | |
meshFn.getPoints(pointArray) | |
points = [] | |
for i in range(pointArray.length()): | |
pt = pointArray[i] | |
points.append(V3f(pt.x, pt.y, pt.z)) | |
p_points = setArray( | |
P3fTPTraits, | |
*points | |
) | |
#poly | |
iterPolys = om.MItMeshPolygon( child ) | |
topologyArray = [] | |
faceCount = [] | |
while not iterPolys.isDone(): | |
#vertex order | |
verts = om.MIntArray() | |
iterPolys.getVertices( verts ) | |
for v in reversed(list(verts)): | |
topologyArray.append(v) | |
verts = om.MIntArray() | |
iterPolys.getVertices( verts ) | |
faceCount.append(verts.length()) | |
iterPolys.next() | |
p_faceIndices = setArray( | |
Int32TPTraits, | |
*topologyArray | |
) | |
# fase count | |
p_faceCounts = setArray( Int32TPTraits, *faceCount ) | |
# bounding box | |
mx = dagNode.boundingBox().max() | |
mn = dagNode.boundingBox().min() | |
bb = Box3d( V3d(mn.x, mn.y, mn.z), V3d( mx.x, mx.y, mx.z) ) | |
# transform | |
# rotate | |
tmx = om.MTransformationMatrix(dagNode.transformationMatrix()) | |
t = tmx.translation(om.MSpace.kWorld) | |
t = [t[x] for x in range(3)] | |
#rotate | |
q = tmx.rotation() | |
v = om.MVector() | |
angUtil = om.MScriptUtil() | |
angUtil.createFromDouble(0) | |
angDoub = angUtil.asDoublePtr() | |
q.getAxisAngle(v, angDoub) | |
a = om.MScriptUtil.getDouble(angDoub) | |
r = [[v[x] for x in range(3)],a] | |
#scale | |
scaleUtil = om.MScriptUtil() | |
scaleUtil.createFromList([0,0,0],3) | |
scaleVec = scaleUtil.asDoublePtr() | |
tmx.getScale(scaleVec,om.MSpace.kWorld) | |
s = [om.MScriptUtil.getDoubleArrayItem(scaleVec,i) for i in range(0,3)] | |
return p_points, p_faceIndices, p_faceCounts, bb, [t, r, s] | |
def setArray( iTPTraits, *iList ): | |
array = iTPTraits.arrayType( len( iList ) ) | |
for i in range( len( iList ) ): | |
array[i] = iList[i] | |
return array | |
def saveSelected(filename): | |
# framerange = [1,15] | |
tvec = alembic.AbcCoreAbstract.TimeVector() | |
tvec[:] = [1, 2, 3] | |
timePerCycle = 3.0 | |
numSamplesPerCycle = len(tvec) | |
tst = alembic.AbcCoreAbstract.TimeSamplingType(numSamplesPerCycle, timePerCycle) | |
ts = alembic.AbcCoreAbstract.TimeSampling(tst, tvec) | |
f= alembic.Abc.OArchive(filename) | |
top = f.getTop() | |
tsidx = top.getArchive().addTimeSampling(ts) | |
p_points, p_faceIndices, p_faceCounts, bb, tr = get_mesh_data() | |
# create the top xform | |
xform = alembic.AbcGeom.OXform(top, 'cube1', tsidx) | |
xsamp = alembic.AbcGeom.XformSample() | |
xsamp.setTranslation(V3d(*tr[0])) | |
xsamp.setRotation(V3d(*tr[1][0]), math.degrees(tr[1][1])) | |
xsamp.setScale(V3d(*tr[2])) | |
xform.getSchema().set(xsamp) | |
# the mesh shape | |
meshObj = alembic.AbcGeom.OPolyMesh(xform, 'cube1Shape') | |
mesh = meshObj.getSchema() | |
mesh_samp = alembic.AbcGeom.OPolyMeshSchemaSample( | |
p_points, p_faceIndices, p_faceCounts | |
) | |
mesh_samp.setSelfBounds(bb) | |
mesh.set(mesh_samp) | |
# WRITE CUSTOM ATTRIBUTES AND GROUPS | |
# select poly object and | |
saveSelected('c:/file1.abc') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment