[Closed] how to bake textures?
I am trying to figure out how to cause textures to be baked. The functions for setting up the bake are documented well enough…as well as some function for baking over a network…but it’s not clear how to regularly render to textures.
Macro_BakeTextures.mcr
Now, this above file contains the script for the “Render to Texture” menu that has a “Render” button that does what I need. Unfortunately it’s rather complicated…the on button code for this render button is as follows:
on bRender pressed do if workingObjects.count != 0 do
(
selectedObjectProps.CloseWorkingObjects() -- capture changes
selectedElementProps.CloseSelectedElement() -- capture changes
-- selectedElementProps.OnObjectSelectionChange() -- reselect elements
-- flatten everybody
ignoreSelectionUpdates = true
local old_autoBackup_enabled = autoBackup.enabled
autoBackup.enabled = false
try
(
if _debug do format "doAutoUnwrap_Obj: %; autoUnwrapChannel_Obj: %;
" doAutoUnwrap_Obj autoUnwrapChannel_Obj
if _debug do format "doAutoUnwrap_SubObj: %; autoUnwrapChannel_SubObj: %;
" doAutoUnwrap_SubObj autoUnwrapChannel_SubObj
if doAutoUnwrap_Obj or doAutoUnwrap_SubObj do
(
undo "Flatten Objects" on
(
-- update bake channel on nodes
for obj_i in workingObjects do
(
if doAutoUnwrap_Obj do
obj_i.node.INodeBakeProperties.bakeChannel = autoUnwrapChannel_Obj
if doAutoUnwrap_SubObj do
obj_i.node.INodeBakeProjProperties.subObjBakeChannel = autoUnwrapChannel_SubObj
)
BatchFlatten workingObjects autoUnwrapMappingProps.sThresholdAngle.value autoUnwrapMappingProps.sSpacing.value \
autoUnwrapMappingProps.cRotate.checked autoUnwrapMappingProps.cFillHoles.checked
)
)
)
catch
(
ignoreSelectionUpdates = false
autoBackup.enabled = old_autoBackup_enabled
throw
)
ignoreSelectionUpdates = false
if _debug do format "bRender pressed - starting test
"
try
(
if ExposureControlOK() and (MapCoordsOK workingObjects ) and ((bakedMtlProps.cbRenderToFilesOnly.checked) or (MapTargetsOK workingObjects)) do
(
if _debug do format "bRender pressed - passed test
"
-- then bake the textures
ignoreSelectionUpdates = true
ignoreMtlUpdates = true
if _debug do format "bRender pressed - calling batchBake
"
if commonBakeProps.cNetworkRender.checked then
NetBatchBake workingObjects
else
BatchBake workingObjects
ignoreSelectionUpdates = false
ignoreMtlUpdates = false
)
)
catch
(
autoBackup.enabled = old_autoBackup_enabled
throw
)
autoBackup.enabled = old_autoBackup_enabled
)
The key command seems to be “BatchBake workingObjects”
BatchBake isn’t in the ref…but I’m not sure how to turn my object into a “working object” in order to bake it.
Ok, after further investigation BatchBake is just an internal function…which leads to more and yet more internal functions.
To actually do the baking, there is a secret undocumented render type:
render rendertype:#bakeSelected
[size=2]It’s not listed among the complete list of rendertypes in the max reference.[/size]
[size=2][/size]
If anyone has any information, It’d be appreciated…because there appears to be some other settings that have to be done before calling this render in order for it to actually do the baking.
edit: this is what I have so far
--get an object first
obj = selection[1]
--something you have to do
bi = obj.INodeBakeProperties
--dim of the texture map to create
dim = 512
--type of map to bake
alightMap = LightingMap()
alightMap.outputSzX = dim
alightMap.outputSzY = dim
alightMap.targetMapSlotName = "Diffuse Color"
--something you have to do
bi.addBakeElement alightMap
--refs they use in the format line
inbp = obj.INodeBakeProperties
inbpp = obj.INodeBakeProjProperties
--variables I attempted to set by guessing at what they should be
indpp.enabled = true
indpp.projectionMod = Projection()
indpp.projectionModTarget = alightMap.targetMapSlotName
--line of code from official script
format "rendering node: % : % : % : % : %
" obj.name inbpp.enabled inbpp.projectionMod inbpp.projectionModTarget inbp.bakeChannel
--render command
render rendertype:#bakeSelected
It doesn’t give me any errors but the render is just blank, and it doesn’t create a bmp in the diffuse slot either.
That is strange. The first time that i used this script, I had an image of textures ! (but with an bitmap creation error).
After I realized that you used “indpp” instead of “inbpp” and I made some changes… and the interface crashed
Since, I have just a blank screen like you…
That would be an fanstastic option to be able to control the “render to texture” with MAXscript.
All of the information necessary to do this properly is contained within this file:
c:\3dsmax7\UI\MacroScripts\Macro_BakeTextures.mcr
It’s just not easy to trace it through!
update: ok, got it to render the texture. you need to set
obj.INodeBakeProperties.bakeEnabled = true
because it is false by default
also, you don’t need the projection stuff
So, this “sort of works” but still im getting an error like “failed to write bitmap” after it finishes rendering
obj = selection[1]
bi = obj.INodeBakeProperties
--create shadow map bake element
alightMap = LightingMap()
alightMap.outputSzX = 256
alightMap.outputSzY = 256
--alightMap.targetMapSlotName = "Diffuse Color"
alightMap.filename = "Sphere01LightingMap.bmp"
bi.addBakeElement alightMap
bi.bakeEnabled = true
format "rendering node: % : % : % : % : %
" obj.name undefined undefined undefined bi.bakeChannel
render rendertype:#bakeSelected outputwidth: alightMap.outputSzX outputheight: alightMap.outputSzY outputfile: alightMap.filename
I don’t get the error message, but a black image.
If I open the Render to Texture Dialog the object has a LightingMap in the output for everytime I execute the script …
**update: if I execute your script after opening the dialog the texture renders but a error message is also raised: Error creating Bitmap
Georg
I have just found a solution.
That seems to work without error.
Say me if that’s okay for you too.
fn renderLightmap obj currentMapType Xsize Ysize channel fullFileName =
(
fPath=getFilenamePath fullFileName
fName=getFilenameFile fullFileName
fType=getFilenameType fullFileName
bi = obj.INodeBakeProperties
currentMapType.outputSzX=Xsize
currentMapType.outputSzY=Ysize
currentMapType.filename=fName
currentMapType.fileType=fType
bi.addBakeElement currentMapType
bi.bakeChannel=channel
bi.bakeEnabled = true
render rendertype:#bakeSelected outputwidth:Xsize outputheight:Ysize outputfile:(fPath+fName+fType)
bi.removeAllBakeElements()
bi.bakeEnabled = false
format "rendering node:% channel:%
" obj.name bi.bakeChannel
)
obj=selection[1]
alightMap=LightingMap()
renderLightmap obj alightMap 384 384 1 "C:\ heLightingMap.tga"
-- fullFileName: use \\ in string instead of \
-- "C:\ est\ heLightingMap.tga"
Nice! Thanks prettyPixel!
There’s just one more thing I’d like to know how to do with this…and it’s more of a general rendering parameter… is there a way to suppress/prevent the rendering window from coming up? I tried quiet mode but that doesn’t do it.
figured it out: set vfb: false
exact
fn renderLightmap obj currentMapType Xsize Ysize channel fullFileName virtualFrameBufferState =
(
fPath=getFilenamePath fullFileName
fName=getFilenameFile fullFileName
fType=getFilenameType fullFileName
bi = obj.INodeBakeProperties
currentMapType.outputSzX=Xsize
currentMapType.outputSzY=Ysize
currentMapType.filename=fName
currentMapType.fileType=fType
bi.addBakeElement currentMapType
bi.bakeChannel=channel
bi.bakeEnabled = true
render rendertype:#bakeSelected outputwidth:Xsize outputheight:Ysize outputfile:(fPath+fName+fType) vfb:virtualFrameBufferState
bi.removeAllBakeElements()
bi.bakeEnabled = false
format "rendering node:% channel:%
" obj.name bi.bakeChannel
)
obj=selection[1]
alightMap=LightingMap()
renderLightmap obj alightMap 384 384 1 "C:\ heLightingMap.tga" false