Created
November 14, 2015 18:37
-
-
Save paulwinex/6814446f292d7eab822a to your computer and use it in GitHub Desktop.
Full example for export Maya polygons to Houdini winth attributes and groups
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
import maya.OpenMaya as om | |
from imath import * | |
from alembic.Abc import * | |
from alembic.AbcGeom import * | |
import math | |
import alembic | |
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): | |
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) | |
# point attrib vector | |
color = OV3fGeomParam(arb, "Cd", False, GeometryScope.kVertexScope, 1) | |
array = P3fTPTraits.arrayType( len(p_points) ) | |
for i in range( len(p_points) ): | |
array[i] = V3f(1.0, 0.5, 1.0) | |
samp = OV3fGeomParamSample() | |
samp.setVals( array ) | |
color.set(samp) | |
# face set1 | |
s1 = [x for x in range(len(p_faceCounts)) if x%2] | |
arr = Int32TPTraits.arrayType(len(s1)) | |
for i in range( len(s1) ): | |
arr[i] = s1[i] | |
samp = OFaceSetSchemaSample() | |
samp.setFaces(arr) | |
faceSet = mesh.createFaceSet('my_group_1') | |
faceSetSchema = faceSet.getSchema() | |
faceSetSchema.set(samp) | |
# face set2 | |
s2 = [x for x in range(len(p_faceCounts)) if not x%2] | |
arr2 = Int32TPTraits.arrayType(len(s2)) | |
for i in range( len(s2) ): | |
arr2[i] = s2[i] | |
samp2 = OFaceSetSchemaSample() | |
samp2.setFaces(arr2) | |
faceSet2 = mesh.createFaceSet('my_group_2') | |
faceSetSchema2 = faceSet2.getSchema() | |
faceSetSchema2.set(samp2) | |
# select poly object | |
saveSelected('c:/maya_geo.abc') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi
so how did you fetch data in houdini?
I mean how did you extract intrinsic attributes from packed primitive in houdini?
did you unpack the packed primitive before extracting data?