Importing meshes and rendering in rhinopython

I have a directory of .obj files. I want to open each in turn, render it, save the image and move on to the next file.

I couldn’t immediately find a way to import an obj file into a layer of a Rhino document in rhinopython - is this something that I could do via Command? Same question for rendering and saving the output image.

You cannot import geometry onto a specific layer, as some file formats dictate the layer on which geometry lies. But you can move geometry to another layer quite easily using the SelLast and ChangeLayer commands. Does this help?

Thanks Dale, it sounds like it should be possible to do what I want to do, but would I be correct in saying that I will have to do the importing and rendering via Command rather than rhinopython? (I am on a Mac, so I cannot use RhinoScript directly.)

I have never really understood the syntax needed to run commands via rhinopython, particularly for something needing arguments (such as importing a file). Is there a guide somewhere?

Ah, it looks like the Import command isn’t yet implemented for the Mac version - ok, I don’t think I can do this yet!

Ok, I’m back again, this time with a different project. I don’t need to import geometry, I can build it directly from python code. But then I want to render the geometry, save the image in some directory, then delete the geometry and repeat.

Is there a way to render an image and save it entirely within rhino python?

I tried CreatePreviewImage, but I couldn’t get it to work - the following code just prints “False”:

import rhinoscriptsyntax as rhino
print rhino.CreatePreviewImage('foo.bmp', size = (500,500))

CreatePreviewImage() is for making the thumbnails that you see in a file explorer, not for rendering. To render something you will need to actually launch a render, using the built-in Rhino render or something else. That involves a number of things, not all of which have Python/RhinoCommon hooks yet. Roughly

  1. Get a view which you want to render. You have all sorts of view methods available via Python/Rhino
  2. Assign materials to objects. Some of this (the basics) is available via scripting.
  3. Create lighting (or use default). You can manipulate lights with a script.
  4. Add any environment stuff you need. Some available via scripting, I think.
  5. Launch the render. This you need to do with a scripted Rhino command.
  6. Wait for the render to finish and save the file.
  7. Continue.

There is a batch renderer plug-in called Badger available for Rhino (not free).

Another way to go without having to launch renders is to use simple view captures of a viewport. That is much easier to do:

  1. Set up/customize your display mode beforehand
  2. Make your desired viewport active.
  3. Call rs.Command("_-ViewCaptureToFile…") with the appropriate command line options and a file-save naming scheme.
  4. Continue, and loop.

It might be possible to script real-time rendered viewports such as Neon with Python, don’t know how to do that myself.

–Mitch

Thanks Mitch, ViewCaptureToFile might be good enough for what I’m trying to do.