/* export_obj_plugin.a8s */ /* * This script is a simple .obj format Object export plugin. * * Export scripts are not allowed to modify a project. Any * attempt to change a model is an error. * * The .obj format is written to a text file that's passed to * the script from Anim8or. * * Copyright 2006 R. Steven Glanville. Permission granted for * modification and use, including commercial use. Source * distribution must include this copyright. * */ /* * Directives to install script as export plugin in Object editor. * * Required directives: * * #plugin("object", "export", , ); * #file(, [ "text" | "binary" ]); * #return(); * * - string, including initial '.' * - comment for export file open dialog. * - name of file variable. Must be declared later. * - int variable with result. 1 = success, 0 = failure. * */ #plugin("object", "export", "Wavefront", ".obj"); #file($output, "text"); #return($result); file $output; int $result; object $curObject; $curObject = project.curObject; $output.print("# Created with Anim8or %s\n", version); $output.print("# Object \"%s\":\n", $curObject.name); $output.print("\n"); $output.print("mtllib %s.%s\n", $output.GetRoot(), "mtl"); $output.print("\n"); shape $shape, $shapes[1], $childShapes[1]; meshdata $mdata; int $numPoints, $numNormals, $numTexCoords, $numFaces, $numSides; int $totalPoints, $totalNormals, $totalTexCoords; int $ii, $jj; point3 $point, $normal; point2 $uv; float4x4 $transformMat, $normalTransformMat; file $materialFile; string $mFileName, $matName; material $material; /* Create material file and clear marked materials: */ $mFileName = $output.GetDir() + $output.GetRoot() + "." + "mtl"; $materialFile.open($mFileName, "w"); $materialFile.print("# Created with Anim8or %s\n", version); $materialFile.print("# Object \"%s\":\n", $curObject.name); $materialFile.print("\n"); project.MarkMaterials(false); /* Initialize counters: */ $totalPoints = 0; $totalNormals = 0; $totalTexCoords = 0; /* The shapes in $shapes are processed in reverse order so */ /* reverse the array of values that GeetShapes returns: */ $curObject.GetShapes($childShapes); $shapes.size = 0; while ($childShapes.size > 0) $shapes.push($childShapes.pop()); while ($shapes.size > 0) { $shape = $shapes.pop(); /* If $shape is a group push child shapes onto stack: */ if ($shape.GetKind() == SHAPE_KIND_GROUP) { $shape.GetShapes($childShapes); $output.print("# Group \"%s\" has %d children:\n", $shape.name, $childShapes.size); while ($childShapes.size > 0) { $shapes.push($childShapes.pop()); } } else if ($shape.GetKind() == SHAPE_KIND_PATH || $shape.GetKind() == SHAPE_KIND_MODIFIER || $shape.GetKind() == SHAPE_KIND_TEXT) { /* No 3D mesh to output. */ } else { $output.print("# Shape \"%s\":\n", $shape.name); $output.print("g %s\n", $shape.name); $mdata = $shape.GetMeshData(); $transformMat = $shape.GetGlobalTransform(); $normalTransformMat = $shape.GetGlobalNormalTransform(); $numPoints = $mdata.GetNumPoints(); $output.print("# No. points %d:\n", $numPoints); for $ii = 0 to $numPoints - 1 do { $point = $mdata.GetPoint($ii); $point = $transformMat.Project($point); $output.print("v %.6g %.6g %.6g\n", $point); } $output.print("\n"); $numNormals = $mdata.GetNumNormals(); $output.print("# No. normals %d:\n", $numNormals); for $ii = 0 to $numNormals - 1 do { $normal = $mdata.GetNormal($ii); $normal = $normalTransformMat.Project($normal); $output.print("vn %.5f %.5f %.5f\n", $normal); } $output.print("\n"); $numTexCoords = $mdata.GetNumTexCoords(); $output.print("# No. texture coordinates %d:\n", $numTexCoords); if ($numTexCoords > 0) { for $ii = 0 to $numTexCoords - 1 do { $uv = $mdata.GetTexCoord($ii); $output.print("vt %.5f %.5f\n", $uv); } $output.print("\n"); } $numFaces = $mdata.GetNumFaces(); if ($numFaces > 0) { $output.print("# No. faces %d:\n", $numFaces); $material = $mdata.GetMaterial(0); $matName = $material.name; if ($matName == " -- default --") { /* Change Anim8or's default material name to an identifier: */ $matName = "___default___"; } $output.print("\n"); $output.print("usemtl %s\n", $matName); $output.print("\n"); /* Add material to the .mtl file is it isn't already there: */ if (!$material.Marked) { $materialFile.print("newmtl %s\n", $matName); $materialFile.print("Ka %.3g %.3g %.3g\n", $material.ambient*$material.Ka); $materialFile.print("Kd %.3g %.3g %.3g\n", $material.diffuse*$material.Kd); $materialFile.print("Ks %.3g %.3g %.3g\n", $material.specular*$material.Ks); $materialFile.print("Ns %.1f\n", $material.roughness); $materialFile.print("\n"); $material.Marked = true; } for $ii = 0 to $numFaces - 1 do { $output.print("f"); $numSides = $mdata.GetNumSides($ii); for $jj = $numSides - 1 to 0 step -1 do { $output.print(" %d", $mdata.GetFacePointIndex($ii, $jj) + $totalPoints +1); if ($mdata.GetFaceHasTexCoords($ii)) { $output.print("/%d", $mdata.GetFaceTexCoordIndex($ii, $jj) + $totalTexCoords + 1); } if ($mdata.GetFaceHasTexCoords($ii)) { $output.print("/%d", $mdata.GetFaceNormalIndex($ii, $jj) + $totalNormals + 1); } } $output.print("\n"); } } $output.print("\n"); $totalPoints = $totalPoints + $numPoints; $totalNormals = $totalNormals + $numNormals; $totalTexCoords = $totalTexCoords + $numTexCoords; } } $output.print("# End of Object \"%s\"\n", $curObject.name); $materialFile.close(); $result = 1; /* Assume export will succeed. */