developer:sdksamples:addtexture


Adding Textures to Objects

.NET

Question

I'm trying to add a texture to an object from a plug-in. If I have a file opened in Rhino, I can do that from the Materials page in the Object Properties window. I can I do the same by code?

Answer

See the following:

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  // Select object to add texture
  MRhinoGetObject go = new MRhinoGetObject();
  go.SetCommandPrompt("Select object to add texture");
  go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object |
                       IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object |
                       IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object
                       );
  go.GetObjects(1, 1);
  if (go.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  // Verify object
  MRhinoObjRef objref = go.Object(0);
  IRhinoObject obj = objref.Object();
  if (obj == null)
    return IRhinoCommand.result.failure;
 
  // Select texture
  MRhinoGetFileDialog gf = new MRhinoGetFileDialog();
  gf.SetScriptMode( context.IsInteractive() ? false : true );
  bool rc = gf.DisplayFileDialog( IRhinoGetFileDialog.file_dialog_type.open_bitmap_dialog, , RhUtil.RhinoApp().MainWnd());
  if( !rc )
    return IRhinoCommand.result.nothing;
 
  // Verify texture
  string bitmap_filename = gf.FileName().Trim();
  if (string.IsNullOrEmpty(bitmap_filename))
    return IRhinoCommand.result.nothing;
 
  // Copy object's attributes so they can be modified if necessary
  MRhinoObjectAttributes att = new MRhinoObjectAttributes(obj.Attributes());
 
  // Make sure the object has it's material source set to "material_from_object"
  bool bModified = false;
  if( att.MaterialSource() != IOn.object_material_source.material_from_object )
  {
    att.SetMaterialSource(IOn.object_material_source.material_from_object);
    bModified = true;
  }
 
  // Make sure the object has a material assigned
  int material_index = att.m_material_index;
  if (material_index < 0)
  {
    // Create a new material based on Rhino's default material
    OnMaterial mat = new OnMaterial(RhUtil.RhinoApp().AppSettings().DefaultMaterial());
    // Add the new material to the material table
    material_index = context.m_doc.m_material_table.AddMaterial( mat );
    // Assign the new material (index) to the object.
    att.m_material_index = material_index;
    bModified = true;
  }
 
  if (material_index >= 0)
  {
    // Copy the material so it can be modified
    OnMaterial mat = new OnMaterial(context.m_doc.m_material_table[material_index]);
 
    // Find the texture
    int texture_index = mat.FindTexture(, IOnTexture.TYPE.bitmap_texture);
    if( texture_index >= 0 )
      // Already had a texture, so just replace it
      mat.m_textures[texture_index].m_filename = bitmap_filename;
    else
      // Does not have a texture, so add one
      mat.AddTexture(bitmap_filename, IOnTexture.TYPE.bitmap_texture);
 
    // Modify the material
    context.m_doc.m_material_table.ModifyMaterial(mat, material_index);
 
    // Don't forget to update the object, if necessary
    if (bModified)
      context.m_doc.ModifyObjectAttributes(objref, att);
 
    context.m_doc.Regen();
    return IRhinoCommand.result.success;
  }
 
  return IRhinoCommand.result.failure;
}
developer/sdksamples/addtexture.txt · Last modified: 2010/01/26 16:37 (external edit) Driven by DokuWiki Recent changes RSS feed

 © 1997-2010 

McNeel North America Europe Latin AmericaAsia