Python 如何为three.js模型加载器将对象转换为json文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15887736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:16:57  来源:igfitidea点击:

How to convert object to json file for three.js model loader

pythonjsonthree.jsmaya

提问by user2259103

I am building a browser game with three.jsand I want to load a model from Maya 2013 into my scene. I have exported the model as an obj file.

我正在构建一个浏览器游戏,three.js我想将 Maya 2013 中的模型加载到我的场景中。我已将模型导出为 obj 文件。

Now I need to know how to convert it into an JS file for the three.js-loader.

现在我需要知道如何将它转换为three.js-loader的 JS 文件。

This is my loader so far:

到目前为止,这是我的装载机:

 var jsonLoader = new THREE.JSONLoader();
 jsonLoader.load( "models/model.js", addModelToScene );

Thanks in advance

提前致谢

回答by Hobbes

Use Blender.

使用搅拌机。

Blender 3D

搅拌机 3D

Then install this addon:

然后安装这个插件:

Three.js exporter

三.js 导出器

回答by Videep Mishraa

Thats a pretty late reply. I have written a converter to read obj files and convert to ThreeJS supported JSON format: https://github.com/theMaxscriptGuy/Windows_Programs/tree/master/Obj_To_ThreeJS

那是一个很晚的回复。我编写了一个转换器来读取 obj 文件并转换为 ThreeJS 支持的 JSON 格式:https: //github.com/theMaxscriptGuy/Windows_Programs/tree/master/Obj_To_ThreeJS

Download all the files as zip and run converter.exe...

将所有文件下载为 zip 并运行 converter.exe...

回答by captDaylight

If you don't want to use blender, mrdoob(made threejs) has a simple python script to convert obj to json. Just run --this script--in terminal like so:

如果你不想使用 Blender,mrdoob(madethreejs) 有一个简单的 python 脚本来将 obj 转换为 json。只需在终端中运行--this script--,如下所示:

python convert_obj_three.py -i infile.obj -o outfile.js

回答by Adrian Moisa

When exporting 3ds max models to three.js using the [convert_obj_three.py][1]script, a little degree of automation can be achieved with the following command in cmd promt:

使用[convert_obj_three.py][1]脚本将 3ds max 模型导出到three.js 时,可以通过cmd promt 中的以下命令实现一点自动化:

cd C:\Python27 &
python convert_obj_three.py -i _obj\chromeBand.obj -o _json\chromeBand.js &
python convert_obj_three.py -i _obj\controlPanelCover.obj -o _json\controlPanelCover.js

I wrote a 3ds max script that will print the phyton script command for multiple models and also a json with the models relative urls and coordinates. The json is useful for placing multiple models at the correct position in scene.

我写了一个 3ds max 脚本,它将打印多个模型的 phyton 脚本命令,以及一个带有模型相对 url 和坐标的 json。json 可用于将多个模型放置在场景中的正确位置。

(
local inputPath = "D:\Archive\Work\_Dev\_Convert\Obj\"
local outputPath = "C:\Server\htdocs\viewer\assets\models\my-project\"
local jsonPath = "assets/models/my-project/"
local myListEntry = "\n"

for o in selection do
    (
        myListEntry +=
        "\"" + o.name + "\": {\n" +
            "\t\"url\": \"" + jsonPath + o.name + ".js\",\n" +
            "\t\"position\": {\n" +
                "\t\t\"x\": \"" + o.position.x as string + "\",\n" +
                "\t\t\"y\": \"" + o.position.z as string + "\",\n" +
                "\t\t\"z\": \"" + (o.position.y * -1) as string + "\"\n" +
            "\t}\n" +
        "},\n"
    )

myListEntry += "\n\n\n=== Run this using cmd promt: ===\n"
myListEntry += "cd C:\Python27 &\n"

for o in selection do
    (
        myListEntry += "python D:\Archive\Work\_Dev\_Convert\convert_obj_three.py -i " + 
            inputPath + o.name + ".obj -o " + 
            outputPath + o.name + ".js & \n"
    )

myListEntry += "exit \n"
print myListEntry
actionMan.executeAction 0 "40472"
)
  • Create a new txt file, rename it to coordinates.mcrand drag and drop the file into the 3ds max window. It will automatically open MaxScript Listener window and print the results. Copy paste the python command in cmd. Use the printed json to batch load your models in your project.
  • Also, scripts can be assigned to buttons in 3dsMax toolbars, for fast access.
  • The above script could use further tweaking. Not the best solution but at least it speeds up things, hopefuly.
  • Use Jos Balcaen's script for exporting obj files in batch(3ds Max)
  • 创建一个新的 txt 文件,将其重命名为coordinates.mcr并将文件拖放到 3ds max 窗口中。它将自动打开 MaxScript Listener 窗口并打印结果。将 python 命令复制粘贴到 cmd 中。使用打印的 json 在项目中批量加载模型。
  • 此外,可以将脚本分配给 3dsMax 工具栏中的按钮,以便快速访问。
  • 上面的脚本可以使用进一步的调整。不是最好的解决方案,但至少它可以加快速度,希望如此。
  • 使用 Jos Balcaen 的脚本批量导出 obj 文件(3ds Max)

Edit:

编辑:

After experimenting a bit with the script I have made a setup that allows me to export my models (counting more than 20) in under 1 minute from 3ds max to open browser tab. Of course, you may need to adjust the json output to your liking. Hope you will find this useful.

在对脚本进行了一些试验后,我做了一个设置,允许我在 1 分钟内从 3ds max 导出我的模型(计数超过 20 个)以打开浏览器选项卡。当然,您可能需要根据自己的喜好调整 json 输出。希望你会发现这很有用。

回答by Karl Rosaen

Note: the python converter script has been updated to a js based tool https://github.com/mrdoob/three.js/tree/dev/utils/converters

注意:python 转换器脚本已更新为基于 js 的工具https://github.com/mrdoob/three.js/tree/dev/utils/converters

Usage:

用法:

node obj2three.js model.obj

node obj2three.js model.obj

回答by Andy

Create a Python file named conversion.pywith the following content: click here to see it.

创建一个以conversion.py以下内容命名的 Python 文件:单击此处查看

And then run this command in your Terminal:

然后在您的终端中运行此命令:

python conversion.py -i file.obj -o file.js