OBJ material names by style

Next
 From:  Micha
5790.1 
Hello Michael,

could you please modify the attached MoI script so that the exported obj material definition contain material names with a defines sign for spaces in the material name? At the moment the materials are names like "Layer_01", but I would like to use a more exotic sign like "§". For the object names it was implemented in this script, but the material names are not part of the script. Or is there an ini command to set the sign for the spaces in materials? I would like to get it run with MoI 2.

Best-
Micha


function AssignObjectNamesFromStyles()
{
// Set object names to be their Style name + unique number.

var objs = moi.geometryDatabase.getObjects();
var styles = moi.geometryDatabase.getObjectStyles();

for ( var i = 0; i < objs.length; ++i )
{
var obj = objs.item(i);
var name = styles.item(obj.styleIndex).name + '§' + i;

// Replace all spaces with underscore characters, since spaces don't work as
// object names in OBJ files.
obj.name = name.replace( /\s/g, '§' );
}
}

function Convert(FileName)
{
var gd = moi.geometryDatabase;

// Open the file, set 2nd param to true to suppress any save changes prompt.
gd.open( FileName, true );

AssignObjectNamesFromStyles();

moi.view.resetAll();

// Create the output file name by breaking off the file extension and adding 'obj'.

OBJFileName = FileName.substr( 0, FileName.lastIndexOf('.') + 1 ) + 'obj';

// Save out to the output file, passing the option to suppress the UI. You
// add other options separated by semi-colons with no spaces. These
// options are available for controlling the meshing:
// NoUI=true
// Angle=6.0
// Output=ngons | quads | triangles
// MaxLength=
// MaxLengthApplyTo=curved | planes | all
// MinLength=4
// AspectRatio=0.0
// Weld=true
// Display=shadedwithedges | shadednoedges | wireframe
// ExpandedDialog=false

gd.saveAs( OBJFileName, 'Angle=;MinLength=;MaxLength=;AspectRatio=;Weld=true;Output=quads');

// Let's clear out and suppress any save changes prompt again.
gd.fileNew( true );
}

Convert('d:\\Temp\\#moi.3dm');

moi.exit( true ); // Pass true to suppress save changes prompt.

Visualisation for Designer and Architects | http://www.simulacrum.de
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
5790.2 In reply to 5790.1 
Hi Micha, in the just recently released Mar-17 v3 beta there is a new function you requested for exporting to OBJ using the style name as the group name in the OBJ file.

That should then allow you import into Rhino using the option in the OBJ importer to map group names to layers - won't that improve your export into Rhino?

Here's the relevant part of the release notes:

Updated OBJ export settings - added a new setting in moi.ini for OBJ export to help with mesh transfer into Rhino. [OBJ Export] WriteStylesAsGroups=n When this is enabled by changing to WriteStylesAsGroups=y then the style name will be written as the group identifier when generating the mesh. This allows for using the setting in Rhino's OBJ importer for importing groups as layers to get the mesh back to the same layer as the original object. Requested in the forum here: http://moi3d.com/forum/index.php?webtag=MOI&msg=5637.1

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
5790.3 In reply to 5790.1 
Here is a function that you can add to the script that will change style names to replace spaces in any style names:

code:
function RenameStylesWithSpaces()
{
    var styles = moi.geometryDatabase.getObjectStyles();

    for ( var i = 0; i < styles.length; ++i )
    {
         var style = styles.item(i);
         style.name = style.name.replace( /\s/g, '§' );
    }
}


Paste that into that into the top of the script. Then modify the script to call that function before calling AssignObjectNamesFromStyles, like this:



RenameStylesWithSpaces();
AssignObjectNamesFromStyles();



- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
 From:  Micha
5790.4 
Hallo Michael,

sorry for the delayed answer. Thank you very much for your help! I got it run now. :) Maybe someone like to use it too - attached the full MoI Convert script and the Rhino button script for renaming the imported OBJ meshes.

At the moment it stay one main problem, but I suppose so it can be solved by the Rhino team only - Layer names like "Grün" are renamed to "Gr n". At the OBJ file the names are perfect, but after the import to Rhino the names are destroyed. I hope it can be fixed.

The script for bringing the meshes to the right layer is quite slow. Doe's anybody know how to get it faster? Is it because I packed it to a button script?

Best-
Micha



Here my Rhino button script:

_-import "d:\temp\#moi.obj" ImportOBJGroups=DoNotImportOBJGroups ImportOBJObjects=Yes IgnoreTextures=Yes MapOBJYToRhinoZ=No _enter
_QuadrangulateMesh _enter

_-RunScript (

Option Explicit

Call ObjToLayerByName

Sub ObjToLayerByName()
Dim arrObj, strObject, strName
arrObj = Rhino.GetObjects ("Select meshes", 32, , true)
If IsArray(arrObj) Then
For Each strObject In arrObj
strName = Rhino.ObjectName(strObject)
If InStr(strName, "[") > 0 Then
strName = Left(strName,InStr(strName, "[")-1)
End If
If Rhino.IsLayer(strName) Then
Rhino.ObjectLayer strObject, strName
Else
Rhino.AddLayer strName
Rhino.ObjectLayer strObject, strName
End If
Next
End If
End Sub
)

Visualisation for Designer and Architects | http://www.simulacrum.de
Attachments:

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged
 

Reply to All Reply to All