Everything related to SDK.
User avatar
By seghier
#395726
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
User avatar
By Brany
#395735
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.
User avatar
By Brany
#395737
Did you tried to set a "dummy" preview by yourself? CmaxwellMaterial.setPreview(...) to replace the current preview (that seems to be corrupted someway)?
User avatar
By seghier
#395745
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')
User avatar
By Brany
#395747
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))
User avatar
By seghier
#395752
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
User avatar
By Brany
#395763
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.

...and 3 Days later, 82.528 Views !!! ...NL, every[…]

Hello dear customers, We have just released a new[…]

grass generator extension

Just downloaded MWR5 for Rhino 6 and want to use g[…]

Hello everyone, I have a new bug with the latest M[…]