BakeToTexture : UtilityPlugin

Provides access to the Bake To Texture dialog and it's functionality. Available in 3ds Max 2021 and higher.Note: there are some new additions in 3ds Max 2021.1 Update, as indicated below.

Constructor:

class instances not creatable by MAXScript

Interface: BakeToTexture

This interface provides access to the functionality of the Bake to Texture dialog, including output file options, file types and sizes, and the baking process. The BakeToTextureMap interface (described below) exposes the functionality of individual bake maps themselves.

See the sample at the end of this topic for an illustration of how to use some of these APIs together.

Properties::

BakeToTexture.outputPath : string : Read|Write

Gets or sets the output path for baked map files.

BakeToTexture.autoCloseProgressDialog : boolean : Read|Write

Gets or sets whether the progress dialog closes immediately after all the maps in the queue have been baked. If set to false, the dialog remains open after the process completes.

BakeToTexture.showFrameBuffer : boolean : Read|Write

Specifies whether to show the frame buffer window during the baking process.

BakeToTexture.alwaysOverwriteExistingFiles : boolean : Read|Write

Specifies whether to overwrite existing map files.

When true existing files are replaced when new files are generated in the baking process, and the alwaysSkipExistingFiles property is set to false.

When false, existing files are not replaced. If both this property and the alwaysSkipExistingFiles property are false, a warning is displayed and the user must make the decision whether to overwrite or skip each file.

BakeToTexture.alwaysSkipExistingFiles : boolean : Read|Write

Specifies whether to skip existing map files. Available in 3ds Max 2021.1 Update and higher. When true, existing files are not baked again in the baking process. Also the alwaysOverwriteExistingFiles property is set to false.

When false, existing files are not skipped. Therefore, if both this property and the alwaysOverwriteExistingFiles property are false, a warning is displayed and the user must make the decision whether to overwrite or skip each file.

Methods:

<integer>BakeToTexture.getMapsCount <node>object

Returns the total number of maps configured for the specified object.

<node by value array>BakeToTexture.getObjects()

Returns and array that contains all the scene objects that have bake to texture maps associated with them.

<Interface by value array>BakeToTexture.getAllMaps()

Returns an array of all the maps configured for objects in the Bake to Texture dialog.

<Interface by value array>BakeToTexture.getMaps <node>object

Returns an array of all the maps configured for the specified object.

<boolean>BakeToTexture.bakeableMapsAreValid()

Returns true if all of the maps set as renderable are valid. This means they do not generate warnings and are compatible with the currently configured renderer.

<boolean>BakeToTexture.bake onMapBakedCallback:<value>

Starts the baking process. The onMapBakedCallback callback function is called after each map file is saved.

A map can be a single file or multiple files (as in the case of UDIM tiles or animated frames), and the callback is called after each file. Available in 3ds Max 2021.1 Update and higher. The callback function receives two parameters:

  1. successfulBake: a boolean that indicates whether a map file was successfully baked and saved.
  2. bakeFilePath: a string containing the full file path of the baked map's image file.

The callback function should return a boolean value indicating if the baking process should continue with the next map file (for multi-file maps) or the map in the queue. True indicates the process should continue, while false halts the process.

For example:

fn onMapBaked successfulBake MapFileString = 
(
     if successfulBake and doesFileExist(MapFileString) then
     (
          print MapFileString
          return true
     ) else return false
)
BakeSuccess = BakeToTexture.bake onMapBakedCallback:onMapBaked
<Interface>BakeToTexture.addMap <node>object <enum>mapTypeEnum
mapTypeEnum enums: {#AlphaMap|#BlendMap|#CavityMap|#CompleteMap|#DensityMap|#DiffuseMap|#DustMap|
#HeightMap|#LightMap|#NormalMap|#OcclusionMap|#ShadowMap|#SpecularMap|#SubSurfaceMap|#OverrideMap}

Adds a map of a type specified by mapTypeEnum to the specified object. This function returns the map that was created and added. The map implements the BakeToTextureMap interface (see below).

Note: Not all map types are compatible with all renderers. To get a list of map types compatible with the current renderer, use getCompatibleMapTypes() described below.
<Interface>BakeToTexture.addMapByClassID <node>object <integer64 array>classID

Adds a map of a type specified by classID to the specified object. This function returns the map that was created and added. The map implements the BakeToTextureMap interface (see below).

Note: Not all map types are compatible with all renderers. To get a list of map types compatible with the current renderer, use getCompatibleMapTypes() described below.
<Interface>BakeToTexture.getMapById <integer>mapID

Returns the map with the specified mapID. The map implements the BakeToTextureMap interface (see below).

<void>BakeToTexture.deleteMapById <integer>mapID

Removes the map with the specified mapID from the Bake to Texture list.

<void>BakeToTexture.deleteAllMaps()

Removes all the maps from the Bake to Texture list.

<void>BakeToTexture.openOutputLocation()

Opens the Bake to Texture output directory in the Windows File Explorer.

<string array>BakeToTexture.getCompatibleMapTypes()

Returns a list of all the map types that are compatible with the current renderer. The list is an array of strings, each string containing the name of the map and it's classID.

Note: The list of compatible maps for the current renderer is populated when the Bake to Texture utility is initialized. If the call to this function happens before any other call (such as BakeToTexture.showDialog()), this list will not be complete.
<void>BakeToTexture.showDialog()

Opens the Bake to Texture dialog.

<void>BakeToTexture.hideDialog()

Closes the Bake to Texture dialog.

<enum>getOutputTo <node>object
       getOutputTo enums: {#FilesOnly|#OriginalMaterial|#DuplicateOriginalMaterial|#CreateNewMaterial|#InvalidType

Gets the "Output to" mode for the specified object. Available in 3ds Max 2021.1 Update and higher.

    <boolean>setOutputTo <node>object <enum>output material:<material>
       output enums: {#FilesOnly|#OriginalMaterial|#DuplicateOriginalMaterial|#CreateNewMaterial|#InvalidType}
       material default value: undefined

Sets the "Output to" mode for the specified object. If the mode is set to #CreateNewMaterial the material must also be provided with the material:<material> argument. Available in 3ds Max 2021.1 Update and higher. If the material is created directly in the command by passing the material's constructor, the material constructor needs to be in parenthesis. For example:

BakeToTexture.SetOutputTo $Teapot001 #CreateNewMaterial material:(Physical_Material())
<material>getNewMaterial <node>object

Returns the material that is set to be the new material when #CreateNewMaterial is chosen as the "Output To" mode. Available in 3ds Max 2021.1 Update and higher.

<string array>getTargetMaterialSlots <node>object

Gets the list of all the material input slots that can be used as targets for the object's maps. The function returns an array of strings that can be used as target definitions for the map's "Output To" function. Available in 3ds Max 2021.1 Update and higher.

<void>clearShellKeepBaked nodes:<&node array>
       nodes default value: #()
       nodes is In and Out parameter

Clears all Shell materials in the scene or on a specific array of nodes, and replaces the material on the objects by the baked material connected to the shell material. It makes sure that the baked material is directly assigned to the object rather than the shell material. This is a quick way to prepare a scene for export. Available in 3ds Max 2021.1 Update and higher. If no array of nodes is provided, the Shell materials in the whole scene are cleared. If an array of nodes is provided, only the Shell materials assigned to those nodes is cleared.

<void>clearShellKeepOriginal nodes:<&node array>
       nodes default value: #()
       nodes is In and Out parameter

Clears all Shell materials in the scene or on a specific array of nodes, and replaces the material on the objects by the original material connected to the Shell material. It makes sure that the original material is directly assigned to the object rather than the Shell material. This is a quick way to prepare a scene for export. Available in 3ds Max 2021.1 Update and higher. If no array of nodes is provided, the Shell materials in the whole scene are cleared. If an array of node is provided, only the Shell material assigned to those nodes is cleared.
Interface: BakeToTextureMap

This interface is implemented by the various types of texture maps created on objects in the Bake to Texture dialog. For example, the maps returned by BakeToTexture.addMap() and BakeToTexture.getMaps() implement this interface.

Properties:

BakeToTextureMap.sceneObject : node : Read

Returns the object associated with the map.

BakeToTextureMap.id : integer : Read

The map's unique ID.

BakeToTextureMap.mapTypeID : integer64 array : Read

The map's classID.

BakeToTextureMap.mapTypeName : string : Read

The map's type name, as a string. This is the same name displayed in the Bake to Texture dialog.

BakeToTextureMap.renderable : boolean : Read|Write

Gets or sets whether the map is in the baking queue.

BakeToTextureMap.frozen : boolean : Read

The map's support status. If the map is not supported in the current renderer, this property is true, otherwise it is false. When this property is true, the map is not baked in the baking process.

BakeToTextureMap.bakeable : boolean : Read

Gets whether the map can be baked. This means all the properties and parameters for the map are valid, and the map type is compatible with the current renderer. If false, the map will not be baked in the baking process.

BakeToTextureMap.fileName : filename : Read|Write

The name of the file associated with this map. By default this is "[object name]_[map name]", for example, "Teapot001_normals".

BakeToTextureMap.fileType : string : Read|Write

The image file type extension associated with the map.

BakeToTextureMap.uvChannel : integer : Read|Write

The UV channel of the map.

BakeToTextureMap.imageWidth : integer : Read|Write|Validated by Range: 1 to 65535

The width, in pixels, of the map's output image.

BakeToTextureMap.imageHeight : integer : Read|Write|Validated by Range: 1 to 65535

The height, in pixels, of the map's output image.

BakeToTextureMap.edgePadding : integer : Read|Write|Validated by Range: 0 to 128

The edge padding of the map's output image, in pixels.

BakeToTextureMap.backgroundColor : color : Read|Write

The background color of the map's output image.

Note: The background color cannot be changed for surface maps.
BakeToTextureMap.projectionModifierIndex : integer : Read|Write

The index of the projection modifier used on the map, in order of bottom to top of the projection modifier stack. An index of 0 indicates "Self".

BakeToTextureMap.projectionModifierName : string : Read

The name of the projection modifier used on the map, as a string.

BakeToTextureMap.projectionMethod : integer : Read|Write|Validated by Range: 0 to 2

The projection method for the map, where:

Note: You should ensure that the projection method for the map is not "Self" before accessing this property, as this will cause a MAXScript runtime error.
BakeToTextureMap.projectionMaxRayDistance : float : Read|Write|Validated by Range: 0.0 to 10000.0

The maximum ray distance.

BakeToTextureMap.projectionResolveHitType : integer : Read|Write|Validated by Range: 0 to 1

The Projection Resolve Hit Type for the map, where:

BakeToTextureMap.projectionHitOnlyMatchingMatID : boolean : Read|Write

Specifies whether the Hit Only Matching Material ID setting is enabled.

BakeToTextureMap.projectionRayMissColorType : integer : Read|Write|Validated by Range: 0 to 2

The Projection Ray Miss Color setting, where:

BakeToTextureMap.projectionDiagnosticColor : color : Read|Write

The color used for the Color Diagnostic setting if projectionRayMissColorType is set to 2 (Use Color for Diagnostic).

BakeToTextureMap.UDIMEnabled : boolean : Read|Write

Specifies whether UDIM Tiles baking is enabled for the map.

BakeToTextureMap.UDIMFilePattern : enum : Read|Write
       UDIMFilePattern enums: {#UDIM|#ZBrush|#Mudbox}

Specifies the filename format for UDIM files when they are saved for the map. The file formats are: Available in 3ds Max 2021.1 Update and higher.

<void>BakeToTextureMap.setImageSize <integer>width <integer>height
 width Validated by Range: 1 to 65535
 height Validated by Range: 1 to 65535

Sets the map's width and height.

<boolean>BakeToTextureMap.applyImageTypeSettings()

Applies the current BitmapIO class settings to the matching image type defined for this map. Returns true if successful, false if there was an error.

For example, to change an existing map map1 to png output, and set some png settings:

map1.setImageType "png"
map1.setFilename "Map1"
-- Enable Alpha and use 48bit color
pngio.setAlpha(true)
pngio.SetType(#true48)
map1.applyImageTypeSettings()    

See BitmapIO : MAXWrapper for a list of classes that hold image format settings.

<integer>BakeToTextureMap.getProjectionModifiersCount()

Returns the total number of projection modifiers available for the map.

<integer>BakeToTextureMap.getOptionsCount()

Returns the total number of options available for this map's type.

<string>BakeToTextureMap.getOptionName <index>index

Returns the name, as a string, of the map option specified by index.

<integer>BakeToTextureMap.getOptionType <index>index

Returns the type of the option specified by index, where:

<boolean>BakeToTextureMap.isOptionReadOnly <index>index

Returns true if the map option specified by index is read only, false if it can be modified.

<value>BakeToTextureMap.getOptionValue <index>index

Returns the current value for the map option specified by index.

<boolean>BakeToTextureMap.setOptionValue <index>index <value>value

Sets the value for the map option specified by index, and returns true if the value was successfully set, otherwise false.

<string>BakeToTextureMap.getTargetMapSlot()

Gets the name of the Target Slot on the material of the object. This is where the map is connected to when baked. Available in 3ds Max 2021.1 Update and higher.

<boolean>BakeToTextureMap.setTargetMapSlot <string>slot

Sets the Target Slot on the material of the object. This defines where the map will be connected when baked. The target map slots are defined as strings and the valid target map slots for the parent object can be obtained by using BakeToTexture.getTargetMaterialSlots(). Available in 3ds Max 2021.1 Update and higher.

Full example

The following example creates some objects and creates bake to texture maps for them. It sets some options on the maps, and then kicks off the bake process.

-- Bake to texture example
resetMaxFile #noprompt

-- set up our object:
t = Teapot()
s = Sphere pos:[40,40,0]
pm = PhysicalMaterial()
cm = OSL_Checker()
pm.base_color_map = cm
t.material = pm 

-- need uvw for baking maps
u = uvwmap()
addModifier t u

-- open the dialog:
BakeToTexture.showDialog()                   
-- set up some maps:
-- first, let's see what's compatible with the current renderer
-- this example assumes Arnold is the current renderer
compatible_maps = BakeToTexture.getCompatibleMapTypes()
compatible_map_names = for s in compatible_maps collect ((filterString s " :")[1])

-- we can get a map from addMap() for native 3ds Max maps
if findItem compatible_map_names "Lighting" > 0 do 
    ( light_map = BakeToTexture.addMap t #LightMap )

-- we can also get it later by index and add from a class ID for non native 3ds max maps:
if findItem compatible_map_names "Arnold_albedo" > 0 do
    ( 
        BakeToTexture.addMapByClassID t #(1236154164, -1994915067) 
        albedo_map = (BakeToTexture.getMaps t)[2]
    )  

-- let's change some settings on the maps 
albedo_map.fileType = "jpg"
albedo_map.setImageSize 512 512
light_map.setImageSize 512 512

-- turn off shadows option if it is present
for i = 1 to light_map.getOptionsCount() do 
    (
        if light_map.getOptionName i == "Shadows" do 
        ( light_map.setOptionValue i 0 )
    )

BakeToTexture.showDialog()

-- start bake process, without a callback:
bake_success = BakeToTexture.bake()

Projection Map Example:

-- Add projection modifier maps
-- Create two objects
-- Note, this is not a real-world example, the map will be "flat"
-- we are just illustrating how to set things up
myBoxLowRes = Box mapCoords:True name:"Low Res"
myBoxHighRes= Box mapCoords:true name:"High Res"

-- Add Projection Modifier to one
modPanel.addModToSelection (Projection name:"My Projection 1") ui:on
addPModObjects  myBoxLowRes false true objList:myBoxHighRes

-- Add a normal map:
map =    BakeToTexture.addMap myBoxLowRes #NormalMap

-- Get various Projection properties
map.projectionModifierName -- should return #(Projection:Projection)
map.projectionModifierIndex -- should return 1
map.projectionMethod -- should return 0
map.projectionMaxRayDistance -- should return 10.0
map.projectionResolveHitType -- should return 1
map.projectionHitOnlyMatchingMatID -- should return false
map.projectionRayMissColorType -- should return 0
map.projectionDiagnosticColor -- should return (color 255 0 0)