📄 macro_baketextures.mcr
字号:
Macroscript BakeDialog
ButtonText:"Render To Texture..."
category:"Render"
internalcategory: "Render"
toolTip:"Render to Texture..."
---------------------------------------------------------------------------
--
-- Render to Texture Dialog
-- 2/06/02
-- Kells Elmquist, discreet3d
--
--
--
(
-- these control behaviour of the dialog, change these statically
local allowControlDisable = false
-- these are the globals
global gTextureBakeDialog -- the shell holds one instance of each of the other rolloups
global gTextureBakeOverwriteFilesOk -- 0 == no overWrite, 1 == ok to overwrite, 2 == ok & not again
-- one time init on new session....
if gTextureBakeOverwriteFilesOk == undefined then
gTextureBakeOverwriteFilesOk = 0
local commonBakeProps
local selectedObjectProps
local uniqueElementProps -- constructed for each element w/ unique parameters
-- this will hold the list of objects to bake
local ObjList = #()
local selectedObjects = #()
local selectedElement
local selectedElementIndex = 0
local allowSelectionUpdates = true
local commonElements = #( #(), #(), #(), #(), #(), #(), #(), #(), \
#(), #(), #(), #(), #(), #(), #(), #() )
local isCancel = false
local defaultFileType = ".tga"
local curFrameString = "0000" -- filename fragment for sequences
local curBM = undefined
local sequenceStart
local sequenceEnd
local overwriteFileName = ""
local updateFileNames = #()
local newFileLoaded = false
local initialRender = true
------------------------------------------------------------------------
--
-- persistant dialog size & position
--
local pDialogHeight
local pDialogPos
local pCommonRolledUp
local pSelectedRolledUp
local pUniqueRolledUp
-----------------------------------------------------------------------------------------
--
-- this section allows any generated bake map to be baked to any material map channel
--
local stdTextures = #( "ambientMap", "diffuseMap", "specularMap", "diffuseLevelMap", \
"specularLevelMap", "glossinessMap", "selfIllumMap", "opacityMap", "filterMap", \
"bumpMap", "reflectionMap", "refractionMap", "displacementMap", "diffuseRoughnessMap", \
"anisotropyMap", "orientationMap", "glossinessMap2", "anisotropyMap2", "orientationMap2" )
local pbmTextures = #( "ambientMap", "diffuseMap", "specularMap", \
"specularLevelMap", "glossinessMap", "selfIllumMap", "opacityMap", "filterMap", \
"bumpMap", "reflectionMap", "refractionMap", "displacementMap" )
local anisoTextures = #( "ambientMap", "diffuseMap", "specularMap", "diffuseLevelMap", \
"specularLevelMap", "glossinessMap", "selfIllumMap", "opacityMap", "filterMap", \
"bumpMap", "reflectionMap", "refractionMap", "displacementMap", "diffuseRoughnessMap", \
"anisotropyMap", "orientationMap" )
local onbTextures = #( "ambientMap", "diffuseMap", "specularMap", "diffuseLevelMap", \
"specularLevelMap", "glossinessMap", "selfIllumMap", "opacityMap", "filterMap", \
"bumpMap", "reflectionMap", "refractionMap", "displacementMap", "diffuseRoughnessMap" )
local straussTextures = #( "diffuseMap", "glossinessMap", "metalnessMap", "opacityMap", "filterMap", \
"bumpMap", "reflectionMap", "refractionMap", "displacementMap" )
-- these are set up by the selected bake material mappings ini file
local bakeTextures = #( "","","","", "","","","", "","","","", "","","","", "","","","" )
-- these are the sections of the ini file
local numMaxFileMappings = 20
local fileMappings = #( "", "", "", "", "", "", "" , "", "", "", "" )
local useShader = "blinn"
local hwShaders = #( "", "", "" )
local useHWShader = 0
--------------------------------------------------------------------
--
-- hardware shaders
-- change these if the official names of these shaders changes
--
-- local generalHWShaderName = "Material Shader"
local generalHWShaderName = "Metal Bump"
local lightMapHWShaderName = "LightMap"
--------------------------------------------------------------------------
--
-- ini files
--
-- this is the default ini file for the file mappings
local bakeMtlMappingsIniFile = "$plugcfg/BakeTextureMappings.ini"
-- this is the dialog state ini file, holds persistent dialog state
local iniFile = "$plugcfg/BakeTexture.ini"
----------------------------------------------------------------------------
-- this function is used everywhere to determine if an object can be baked
fn objectIsBakable _obj =
(
local isBakable = (superclassof _obj == geometryClass) \
and (classof _obj != Spray ) \
and (classof _obj != SuperSpray ) \
and (classof _obj != PCloud ) \
and (classof _obj != PArray ) \
and (classof _obj != Snow ) \
and (classof _obj != Blizzard ) \
and (classof _obj != Targetobject ) \
and ( _obj.isHidden == false )
--format "superClass = %, class = %, isBakable = % \n" (superclassof _obj)(classof _obj)(isBakable)
return isBakable
)
-- the unwrapper returns 0 for channel 1 (0,2,3..) for strange historical reasons
fn getMapChannel _unwrapMod =
(
local n = _unwrapMod.getMapChannel()
return if( n == 0 ) then 1 else n
)
-- function to delete the autoFlattener on an object
fn deleteAutoFlatten _obj =
(
-- test each modifier on the object
unwrapMod = undefined
for nMod = 1 to _obj.modifiers.count do
(
-- get the next modifier
unwrapMod = _obj.modifiers[ nMod ]
if (classof unwrapMod as string) == "Unwrap_UVW" then
(
-- it's an unwrap modifier,
if unwrapMod.name == "Automatic Flatten UVs" then
(
--format "removing auto unwrapper: % \n" unwrapMod.name
deleteModifier( _obj )( unwrapMod )
)
) -- end, it's an unwrapper
) -- end, for each modifier
)
fn deleteBakeMaterial _curObj =
(
-- if the material is a shell material, lose it
materialType = (classof _curObj.material as string)
-- format "material type = %\n" materialType
if (materialType == "Shell_Material") then
(
--format "remove bake material on %\n" (curObj as string)
origMaterial = _curObj.material.originalMaterial
_curObj.material.originalMaterial = undefined
_curObj.material.bakedMaterial = undefined
_curObj.material = origMaterial
) -- end, has shell material
)
---------------------------------------------------------------------------
--
-- Function to auto-flatten the objects from a list
--
function batchFlatten _ObjectList =
(
undo on
(
if commonBakeProps.cAutoFlattenOn.checked then
(
--format "flatten % objects \n" (_ObjectList.count)
-- first put up the progress dialog
rollout flattenProgress "Progress..." width:183 height:46
(
label lbl1 "Flattening UV's..."
pos:[48,5] width:94 height:21
progressBar pb1 ""
pos:[5,21] width:174 height:17
)
createdialog flattenProgress -- style:#(#style_border,#style_toolwindow)
local progressScale = 100 / (_ObjectList.count + 1)
flattenProgress.pb1.value = progressScale
-- why is this here? must be in modify mode to use flatten operator
max modify mode
with redraw off (
-- for each object...
local nObj = 0
for curObj in _ObjectList do
(
nObj += 1
local skipObject = false
if (curObj.effectiveEnable() == false ) or (objectIsBakable( curObj ) == false) then
(
--format "ignoring object: % \n" (classof curObj as string)
continue
)
restoreToGroup = false
if isGroupMember( curObj ) then
(
setGroupMember( curObj )( false )
restoreToGroup = true
)
-- select the object to apply flatten operator
select( curObj )
-->>>>>>>>>
-- get rid of sub-object selections
-- local hasSubObjectSelections = false
local curClass = (classof curObj as string)
--format "object class = % \n" curClass
if (curClass == "Editable_Patch") or ( curClass == "quadPatch") or (curClass == "triPatch") then
messageBox "Editable patch objects not currently supported for flattening and may produce poor results."
-- if (curClass == "Editable_mesh") or (curClass == "Editable_poly") then
-- (
-- objSelections = getFaceSelection( curObj )
-- format "undo subobject selection: % \n" (objSelections as string)
-- setFaceSelection( curObj )( #() )
-- hasSubObjectSelections = true
-- )
unwrapMod = undefined
hasModifier = false
-- test each modifier on the object
for nMod = 1 to curObj.modifiers.count do
(
-- get the next modifier
unwrapMod = curObj.modifiers[ nMod ]
--format "modifier class = % \n" (classof unwrapMod as string)
if (classof unwrapMod as string) == "Unwrap_UVW" then
(
---format "class Unwrap_UVW\n"
-- it's an unwrap modifier,
if unwrapMod.name == "Automatic Flatten UVs" then
(
--format "has auto unwrapper: % \n" unwrapMod.name
-- If force re flatten is on and the object has the right modifiers,
hasModifier = true
-- compare against previous global settings for changes
--format "channel= %, angle = %, spacing = %, normalize = % rotate = %, fill = %, newFile = % \n" \
-- (unwrapMod.getMapChannel())(unwrapMod.getFlattenAngle())(unwrapMod.getFlattenSpacing()) \
-- (unwrapMod.getFlattenNormalize())(unwrapMod.getFlattenRotate())(unwrapMod.getFlattenFillHoles())(newFileLoaded)
--format "mod channel= %, obj channel = %\n" (unwrapMod.getMapChannel())(curObj.bakeChannel)
-- the baked material has bad uv coords if these dont match
if ( getMapChannel( unwrapMod ) != curObj.bakeChannel ) then
(
deleteBakeMaterial( curObj )
)
if ( unwrapMod.getPreventFlattening() == false ) \
and( newFileLoaded \
or ( getMapChannel( unwrapMod ) != curObj.bakeChannel )\
or ( unwrapMod.getFlattenAngle() != commonBakeProps.sThresholdAngle.value ) \
or ( unwrapMod.getFlattenSpacing() != commonBakeProps.sSpacing.value ) \
or ( unwrapMod.getFlattenRotate() != commonBakeProps.cRotate.checked ) \
or ( unwrapMod.getFlattenFillHoles() != commonBakeProps.cFillHoles.checked ) ) then
(
--format "Update unwrap_uvw\n"
deleteAutoFlatten( curObj )
deleteBakeMaterial( curObj )
hasModifier = false -- force new modifier
/* update doesnt work when you change channels for some reason. 9/5/02
format "curChannel= % \n" (unwrapMod.getMapChannel())
unwrapMod.setMapChannel( curObj.bakeChannel )
unwrapMod.setFlattenAngle( commonBakeProps.sThresholdAngle.value )
unwrapMod.setFlattenSpacing( commonBakeProps.sSpacing.value )
unwrapMod.setFlattenNormalize( true )
unwrapMod.setFlattenRotate( commonBakeProps.cRotate.checked )
unwrapMod.setFlattenFillHoles( commonBakeProps.cFillHoles.checked )
unwrapMod.flattenMap \
commonBakeProps.sThresholdAngle.value \
#([1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]) \
commonBakeProps.sSpacing.value \
true \
2 \
commonBakeProps.cRotate.checked \
commonBakeProps.cFillHoles.checked
*/
) -- end, unwrapper not locked
else (
--format "no change in object: % \n" (curObj as string)
)
) -- end, has autoflatten unwrapper
else if ( getMapChannel( unwrapMod ) == curObj.bakeChannel ) then
(
-- channel match, it's a user unwrapper for this obj, leave it alone
--format "non-automatic unwrapper found with matching channel\n"
hasModifier = true
) else (
--format "non-automatic unwrapper found with un-matched channel= % \n" (unwrapMod.getMapChannel())
)
) -- end, is unwrap modifier
else (
--format "object class = %\n" (classof unwrapMod as string)
if (classof unwrapMod as string) == "Uvwmap" then
-- or (classof unwrapMod as string) == "UVW_Xform" then
(
--format "is uvwmap\n"
-- potential mapping channel conflict
mapChan = unwrapMod.mapChannel
if mapChan == 0 then mapChan = 1
if( mapChan == curObj.bakeChannel ) then (
messageBox "Map Channel in UVW_Mapping modifier conflicts with the channel specified for render to texture. Select a different render to texture channel."
skipObject = true
curObj.nDilations = if curObj.nDilations == 0 then -100 else -curObj.nDilations
)
)
)
)-- end, for each modifier
-- If the object doesn't have a modifier applied, create one and flatten it
if (hasModifier == false) and objectIsBakable( curObj ) and (skipObject == false ) then
(
-- create a new autoflatten unwrapper
--format "Create new unwrap_uvw\n"
unwrapMod = unwrap_UVW()
unwrapMod.setAlwaysEdit ( false )
unwrapMod.setMapChannel ( curObj.bakeChannel )
unwrapMod.setFlattenAngle ( commonBakeProps.sThresholdAngle.value )
unwrapMod.setFlattenSpacing( commonBakeProps.sSpacing.value )
--unwrapMod.setFlattenNormalize( commonBakeProps.cNormalize.checked )
unwrapMod.setFlattenNormalize( true )
unwrapMod.setFlattenRotate( commonBakeProps.cRotate.checked )
unwrapMod.setFlattenFillHoles( commonBakeProps.cFillHoles.checked )
unwrapMod.setApplyToWholeObject( true )
unwrapMod.name = "Automatic Flatten UVs"
-- add it to the object
-- add directly to the object to avoid groupness
undo on (
addModifier( curObj )( unwrapMod )
)
-- #([1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]) \
-- #([0,0,1],[0,0,-1]) \
-- 2 \
-- & flatten things
unwrapMod.flattenMap \
commonBakeProps.sThresholdAngle.value \
#([1,0,0],[-1,0,0], [0,1,0],[0,-1,0], [0,0,1],[0,0,-1]) \
commonBakeProps.sSpacing.value \
true \
2 \
commonBakeProps.cRotate.checked \
commonBakeProps.cFillHoles.checked
-- or use instead of true: commonBakeProps.cNormalize.checked \
) -- end, create new unwrapper
-- if it was in a group put it back
if restoreToGroup then
setGroupMember( curObj )( true )
-- if( hasSubObjectSelections == true ) then
-- setFaceSelection( curObj )(objSelections)
-- update the progress bar
flattenProgress.pb1.value = progressScale * (nObj + 1)
) -- end, for each object
) -- end, with redrawOff
-- Auto Flatten endgame
destroydialog flattenProgress
) -- end, not already flattenned
-- move to caller: newFileLoaded = false -- reset the new file switch
)
) --end, function autoflatten
-----------------------------------------------------------------------
--
-- utility function to find elements by class name, returns index or 0
--
function findByClass _obj _className =
(
for i = 1 to _obj.NumBakeElements() do
(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -