Skip to content

Commit

Permalink
Fix OBJ exporter script.
Browse files Browse the repository at this point in the history
Fix wrong face normal direction, fix wrong UV coordinates.
  • Loading branch information
codereader committed Aug 24, 2017
1 parent 36ffcbf commit 7cdeabb
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions install/scripts/commands/export_obj.py
Expand Up @@ -24,15 +24,17 @@
def execute():
script = "DarkRadiant Wavefront OBJ Export (*.obj)"
author = "Python port by greebo, based on original exporter C++ code in DarkRadiant and the ASE exporter scripts"
version = "0.1"
version = "0.2"

import darkradiant as dr

# Check if we have a valid selection

selectionInfo = GlobalSelectionSystem.getSelectionInfo()

# Don't allow empty selections or selected components only
if selectionInfo.totalCount == 0 or selectionInfo.totalCount == selectionInfo.componentCount:
errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', Dialog.ERROR)
errMsg = GlobalDialogManager.createMessageBox('No selection', 'Nothing selected, cannot run exporter.', dr.Dialog.ERROR)
errMsg.run()
return

Expand Down Expand Up @@ -87,7 +89,8 @@ def processBrush(brushnode):

# Append the face to the list, referencing vertices from firstVertex to the current count
# Face indices are 1-based, so increase by 1 each time
geometry.faces.append([i for i in range(firstVertex+1, geomlist.vertexCount+1)])
# Reverse the list to produce the correct face normal direction
geometry.faces.append([i for i in reversed(range(firstVertex+1, geomlist.vertexCount+1))])

print('Processed brush geometry: {0} verts and {1} faces'.format(len(geometry.vertices), len(geometry.faces)))
geomlist.objects.append(geometry)
Expand Down Expand Up @@ -137,7 +140,7 @@ def processPatch(patchnode):
return

# Traversor class to visit child primitives of entities
class nodeVisitor(SceneNodeVisitor):
class nodeVisitor(dr.SceneNodeVisitor):
def pre(self, scenenode):
if scenenode.isBrush():
processBrush(scenenode.getBrush())
Expand All @@ -147,7 +150,7 @@ def pre(self, scenenode):
# Traverse all child nodes, regardless of type
return 1

class SelectionWalker(SelectionVisitor):
class SelectionWalker(dr.SelectionVisitor):
def visit(self, scenenode):
if scenenode.isBrush():
processBrush(scenenode.getBrush())
Expand Down Expand Up @@ -180,7 +183,7 @@ def visit(self, scenenode):
exportCaulkHandle = dialog.addCheckbox("Export caulked faces")
dialog.setElementValue(exportCaulkHandle, GlobalRegistry.get('user/scripts/objExport/exportcaulk'))

if dialog.run() == Dialog.OK:
if dialog.run() == dr.Dialog.OK:
fullpath = dialog.getElementValue(pathHandle) + '/' + dialog.getElementValue(fileHandle)
if not fullpath.endswith('.obj'):
fullpath = fullpath + '.obj'
Expand All @@ -194,8 +197,8 @@ def visit(self, scenenode):
try:
file = open(fullpath, 'r')
file.close()
prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', Dialog.ASK)
if prompt.run() == Dialog.YES:
prompt = GlobalDialogManager.createMessageBox('Warning', 'The file ' + fullpath + ' already exists. Do you wish to overwrite it?', dr.Dialog.ASK)
if prompt.run() == dr.Dialog.YES:
overwrite = True
else:
overwrite = False
Expand Down Expand Up @@ -241,7 +244,7 @@ def visit(self, scenenode):

# Texture coord list (vt)
for texcoord in x.texcoords:
objstr = objstr + "vt {0} {1}\n".format(texcoord.x(), texcoord.y())
objstr = objstr + "vt {0} {1}\n".format(texcoord.x(), 1-texcoord.y()) # reverse V coord

objstr = objstr + "\n"

Expand Down

0 comments on commit 7cdeabb

Please sign in to comment.