首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Maya中的Python,UV脚本的语法错误

Maya中的Python,UV脚本的语法错误
EN

Stack Overflow用户
提问于 2022-07-15 09:59:44
回答 1查看 188关注 0票数 0

我试图分配根UVs到一个alembic文件,以便我可以给XGen毛皮的网格下的颜色在虚幻的引擎。我有一个美洲虎网与UV分配,并试图把他们转移到马夫。我正在使用Epic代码,但是我一直收到"# Error:无效syntax#“通知。我在用玛雅2022。我在编程方面不是很有经验,所以如果有人能发现导致代码崩溃的原因,我会非常感激的。提前谢谢你!

Epic文档:https://docs.unrealengine.com/4.27/en-US/WorkingWithContent/Hair/XgenGuidelines/

代码语言:javascript
运行
复制
from maya import cmds
from maya import OpenMaya
import os

def create_root_uv_attribute(curves_group, mesh_node, uv_set='map1'):
'''
Create "groom_root_uv" attribute on group of curves.
'''

# check curves group
if not cmds.objExists(curves_group):
raise RuntimeError('Group not found: "{}"'.format(curves_group))

# get curves in group
curve_shapes = cmds.listRelatives(curves_group, shapes=True, noIntermediate=True)
curve_shapes = cmds.ls(curve_shapes, type='nurbsCurve')
if not curve_shapes:
raise RuntimeError('Invalid curves group. No nurbs-curves found in group.')
else:
print "found curves"
print curve_shapes

# get curve roots
points = list()
for curve_shape in curve_shapes:
point = cmds.pointPosition('{}.cv[0]'.format(curve_shape), world=True)
points.append(point)

# get uvs
values = list()
uvs = find_closest_uv_point(points, mesh_node, uv_set=uv_set)
for u, v in uvs:
values.append([u, v, 0])
#print (str(u) + " , " + str(v) )

# create attribute
name = 'groom_root_uv'
cmds.addAttr(curves_group, ln=name, dt='vectorArray')
cmds.addAttr(curves_group, ln='{}_AbcGeomScope'.format(name), dt='string')
cmds.addAttr(curves_group, ln='{}_AbcType'.format(name), dt='string')

cmds.setAttr('{}.{}'.format(curves_group, name), len(values), *values, type='vectorArray')
cmds.setAttr('{}.{}_AbcGeomScope'.format(curves_group, name), 'uni', type='string')
cmds.setAttr('{}.{}_AbcType'.format(curves_group, name), 'vector2', type='string')

return uvs

def find_closest_uv_point(points, mesh_node, uv_set='map1'):
'''
Find mesh UV-coordinates at given points.
'''

# check mesh
if not cmds.objExists(mesh_node):
raise RuntimeError('Node not found: "{}"'.format(mesh_node))

# check uv_set
uv_sets = cmds.polyUVSet(mesh_node, q=True, allUVSets=True)
if uv_set not in uv_sets:
raise RuntimeError('Invalid uv_set provided: "{}"'.format(uv_set))

# get mesh as dag-path
selection_list = OpenMaya.MSelectionList()
selection_list.add(mesh_node)

mesh_dagpath = OpenMaya.MDagPath()
selection_list.getDagPath(0, mesh_dagpath)
mesh_dagpath.extendToShape()

# get mesh function set
fn_mesh = OpenMaya.MFnMesh(mesh_dagpath)

uvs = list()
for i in range(len(points)):

script_util = OpenMaya.MScriptUtil()
script_util.createFromDouble(0.0, 0.0)
uv_point = script_util.asFloat2Ptr()

point = OpenMaya.MPoint(*points[i])
fn_mesh.getUVAtPoint(point, uv_point, OpenMaya.MSpace.kWorld, uv_set)

u = OpenMaya.MScriptUtil.getFloat2ArrayItem(uv_point, 0, 0)
v = OpenMaya.MScriptUtil.getFloat2ArrayItem(uv_point, 0, 1)

uvs.append((u, v))

return uvs

def abc_export(filepath, node=None, start_frame=1, end_frame=1, data_format='otawa', uv_write=True):

job_command = '-frameRange {} {} '.format(start_frame, end_frame)
job_command += '-dataFormat {} '.format(data_format)

job_command += '-attr groom_root_uv '

if uv_write:
job_command += '-uvWrite '

job_command += '-root {} '.format(node)

job_command += '-file {} '.format(filepath)

cmds.AbcExport(verbose=True, j=job_command)

def main():

export_directory = 'D:/Projects/Jaguar_Groom/Jag_Groom_copied_14_07/New/Maya_Xgen'
hair_file = os.path.join(export_directory, 'hair_export.abc')
curve_top_group= 'alembic_curves:jaguar_main_fur_splineDescription'
uv_mesh='retopo_furmesh'

create_root_uv_attribute( curve_top_group , uv_mesh)
abc_export(hair_file, curve_top_group)

main()
EN

回答 1

Stack Overflow用户

发布于 2022-07-15 14:51:20

对我来说,只要做一次修改就行了。我更新了create_root_uv_attribute(.)中的两个打印语句有括号()。

默认情况下,Maya 2022使用Python 3,而这段代码似乎是为Python 2编写的,例如,print 'Hello World‘在2中有效,在3中无效,预期打印(’Hello World')。

您可以按照下面链接中的说明启动Python 2中的Maya 2022:

https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2022/ENU/Maya-Scripting/files/GUID-C0F27A50-3DD6-454C-A4D1-9E3C44B3C990-htm.html

我已经包括了下面更新的代码。

代码语言:javascript
运行
复制
from maya import cmds
from maya import OpenMaya
import os

def create_root_uv_attribute(curves_group, mesh_node, uv_set='map1'):
    '''
    Create "groom_root_uv" attribute on group of curves.
    '''

    # check curves group
    if not cmds.objExists(curves_group):
        raise RuntimeError('Group not found: "{}"'.format(curves_group))

    # get curves in group
    curve_shapes = cmds.listRelatives(curves_group, shapes=True, noIntermediate=True)
    curve_shapes = cmds.ls(curve_shapes, type='nurbsCurve')
    if not curve_shapes:
        raise RuntimeError('Invalid curves group. No nurbs-curves found in group.')
    else:
        print("found curves")
        print(curve_shapes)

    # get curve roots
    points = list()
    for curve_shape in curve_shapes:
        point = cmds.pointPosition('{}.cv[0]'.format(curve_shape), world=True)
        points.append(point)

    # get uvs
    values = list()
    uvs = find_closest_uv_point(points, mesh_node, uv_set=uv_set)
    for u, v in uvs:
        values.append([u, v, 0])

    # create attribute
    name = 'groom_root_uv'
    cmds.addAttr(curves_group, ln=name, dt='vectorArray')
    cmds.addAttr(curves_group, ln='{}_AbcGeomScope'.format(name), dt='string')
    cmds.addAttr(curves_group, ln='{}_AbcType'.format(name), dt='string')

    cmds.setAttr('{}.{}'.format(curves_group, name), len(values), *values, type='vectorArray')
    cmds.setAttr('{}.{}_AbcGeomScope'.format(curves_group, name), 'uni', type='string')
    cmds.setAttr('{}.{}_AbcType'.format(curves_group, name), 'vector2', type='string')

    return uvs

def find_closest_uv_point(points, mesh_node, uv_set='map1'):
    '''
    Find mesh UV-coordinates at given points.
    '''

    # check mesh
    if not cmds.objExists(mesh_node):
        raise RuntimeError('Node not found: "{}"'.format(mesh_node))

    # check uv_set
    uv_sets = cmds.polyUVSet(mesh_node, q=True, allUVSets=True)
    if uv_set not in uv_sets:
        raise RuntimeError('Invalid uv_set provided: "{}"'.format(uv_set))

    # get mesh as dag-path
    selection_list = OpenMaya.MSelectionList()
    selection_list.add(mesh_node)

    mesh_dagpath = OpenMaya.MDagPath()
    selection_list.getDagPath(0, mesh_dagpath)
    mesh_dagpath.extendToShape()

    # get mesh function set
    fn_mesh = OpenMaya.MFnMesh(mesh_dagpath)

    uvs = list()
    for i in range(len(points)):

        script_util = OpenMaya.MScriptUtil()
        script_util.createFromDouble(0.0, 0.0)
        uv_point = script_util.asFloat2Ptr()

        point = OpenMaya.MPoint(*points[i])
        fn_mesh.getUVAtPoint(point, uv_point, OpenMaya.MSpace.kWorld, uv_set)

        u = OpenMaya.MScriptUtil.getFloat2ArrayItem(uv_point, 0, 0)
        v = OpenMaya.MScriptUtil.getFloat2ArrayItem(uv_point, 0, 1)

        uvs.append((u, v))

    return uvs

def abc_export(filepath, node=None, start_frame=1, end_frame=1, data_format='otawa', uv_write=True):

    job_command = '-frameRange {} {} '.format(start_frame, end_frame)
    job_command += '-dataFormat {} '.format(data_format)

    job_command += '-attr groom_root_uv '

    if uv_write:
        job_command += '-uvWrite '

    job_command += '-root {} '.format(node)   

    job_command += '-file {} '.format(filepath) 

    cmds.AbcExport(verbose=True, j=job_command)

def main():

    export_directory = 'D:/Projects/Jaguar_Groom/Jag_Groom_copied_14_07/New/Maya_Xgen'
    hair_file = os.path.join(export_directory, 'hair_export.abc')
    curve_top_group='alembic_curves:jaguar_main_fur_splineDescription'
    uv_mesh='retopo_furmesh'

    create_root_uv_attribute( curve_top_group , uv_mesh)
    abc_export(hair_file, curve_top_group)

main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72992272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档