Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MT method parameter ,smooth normal generation for MT and correct bug in getChildrenOf #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions exporter/osg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ class OSGGUI(bpy.types.Operator, ExportHelper):
default=False)
BAKE_ALL = BoolProperty(name="Bake all animations", description="Force baking for all animations",
default=True)
morph_target_export_modes_list = [("NORMALIZED", "NORMALIZED", "NORMALIZED"),
("RELATIVE", "RELATIVE", "RELATIVE")]
MORPHTARGETEXPORTMODE = EnumProperty(name="MorphTargets Mode", items=morph_target_export_modes_list, description="MorphTargets export mode.", default='NORMALIZED')

USE_QUATERNIONS = BoolProperty(name="Use quaternions", description="Bake rotations using quaternions",
default=True)
BAKE_CONSTRAINTS = BoolProperty(name="Bake Constraints", description="Bake constraints into actions", default=True)
Expand Down Expand Up @@ -222,6 +226,7 @@ def draw(self, context):
layout.row(align=True).prop(self, "EXPORT_ALL_SCENES")
layout.row(align=True).prop(self, "APPLYMODIFIERS")
layout.row(align=True).prop(self, "BAKE_ALL")
layout.row(align=True).prop(self, "MORPHTARGETEXPORTMODE")
layout.row(align=True).prop(self, "USE_QUATERNIONS")
layout.row(align=True).prop(self, "BAKE_CONSTRAINTS")
layout.row(align=True).prop(self, "ARMATURE_REST")
Expand Down Expand Up @@ -268,6 +273,7 @@ def invoke(self, context, event):
self.ZERO_TRANSLATIONS = self.config.zero_translations
self.LOG = self.config.log
self.BAKE_ALL = self.config.bake_animations
self.MORPHTARGETEXPORTMODE = "RELATIVE" if self.config.morph_target_mode_relative else "NORMALIZED"
self.USE_QUATERNIONS = self.config.use_quaternions
self.BAKE_CONSTRAINTS = self.config.bake_constraints
self.BAKE_FRAME_STEP = self.config.bake_frame_step
Expand Down Expand Up @@ -310,6 +316,7 @@ def execute(self, context):
self.config.log = self.LOG
self.config.zero_translations = self.ZERO_TRANSLATIONS
self.config.bake_animations = self.BAKE_ALL
self.config.morph_target_mode_relative = self.MORPHTARGETEXPORTMODE=="RELATIVE"
self.config.use_quaternions = self.USE_QUATERNIONS
self.config.bake_constraints = self.BAKE_CONSTRAINTS
self.config.bake_frame_step = self.BAKE_FRAME_STEP
Expand Down
1 change: 1 addition & 0 deletions exporter/osg/osgconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def activate(self):
self.defaultattr("zero_translations", False)
self.defaultattr("apply_modifiers", False)
self.defaultattr("bake_animations", False)
self.defaultattr("morph_target_mode_relative", False)
self.defaultattr("use_quaternions", False)
self.defaultattr("bake_constraints", True)
self.defaultattr("bake_frame_step", 1)
Expand Down
13 changes: 12 additions & 1 deletion exporter/osg/osgdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,8 +1404,19 @@ def parseMorphTargets(self, obj, geometry, morph_vertex_map, material_index):
key.data[morph_vertex_map[i]].co[2]])

target.vertexes = osg_vertexes
# FIXME we don't currently generate normals, so osganimationviewer will crash
target.primitives = geometry.primitives

# FIXME we regenerate normals assuming it is smooth..perhaps using to_mesh would be better
target.generateSmoothNormals()
if self.config.morph_target_mode_relative:
geometry.method = "RELATIVE"
for i in range(len(target.vertexes.getArray())):
for j in range(3):
target.vertexes.getArray()[i][j] = target.vertexes.getArray()[i][j] - geometry.vertexes.getArray()[i][j]
for i in range(len(target.normals.getArray())):
for j in range(3):
target.normals.getArray()[i][j] = target.normals.getArray()[i][j] - geometry.normals.getArray()[i][j]

geometry.morphTargets.append(target)
target.factor = key.value

Expand Down
32 changes: 32 additions & 0 deletions exporter/osg/osgobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,36 @@ def copyFrom(self, geometry):
self.uvs = geometry.uvs
self.stateset = geometry.stateset

def generateSmoothNormals(self):
if not self.normals or len(self.vertexes.getArray()) != len(self.normals.getArray()):
self.normals= NormalArray()
for i in range(len(self.vertexes.getArray())):
self.normals.getArray().append([0,0,0])

for prim in self.primitives:
priminc=1
if prim.type == "GL_TRIANGLES":
priminc = 3
elif prim.type == "GL_QUADS":
priminc = 4
else: continue;
primit=0
while primit<len(prim.indexes):
v0=Vector((self.vertexes.getArray()[ prim.indexes[primit]][0],self.vertexes.getArray()[ prim.indexes[primit]][1],self.vertexes.getArray()[ prim.indexes[primit]][2]))
v1=Vector((self.vertexes.getArray()[ prim.indexes[primit+1]][0],self.vertexes.getArray()[ prim.indexes[primit+1]][1],self.vertexes.getArray()[ prim.indexes[primit+1]][2]))
v2=Vector((self.vertexes.getArray()[ prim.indexes[primit+2]][0],self.vertexes.getArray()[ prim.indexes[primit+2]][1],self.vertexes.getArray()[ prim.indexes[primit+2]][2]))
norm=(v1-v0).cross(v2-v0)
norm.normalize();
for i in range(3):
for j in range(priminc):
self.normals.getArray()[prim.indexes[primit+j]][i]=self.normals.getArray()[prim.indexes[primit+j]][i]+norm[i]
primit=primit+priminc
#normalize array at the end
for i in range(len(self.normals.getArray())):
norm=Vector((self.normals.getArray()[i][0],self.normals.getArray()[i][1],self.normals.getArray()[i][2]))
norm.normalize()
for j in range(3):self.normals.getArray()[i][j]=norm[j];

def serialize(self, output):
output.write(self.encode("$%s {\n" % self.getNameSpaceClass()))
Object.serializeContent(self, output)
Expand Down Expand Up @@ -1237,6 +1267,7 @@ def __init__(self, *args, **kwargs):
self.dataVariance = "DYNAMIC"
self.morphTargets = []
self.update_callbacks = []
self.method = "NORMALIZED"

def className(self):
return "MorphGeometry"
Expand All @@ -1252,6 +1283,7 @@ def serialize(self, output):
output.write(self.encode("$}\n"))

def serializeContent(self, output):
output.write(self.encode("$#Method %s \n" % self.method))
if self.morphTargets:
output.write(self.encode("$#MorphTargets %s {\n" % len(self.morphTargets)))
for target in self.morphTargets:
Expand Down
8 changes: 4 additions & 4 deletions exporter/osg/osgutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ def getDeltaMatrixFromMatrix(parent, child):
return bi * child


def getChildrenOf(scene, object):
def getChildrenOf(scene, obj):
children = []
for obj in scene.objects:
if obj.parent == object:
children.append(obj)
for o in scene.objects:
if o.parent is obj:
children.append(o)
return children


Expand Down