Commit 45128085 authored by JriusUru's avatar JriusUru

Import and assign VisRegions ! Yeah !

parent ef885b33
......@@ -340,6 +340,18 @@ class DrawImporter:
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.sort_elements(type="MATERIAL") # may be better for some engines like Unity
bpy.ops.object.mode_set(mode="OBJECT")
if len(draw.regions):
bpy.ops.object.plasma_modifier_add(types="visibility")
visModifier = blObj.plasma_modifiers.visibility
def setVisRegion(index, softVolumeBlObject):
visModifier.regions[index].control_region = softVolumeBlObject
for visRegionIndex, visRegionKey in enumerate(draw.regions):
kormanRegion = visModifier.regions.add()
self.parent.softVolumeImporter.registerVisRegionAndCallback(
visRegionKey,
lambda softVolumeBlObject, index=visRegionIndex: setVisRegion(index, softVolumeBlObject))
blObj.select_set(False)
for mat in msh.materials:
......
......@@ -99,25 +99,8 @@ class ModifierImporter:
self.setupReferenceLambdas.append(lambda: setCurrent(kormanMod, plMod))
elif interface.type == pl.plFactory.kVisRegion:
bpy.ops.object.plasma_modifier_add(types="visregion")
plMod = interface.object
kormanMod = blObj.plasma_modifiers.visregion
if plMod.getProperty(pl.plVisRegion.kDisable):
kormanMod.mode = "fx"
else:
kormanMod.mode = "exclude" if plMod.getProperty(pl.plVisRegion.kIsNot) else "normal"
if self.parent.validKey(plMod.region):
softRegKey = plMod.region.object.owner
def setSoftReg(softRegKey, kormanMod):
softRegBlObj = self.parent.getBlObjectFromKey(softRegKey)
if softRegBlObj:
kormanMod.soft_region = softRegBlObj
self.setupReferenceLambdas.append(lambda: setSoftReg(softRegKey, kormanMod))
kormanMod.replace_normal = plMod.getProperty(pl.plVisRegion.kReplaceNormal)
# ignore it, objectImporter and softVolumeImporter handle this
pass
elif interface.type == pl.plFactory.kSoftVolumeSimple:
# ignore it, softVolumeImporter handles this
......
......@@ -187,6 +187,17 @@ class ObjectImporter:
self.parent.animImporter.registerMasterMod(sceneObjKey, modifierKey)
dataDict[modifierKey] = master
# also process visregions (yet another special case...)
for interfaceKey in interfaces:
if self.parent.validKey(interfaceKey):
if dataDict.get(interfaceKey):
# already imported, ignore
continue
if interfaceKey.type == pl.plFactory.kVisRegion:
# add a visreg to the master... Let softVolumeImporter handle this
dataDict[interfaceKey] = self.parent.softVolumeImporter.importVisRegion(master, interfaceKey)
self.parent.modifierImporter.importModifiers(sceneObjKey, master)
# now, merge dataDict into the main key-to-blObject database
......
......@@ -46,8 +46,12 @@ class SoftVolumeImporter:
# { softVolumeKey, [callback, callback, ...] }
# Will be executed once we are sure ALL softregions are imported.
self.softVolumesCallbacks = {}
# { visRegionKey, [callback, callback, ...] }
# Will be executed once we are sure ALL visregions are imported.
self.visRegionsCallbacks = {}
self.softVolumesToImport = Queue()
self.visRegionsToImport = Queue()
def registerSoftVolumeAndCallback(self, softVolumeKey, callback):
"""Register the given soft volume/method pair. The softvolume will be imported later, and the callback executed once the object can be safely assigned to a Korman modifier, with the softvolume's Blender object as only parameter."""
......@@ -58,6 +62,19 @@ class SoftVolumeImporter:
self.softVolumesCallbacks[softVolumeKey] = [callback];
self.softVolumesToImport.put(softVolumeKey)
def registerVisRegionAndCallback(self, visRegionKey, callback):
"""Register the given visregion/method pair. The visregion will be imported later, and the callback executed once the object can be safely assigned to a Korman modifier, with the visregion's Blender object as only parameter."""
callbacks = self.visRegionsCallbacks.get(visRegionKey)
if callbacks:
callbacks.append(callback)
else:
self.visRegionsCallbacks[visRegionKey] = [callback];
self.visRegionsToImport.put(visRegionKey)
if self.parent.validKey(visRegionKey.object.region):
# If the visreg is ownerless, we'll attach it to the SV it uses.
# Register the SV so it gets imported first. We'll attach the VR to it later.
self.softVolumesToImport.put(visRegionKey.object.region)
def importSoftVolume(self, sceneObjKey, volumeKey):
if not self.parent.validKey(volumeKey):
return None
......@@ -115,9 +132,13 @@ class SoftVolumeImporter:
(upperBound[1] + lowerBound[1]) / 2,
(upperBound[2] + lowerBound[2]) / 2))
volumeApproxSize = sqrt(diagonal[0] ** 2 + diagonal[1] ** 2 + diagonal[2] ** 2)
# the more planes we have in our SV, the smaller each of our floating face get.
# The following formula is random but should yield fairly acceptable results.
faceSizeMultiplier = volumeApproxSize / (len(plVolumeMod.volume.planes) * 2)
if volumeApproxSize > 0 and len(plVolumeMod.volume.planes) >= 2:
# the more planes we have in our SV, the smaller each of our floating face get.
# The following formula is random but should yield fairly acceptable results.
faceSizeMultiplier = volumeApproxSize / (len(plVolumeMod.volume.planes) * 2)
else:
faceSizeMultiplier = 1
bm = bmesh.new()
assert(isinstance(plVolumeMod.volume, pl.plConvexIsect))
......@@ -155,7 +176,7 @@ class SoftVolumeImporter:
del bm
if self.parent.kormanAvailable:
kormanMod = self.createKormanMod(blObj)
kormanMod = self.createKormanSoftVolumeMod(blObj)
kormanMod.use_nodes = False
kormanMod.invert = False
kormanMod.inside_strength = plMod.insideStrength * 100
......@@ -180,7 +201,7 @@ class SoftVolumeImporter:
plVolumeMod = volumeKey.object
kormanMod = self.createKormanMod(blObj)
kormanMod = self.createKormanSoftVolumeMod(blObj)
kormanMod.use_nodes = True
tree = bpy.data.node_groups.new(volumeKey.name, "PlasmaNodeTree")
kormanMod.node_tree = tree
......@@ -224,7 +245,7 @@ class SoftVolumeImporter:
plVolumeMod = volumeKey.object
kormanMod = self.createKormanMod(blObj)
kormanMod = self.createKormanSoftVolumeMod(blObj)
kormanMod.use_nodes = True
tree = bpy.data.node_groups.new(volumeKey.name, "PlasmaNodeTree")
kormanMod.node_tree = tree
......@@ -271,7 +292,7 @@ class SoftVolumeImporter:
plVolumeMod = volumeKey.object
kormanMod = self.createKormanMod(blObj)
kormanMod = self.createKormanSoftVolumeMod(blObj)
kormanMod.use_nodes = True
tree = bpy.data.node_groups.new(volumeKey.name, "PlasmaNodeTree")
kormanMod.node_tree = tree
......@@ -304,7 +325,7 @@ class SoftVolumeImporter:
return blObj
def createKormanMod(self, blObj):
def createKormanSoftVolumeMod(self, blObj):
blObj.select = True
bpy.context.scene.objects.active = blObj
bpy.ops.object.plasma_modifier_add(types="softvolume")
......@@ -312,15 +333,63 @@ class SoftVolumeImporter:
blObj.select = False
return kormanMod
def importVisRegion(self, blObj, visRegionKey):
if not self.parent.validKey(visRegionKey):
return None
plVisRegion = visRegionKey.object
if not blObj:
# "floaty" visregion without owner
# since objectImporter does not handle this object, we have to register it ourself and put it in the correct scene...
if self.parent.validKey(plVisRegion.region):
# just put it on the same object as the softvolume for simplicity...
blObj = self.parent.getBlObjectFromKey(plVisRegion.region)
else:
blObj = bpy.data.objects.new(visRegionKey.name, None)
self.parent.sceneImporter.appendObjectToScenesWithPlLocation(blObj, SceneImporter.layerSoftVolumes, visRegionKey.location)
self.parent.registerObject(visRegionKey, blObj)
blObj.select = True
bpy.context.scene.objects.active = blObj
bpy.ops.object.plasma_modifier_add(types="visregion")
kormanMod = blObj.plasma_modifiers.visregion
blObj.select = False
if plVisRegion.getProperty(pl.plVisRegion.kDisable):
kormanMod.mode = "fx"
else:
kormanMod.mode = "exclude" if plVisRegion.getProperty(pl.plVisRegion.kIsNot) else "normal"
if self.parent.validKey(plVisRegion.region):
softRegKey = plVisRegion.region
def setSoftVolume(softVolumeObject):
kormanMod.soft_region = softVolumeObject
self.registerSoftVolumeAndCallback(softRegKey, setSoftVolume)
kormanMod.replace_normal = plVisRegion.getProperty(pl.plVisRegion.kReplaceNormal)
return kormanMod
def importOwnerlessSoftvolumes(self):
"""Create Blender objects for all softvolumes that haven't been imported yet."""
"""Create Blender objects for all softvolumes/visregions that haven't been imported yet."""
while not self.softVolumesToImport.empty():
softVolumeKey = self.softVolumesToImport.get()
if self.parent.validKey(softVolumeKey):
if not self.parent.isPlObjectImported(softVolumeKey):
self.importSoftVolume(None, softVolumeKey)
while not self.visRegionsToImport.empty():
visRegionKey = self.visRegionsToImport.get()
if self.parent.validKey(visRegionKey):
if not self.parent.isPlObjectImported(visRegionKey):
self.importVisRegion(None, visRegionKey)
def setupReferences(self):
for visRegionKey, callbacks in self.visRegionsCallbacks.items():
for callback in callbacks:
callback(self.parent.getBlObjectFromKey(visRegionKey));
for softVolumeKey, callbacks in self.softVolumesCallbacks.items():
for callback in callbacks:
callback(self.parent.getBlObjectFromKey(softVolumeKey));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment