一个物体“进入黑社会"是什么意思? [英] What does it mean for an object to be "in the underworld?"

查看:44
本文介绍了一个物体“进入黑社会"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

def block_stacks(num):
    stack = cmds.group(empty=True, name='Stacks#')
    size = num
    for var in range(num):
        i = 0
        r_rot = random.uniform(0,359)
        block = cmds.polyCube(h=0.5, w=0.5, d=0.5, name='block#')
        cmds.parent(block, stack)
        cmds.move(0, 5.38 + i, 0, 'block*')
        cmds.rotate(0, r_rot, 0, 'block*')
        rR= random.uniform(0, 1.0)
        rG= random.uniform(0, 1.0)
        rB= random.uniform(0, 1.0)
        cmds.polyColorPerVertex('block*', rgb=[rR,rG,rB], cdo = True)
        i+=0.5

block_stacks(5)

在Maya的脚本编辑器中.当我运行它时,随机旋转和随机颜色可以正常工作,并且该块放置在正确的位置,但是它只会创建1个块而不是5个块(就像我打算的那样)并说

in Maya's Script Editor. When I run it, the random rotation, and random color work fine, and the block places at the correct location, but it only creates 1 block instead of 5 (like I intend for it to) and says

警告:无法在地下世界中建立组件或对象的父级."

"Warning: Cannot parent components or objects in the underworld."

多次.我完全不知道这意味着什么,而且显然整个互联网上的任何地方都没有答案,确切说明了此错误是什么.当我运行它时,它仍然会创建该对象,并且不会给出任何红色错误消息.有谁知道这意味着什么,为什么它只使堆栈高1个块而不是原来的5个块?我已经尝试将其修复了将近2个小时,而现在我已经精疲力尽了.

multiple times. I have absolutely no idea what this means, and apparently there is no answer anywhere on the entire internet that says what exactly this error is. It still creates the object when I run it, and it doesn't give any red error message. Does anyone know what this means, and why it only makes a stack 1 block high instead of 5 like it's supposed to? I've been trying to fix this for almost 2 hours and I'm pretty much burnt out now.

推荐答案

我认为该错误意味着您无法将dg节点(没有任何变换的节点)作为dag节点的父级.例如,尝试将对象集作为转换的父项.它不会让您,因为dg节点本身没有转换,因此不能属于层次结构.

I believe the error means that you can't parent a dg node (something that has no transform) to a dag node. For example, try parenting an objectSet to a transform. It won't let you, because dg nodes have no transforms themselves and cannot belong in a hierarchy.

现在它给您这个错误,因为您正尝试将多维数据集的polyCube输入作为父项,而该输入没有任何变换!这是偶然完成的,因为您假设 cmds.polyCube 返回了多维数据集的转换.它不是.实际上,它返回2个项目的列表:多维数据集的转换和多维数据集的polyCube输入.而且,由于 cmds.parent 可以在其第一个参数中接受列表,因此您实际上是在尝试将转换AND polyCube作为堆栈转换的父级.您可以通过抓住命令的第一个索引来轻松避免这种情况,如下所示: cmds.polyCube()[0]

Now it's giving you this error because you're trying to parent the cube's polyCube input, which has no transform! This is being done by accident because you're assuming that cmds.polyCube returns the cube's transform. It does not. In fact, it returns a list of 2 items: the cube's transform, and the cube's polyCube input. And since cmds.parent can accept a list in its first parameter, you are essentially trying to parent the transform AND polyCube to the stack transform. You can easily avoid this by grabbing the command's first index like this: cmds.polyCube()[0]

现在另一个问题是所有多维数据集都移到同一位置.这是因为您的 i 变量位于for循环内.因此,每次迭代 i 都会重置为0而不是递增,因此它们都移动到同一位置.

Now another issue is that all of the cubes move to the same place. This is because your i variable is INSIDE the for loop. So every iteration i resets to 0 instead of being incremented, thus they all move to the same position.

另一个问题是,在许多命令中,您正在使用"block *" .这样做并没有引用 block 变量,而是实际上将捕获所有以"block" 名称开头的转换.实际上,您根本不需要"*" ,只需传递变量 block .

Another issue is that in a lot of your commands you are using "block*". Doing this doesn't refer to the block variable, instead it will actually grab all transforms that start with the name "block". In fact you don't need the "*" at all, just pass the variable block.

牢记所有这些,下面是工作代码:

With all of this in mind, here's the working code:

import random
import maya.cmds as cmds


def block_stacks(num):
    stack = cmds.group(empty=True, name='Stacks#')
    i = 0  # Need to move this OUT of the loop otherwise it always resets to 0 and all of the blocks will move to the same place.

    for var in range(num):
        r_rot = random.uniform(0,359)
        block = cmds.polyCube(h=0.5, w=0.5, d=0.5, name='block#')[0]  # This command actually returns a list of 2 items, the transform and the polyCube input, so grab the first index.
        cmds.parent(block, stack)
        cmds.move(0, 5.38 + i, 0, block)  # Pass the variable.
        cmds.rotate(0, r_rot, 0, block)
        rR = random.uniform(0, 1.0)
        rG = random.uniform(0, 1.0)
        rB = random.uniform(0, 1.0)
        cmds.polyColorPerVertex(block, rgb=[rR, rG, rB], cdo=True)
        i += 0.5


block_stacks(5)

这篇关于一个物体“进入黑社会"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆