Thursday, April 17, 2014

Dry Ice - Some fun with maya fluids

Was having a bit of fun with Maya's Fluid dynamics. This is so much fun! (And yea, I know, everyone does Dry Ice... That still doesn't stop it from being very fun to do! :D)

fluid test from Dimitry Kachkovski on Vimeo.

Saturday, April 12, 2014

Testing the UV export

To add to my last post on Alembic, I wanted to also share how you can check if your values got exported. One of the easiest ways is to check this using Alembic's python bindings to traverse you ".abc" file. I'll post an example to show the way to do it using general Alembic python module, but there is also a slightly easier way to access the alembic files via Cask (a module provided with Alembic's examples that helps reading and modifying ".abc" files with python in a more straight forward way). You can find and download it here: http://code.google.com/p/alembic/source/browse/#hg%2Fpython%2Fexamples%2Fcask.

(NOTE - this example is for linux, but can be modified pretty easily for windows)
So lets say you have a file called test_cache.abc located in "/home/userName/alembic_cache/". In the alembic file with have a simple sphere exported, named by default as "pSphere1". The test will look like this:
import imath, alembic

archive = alembic.Abc.IArchive('/home/userName/alembic_cache/test_cache.abc')
      
top = archive.getTop()
mesh = alembic.AbcGeom.IPolyMesh(top.children[0], 'pSphereShape1')
# Should display <class 'alembic.AbcGeom.IPolyMesh'>
print type(mesh)

schema = mesh.getSchema()
# Should display <class 'alembic.AbcGeom.IPolyMeshSchema'>
print type(schema)

arb_params = schema.getArbGeomParams()
# This should show the extra UVs you exported, and, if you did export the
# Vertex colors, those too.
print arb_params.getNumProperties()

# Assuming you didn't export any colors, and only one extra map:
uv_header = arb_params.getPropertyHeader(0)

# Lets make sure that it is indeed the right property, so print the name.
# Should be the same as the UV map in our file (Note, the one that WASN'T the
# active one at the time of export!)
print uv_header.getName()

v2f_param = alembic.AbcGeom.IV2fGeomParam(arb_params, uv_header.getName())

# Lets make sure that what we got back is indeed what we want! Should be "True"
print alembic.AbcGeom.IV2fGeomParam.matches(uv_header)

# Now we get the uv sample that containes the values for the UVs and their
# indices.
uv_sample = v2f_param.getIndexedValue(0)
# Should get <class 'alembic.AbcGeom.IV2fGeomParamSample'>
print type(uv_sample)

uv_values = uv_sample.getVals()
uv_indices = uv_sample.getIndices()

# Lets make sure that what we got back is what it should be.
# Check the type of uv_values: should be <class 'imath.V2fArray'>
print type(uv_values)
# ...and indices: should be <class 'imath.UnsignedIntArray'>
print type(uv_indices)

# Now lets check the values: should get something like V2f(0.432955, 0.681514)
# Note, the values will obviously be different from mine here...
print uv_values[0]
# ...and indices: should just be some integer like 3
print uv_indices[0]
This should get you going and help you test what you exported and see if it worked.

Hope that helps,

Cheers,

DK

Reaping the fruits of "Alembic" proportions

Many have heard about Alembic. It is now becoming the #1 used format for geometry data interchange in the VFX/Animation/Games communities. Created by the joined effort of Sony Imageworks and Industrial Light and Magic, all of us in the industry got a fantastic tool.

In my recent endeavors I had to work on modifying Maya's Alembic Exporter to support multiple UV sets exporting. I will try to document my whole experience here so not to loose the info I learned, and also, perhaps, assist someone later in achieving the same thing easier, and perhaps better then I did.

In essence, the actual theory behind the idea is not overly difficult, and Alembic supports such a possibility pretty straight forward. So, theoretically, I knew exactly what I was to do, or specifically:

  1. Parse if the object contains more then one UV set.
  2. Get those UVs
  3. Convert the data to a format that alembic can read and store.
  4. Store it.
  5. DONE!
Looking around the net for information, I stumbled onto a pretty large topic on the google alembic-discussion board:
From there I gathered some more detailed info on what needed to happen such as:
  • UVs are stored as V2fGeomParam with a scope of varying or facevarying.
  • The original UVs, the default ones, are stored within the PolyMeshSchema of the mesh we are exporting through it's setUVs function, which is able to store only a single V2fGeomParamSample.
  • Any additional Params need to be stored within the arbGeomParams of the PolyMeshSchema.
The biggest challenge I was going to face, as I learned quite quickly, was dealing with Alembic API. For such a popular tool, I have to say, the documentation of it is superbly limited. Having gone through the official documentation on http://code.google.com/p/alembic/, I realized that it was pretty much all the information they had... The rest had to be derived almost entirely from their example code (that had nearly no doc strings) and the actual header files (yea, I built the Doxygen, which, without docstrings, is also not the most useful tool...) .

Having been coding Python almost exclusively, digging in head-first into such a project was a bit of a shock... One really starts to look at code documentation with greater respect... So for the first 2 days I felt very much like this:

Well, I am glad to say that I did finally finish the implementation, and I will share it here. So if anyone is interested in adding the code to their own exporter, they will be able to export multiple UVs themselves. I still need to write the modifications to the AbcImporter for Maya so that the multi UVs will be read and added to the imported meshes, but for the time being this works for things like Maya->Katana pipelines.

So the main change I did was to modify the creator function of the MayaMeshWriter. There are two parts in that creator function that run for a SubD mesh export and a PolyMesh export. I will post the poly mesh one, and will specify what needs to be changed for the SubD stuff.

So you should replace this:
...
Alembic::AbcGeom::OPolyMesh obj(iParent, name.asChar(), iTimeIndex);
mPolySchema = obj.getSchema();

Alembic::AbcGeom::OV2fGeomParam::Sample uvSamp;

if ( mWriteUVs )
{
    getUVs(uvs, indices);
    if (!uvs.empty())
    {

 uvSamp.setScope( Alembic::AbcGeom::kFacevaryingScope );
 uvSamp.setVals(Alembic::AbcGeom::V2fArraySample(
     (const Imath::V2f *) &uvs.front(), uvs.size() / 2));
 if (!indices.empty())
 {
     uvSamp.setIndices(Alembic::Abc::UInt32ArraySample(
  &indices.front(), indices.size()));
 }
    }
}

Alembic::Abc::OCompoundProperty cp;
Alembic::Abc::OCompoundProperty up;
if (AttributesWriter::hasAnyAttr(lMesh, iArgs))
{
    cp = mPolySchema.getArbGeomParams();
    up = mPolySchema.getUserProperties();
}

// set the rest of the props and write to the writer node
mAttrs = AttributesWriterPtr(new AttributesWriter(cp, up, obj, lMesh,
    iTimeIndex, iArgs));

writePoly(uvSamp);
...
With this:
...
Alembic::AbcGeom::OPolyMesh obj(iParent, name.asChar(), iTimeIndex);
mPolySchema = obj.getSchema();
 
std::vector<Alembic::AbcGeom::OV2fGeomParam::Sample> uvSamps;
Alembic::AbcGeom::OV2fGeomParam::Sample defaultUV;
 
std::vector<float> uvs;
std::vector<Alembic::Util::uint32_t> indices; 
MStringArray uvSetNames;
lMesh.getUVSetNames(uvSetNames);
if ( mWriteUVs )
{
    MGlobal::displayInfo("Writing the default UVs");
    getUVs(uvs, indices);
 
    if (!uvs.empty())
    {
         defaultUV.setScope( Alembic::AbcGeom::kFacevaryingScope );
         defaultUV.setVals(Alembic::AbcGeom::V2fArraySample(
              (const Imath::V2f *) &uvs.front(), uvs.size() / 2));
         if (!indices.empty())
         {
              defaultUV.setIndices(Alembic::Abc::UInt32ArraySample(
                   &indices.front(), indices.size()));
         }
    } 
}

Alembic::Abc::OCompoundProperty cp;
Alembic::Abc::OCompoundProperty up;
if (AttributesWriter::hasAnyAttr(lMesh, iArgs))
{
        cp = mPolySchema.getArbGeomParams();
        up = mPolySchema.getUserProperties();
}

// set the rest of the props and write to the writer node
mAttrs = AttributesWriterPtr(new AttributesWriter(cp, up, obj, lMesh,
            iTimeIndex, iArgs));

writePoly(defaultUV);
        
//Write multiple UVs if present
if (uvSetNames.length() > 1)
{
 MGlobal::displayInfo("Will be exporting multiple UVs now!");
 Alembic::Abc::OCompoundProperty arbParams;
 arbParams =  mPolySchema.getArbGeomParams();
 
 MString lastUVSetName = lMesh.currentUVSetName();
 for (int i = 0; i < uvSetNames.length(); i++)
 {
  MString uvSetName = uvSetNames[i];
  
  if (uvSetName == lastUVSetName)
  {
   continue; 
  }
   
  std::string uvSetPropName = uvSetName.asChar();
 
  Alembic::AbcCoreAbstract::MetaData md;

  Alembic::AbcGeom::OV2fGeomParam uvProp(arbParams, uvSetPropName, true,
   Alembic::AbcGeom::kFacevaryingScope, 1, iTimeIndex, md);
  mUVParams.push_back(uvProp);
 }
 std::vector <Alembic::AbcGeom::OV2fGeomParam>::iterator uvIt;
 std::vector <Alembic::AbcGeom::OV2fGeomParam>::iterator uvItEnd;
      
 uvIt = mUVParams.begin();
 uvItEnd = mUVParams.end();

 for(; uvIt != uvItEnd; ++uvIt)
 {
  MString uvSetName(uvIt->getName().c_str());
  lMesh.setCurrentUVSetName(uvSetName);
  Alembic::AbcGeom::OV2fGeomParam::Sample uvSamp;    

  getUVs(uvs, indices);

  if (!uvs.empty())
  {
   uvSamp.setScope( Alembic::AbcGeom::kFacevaryingScope );
   uvSamp.setVals(Alembic::AbcGeom::V2fArraySample(
       (const Imath::V2f *) &uvs.front(), uvs.size() / 2));
   if (!indices.empty())
   {
    Alembic::Abc::UInt32ArraySample vals(&indices.front(), indices.size());
    uvSamp.setIndices(vals);
   }
  }
  uvIt->set(uvSamp);
  uvSamps.push_back(uvSamp);
 }
 lMesh.setCurrentUVSetName(lastUVSetName);
}
...
I am not sure how optimized that is, but it works. I more then welcome comments on how to improve this, btw! Now a few things to add. You need to include a definition of the mUVParams to the MayaMeshWriter.h file:
...
std::vector&ltAlembic::AbcGeom::OV2fGeomParam&gt mUVParams;
...
Now, I made a mistake here, which I learned is a big one. When you define the entry in the header, you will need to recompile most of the plugin to make sure your symbol table matches (I still need to read up more on that to know exactly what the meaning is, unfortunately, but as far as I understand, the mapping of the variables to the memory addresses gets messed up if you don't do the re-compile...). When I forgot to do that as I was testing the implementations, I got very weird memory crashes, until it hit me that perhaps my headers information wasn't being updated with the rest of the plugin.

And finally, the only thing needed to change for that code to suit it for the SubDiv part of MayaMeshWriter::MayaMeshWriter, is to replace all of the mPolySchema variables with mSubDSchema. Note the way OSubD stuff is defined in the code originally, and try to maintain that so that you don't screw up the schema writing process.

Hope that helps!

Cheers,

DK