gpt4 book ai didi

python - 从 Rhino3d 导出图层为 obj

转载 作者:行者123 更新时间:2023-12-04 23:00:33 26 4
gpt4 key购买 nike

任务

从 Rhino 导出图层为 obj 文件。

问题

使用 Python 脚本时,我运行一个导出命令。界面中会显示一个对话框,而不是导出模型。如果我点击导出界面,它工作正常。然而,它会为每一层弹出对话框。虽然我有很多层,但我想自动化整个导出过程。

最终目标是通过 three.js 在 webGL 中显示 obj 文件。

我是 Python 和 Rhino 的新手,但了解 PHP 和 JavaScript,所以我对这些概念有足够的了解,并且看过一些 Python 教程,所以我可以开始处理这个脚本。

我尝试过的

我正在使用 Rhino、Atom/Python。

import scriptcontext
import rhinoscriptsyntax as rs
from Rhino.Geometry import Point3d

def layerNames(sort=False):
rc = []
for layer in scriptcontext.doc.Layers:
if not layer.IsDeleted: rc.append(layer.FullPath)
if sort: rc.sort()
return rc


rs.EnableRedraw(False)

strPath = rs.DocumentPath()
strName = rs.DocumentName()

arrLayers = layerNames(False)
for layerName in arrLayers:
objs = scriptcontext.doc.Objects.FindByLayer(layerName)
rs.Command("_-Export "+layerName+".obj", False)

注意事项

我正在考虑使用 python native 文件保存 (open("layername.json", "a")。我的想法是以某种方式从每一层中的对象获取网格,将其转换为 three.js json 并使用它相反。但我不知道如何从图层中获取网格。我已经导入了 Rhino.Geometry 以查看它是否有帮助。我不知道如何找到要转换的网格,或者我是否可以以某种方式使用以自动方式执行 native 导出命令,仅使用 obj 文件。

最佳答案

这是导出 dae、obj 和 STL 的最终脚本。适当的设置是相当积极的多边形减少。调整角度和密度来改变它。

我发现您需要先将密度设置为零,然后再将其设置为另一个值,这解决了网格转换中的多边形计数问题。

Also available on gist.

import os
import scriptcontext
import rhinoscriptsyntax as rs


print "//export run started/////////////"

# this function via mcneel/rhinoscriptsyntax
#https://github.com/mcneel/rhinoscriptsyntax/blob/master/Scripts/rhinoscript/layer.py
def layerNames(sort=False):
rc = []
for layer in scriptcontext.doc.Layers:
if not layer.IsDeleted: rc.append(layer.FullPath)
if sort: rc.sort()
return rc

def GetDAESettings():
e_str = ""
return e_str

def GetOBJSettings():
e_str = "_Geometry=_Mesh "
e_str+= "_EndOfLine=CRLF "
e_str+= "_ExportRhinoObjectNames=_ExportObjectsAsOBJGroups "
e_str+= "_ExportMeshTextureCoordinates=_Yes "
e_str+= "_ExportMeshVertexNormals=_No "
e_str+= "_CreateNGons=_No "
e_str+= "_ExportMaterialDefinitions=_No "
e_str+= "_YUp=_No "
e_str+= "_WrapLongLines=Yes "
e_str+= "_VertexWelding=_Welded "
e_str+= "_WritePrecision=4 "
e_str+= "_Enter "

e_str+= "_DetailedOptions "
e_str+= "_JaggedSeams=_No "
e_str+= "_PackTextures=_No "
e_str+= "_Refine=_Yes "
e_str+= "_SimplePlane=_No "

e_str+= "_AdvancedOptions "
e_str+= "_Angle=50 "
e_str+= "_AspectRatio=0 "
e_str+= "_Distance=0.0"
e_str+= "_Density=0 "
e_str+= "_Density=0.45 "
e_str+= "_Grid=0 "
e_str+= "_MaxEdgeLength=0 "
e_str+= "_MinEdgeLength=0.0001 "

e_str+= "_Enter _Enter"

return e_str

def GetSTLSettings():
eStr = "_ExportFileAs=_Binary "
eStr+= "_ExportUnfinishedObjects=_Yes "
eStr+= "_UseSimpleDialog=_No "
eStr+= "_UseSimpleParameters=_No "
eStr+= "_Enter _DetailedOptions "
eStr+= "_JaggedSeams=_No "
eStr+= "_PackTextures=_No "
eStr+= "_Refine=_Yes "
eStr+= "_SimplePlane=_No "
eStr+= "_AdvancedOptions "
eStr+= "_Angle=15 "
eStr+= "_AspectRatio=0 "
eStr+= "_Distance=0.01 "
eStr+= "_Grid=16 "
eStr+= "_MaxEdgeLength=0 "
eStr+= "_MinEdgeLength=0.0001 "
eStr+= "_Enter _Enter"
return eStr

settingsList = {
'GetDAESettings': GetDAESettings,
'GetOBJSettings': GetOBJSettings,
'GetSTLSettings': GetSTLSettings
}




fileName = rs.DocumentName()
filePath = rs.DocumentPath().rstrip(fileName)

arrLayers = layerNames(False)



def initExportByLayer(fileType="obj", visibleonly=False, byObject=False):
for layerName in arrLayers:
layer = scriptcontext.doc.Layers.FindByFullPath(layerName, True)
if layer >= 0:
layer = scriptcontext.doc.Layers[layer]
save = True;
if visibleonly:
if not layer.IsVisible:
save = False
if rs.IsLayerEmpty(layerName):
save = False
if save:
cutName = layerName.split("::")
cutName = cutName[len(cutName)-1]
objs = scriptcontext.doc.Objects.FindByLayer(cutName)
if len(objs) > 0:
if byObject:
i=0
for obj in objs:
i= i+1
saveObjectsToFile(cutName+"_"+str(i), [obj], fileType)
else:
saveObjectsToFile(cutName, objs, fileType)



def saveObjectsToFile(name, objs, fileType):
rs.EnableRedraw(False)
if len(objs) > 0:
settings = settingsList["Get"+fileType.upper()+"Settings"]()
rs.UnselectAllObjects()
for obj in objs:
obj.Select(True)
name = "".join(name.split(" "))
command = '-_Export "{}{}{}" {}'.format(filePath, name, "."+fileType.lower(), settings)
rs.Command(command, True)
rs.EnableRedraw(True)


initExportByLayer("obj",True, False)
initExportByLayer("dae",True, False)
initExportByLayer("stl",True, False)

print "//export run ended/////////////"

关于python - 从 Rhino3d 导出图层为 obj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29401951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com