PDA

View Full Version : how to bake textures?


stuh505
10-05-2005, 05:31 AM
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: %; \n" doAutoUnwrap_Obj autoUnwrapChannel_Obj
if _debug do format "doAutoUnwrap_SubObj: %; autoUnwrapChannel_SubObj: %; \n" 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\n"
try
(
if ExposureControlOK() and (MapCoordsOK workingObjects ) and ((bakedMtlProps.cbRenderToFilesOnly.checked) or (MapTargetsOK workingObjects)) do
(
if _debug do format "bRender pressed - passed test\n"

-- then bake the textures
ignoreSelectionUpdates = true
ignoreMtlUpdates = true
if _debug do format "bRender pressed - calling batchBake\n"
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.

stuh505
10-05-2005, 04:08 PM
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

It's not listed among the complete list of rendertypes in the max reference.

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: % : % : % : % : %\n" 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.

prettyPixel
10-06-2005, 12:21 PM
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.

stuh505
10-06-2005, 03:18 PM
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

stuh505
10-06-2005, 06:50 PM
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: % : % : % : % : %\n" obj.name undefined undefined undefined bi.bakeChannel

render rendertype:#bakeSelected outputwidth: alightMap.outputSzX outputheight: alightMap.outputSzY outputfile: alightMap.filename

rdg
10-06-2005, 07:08 PM
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

prettyPixel
10-06-2005, 07:54 PM
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:%\n" obj.name bi.bakeChannel
)

obj=selection[1]
alightMap=LightingMap()
renderLightmap obj alightMap 384 384 1 "C:\\theLightingMap.tga"

-- fullFileName: use \\ in string instead of \
-- "C:\\test\\theLightingMap.tga"

rdg
10-06-2005, 08:16 PM
seems to work!

Great!

stuh505
10-06-2005, 09:54 PM
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

prettyPixel
10-06-2005, 10:44 PM
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:%\n" obj.name bi.bakeChannel
)

obj=selection[1]
alightMap=LightingMap()
renderLightmap obj alightMap 384 384 1 "C:\\theLightingMap.tga" false

stuh505
10-07-2005, 09:19 PM
It's also important to add the line "select obj" before rendering....this was causing me some confusion because I would test, it would work, then inside my code it wouldn't work :)

arcburn
10-26-2005, 06:21 AM
How come this does not work when replacing lightingmap() with normalsmap() ? It renders but with no normal map.

stuh505
10-27-2005, 05:40 PM
Because the normal map needs a cage to calculate from, it can be done because they did it in the RTT code...

The other question is, why doesn't this code work when using the MR renderer?

jbrophy
01-11-2006, 05:30 PM
Awsome guys

I was starting a render to texture batch script and was banging my head on the wall over this. This thread is greatly appreciated.

My question is, though it states it is rendering out a lightmap, why does it look more like a diffuse map? Example. If I have a green sphere, my render comes out green. Would't a lightmap be Black and white? Also it looks like my texture is showing up in the map, shouldn't this just be the grey shaded geometry?

Thanks and sorry I am not getting some of this.

hblan
01-14-2006, 11:41 AM
used ur function , change the lightmap to completeMap . i can only get a black map . it is empty map , i dont know why . in max5 get a error , in max6 ,get a black map .

so puzzle .:sad:

now i need a completeMap bake texture . but why ?? cant ??

jbrophy
01-16-2006, 07:18 PM
Any insight as to my previous post anyone? I still am only able to make comlete maps with the current posted code. Does not make any sense to me.

Thanks
Jesse

hblan
01-17-2006, 12:50 PM
i have posted my scripts about bake texture ,and also have some questions , im going on explore bake , pls check the other topic about bake :" Baking gives "Error creating bitmap" (http://forums.cgsociety.org/showthread.php?t=159343)

thanks . wish have more helpful information from u .

CGTalk Moderation
01-17-2006, 12:50 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.