redmotion: Convex collision mesh creation with Stanhull/Softimage

04 October 2009

Convex collision mesh creation with Stanhull/Softimage

I use a number of scripts for speeding up the creation of collision boxes for Valves SMD exporter in Softimage. The process I follow is laid out below:

First you will need to download this file. Its called Stanhull. It's a simple program that spits out an obj file containing a convex mesh based on an input mesh (inside an .obj file). Extract and place in a folder of your choice. If you don't want to edit the script below use the path: C:\3d\Stanhull\ 

1. Create your physbox model components. I do this by breaking up the original model into roughly convex forms. You are limited to the number of these by default (to 16 I think) but you can overide the limit with a line in your .qc file. The $maxconvexpieces QC command can increase this.

2. Select the new convex model pieces and run the convex script:
    'Objdir = the directory you want the exported Obj to go to (change if you wish)
        Objdir = "C:\3d\Stanhull\"
        'StanhullDir = the directory you have put stanhull.exe in (change if you wish)
        StanhullDir = "C:\3d\Stanhull\"
        'NOTE: file is always created in the Softimage\version xx\Application\bin dir and always called hull.obj
        fileOutLoc = Application.InstallationPath( siFactoryPath ) & "\Application\bin\hull.obj"
      
        set list = GetValue( "SelectionList" )
        if list.count = 0 then
               MsgBox "Please select at least one polymesh object first!!"
               else
               for each item in list
                  logmessage item.name
                  name = item.name
                  if item.type = "polymsh" then
                      'select objects one by one and export
                     SelectObj name, , True
                     getObjname = Objdir & "convert.obj"
                     ObjExport getObjname, , , , , , , , , , 0, False, , , False
                     'set up s.ystem command to run Stanhull.exe
                     cmd = StanhullDir & "StanHull " & getObjname & " -t"   
                     cmdResult = s.ystem( cmd )
                     set inObj = ObjImport( fileOutLoc, 1, 0, True, True, False, True )
                     'select new object and set up geometry approximation as needed for .phy SMDs
                     SelectObj inObj, , True
                     MakeLocal inObj & ".geomapprox", siDefaultPropagation
                     SetValue inObj & ".geomapprox.gapproxmoad", False
                     CreateProjection , siTxtCubic, siTxtDefaultCubic, , "Texture_Projection"
                     FreezeModeling
                     end if
                 next
            end if


3. Now with the new convex imported back in, select them and remove automatic discontinuity from them all with the following script:
dim name
set list = GetValue( "SelectionList" )
if list.count = 0 then
   MsgBox "Please select at least one polymesh object first!!"
   else
'start rendermap make routine - depending on scene size and object complexity this could take a while... :)
   for each item in list
      logmessage item.name
      name = item.name
      if item.type = "polymsh" then
         SelectObj name, , True
         MakeLocal ".geomapprox", siDefaultPropagation
         SetValue ".geomapprox.gapproxmoad", False
         end if
      next
end if

4. Lastly, I apply cubic UVs to all of the physics box objects by selecting them and running this script (below). Once frozen, they are ready for exporting as SMD files for compiling.
dim name
set list = GetValue( "SelectionList" )
if list.count = 0 then
   MsgBox "Please select at least one polymesh object first!!"
else
'start rendermap make routine - depending on scene size and object complexity this could take a while... :)
   for each item in list
      logmessage item.name
      name = item.name
      if item.type = "polymsh" then
         SelectObj name, , True
         CreateProjection , siTxtCubic, siTxtDefaultCubic, , "Texture_Projection"
         end if
      next
end if

freeze ALL. (You may need to add an image to the material).