MEGAN RIEL-MEHAN
  • Home
  • Science Visualization
  • Research
    • Initial findings
    • Image processing
  • Civic Tech
  • Fine Art
  • News
  • Methods and Notes
  • CV and Resume

Generating obj meshes and ambient occlusion texture maps for use in THREE.js

4/30/2018

0 Comments

 
Picture
Github Repo (with scripts and .c4d starter file)
Goal: Automate a process create meshes and ambient occlusion texture maps for THREE.js from ome.tiff files of microscopy images. More specifically, we have 6 image files (1 for each stage of the cell cycle) for each of our published cell lines.

Method:
1. use USCF chimera to convert volumentic data to .obj files. 
2. use cinema4D to create UVs and texture maps. We started with a process developed by Graham Johnson using the C4D's GUI and Dan Toloudis and I converted it to python using MAXON's C4D python SDK and uPy

Results: 
One lesson was how difficult the MAXON documentation was to parse, which is why I decided to write this up.
For simple meshes the process was reasonably fast, the more complex meshes would hang for a long time but eventually finish. This was very polygon count dependent, changing the polygon reduction from 90% to anything lower made a huge difference in processing time. 

Creating the OBJ files 

Download UCSF chimera
The first step is to open the tiff files in chimera, adjust the settings, and then export the .obj.
The main settings to change are:
  • the voxel sizing (if it's not 1, 1, 1) 
  • the step size
  • the isovalue (level)
The nice thing UCSF Chimera does is exports the .obj as a single nested file, instead of having to deal with multiple files. This is currently only true for Chimera and not ChimeraX, but may change in the future. 
Node template to create chimera code. 

    
To run a script in chimera, just right click on it and choose open with chimera. 
Picture
chimera-export.cmd

    
The result of this script is a whole set of .obj files to process. 

Creating texture maps in C4D

To start with, make sure to download and install uPy. 
Download upy
visualcell.py script imports and setup

    

Opening and merging files

We had two files to start with, the .obj file containing our meshes, and the C4D starter file with our render settings and pre-made deformers. One issue we ran into right away was being able to open the .obj file without the dialog box popping up, using MergeDocument without the SCENEFILTER_DIALOGSALLOWED flag fixed this. 
visualcell.py importing files 

    

Looping over elements

We make heavy use of this pattern throughout the code. We could have optimized it do less loops, but it was easier to follow the logic if we broke it down to the steps we would do in the GUI. 
visualcell.py Looping over elements in the hierarchy 

    

Creating/applying deformers

We experimented with two different ways of applying deformers. One was to make the deformer using the API, and the other was to create the deformers in a pre-made file and then apply them using parenting. We went with this second method because it was easier to set up, and some things are not exposed to the API, including the shader for the displacer deformer, but for some applications it's good to know how to make the deformers directly. 
Code Editor

    

Current state to object

After we've reduced the polygons, smoothed, and deformed the objects, we need to convert them to meshes. In the GUI you do this by right clicking on each one in the object bar and selecting 'current state to object'.
visusalcell.py current state to object

    

Generating UV maps

Now we need to create UVs for our newly created meshes.
Steps for doing this in the GUI: 
​
  1. Change C4D Layout: from Standard to {BP-UV Edit}
    1. In the ObjectManager (lower left), select one of the meshes, e.g. "TUBA1B_Int_20170124_C07_004.czi_cell_seg.tiff_1"
    2. In the UV editing window (upper right), turn on uv mesh/ShowUVmesh
    3. Select the Texture tag of that mesh in the Object Manager
      1. In the Attributes Manager, confirm (or change if needed) the Tag's Projection method to anything other than UVW Coordinates, e.g. "Spherical" (if you change these around, you should see UVs change in teh UV Editing Window
    4. Right click on the tag to choose "Generate UVW Coordinates"
    5. Optimize the UVs
      1. Choose the UV Mapping/OptimalMapping tab in the lower right panel (likely grayed out at this point)
      2. Select the new UV tag on the mesh object that you just creted in the Object Manager
      3. In the top Toolbar, select the Polygon edit mode (grey cube with orange top face)
      4. The Optimal Mapping panel should now be active
        1. Choose the Optimal Angle option
        2. Set Optimal Angle's parameters to:
          1. Preserve Orientation = off
          2. Stretch to fit = on
          3. Maximum Distortion = 1%
          4. Relaxation Steps = 0
          5. Spacing = 1%
        3. Click [Apply]
          1. This should take ~1 sec for the simple DNA shapes, ~10 sec for the membrane, and but up to 3 minutes for an ugly mesh like Microtubules.
        4. The ugly Mesh should now be spread realtively evenly in the UV Editing Panel
      5. Repeat for each mesh
visual.py genenerate UVs

    

Baking AO texture

Next we need to generate and save the ambient occlusion texture. We went back and forth on using this command and trying to set the settings in the GUI. We did find a command that allows you to trigger a button press in the GUI. But we never got it to bake correctly. The c4d.utils.BakeTexture command was also difficult to figure out, but after some research Dan was able to get it to work with this BakeOneOjb class that creates a new thread for the baking progress. 
Steps for doing this in the GUI:
Step 6: Apply a Bake Texture tag and Bake the textures
  1. Save the C4D file and give it a name- otherwise the following steps won't work
  2. Add a Subdivision Surface object to the scene and set: Sudivisions: Editor = 1, Render = 1
  3. Select one of the meshes that has new UVs from Step 4 and make it a child of this SubDivision Surface
  4. Add a Bake Texture tag to the mesh
  5. Set parameters for the Bake Texture tag:
    1. In the Basic panel:
    2. Set a file path with sensible name
    3. Increase resolution to 1024^2
    4. Pixel border = 3
    5. Turn on Options panel:
    6. Options/Ambient Occlusion = on
    7. Set Options/AmbientOcclusion/OptimalMapping= off
  6. Click [BAKE]
visualcell.py

    

Saving obj objects

The final step is to save the meshes as .obj files and then close everything out. We save the file as a tmp file just so the 'are you sure you want to close without saving' dialog didn't stop the process. 

    

Putting it all together

We started by just running through one file, but the goal of this was to be able to loop over a whole list of files. 
visualcell.py

    
0 Comments



Leave a Reply.

    Categories

    All
    Animation
    Cell Crawling
    HBV
    Heroku
    PGRN.org
    R
    Web Development

    RSS Feed

Download CV
Contact me:
  • Home
  • Science Visualization
  • Research
    • Initial findings
    • Image processing
  • Civic Tech
  • Fine Art
  • News
  • Methods and Notes
  • CV and Resume