Notifications
Clear all

[Closed] Grr… m.diffusemap = myBitmap

This seems like it should work, why doesn’t it?

local m = standard()
      local myBitmap = Bitmaptexture fileName:myTiffs[i][i]
      m.diffuseMapEnable = on
      m.diffusemap = myBitmap  --or m.diffuseMap = Bitmaptexture fileName:myTiffs[i]
      m.diffuseMap.alphaSource = 2
      m.diffuseMapAmount = 100
  [/i]It's error free, but the map is not displayed in the diffuse map slot when I set the material editor to inherit the m material later on :[i] for j = 1 to myNodes.count do (meditMaterials[j] = m)[/i], nor is the bitmap texture applied to the node : [i]mySelectedsArray[j].material = m[/i]. 
  I'm on max9.  All help and online documentation agrees with the above code.  Am I missing something silly?
  
  Here is how I'm collecting the .tiff files, is there a problem with double backslashes when using this method?  I don't think so.
local images_folder
      on folderBTN pressed do (
      images_folderPre = getSavePath caption:"Set folder" initialDir:(GetDir #scripts)
      images_folder = images_folderPre + "\\"
      )
      --images_folder example: "C:\Documents and Settings\Administrator\Desktop\multiMap importer\"
      local whatAmIloookingFor
      local searchType = "tif"
      if searchType == "tif" then (whatAmIloookingFor = images_folder + "*.tif")
      local myTiffs = getFiles whatAmIloookingFor
      

So thats how the array myTiffs is populated.

  I should also add that the following code is working just fine:
m.bumpMapEnable = on
      m.bumpMapAmount = 100
      m.bumpMap = Normal_Bump ()
      m.bumpMap.normal_map = Bitmaptexture fileName:myTiffs[i]
      m.bumpMap.normal_map.alphaSource = 2
  That code will load in the normal map if the naming conventions match successfully, so I know that myTiffs[i] is a correctly formatted string.
 
 :banghead:
8 Replies

I’m building a multi-map importer for 3ds max (built to work with the multimap exporter from zbrush 4).
It automates the process of assigning specific materials to specific meshes with specific texture maps, based on naming conventions.

I think that I wasn’t being clear enough before. So here’s the wip code so far:


  --multimap importer v0, by garrick@garrickcampsey.com
  global multiMapImporterRollout
  try destroyDialog multiMapImporterRollout catch()
  rollout multiMapImporterRollout " Multimap Importer v1.0"
  (
  	local images_folder = undefined --the address to the folder where the images are saved
  	local searchType = "tif" --what type of file to look for
  	--
  	label folderSelectLabel "Set folder :" pos:[10,10]
  	button folderBTN "Choose Folder" pos:[65,10] width:117 height:18	
  	--
  	checkbox diffuseCheck "Diffuse?" checked:true pos:[10,40]
  	checkbox normalCheck "Normal?" checked:true pos:[90,40]
  	checkbox bumpCheck "Bump?" checked:true pos:[10,60] enabled:false
  	checkbox dispCheck "Displacement?" checked:true pos:[90,60] enabled:false
  	checkbox specCheck "Specular?" checked:true pos:[10,80] enabled:false
  	checkbox opacityCheck "Opacity?" checked:true pos:[90,80] enabled:false
  	--
  	checkbox otherACheck "Other" checked:false pos:[10,110] enabled:false
  	edittext otherATxt text:"naming convention" pos:[57,110] fieldWidth:120 height:18 enabled:false
  	checkbox otherBCheck "Other" checked:false pos:[10,130] enabled:false
  	edittext otherBTxt text:"naming convention" pos:[57,130] fieldWidth:120 height:18 enabled:false
  	--
  	label selectMeshesLabel "Select meshes to add maps to, then" pos:[10,165]
  	button goBTN "auto-import maps to materials" pos:[10,185] width:170 height:18
  	--
  	edittext theFeedbackTxt text:"Ready to build." pos:[6,210] fieldWidth:170 height:36
  	progressBar namesProgress "" pos:[10,250] width:170 height:10 value:0 color:[0,96,0]
  	progressBar imagesProgress "" pos:[10,262] width:170 height:10 value:0 color:[0,96,0]
  	--
  	--EVENTS
  	on goBTN pressed do	
  	(
  		--collect all selected meshes names into an array
  		local mySelectedsNamesArray = #()
  		local mySelectedsArray = #()
  		for j in $ do 
  		(append mySelectedsNamesArray j.name;append mySelectedsArray j)
  		clearSelection()
  		--print mySelectedsNamesArray
  		
  		--for every tif image found, collect it into an array
  		local whatAmIloookingFor
  		if searchType == "tif" then (whatAmIloookingFor = images_folder + "*.tif")
  		local myTiffs = getFiles whatAmIloookingFor
  		--print myTiffs
  		for i = 1 to myTiffs.count do
  		(
  			--print myTiffs[i]
  			local reduceDirs = filterString myTiffs[i] "\\"  --for the count of the array, filterString each item with "\"
  			--print reduceDirs[reduceDirs.count]
  			local findNamingConvention = filterString reduceDirs[reduceDirs.count] "-"  --for (the last one) filterString with "-"
  			--REF:ModelName-meshPartName-mapType.filetype
  			if findNamingConvention[1] != undefined and findNamingConvention[2] != undefined and findNamingConvention[3] != undefined then
  			( --check to see if dataset is valid, else invalid naming convention
  				--print findNamingConvention
  				for j = 1 to mySelectedsNamesArray.count do
  				(
  					local mySNAfiltered = filterString mySelectedsNamesArray[j] "-"
  					--findNamingConvention[1] mySNAfiltered[1] --model name
  					--findNamingConvention[2] mySNAfiltered[2] --part name
  					--findNamingConvention[3] --the map type and file type
  					if findNamingConvention[1] == mySNAfiltered[1] and findNamingConvention[2] == mySNAfiltered[2] then
  					( --check to see if model and part names match
  						--build a material to operate on
  						local m = standard()
  						m.twoSided = on
  						--compare findNamingConvention[3] to Zbrush naming conventions:
  						if findNamingConvention[3] == "DF.tif" then 
  						(
  							print "MMI says: DIFFUSE:"
  							--
  							local myBitmap = Bitmaptexture fileName:myTiffs[i]
  							m.diffuseMapEnable = on
  							m.diffusemap = myBitmap
  							--m.diffusemap.filename = myTiffs[i] 
  							m.diffuseMap.alphaSource = 2
  							m.diffuseMapAmount = 100
  							--showTextureMap m m.diffuseMap on
  							--
  							print mySelectedsArray[j]
  							print myTiffs[i]							
  						)
  						--
  						if findNamingConvention[3] == "NM.tif" then 
  						(
  							print "MMI says: NORMAL:"
  							--
  							m.bumpMapEnable = on
  							m.bumpMapAmount = 100
  							m.bumpMap = Normal_Bump ()
  							m.bumpMap.normal_map = Bitmaptexture fileName:myTiffs[i]
  							m.bumpMap.normal_map.alphaSource = 2
  							--
  							print mySelectedsArray[j]
  							print myTiffs[i]
  						)
  						--
  						--	--put each mesh's material in material editor?
  						meditMaterials[j] = m
  						--
  						mySelectedsArray[j].material = m --assign the material to the correct mesh
  						--
  					)--end if-and
  					--
  					--provide names Progress updates
  					--					
  				)--end for-do()			
  			) else ()--a files naming convention is messed up, feedback file name to listener
  			--provide images progressbar updates
  			--provide feedback text:
  			--# of meshes selected, # of maps found, # of meshes successfully processed
  			--# of meshes failed
  		)--end for	 
  	)--end on
  	on folderBTN pressed do	
  	(
  		images_folderPre = getSavePath caption:"Set folder" initialDir:(GetDir #scripts)
  		images_folder = images_folderPre + "\\"
  		local imagesString = images_folderPre as string
  		local reduceDirs = filterString imagesString "\\"
  		local theImagesFolder = reduceDirs[reduceDirs.count]
  		folderBTN.text = theImagesFolder + "\\"
  	)
  	--
  	button launchBTN "< Indexr" pos:[10,278] width:50 height:20
  	on launchBTN pressed do	(try(fileIn "indexr.ms")catch(messagebox "Cannot find indexr.ms"))
  	button howItWorksBTN "How it works" pos:[61,278] width:103 height:20
  	on howItWorksBTN pressed do (messagebox "Naming conventions!
ModelName-meshPartName-mapType.filetype")
  	button whoIsBTN "?" pos:[165,278] width:15 height:20
  	on whoIsBTN pressed do (messagebox "Send errors to: Garrick@GarrickCampsey.com")
  )--close rollout
  createDialog multiMapImporterRollout 190 305 5 90 lockWidth:true lockHeight:true style:#(#style_toolwindow, #style_sysmenu, #style_resizing)
  

I think this thread explains why it isn’t working as you expect it to:
http://forums.cgsociety.org/showthread.php?f=98&t=953369
You’re not loading the .tifs with

local myBitmap = Bitmaptexture fileName:myTiffs[i]

you’re creating new ones.

A thousand thanks! That got me going in the right direction. I’m able to load the bitmap in, but I’m getting some weirdness when trying to apply it to the diffuse map slot. I’m trying:

m.diffuseMapEnable = on
   local myBitmap = openBitmap myTiffs[i]
   m.diffusemap = myBitmap

Here’s variations I’ve tried, all fail:

local m = standard()
   m.twoSided = on
   --myTiffs[i] ex: "C:\Documents and Settings\Administrator\Desktop\multiMap importer\blue.tif"
   m.diffuseMapEnable = on
   --local myBitmap = openBitmap myTiffs[i]
   --m.diffusemap = TextureMap
   --m.diffusemap.texturemap = myBitmap
   m.diffusetexture = openBitmap myTiffs[i]
   /*
   --1
   m.diffusemap = bitmaptexture
   m.diffusemap.filename = myBitmap
   --2
   m.diffusemap = myBitmap
   */
   m.diffuseMap.alphaSource = 2
   m.diffuseMapAmount = 100
   --showTextureMap m m.diffuseMap on
   

Getting:

MAXScript Rollout Handler Exception: – Unable to convert: BitMap:C:\Documents and Settings\Administrator\Desktop\multiMap importer\myModel-part1-DF.tif to type: TextureMap <<

So, I think I need to load the bitmap into a texture map, or something?

Want:

local m = standard()
   local myBmp = openBitmap myTiffs[i]
   m.diffusemap = myBmp

Dont want:

$.material.diffusemap = myBmp
   --or
   meditmaterials[1].diffusemap = myBmp

Thanks for any help anyone has!

Does this help?


mypath = "m:\\maps\\brick\\brkwea02.jpg"
mymaterial = createinstance standardmaterial 					--Create a new standard material - mymaterial
diffusefile = openbitmap mypath	--Load a bitmap stored as diffusefile
diffusebitmap = createinstance bitmaptexture					--Create a bitmaptexture - diffusebitmap
diffusebitmap.bitmap = diffusefile 									--Put the bitmap into the bitmaptexture
mymaterial.diffusemap = diffusebitmap								--Put the bitmaptexture in the material
setmeditmaterial 1 mymaterial

Obviously you’ll have to change the value of mypath but when run will create a material and put a bitmap into the diffuse slot.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

it’s the same:


 mymaterial = standardmaterial diffusemap:(bitmaptexture filename:mypath) 
 

A million thanks, that’s what I needed!

Thanks DenisT, that’s a much neater way of doing it.

I like your method denisT! I wound up using:

local m = standard()
  m.twoSided = on
  m.diffuseMapEnable = on
  m.bumpMapEnable = on
  m.bumpMapAmount = 100
  m.bumpMap = Normal_Bump ()
  m.bumpMap.map1on = on
  m.bumpMap.map2on = on

then setting the textures into the materials with:

--diffuse
  m.diffuseMap = Bitmaptexture fileName:myTiffs[j]
  m.diffuseMap.alphaSource = 2
  m.diffuseMapAmount = 100
  showTextureMap m m.diffuseMap on
  --normal
  m.bumpMap.normal_map = Bitmaptexture fileName:myTiffs[j]
  m.bumpMap.normal_map.alphaSource = 2
  m.bumpMap.mult_spin = 1.0
  --bump in normal
  m.bumpMap.bump_map = Bitmaptexture fileName:myTiffs[j]
  m.bumpMap.bump_map.alphaSource = 2
  m.bumpMap.bump_spin = 1.0

Here’s the working code if anyone is interested:

--multimap importer v1, by garrick@garrickcampsey.com
  
  --todo: add more types of textures, bump, displacement, specular, opacity, reflection, etc...
  
  global multiMapImporterRollout
  try destroyDialog multiMapImporterRollout catch()
  rollout multiMapImporterRollout " Multimap Importer v1.0"
  (
  	local images_folder = undefined --the address to the folder where the images are saved
  	local searchType = "tif" --what type of file to look for
  	--
  	label folderSelectLabel "Set folder :" pos:[10,10]
  	button folderBTN "Choose Folder" pos:[65,8] width:225 height:18	
  	--
  	groupBox typesGroup "Choose types and naming conventions" pos:[10,32] width:280 height:200
  		checkbox nmACheck "Diffuse" checked:true pos:[20,55] enabled:true
  		on nmACheck changed theState do nmACheck.state = true
  		label nmALabel "naming convention : " pos:[95,57]
  		edittext nmATxt text:"TM" pos:[190,55] fieldWidth:85 height:18 enabled:true
  		--
  		checkbox nmBCheck "Normal" checked:true pos:[20,75] enabled:true
  		on nmBCheck changed theState do nmBCheck.state = true
  		label nmBLabel "naming convention : " pos:[95,77]
  		edittext nmBTxt text:"NM" pos:[190,75] fieldWidth:85 height:18 enabled:true
  		--
  		checkbox nmCCheck "Bump" checked:false pos:[20,95] enabled:false
  		label nmCLabel "naming convention : " pos:[95,97] enabled:false
  		edittext nmCTxt text:"CV" pos:[190,95] fieldWidth:85 height:18 enabled:false
  		--
  		checkbox nmDCheck "Disp." checked:false pos:[20,115] enabled:false
  		label nmDLabel "naming convention : " pos:[95,117] enabled:false
  		edittext nmDTxt text:"DM" pos:[190,115] fieldWidth:85 height:18 enabled:false
  		--
  		checkbox nmECheck "Specular" checked:false pos:[20,135] enabled:false
  		label nmELabel "naming convention : " pos:[95,137] enabled:false
  		edittext nmETxt text:"SP" pos:[190,135] fieldWidth:85 height:18 enabled:false
  		--
  		checkbox nmFCheck "Opacity" checked:false pos:[20,155] enabled:false
  		label nmFLabel "naming convention : " pos:[95,157] enabled:false
  		edittext nmFTxt text:"OP" pos:[190,155] fieldWidth:85 height:18 enabled:false
  		--
  		checkbox nmGCheck "Reflection" checked:false pos:[20,175] enabled:false
  		label nmGLabel "naming convention : " pos:[95,177] enabled:false
  		edittext nmGTxt text:"RF" pos:[190,175] fieldWidth:85 height:18 enabled:false
  		--
  	--
  	label selectMeshesLabel "Select the meshes to add the maps to, then press:" pos:[30,237]
  	button goBTN "textures to materials (shift+click to import objs too)" pos:[10,255] width:280 height:40
  	--
  	edittext theFeedbackTxt text:"Ready to build." pos:[6,300] fieldWidth:280 height:50
  	progressBar meshesProgress "" pos:[10,355] width:280 height:10 value:0 color:[0,96,0]
  	progressBar imagesProgress "" pos:[10,370] width:280 height:10 value:0 color:[0,96,0]
  	--
  	button launchBTN "< Indexr" pos:[10,387] width:55 height:20
  	on launchBTN pressed do	(try(fileIn "indexr.ms")catch(messagebox "Cannot find indexr.ms"))
  	button howItWorksBTN "How it works" pos:[170,387] width:103 height:20
  	on howItWorksBTN pressed do (messagebox "Use naming conventions!

Obj/Mesh:
ModelName-partName
Texture filename:
ModelName-meshPartName-mapType.filetype")
  	button whoIsBTN "?" pos:[275,387] width:15 height:20
  	on whoIsBTN pressed do (messagebox "Send errors to: Garrick@GarrickCampsey.com")
  	--
  	
  	--EVENTS
  	on goBTN pressed do	
  	(
  		theFeedbackTxt.text = "working..."
  		--collect all selected meshes names into an array
  		local mySelectedsNamesArray = #()
  		local mySelectedsArray = #()
  		--
  		local myImportedTextures = #()
  		--
  		local start = timeStamp()
  		--
  		if keyboard.shiftPressed == true then ( --check to see if shift button is pressed
  			--collect all the objs in the folder directory, import each, name and append to their respective arrays 
  			local whatAmIloookingFor = images_folder + "*.obj"
  			local myObjs = getFiles whatAmIloookingFor
  			--		
  			for i=1 to myObjs.count do 
  			(
  				--import the obj file
  				importFile myObjs[i] #noPrompt using:Wavefront_Object
  					--print myObjs[i]
  				local o=openFile myObjs[i] mode:"r+"
  				local myReadData = #()
  				--get the OBJ name from the MTL line in the OBJ file, 9th line
  				for g = 1 to 9 do (
  					myRead = readLine o
  					append myReadData myRead
  				)
  				close o
  				--
  				local myFilteredMtlName = filterString myReadData[9] " " --seperate by the space
  				local myFilteredName = filterString myFilteredMtlName[2] "." --separate by the .mtl
  					--print myFilteredName[1]
  				--assign this name to the newly imported object
  				select $objZBrushPolyMesh3D* --why doesn't max import the OBJ name properly?
  				$.name = myFilteredName[1]				
  				append mySelectedsNamesArray $.name --put this name into mySelectedsNamesArray
  				append mySelectedsArray $ --put object into mySelectedsArray
  				clearSelection()
  					--print myObjs[i]
  			)	
  		--if shift is not pressed, just get the selected meshes	
  		) else (
  			--check to make sure something is selected, if so, append the nodes into arrays, else msg the user
  			try(for j in $ do (append mySelectedsNamesArray j.name;append mySelectedsArray j);clearSelection()
  			)catch(messagebox "Please select some meshes to work on.")
  		)
  		--
  		if mySelectedsArray.count > 0 then (
  
  			--get the values from the editText fields
  			local nmAText = nmATxt.text + ".tif"
  			local nmBText = nmBTxt.text + ".tif"
  			local nmCText = nmCTxt.text + ".tif"
  	
  			--for every tif image found, collect it into an array
  			local whatAmIloookingFor
  			if searchType == "tif" then (whatAmIloookingFor = images_folder + "*.tif") --**** FILETYPE
  			local myTiffs = getFiles whatAmIloookingFor
  							
  			--for every node in mySelectedsArray do
  			for i = 1 to mySelectedsNamesArray.count do 
  			(
  				
  				--filter string name
  				local splitModelAndPart = filterString mySelectedsNamesArray[i] "-"  --split the model and part name
  					--print splitModelAndPart[1] --this is the model name
  					--print splitModelAndPart[2] --this is the part/subObject name
  				--compare model name to myTiffs[], find match
  				
  				--build a material for the mesh
  				local m = standard()
  				m.twoSided = on
  				m.diffuseMapEnable = on
  				m.bumpMapEnable = on
  				m.bumpMapAmount = 100
  				m.bumpMap = Normal_Bump ()
  				m.bumpMap.map1on = on
  				m.bumpMap.map2on = on
  							
  				for j = 1 to myTiffs.count do
  				(
  					local reduceDirs = filterString myTiffs[j] "\\"  --for the count of the array, filterString each item with "\"
  					local findNamingConvention = filterString reduceDirs[reduceDirs.count] "-"  --for (the last one) filterString with "-"
  					--REF:ModelName-meshPartName-mapType.filetype
  					if findNamingConvention[1] != undefined and findNamingConvention[2] != undefined and findNamingConvention[3] != undefined then
  					(   --check to see if dataset is valid, else invalid naming convention
  							--print findNamingConvention[1] --this is the model name
  							--print findNamingConvention[2] --this is the part number
  							--print findNamingConvention[3] --this is the filetype
  						if splitModelAndPart[1] == findNamingConvention[1] and splitModelAndPart[2] == findNamingConvention[2] then
  						(	--the mesh matches the texture file
  							--assign the model and part name to the material
  							m.name = (findNamingConvention[1] as string) + "-" + (findNamingConvention[2] as string)
  
  							--then do comparisons to see where incoming images should go
  							local myString = findNamingConvention[3] as string
  							--compare strings indices 1 and 2 as chars
  							if myString[1] == (nmATxt.text[1] as string) and myString[2] == (nmATxt.text[2] as string) then 
  								(	--DIFFUSE
  								print "diffuse:"
  								print myTiffs[j]
  								m.diffuseMap = Bitmaptexture fileName:myTiffs[j]
  								m.diffuseMap.alphaSource = 2
  								m.diffuseMapAmount = 100
  								showTextureMap m m.diffuseMap on
  								--put in success array
  								append myImportedTextures myTiffs[j]
  								)
  							if myString[1] == (nmBTxt.text[1] as string) and myString[2] == (nmBTxt.text[2] as string) then 
  								(	--NORMAL
  								print "normal:"
  								print myTiffs[j]								
  								m.bumpMap.normal_map = Bitmaptexture fileName:myTiffs[j]
  								m.bumpMap.normal_map.alphaSource = 2
  								m.bumpMap.mult_spin = 1.0
  								--put in success array
  								append myImportedTextures myTiffs[j]
  								)
  								
  							/*
  							if myString[1] == (nmCTxt.text[1] as string) and myString[2] == (nmCTxt.text[2] as string) then 
  								( 	--BUMP
  								print "bump:"
  								print myTiffs[j]
  								m.bumpMap.bump_map = Bitmaptexture fileName:myTiffs[j]
  								m.bumpMap.bump_map.alphaSource = 2
  								m.bumpMap.bump_spin = 1.0
  								--put in success array
  								append myImportedTextures myTiffs[j]
  								)	
  							*/
  
  						) --end naming convention comparison
  						--
  					) else () --invalid dataset, texFile is named incorrectly
  					-- update images prog bar			
  					local myPercent = 100 * j / myTiffs.count			
  					imagesProgress.value = myPercent
  					imagesProgress.color = [200 - myPercent * 2,myPercent * 2,0]	
  				)--end for j in myTiffs do
  				--
  				--print mySelectedsArray[i]
  				mySelectedsArray[i].material = m --assign the material to the correct mesh
  				meditMaterials[i] =  m --then copy m to appropriate map slot
  				-- update meshes prog bar			
  				local myPercent = 100 * i / mySelectedsNamesArray.count			
  				meshesProgress.value = myPercent
  				meshesProgress.color = [200 - myPercent * 2,myPercent * 2,0]
  			)--end for i = 1 to mySelectedsNamesArray.count do )
  			
  			--provide feedback/report on meshes and textures loaded in
  			local matchedTexs = myTiffs.count - myImportedTextures.count
  			local myUpdateString = "Built in: " + (formattedPrint ((timeStamp() - start) / 1000.0) format:".1f") + "sec." +\
  			"
Total meshes: " + mySelectedsArray.count as string + "	Textures applied: " + myImportedTextures.count as string +\
  			"
Unused textures: " + matchedTexs as string
  			theFeedbackTxt.text = myUpdateString
  			--
  		) else (print "no .objs found in folder.")--end if mySelectedsArray.count > 0 then )
  	)--end on
  	on folderBTN pressed do	
  	(
  		images_folderPre = getSavePath caption:"Set folder" initialDir:(GetDir #scripts)
  		if images_folderPre != undefined then 
  		(
  		images_folder = images_folderPre + "\\"
  		local imagesString = images_folderPre as string
  		local reduceDirs = filterString imagesString "\\"
  		local theImagesFolder = reduceDirs[reduceDirs.count]
  		folderBTN.text = theImagesFolder + "\\"
  		)
  	)
  	--
  )--close rollout
  createDialog multiMapImporterRollout 300 415 5 90 lockWidth:true lockHeight:true style:#(#style_toolwindow, #style_sysmenu, #style_resizing)
  

Double progress bars!