我正在听一个课堂讲座,它是 Maya 中的 Python,我遇到了第 18 行“没有对象匹配名称:p”的问题,我不确定这意味着什么,
p
它的引用来自单词 point,它应该利用来自球体位置的信息。
import maya.cmds as cmds
import random
def create_random_sphere():
x = random.uniform (-100,100)
y = random.uniform (-100,100)
z = random.uniform (-100,100)
sphere = cmds.polySphere()[0]
cmds.move(x,y,z, sphere)
return sphere
def create_pipe(start,end):
cmds.curve(degree = 1, point=[cmds.xform(start,query=True,translation=True), cmds.xform(end,query=True,translation=True)])
return curve
def create_spheres_pipes(num_spheres):
spheres = []
for i in range(num_spheres):
create_random_sphere()
spheres.append(sphere)
pipes = []
for i in range(len(spheres)-1):
create_pipe(sphere[i],spheres[i+1])
pipes.append(pipe)
pipe= create_pipe(spheres[-1],spheres[0])
pipes.append(pipe)
return spheres, pipes
def createspheresandpipes(num_spheres):
spheres, pipes = create_spheres_pipes(num_spheres)
circle = cmds.circle(normal=(0,1,0),radius= 0.2)
for pipe in pipes:
cmds.select(circle[0],pipe)
cmds.extrude(et=2,ucp=1,fpt= True,upn=True)
createspheresandpipes(10)
目标是制作一堆随机生成的球体,并用管道连接它们,我无法生成管道。
到处都是错别字。
首次运行代码时,我得到以下信息:
# Traceback (most recent call last):
# File "<maya console>", line 47, in <module>
# File "<maya console>", line 40, in createspheresandpipes
# File "<maya console>", line 27, in create_spheres_pipes
# NameError: global name 'sphere' is not defined #
为了解决这个问题,我将第 26 行从
create_random_sphere()
更改为 sphere = create_random_sphere()
。
再次运行我得到您当前的问题:
# Traceback (most recent call last):
# File "<maya console>", line 47, in <module>
# File "<maya console>", line 40, in createspheresandpipes
# File "<maya console>", line 32, in create_spheres_pipes
# File "<maya console>", line 18, in create_pipe
# ValueError: No object matches name: p #
在第 32 行,将
create_pipe(sphere[i],spheres[i+1])
更改为 create_pipe(spheres[i],spheres[i+1])
进行修复。
结果是
# Traceback (most recent call last):
# File "<maya console>", line 47, in <module>
# File "<maya console>", line 40, in createspheresandpipes
# File "<maya console>", line 32, in create_spheres_pipes
# File "<maya console>", line 20, in create_pipe
# NameError: global name 'curve' is not defined #
通过将第 18 行从
cmds.curve(...
更改为 curve = cmds.curve(...
来修复。
结果是:
# Error: local variable 'pipe' referenced before assignment
# Traceback (most recent call last):
# File "<maya console>", line 47, in <module>
# File "<maya console>", line 40, in createspheresandpipes
# File "<maya console>", line 33, in create_spheres_pipes
# UnboundLocalError: local variable 'pipe' referenced before assignment #
因为在第 32 行你做了
create_pipe(spheres[i],spheres[i+1])
而不是 pipe = create_pipe(spheres[i],spheres[i+1])
之后运行它会产生以下结果: