Render image

Render Images

Print

Render Images imports all CAD files within a folder and renders them one at a time. This script can be used to render an animation from an obj file sequence. At the end of the article, you will find the annotated script code for your reference. 

How the script works:

At the top of most scripts we import the external Python libraries first:

import os, re.


Section 1:  Set up a rule for reading the file-extension input.

  • Create a function to check the file-extension input, ext. Remove the period ( . ) if it exists in the string.

  • Create a while loop that checks if the first character of the string starts with a period. If it does, then reassign the ext variable with all the characters starting with the second character.

  • Return a clean variable.


Section 2: Create the main function, define attributes and display them in a user prompt.

  • On line 14, use lux.getMaterialTemplates to get the available material templates.

  • Use lux.getSceneInfo in order to retrieve scene information such as the name, units, triangles, etc...

  • Lines 16 through 26, lux.DIALOG functions define the prompt's attributes:

    • Input frames folder, folder.

    • Input file extension, iext.

    • Output file extension, oext.

    • Frame dimensions, width, and height.

    • Material template (if any), template.

    • Whether or not to add the renderings to the render queue, queue.

    • Whether or not to process the queue, process.
       

  • Lines 27 through 30, include the lux.getInputDialog function which generates the actual user prompt. Inline 30, an "id" tag recalls the most recent input values. For more information regarding the dialog functions, click here.

  • The user prompt opens via lux.getInputDialog.

  • If there is an error with the inputs, the script exits gracefully.

 


 



Section 3: Assign user input to corresponding variables. See below for a line-by-line code breakdown.

  • Lines 33 and 34 checks that the folder box isn't empty, and if it is, give an error message.

  • Assign the folder path to the fld variable.

  • Lines 37 and 38 check that the input extension line isn't empty, and if it is, give an error message.

  • Assign the input extension to the iext variable after it has cleaned it up using cleanExt (defined in section 1).

  • Compile the file name with the extension, and assign it to the reFiles variable.

  • Create a boolean variable called found and set it to "False".

  • Lines 42 to 45, create a loop that checks if reFiles exists in the fld directory. If the file exists, set found to "True"

  • On lines 46 through 48, if found is still false, give an error message

  • Lines 50 and 51 check that the output extension line isn't empty, and if it is, give an error message

  • Assign the output extension to the oext variable after it has cleaned it up using cleanExt (defined in section 1).

  • Assign the width given to the width variable.

  • Assign the height given to the height variable.

  • Assign the chosen material template to the template variable.

  • Create a boolean variable called queue and set it to the correct value depending on the user input.

  • Create a boolean variable called process and set it to the correct value depending on the user input.


Section 4: Create a for-loop that will go through every file in the specified folder and render the corresponding frame.

  • Line 70 puts together fld and the file name into one pathname. Windows and Mac OSX have different directory path separators, using os.path.sep automatically uses the correct ones.

  • Create a new scene.

  • Lines 73 and 74 import the selected file.

  • Lines 76 through 78 sets the correct material template if one is requested by the user.

  • Output a message to the console letting the user know that it is rendering.

  • Render the image using lux.renderImage.

  • Lines 84 through 86 will process the queue if requested by the user.

Finally, call the main function.