Baking to texture Max 2021

So i'm working with this script to bake all the textures to export it as fbx eventually. It's a script that is used here: 

https://www.youtube.com/watch?v=xBYkVS3ctbo

Unfortunatly it does not work yet on my mesh models. I'm getting an active render error and i'm not sure what it means. It won't render the completemap. We use vray. Hoping for some help. And perhaps other people can use it then too. 

 

SCRIPT:

(

-- capture selected objects

selectedObjectsAll = deepcopy (selection as array)

selectedObjects =#()

for a in SelectedObjectsAll do

(

if (superclassof a == GeometryClass) then append SelectedObjects a

)

 

-- clear up the BTT list

BakeToTexture.deleteAllMaps()

 

BakeMaps = #()

 

-- get the project folder and set the destination folder for the MapScaler

BakeToTexture.outputPath = pathConfig.getCurrentProjectFolder() + "\\sceneassets\\images"

 

-- adds a complete map in 1024x1024 for all my selected objects and set it to map channel 2

for a in SelectedObjects do

(

-- if there is no UV channel 2, create One

if (meshop.getMapSupport a.mesh 2) != true then

(

select a

max modify mode

NewModifier = Unwrap_UVW()

addmodifier a NewModifier

a.modifiers[1].setMapChannel 2

a.modifiers[1].setApplyToWholeObject true

a.modifiers[1].flattenMapNoParams()

a.modifiers[1].pack 2 0.001 true true true

subObjectLevel = 0

deselect a

)

 

TheNewMap = BakeToTexture.addMap a #CompleteMap

TheNewMap.imageWidth = 1024

TheNewMap.imageHeight = 1024

TheNewMap.uvChannel = 2

append BakeMaps TheNewMap

)

 

-- bakes the Maps

BakeToTexture.bake()

 

-- moves the UVs from channel 2 to channel 1 so that the objects can use the complete map on channel 1

for a in BakeMaps do

(

ChannelInfo.CopyChannel a.sceneObject 3 2

ChannelInfo.PasteChannel a.sceneObject 3 1

 

a.sceneObject.material = Physical_Material()

a.sceneObject.material.base_color_map = bitmaptexture()

a.sceneObject.material.base_color_map.filename = (BakeToTexture.outputPath + "\\" + a.filetype)

-- put the rendered images in th diffuse slot of all my materials

)

)

 

ERROR:

-- Error occurred in anonymous codeblock; filename: X:\stockpoint - 3D\Scripts\Maxscript\BF Tools\mcr\Development\bf_BakeToTexture\bf_BakeToTexture_01.ms; position: 1169; line: 41

-- Runtime error: Cannot add the map. This map type #(65537, 0) is incompatible with the active renderer, or the specified class ID is not valid.

-- MAXScript callstack:

--thread data: threadID:29068

--------------------------------------------------------

--[stack level: 0]

--In a loop; filename: X:\stockpoint - 3D\Scripts\Maxscript\BF Tools\mcr\Development\bf_BakeToTexture\bf_BakeToTexture_01.ms; position: 1048; line: 36

--member of: anonymous codeblock

--Parameters:

--a: $Editable_Mesh:wiremesh @ [34087.792969,27623.830078,93.220917]

--Locals:

--NewModifier: Unwrap_UVW:Unwrap UVW

--a: $Editable_Mesh:wiremesh @ [34087.792969,27623.830078,93.220917]

--TheNewMap: undefined

--Externals:

--subObjectLevel: SystemGlobal:subObjectLevel : 0

--BakeMaps: #()

--owner: <CodeBlock:anonymous>

--------------------------------------------------------

--[stack level: 1]

--called from anonymous codeblock; filename: X:\stockpoint - 3D\Scripts\Maxscript\BF Tools\mcr\Development\bf_BakeToTexture\bf_BakeToTexture_01.ms; position: 1170; line: 41

--Locals:

--selectedObjects: #($Editable_Mesh:wiremesh @ [34087.792969,27623.830078,93.220917], $Editable_Mesh:Circle048 @ [34138.640625,27628.189453,66.239578], $Editable_Mesh:Canvas002 @ [34155.617188,27617.250000,81.204674], $Editable_Mesh:Lijst002 @ [34155.617188,27617.250000,81.204674], $Editable_Mesh:Canvas003 @ [34174.394531,27607.175781,64.950714], $Editable_Mesh:Lijst003 @ [34174.394531,27607.175781,64.950714], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelblad florance @ [34159.011719,27627.054688,22.475920], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelblad florance @ [34159.011719,27627.054688,64.382820], $Editable_Mesh:Tafelblad glas florance @ [34159.011719,27627.054688,65.943672], $Editable_Mesh:O-Luce - Atollo - lamp frame @ [34168.445313,27642.119141,123.153961], $Editable_Mesh:O-Luce - Atollo - lamp @ [34167.109375,27643.052734,3.342739])

--selectedObjectsAll: #($Editable_Mesh:wiremesh @ [34087.792969,27623.830078,93.220917], $Editable_Mesh:Circle048 @ [34138.640625,27628.189453,66.239578], $Editable_Mesh:Canvas002 @ [34155.617188,27617.250000,81.204674], $Editable_Mesh:Lijst002 @ [34155.617188,27617.250000,81.204674], $Editable_Mesh:Canvas003 @ [34174.394531,27607.175781,64.950714], $Editable_Mesh:Lijst003 @ [34174.394531,27607.175781,64.950714], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelblad florance @ [34159.011719,27627.054688,22.475920], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelpoot Florance @ [34159.011719,27627.054688,25.955999], $Editable_Mesh:Tafelblad florance @ [34159.011719,27627.054688,64.382820], $Editable_Mesh:Tafelblad glas florance @ [34159.011719,27627.054688,65.943672], $VRayLight:VRayLight001 @ [34168.445313,27642.119141,119.625076], $Editable_Mesh:O-Luce - Atollo - lamp frame @ [34168.445313,27642.119141,123.153961], $Editable_Mesh:O-Luce - Atollo - lamp @ [34167.109375,27643.052734,3.342739])

--BakeMaps: #()

--Externals:

--------------------------------------------------------

--[stack level: 2]

--called from top-level