Announcement

Collapse
No announcement yet.

Any way to batch render multiple files?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Any way to batch render multiple files?

    As much as I love using the batch render function for files with multiple scenes, I'd really like to be able to batch render scenes in different files. I suspect there's a way to do so using exported vrscenes, but to be honest I'm not entirely sure how to do so. Is there a way to achieve this?

    Thank you!

  • #2
    Hi onni_it ​,

    ... I'd really like to be able to batch render scenes in different files
    This can be done only with a Ruby scripts.
    I'll consult with the dev team and we might be able to put together a simple example.

    Konstantin

    Comment


    • #3
      The following script allows rendering multiple scenes from multiple SketchUp project files with a single action.
      Please execute the solution using the following steps:
      1. Select scenes from each SketchUp project (.skp) you wish to render.
        If you do not wish to include a specitic Scene, excluding it from animation (checkbox found in the Scene details tray panel) is enough to flag it and V-Ray will skip it.
      2. Copy the script text found below and paste it into a text editor of your choice.
      3. Edit the script by changing the file location for your .skp files and an output location for all rendered images. There are instructions what and how to edit at the bottom of the script text.
      4. Open SketchUp.
      5. Open the Ruby Console (Window > Ruby Console or Window > Developer > Ruby Console).
      6. Paste the edited script text and hit 'Enter'.
        V-Ray then inspects and exports each scene from each project and once this process is done all scenes are rendered in sequence.

      Code:
      class BatchFileRenderer
      
        OSX = !(/darwin/ =~ RUBY_PLATFORM).nil? unless defined?(OSX)
        WINDOWS = !OSX unless defined?(WINDOWS)
        SKETCHUP_VERSION ||= Sketchup.version.split(".").first.to_i + 2000
        UPDATE_TIME ||= 0.1
      
        def initialize(skp_files)
          @skp_files = skp_files.clone
          @vrscene_files = []
          @renderer = VRay::VRayRenderer.new()
          @renderer.subscribe(self)
        end
      
        def export
          @skp_files.each { |skp_file|
            raise "Failed to open file" if !open_model(skp_file)
      
            # activate the context
            context = VRay::Context.active
            context.renderer.subscribe(self)
      
            # export vrscnes
            project = File.basename(skp_file, ".skp")
            tempdir = Dir.tmpdir.encode(Encoding::UTF_8)
            export_path = "#{tempdir}/#{project}"
            FileUtils.mkdir_p(export_path)
            exporter = VRay::BatchExporter.new(context: context)
            @vrscene_files += exporter.export(export_path)
            puts export_path
          }
        end
      
        def render(output_path)
          @output_path = output_path
          render_next
        end
      
        def on_state_changed(renderer, old_state, new_state, instant)
          case new_state
          when :idleStopped, :idleError, :idleFrameDone, :idleDone
            renderer.save_vfb_image(
              @img_file,
              apply_color_corrections: true,
              single_channel: true
            )
            render_next
          end
        end
      
        private
      
        def render_next
          vrscene_file = @vrscene_files.shift
          return  if !vrscene_file
      
          project = File.basename(File.dirname(vrscene_file))
          @img_file = "#{@output_path}/#{project}_#{File.basename(vrscene_file, ".vrscene")}.png"
          @renderer.clear!
          @renderer.load(vrscene_file)
          @renderer.start()
        end
      
        def open_model(path)
          Sketchup.active_model&.close(true)
          VRay.pump_message
          return Sketchup.open_file(path) if SKETCHUP_VERSION < 2021
          status = Sketchup.open_file(path, with_status: true)
          [Sketchup::Model::LOAD_STATUS_SUCCESS, Sketchup::Model::LOAD_STATUS_SUCCESS_MORE_RECENT].include?(status)
        end
      
      end
      
      #SET THE LOCATION OF YOUR SKP PROJECTS HERE
      #ENSURE THE DIRECTORY PATH IS ENCLOSED WITH SINGLE QUOTATION MARKS
      #USE ONLY SLASHES IN YOUR DIR PATH. NO BACKSLASHES.
      #LEAVE THE /**/*.skp AFTER THE DIR PATH TO ENSURE ALL SKP PROJECTS FOUND THERE ARE LOADED.​
      skp_files = Dir.glob(
        'C:/Users/user/Desktop/example_location/**/*.skp'
      )
      
      batch_file_renderer = BatchFileRenderer.new(skp_files)
      batch_file_renderer.export
      
      #SET OUTPUT LOCATION FOR RENDERED IMAGE FILES
      #ENCLOSE THE PATH IN SINGLE QUOTATION MARKS AND USE ONLY SLASHES.
      batch_file_renderer.render('C:/Users/user/Desktop/example_location')
      Hope this script will suffice!
      Be advised it relies on internal classes and method calls that may change in the future. Eventually the script may stop working with future version of V-Ray for SketchUp.
      If it does we will update it accordingly.

      Kind regards,
      Peter
      Peter Chaushev
      V-Ray for SketchUp | V-Ray for Rhino | Product Specialist
      www.chaos.com

      Comment


      • #4
        Hi

        Nice script. I'm using SketchUp Pro 2020 with Vray Next. Will it work?
        I get this error when I try?
        Error: #<NoMethodError: undefined method `subscribe' for #<VRay::VRayRenderer:0x00007f88b098cc00>>
        <main>:11:in `initialize'
        <main>:81:in `new'
        <main>:81:in `<main>'
        SketchUp:1:in `eval'​

        I'm MAC user and use this path "/Users/sebastian/Library/CloudStorage/OneDrive/SketchUp/Test/**/*.skp"


        What do I do wrong?

        Comment

        Working...
        X