Page 1 of 1

Regenerate materials previews

Posted: Sun Oct 15, 2017 12:09 am
by seghier
Hello
is it possible to regenerate materials previews inside a scene with pymaxwell ?
i find a problem and i don't know if this a bug or not .
someone posted a scene snice long time called : "bug or not"( i don't know which version of maxwell used for this scene) and i tried to load it to replace a material ; but always the tool crash.
the size of the scene : 2.23mo ; i open the scene and update all materials previews and save the scene and the size of the new scene decreased to 1.10mo ! than i open it with the tool without error.
the scene:
https://www.dropbox.com/s/hwzdp1uw078if ... e.rar?dl=0

Re: Regenerate materials previews

Posted: Mon Oct 16, 2017 2:49 pm
by Brany
No, you can't regenerate the material previews from pymaxwell. You need to run the render engine for that, so you have to use mxed.exe/studio.exe to do that.

Re: Regenerate materials previews

Posted: Mon Oct 16, 2017 3:09 pm
by seghier
thanks for informations
so some scenes need to fixed with studio

Re: Regenerate materials previews

Posted: Mon Oct 16, 2017 4:11 pm
by Brany
Did you tried to set a "dummy" preview by yourself? CmaxwellMaterial.setPreview(...) to replace the current preview (that seems to be corrupted someway)?

Re: Regenerate materials previews

Posted: Mon Oct 16, 2017 4:32 pm
by seghier
no , i will try it but how it work ? the script generate previews or i need load exist images previews ?

Re: Regenerate materials previews

Posted: Tue Oct 17, 2017 9:13 am
by seghier
i test with this code ; i use numpy array but pymaxwell don't accept the value; i tried to use preview of the first material in the scene and apply it to other materials ; it work and the scene size decreased from 2.23mo to 1.03mo
is there anyway to get and export material preview to png or jpg ? and what is the right method to create numpy array value which is compatible with pymaxwell?
and feature to generate previews is better :)
Code: Select all
from pymaxwell import *
import numpy as np
import os


def preview():

    mxsFile = 'C:/intest/test.mxs'
    img = np.array([[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
                   [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                   [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                   [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                   [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                   [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]])
    print img

    scene = Cmaxwell(mwcallback)
    scene.readMXS(mxsFile)

    it = CmaxwellMaterialIterator()
    material = it.first(scene)
    ima, ok = material.getPreview() # first material preview
    #print ima
    while material.isNull() == False:
         #print material
         image,ok = material.getPreview()
         material.setPreview(ima)
         material = it.next()

    output = 'C:/intest/test_updated.mxs'
    ok = scene.writeMXS(output)


if __name__ == "__main__":

    if preview():
        print ('OK')

Re: Regenerate materials previews

Posted: Tue Oct 17, 2017 10:49 am
by Brany
To save/read PNG files you need a PNG python package. I use this one:

https://drive.google.com/file/d/0B0YVcV ... sp=sharing

you can try to do something like this:
Code: Select all
import sys
import pymaxwell as mw
import os
import numpy
import itertools

sys.path.append('/dir/to/png.py/')
import png

def shrink(im):
  y,x = im.shape
  print x,y

def getFilesFromPath( path, extension ):
	"""getFromPath( path, extension ) -> file names list
	Description: Obtain all file names from within a directory given its extension."""
	res = [];
	filelist = os.listdir(path);
	el = len(extension)
	for file in filelist:
		if extension in file[-el:]:
			res.append(file);
	return res;


rootFolder = '/dir/to/materials/'

# Get all subfolders and files from given path
matList = os.listdir(rootFolder)

mxs = mw.Cmaxwell(mw.mwcallback)
material = mxs.createMaterial('temp')

count = 0
numForRender = len(matList)
# Read each Folder, look for materials

imarr = numpy.empty((500,500,3), numpy.uint8)

for path in matList:
	if os.path.isdir(rootFolder + path):
		mxmList = getFilesFromPath(rootFolder + path,'mxm')
		for file in mxmList:
			if not material.read(rootFolder + path + "/" + file):
				print('ERROR: cannot open '+rootFolder + path + "/" + file)
			else:
				r = png.Reader(rootFolder + path + "/" + file.replace(".mxm","") + "_thumb3.2.png")
				im = r.read()
				
				listIm = list(im[2])

				for i in range(0,500):
					for j in range(0,500):
						for k in range(0,3):
							imarr[i][j][k] = listIm[i][j*3 + k]

				material.setPreview( imarr )
				material.write ( rootFolder + path + "/" + file )
				count +=1
				print "MXM " + str(count) + " of " + str(numForRender)
				#print im[2]

				#image_2d = numpy.vstack(itertools.imap(numpy.uint8, im[2]))

				#material.setPreview( image_2d )

				#material.write ( rootFolder + path + "/" + file.replace(".mxm","") + "_preview.mxm" )

#				mxiPath = rootFolder + path + "/" + file.replace(".mxm","") + "_thumb3.2.mxi"
#				didRender = os.path.isfile(mxiPath)
#				if didRender:
#						mxi = CmaxwellMxi()
#						if mxi.read(mxiPath).failed():
#							print("ERROR: cannot read "+mxiPath)
#						else: 
#							didRender = mxi.getSamplingLevel() >= 18.0
#
#				if not didRender:
#					## Displacement on the fly and adaptative seems to have problems in many materials
#					## We disable adaptative for on the fly displacement materials
#					if newMat.isDisplacementEnabled()[0]:
#						displacementParams = newMat.getDisplacementCommonParameters()
#						if displacementParams[0] == 0: #On the fly displacement
#							heightMapParams = newMat.getHeightMapDisplacementParameters()
#							if heightMapParams[2] == True:
#								newMat.setHeightMapDisplacementParameters(heightMapParams[0],heightMapParams[1],False)
#					parameters = []
#					parameters.append('-mxs:'+rootFolder + path + "/" + "tmpMaterialScene.mxs")
#					parameters.append('-o:'+rootFolder + path + "/" + file.replace(".mxm","") + "_thumb3.2.png")
#					parameters.append('-mxi:'+rootFolder + path + "/" + file.replace(".mxm","") + "_thumb3.2.mxi");
#					parameters.append('-nowait');
#					parameters.append('-nogui');
#					parameters.append('-res:500x500');
#					parameters.append('-dep:' + simballScene);
#					scene.setMxsPath(rootFolder + path + "/" + "tmpMaterialScene.mxs");
#					scene.writeMXS();
#					runMaxwell(parameters)
#					print("File rendered: " + file)
#					os.remove(rootFolder + path + "/" + "tmpMaterialScene.mxs");
#
#		numRendered += 1
#		print ("Already rendered: " + str(numRendered) + " of " + str (numForRender))

Re: Regenerate materials previews

Posted: Tue Oct 17, 2017 10:59 am
by seghier
Thanks alot Brany .
I will try that

Re: Regenerate materials previews

Posted: Tue Oct 17, 2017 11:57 am
by seghier
i try the script ; in this line "for path in matList: " read all files and folders but don't find materials so i disable it to read directly materials from the selected folder but no thumbs are there . can this script get thumbs from mxm files ? in studio or material editor no option to save an external preview or i don't understand how this script should work
Image

Re: Regenerate materials previews

Posted: Wed Oct 18, 2017 12:38 pm
by Brany
The previews were generated running maxwell render, using a base scene and switching the material applied. That output render is used as preview.The important part here is that the script is an example on how to set the preview image for a material using a PNG file, no matter how we create the PNG previews.

Re: Regenerate materials previews

Posted: Wed Oct 18, 2017 1:02 pm
by seghier
thanks ; i will try again