📄 macro_baketextures.mcr
字号:
if end != undefined then (
-- single
sequenceStart = sequenceEnd = end
return n+1
) else
return 0 -- done
) -- end, parse frame string
-- this call sorts out the rendTimeType and returns a sequence
-- get a (perhaps one of many) frame sequence, return 0 if done or strPos for next call. init strPos at 1
fn getSequence _strPos =
(
sequenceStart = sequenceEnd = 0
case rendTimeType of
(
1: ( -- single
sequenceStart = sequenceEnd = currentTime
)
2: ( -- active animation segment
sequenceStart = animationRange.start
sequenceEnd = animationRange.end
)
3: ( -- from...to frame
sequenceStart = rendStart
sequenceEnd = rendEnd
)
4: ( -- string of frames, parse off next sequence
return parseFrameString rendPickupFrames _strPos
)
) -- end, case of rendTimeType
-- convert to frame #
if classof( sequenceStart ) == time then
sequenceStart = (sequenceStart as integer) / ticksPerFrame
if classof( sequenceEnd ) == time then
sequenceEnd = (sequenceEnd as integer) / ticksPerFrame
return if _strPos > 1 then 0 else 2 -- one shot only except pickups
)
-- utility to get total frames in the bake, so sum over all sequences
fn getTotalFrames =
(
n = 0
p = 1
while (p = getSequence( p )) != 0 do
(
--format "start = %, end = %, strPos = %\n" sequenceStart sequenceEnd p
n += sequenceEnd - sequenceStart + 1
)
return n
)
-------------- function to skip objects w/ map conflicts
fn skipObject _obj =
(
if _obj.nDilations < 0 then
(
n = if( _obj.nDilations==-100 ) then 0 else -_obj.nDilations
_obj.nDilations = n
return true
)
return false
)
------------------------------------------------------------------
--
-- Function to bake a set of textures on each of a set of objects
--
function batchBake _ObjectList =
(
--format "bake % objects \n" _ObjectList.count
-- select the settings to use
renderer = if (commonBakeProps.rDraftOrProduction.state == 1) then #production else #draft
-- commit the render scene dialog if it's still up
renderSceneDialog.commit()
-- disableSceneRedraw()
with redraw off (
numFrames = getTotalFrames()
-- create the bake progress dialog
progressScale = 100 / ((_ObjectList.count) * numFrames + 1)
-- progressStart "Baking Textures..."
rollout bakeProgress "Progress..." width:183 height:48
(
label lbl1 "Baking Textures..." pos:[48,6] width:94 height:21
progressBar pb1 "" pos:[5,22] width:174 height:17
)
-- & put it up
createdialog bakeProgress -- style:#(#style_border,#style_toolwindow)
bakeProgress.pb1.value = 0
-- find the frame to update
updateFrameNum = (currentTime as integer)/ticksPerFrame
-- bake each object in turn
for i = 1 to _ObjectList.count do
(
obj = _ObjectList[i];
if (obj.effectiveEnable() == false ) or ( objectIsBakable( obj )==false ) or skipObject( obj ) then
continue
local restoreToGroup = false
if isGroupMember( obj ) then
(
setGroupMember( obj )( false )
restoreToGroup = true
)
-- select the object
select( obj )
-- bake the object
w = obj.renderWidth()
h = obj.renderHeight()
--format " bake object % to % x % \n" (obj.name) w h
if w == 0 or h == 0 then
continue
if (curBM == undefined) or (curBM.width != w ) or (curBM.height != h) then
(
-- create new bm
-- do we need to delete this ???? crashes!
-- if curBM != undefined then delete curBM
if curBM != undefined then
unDisplay( curBM )
curBM = bitmap( w )( h )
)
vfbOn = if commonBakeProps.cDisplayFB.checked then true else false
-- force render to use original material for baking
local saveRenderMtlIndex = -1
materialType = (classof obj.material as string)
if (materialType == "Shell_Material") then
(
saveRenderMtlIndex = obj.material.renderMtlIndex
obj.material.renderMtlIndex = 0
)
-- for each sequence of frames
p = 1
while (p = getSequence( p )) != 0 do
(
-- for each frame of the sequence.....
--format " render frames: % to %\n" sequenceStart sequenceEnd
nFrame = sequenceStart
while( nFrame <= sequenceEnd) do
(
-- update the bitmap names
n = if (rendTimeType == 2) or (rendTimeType == 3) then
(nFrame - sequenceStart + rendFileNumberBase)
else nFrame
updateBitmapFilenames( obj )( n )
-- update the progress bar
bakeProgress.pb1.value = progressScale * ( ((i-1) * numFrames) + ((nFrame+1) as float) )
-- check if the files already exist
if not checkFileOverwrite( obj ) then
(
-- don't overwrite files, boot
--format "can't overwrite files\n"
destroydialog bakeProgress
-- progressEnd()
select( selectedObjects ) -- reselect
enableSceneRedraw()
return 0 -- cancel
)
-- render the texture elements
--format "render frame % \n" nFrame
wasCanceled = false
render rendertype:#bakeSelected frame:nFrame to:curBM vfb:vfbOn cancelled:&wasCanceled
-- check for cancel, check files & other progress bars
--if ( not filesWritten( obj )) then
--format "canceled rendering = % \n" wasCanceled
if ( wasCanceled ) then
(
format "canceled rendering\n"
destroydialog bakeProgress
-- progressEnd()
select( selectedObjects ) -- reselect
enableSceneRedraw()
return 0 -- cancel
)
-- check if this is the frame we want to update to
if( nFrame == updateFrameNum ) then
(
--format "collect files for frame = %\n" nFrame
collectUpdateFiles( obj )
)
nFrame += rendNthFrame -- update to next frame
) -- end, for each frame
) -- end, for each sequence
-- prepare baked materials?
if ( commonBakeProps.cShellMaterials.checked ) then
(
applyUpdateFiles( obj )
updateMaterial( obj )
) -- end, shell materials enabled
-- restore object to the group
if restoreToGroup then
setGroupMember( obj )( true )
if saveRenderMtlIndex >= 0 then
obj.material.renderMtlIndex = saveRenderMtlIndex
-- flag that we can do a redo of the last render
gTextureBakeDialog.bUpdateLast.enabled = true
format "end, bake object\n"
) -- end, for each object
-- toss the progress dialog
destroydialog( bakeProgress )
-- progressEnd()
-- reselect
select( selectedObjects )
) -- end, with redraw off
--enableSceneRedraw()
redrawViews()
) -- end, function batch bake
------------------------------------------------------------------
--
-- Main Texture Baking Shell Rollout
--
rollout gTextureBakeDialog "Render To Textures"
width:350 height:485
(
-- sub rollout for selected object porperties
SubRollout rollouts ""
pos:[1,2] width:342 height:483
-- the "do it" buttons
button bRender "Render"
pos:[7,493] width:66 height:24
enabled:true
button bUpdateLast "Update Last"
pos:[80,493] width:66 height:24
enabled:false
button bClose "Close"
pos:[227,493] width:50 height:24
enabled:true
button bCancel"Cancel"
pos:[284,493] width:50 height:24
enabled:true
------------------------------------------------------------------
--
-- function to update things when the max selection changes
--
function onChangeSelection =
(
-- filter out reselecting the same thing
if selection != selectedObjects then
(
-- format "update selection changed old = % to new = %\n" selectedObjects selection
-- close out & write previously selected object
selectedObjectProps.CloseSelectedObject()
-- & select new one
selectedObjectProps.UpdateObjectSelection()
) -- end, selection is different
)
-----------------------------------------------------------------------------
--
-- this function handles reset & new event callbacks
--
function onReset =
(
if curBM != undefined then
unDisplay( curBM )
-- remove the selectionSetChange callback
callbacks.removeScripts id:#bakeSelectionHandler
callbacks.removeScripts id:#bakeResetHandler
callbacks.removeScripts id:#bakeNewHandler
-- callbacks.removeScripts id:#bakeFileOpenHandler
-- & close the dialog if it's not already
destroydialog( gTextureBakeDialog )
)
function onFileOpen =
(
--format "newFileLoaded == true \n"
newFileLoaded = true
bUpdateLast.enabled = false
)
-------------------------------------------------------------
--
-- Bake Texture Button Pressed
--
on bRender pressed do
(
selectedObjectProps.closeSelectedObject() -- capture changes
selectedObjectProps.UpdateObjectSelection() -- reselect things
-- bake selected
if (commonBakeProps.rSceneType.state) == 1 then
ObjList = selection as array
-- bake all
else if (commonBakeProps.rSceneType.state) == 2 then
ObjList = geometry as array
-- make a directory if it doesn't already exist
validateDirectory( commonBakeProps.GetFilePath() )
-- remove old shell materials
-- this is a hack to get around a bug in vp materials on load: 456913, 454464
if newFileLoaded == true or initialRender == true then
(
--format "remove bake materials, newFile = % \n" newFileLoaded
removeBakeMaterials()
)
-- flatten everybody
allowSelectionUpdates = false
batchFlatten( ObjList )
-- then bake the textures
batchBake( ObjList )
newFileLoaded = false -- reset the new file switch
initialRender = false
allowSelectionUpdates = true
)
-------------------------------------------------------------
--
-- Update Last Bake Texture
-- uses the previous selection
--
on bUpdateLast pressed do
(
if bUpdateLast.enabled then
(
allowSelectionUpdates = false
batchFlatten( ObjList )
-- then bake the textures
batchBake( ObjList )
allowSelectionUpdates= true
)
)
on gTextureBakeDialog open do
(
isCancel = false
toolMode.commandMode = #SELECT
)
-------------------------------------------------------------
--
-- dialog is being closed. only hook for X Button pressed
--
on gTextureBakeDialog close do
(
-- format "close dlg\n"
if isCancel == false then
(
-- format " save open object \n"
-- save things to the selected object
selectedObjectProps.CloseSelectedObject()
)
-- close any vfbs
if curBM != undefined then
unDisplay( curBM )
-- remove the selectionSetChange callback
callbacks.removeScripts id:#bakeSelectionHandler
callbacks.removeScripts id:#bakeResetHandler
callbacks.removeScripts id:#bakeNewHandler
--callbacks.removeScripts id:#bakeFileOpenHandler
pDialogPos = GetDialogPos( gTextureBakeDialog )
--format "dialog pos = ( %, %) \n" pDialogPos.x pDialogPos.y
-- pCommonRolledUp = not commonBakeProps.open
-- pSelectedRolledUp = not selectedObjectProps.open
-- if uniqueElementProps != undefined then
-- pUniqueRolledUp = not uniqueElementProps.open
-- else
pUniqueRolledUp = false
-- format "update rollup states: common = %, selected = % \n" pCommonRolledUp pSelectedRolledUp
-- & close the dialog if it's not already
destroydialog gTextureBakeDialog
)
-------------------------------------------------------------
--
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -