Skip to content

Instantly share code, notes, and snippets.

@paulwinex
Created November 13, 2015 12:09
Show Gist options
  • Save paulwinex/8b53ed7543910974c10d to your computer and use it in GitHub Desktop.
Save paulwinex/8b53ed7543910974c10d to your computer and use it in GitHub Desktop.
Export geometry with custom attributes
import maya.OpenMaya as om
from imath import *
from alembic.Abc import *
from alembic.AbcGeom 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)
node = om.MFnDependencyNode(child)
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 = OXform(top, 'cube1', tsidx)
xsamp = 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 = OPolyMesh(xform, 'cube1Shape')
mesh = meshObj.getSchema()
mesh_samp = OPolyMeshSchemaSample(
p_points, p_faceIndices, p_faceCounts
)
mesh_samp.setSelfBounds(bb)
mesh.set(mesh_samp)
arb = mesh.getArbGeomParams()
# point attrib
mass = ODoubleGeomParam(arb, "mass", False, GeometryScope.kVertexScope, 1)
array = Float64TPTraits.arrayType( len(p_points) )
for i in range( len(p_points) ):
array[i] = 1.23
samp = ODoubleGeomParamSample()
samp.setVals( array )
mass.set(samp)
#vertex Attribute
area = ODoubleGeomParam(arb, "area", False, GeometryScope.kFacevaryingScope, 1)
array2 = Float64TPTraits.arrayType( len(p_faceIndices) )
for i in range( len(p_faceIndices) ):
array2[i] = 1.23
samp2 = ODoubleGeomParamSample()
samp2.setVals( array2 )
area.set(samp2)
#primitive Attribute
count = ODoubleGeomParam(arb, "count", False, GeometryScope.kUniformScope, 1)
array3 = Float64TPTraits.arrayType( len(p_faceCounts) )
for i in range( len(p_faceCounts) ):
array3[i] = 0.5
samp3 = ODoubleGeomParamSample()
samp3.setVals( array3 )
count.set(samp3)
saveSelected('c:/file8.abc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment