Esempio n. 1
0
def zLoadEnv_Execute(filename, XSImodel):

	# if we don't have a model use the scene root #
	if not XSImodel:
		XSImodel = xsi.ActiveSceneRoot
		
	# make sure we have a model #
	if XSImodel.Type != '#model':
		log('Model argument is not of type "model".', c.siError)
		return False

	# make sure the file exists #
	if not os.path.exists(filename):
		log('File doesn\'t exist: %s' % filename, c.siError)
		return False

	# clear any existing modules #
	clearElixirModules(echo=False)
	
	# import the model #
	import zSkinWeightsModel as model
	
	# connect to the database #
	sqliteName = re.subn(r'\\', '/', filename)[0]
	
	# create a new skin object #
	model.metadata.bind = "sqlite:///%s" % sqliteName
	model.setup_all()
	
	# get the skin #
	skins = model.Skin.query.all()
	for skin in skins:
		log(str(skin))
		# get the geometry #
		for geo in skin.geometry:
			log(str(geo))
			
			# find the geometry #
			obj = XSImodel.FindChild(geo.name)
			if not obj:
				log('Unable to find object: %s. Skipping' % geo.name, c.siWarning)
				continue
			
			# make sure the point counts match #
			objPointCount = obj.ActivePrimitive.Geometry.Points.Count
			if len(geo.points) != objPointCount:
				log('Point count doesn\'t match. %s != %s(%d))' % (geo, obj.FullName, objPointCount))
				
			# create a collection of deformers #
			dfmCol = dispatch('XSI.Collection')
			for dfm in geo.deformers:
				log(str(dfm))
				# find the deformer #
				xsiDfm = XSImodel.FindChild(dfm.name)
				if not xsiDfm:
					log('Unable to find deformer: %s. Skipping' % dfm, c.siWarning)
					break
				# add it to the dfm collection #
				dfmCol.Add(xsiDfm)
		
			# skip if we don't have all the deformers #
			if not dfmCol.Count != geo.deformers:
				log('Unable to find all the deformers for %s. Skipping envelope.' % geo, c.siWarning)
				continue
				
			# apply the envelope #
			log(dfmCol.GetAsText())
			env = obj.ApplyEnvelope(dfmCol, c.siNode, c.siNode)
			
			# create an array to hold the weights #
			weights = list(env.Weights.Array)
			for i in xrange(len(weights)):
				weights[i] = list(weights[i])
				
			# set the deformer color #
			color = env.GetDeformerColor(dfmCol(0))
			# step through the deformers again and set colors #
			for d in xrange(env.Deformers.Count):
				# get the deformer #
				dfm = env.Deformers(d)
				# skip over effs #
				if dfm.Type == 'eff': continue
				# get the deformer in the database #
				dbDfm = model.Deformer.get_by(name=dfm.Name)
				# build the color #
				color.Red 	= dbDfm.red
				color.Green = dbDfm.green
				color.Blue 	= dbDfm.blue
				# set the color #
				env.SetDeformerColor(dfm, color)
				# set the weights for the deformer for this geom #
				dbWeights = model.Weight.query.filter_by(deformer=dbDfm).filter_by(geometry=geo).all()
				# step through the weights and set the weight array #
				for wgt in dbWeights:
					weights[d][wgt.point.index] = wgt.value
			
			# set the weights #
			env.Weights.Array = weights
Esempio n. 2
0
def zSaveEnv_Execute(filename, objects, overwrite):

	# clear any existing modules #
	clearElixirModules(echo=False)

	import zSkinWeightsModel as model 
	
	# get a temporary file #
	import tempfile
	tempname = tempfile.mktemp('.skin')

	# import the model #
	import zSkinWeightsModel as model
	
	# format the filename for any backslashes #
	sqliteName = re.subn(r'\\', '/', tempname)[0]
	
	# create a new skin object #
	model.metadata.bind = "sqlite:///%s" % sqliteName
	model.setup_all()
	model.create_all()
	skin = model.Skin('zSaveEnv')
	
	# create a deformer dictionary #
	defDict = {}
		
	# step through each item #
	for item in objects:
		
		# initialize a progress bar #
		pb = XSIUIToolkit.ProgressBar
		pb.Maximum = item.Envelopes(0).Deformers.Count
		pb.Step = 1
		pb.Caption = 'Envelope: %s' % item.name
		if xsi.Interactive: pb.Visible = True
		
		# create a point list #
		pntList = []
		
		# step through each envelope on each object #
		for env in item.Envelopes:	
			
			# add the geometry #
			geom = skin.AddGeometry(item.Name)
			
			# add all the points to the list #
			for p in xrange(item.ActivePrimitive.Geometry.Points.Count):
				# create a new point object #
				pnt = geom.AddPoint(p)
				# add it to the point list #
				pntList.append(pnt)
			
			# step through each deformer #
			for dfm in env.Deformers:

				# skip over effectors #
				if dfm.type == 'eff': continue
	
				# add the deformer to the dictionary #
				deformer = None
				if not dfm.Name in defDict.keys():
					# add the deformer to the database #
					deformer = geom.AddDeformer(dfm.Name)
					# get the deformer color #
					color = env.GetDeformerColor(dfm)
					deformer.red   = color.Red
					deformer.green = color.Green
					deformer.blue  = color.Blue
					# store the def in the dictionary #
					defDict[dfm.Name] = deformer
				
				# if the deformer isn't defined get it from the dictionary #
				if not deformer:
					deformer = defDict[dfm.Name]
					
					# add the deformer to the geometry #
					geom.deformers.append(deformer)
					
				# get the weights of the deformer #
				weights = env.GetDeformerWeights(dfm)
				for w in xrange(len(weights)):
					# add the weight to the point #
					pntList[w].AddWeight(deformer, weights[w])

				
	# close and clean the session #
	model.session.flush()
	model.session.close()
	
	# clear any existing elixir modes #
	clearElixirModules(echo=False)
	
	# if we are overwriting the file, delete it #
	if os.path.exists(filename):
		if overwrite: os.unlink(filename)
	
	# move the tempfile into place #
	# TODO: figure out why the file remains locked after writing to it #
	import shutil
	shutil.copy(tempname, filename)