玛雅人:草生成器 python
Maya : Grass generator python
大家好,我正在尝试创建一个草生成器,使用 Maya python。
我是初学者,我只接触到复制部分,如何将刀片(草)铺在表面上???,谢谢!
import maya.cmds as MC
from random import uniform as RN
blade = MC.polyCone( sx=3, sy=5, sz=0, r=0.04, h=1, ax=(0, 1, 0))
MC.setAttr (blade[0] + '.translateY', (.5))
MC.delete(ch=True)
MC.polySoftEdge ( a=0 )
#adding deformer
bend = MC.nonLinear( type='bend', lowBound=0, highBound = 2,
curvature=30)
vtxPos = MC.xform(blade[0]+'.vtx[0]', q=True, t=True, ws=True)
MC.xform(bend, t=(0,vtxPos[1], 0))
MC.select (blade[0])
MC.delete(ch=True)
MC.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
groupblades = MC.group(empty=True, name=blade[0] + '_grp#')
for i in range(200):
obj = MC.instance(blade[0])
MC.move(RN(1,-1), 0, RN(1,-1), blade[0])
MC.rotate(0, RN(0,360),0, blade[0])
MC.parent(obj, groupblades)
肯定有很多不同的方法可以用来在表面上散布物体。您可以使用几何和法线约束。有 MASH 的框架。但我认为使用毛囊很容易,而且需要的开销更少。唯一的问题是表面必须有紫外线,因为这是我们移动毛囊的方式。
所以这就是我的建议:
- 创建毛囊,并将其连接到要散布的表面。
- 创建你的草对象。
- 每次实例化一个新草时,为毛囊选择一个随机 uv 弹出。这将使它与脸部的位置和法线对齐。
- 使用毛囊的变换move/rotate你的草。
- 冲洗并重复步骤 3 和 4。
这是一个完整的例子:
import maya.cmds as cmds
import random
surface = "pPlane1" # Specify the object you want to scatter on.
fol_shape = cmds.createNode("follicle") # Create a new follicle object.
fol = cmds.listRelatives(fol_shape, f=True, parent=True)[0] # Get the follicle's transform.
# Below we are connecting the follicle to the surface so we can control it with its uvs.
cmds.connectAttr("{}.outMesh".format(surface), "{}.inputMesh".format(fol))
cmds.connectAttr("{}.worldMatrix[0]".format(surface), "{}.inputWorldMatrix".format(fol))
cmds.connectAttr("{}.outTranslate".format(fol_shape), "{}.translate".format(fol))
cmds.connectAttr("{}.outRotate".format(fol_shape), "{}.rotate".format(fol))
cmds.setAttr("{}.simulationMethod".format(fol_shape), 0)
# Begin creating blade of grass.
blade = cmds.polyCone(sx=3, sy=5, sz=0, r=0.04, h=1, ax=(0, 1, 0))
cmds.setAttr(blade[0] + '.translateY', .5)
cmds.delete(ch=True)
cmds.polySoftEdge(a=0)
# Setting wirecolor to green so it's easier to see.
cmds.setAttr(blade[0] + ".overrideEnabled", True)
cmds.setAttr(blade[0] + ".overrideRGBColors", True)
cmds.setAttr(blade[0] + ".overrideColorRGB", 0.06, 0.2, 0.06)
bend = cmds.nonLinear(type='bend', lowBound=0, highBound=2, curvature=30)
vtxPos = cmds.xform(blade[0] + '.vtx[0]', q=True, t=True, ws=True)
cmds.xform(bend, t=(0, vtxPos[1], 0))
cmds.select(blade[0])
cmds.delete(ch=True)
cmds.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
groupblades = cmds.group(empty=True, name=blade[0] + '_grp#')
for i in range(400):
obj = cmds.instance(blade[0])
# Randomly set the follicle's uv so it's jumps somewhere else on the surface. Value needs to be between 0.0-1.0
cmds.setAttr("{}.parameterU".format(fol_shape), random.random())
cmds.setAttr("{}.parameterV".format(fol_shape), random.random())
cmds.matchTransform(obj, fol) # Make grass match follicle's position/rotation/scale.
cmds.rotate(-90, random.random() * 360, 0, obj, r=True) # Rotate it upright and give it random twist.
cmds.move(0, 0.4, 0, obj, r=True) # The grass's pivot is in the center, so need to offset translation a bit up.
cmds.parent(obj, groupblades)
cmds.delete(fol) # Follicle no longer needed, so delete it.
有一部分我需要将草向上移动一点,因为轴心点位于草的中心。这是一个小 hack,真正的解决方案是在其底部构建具有枢轴点的草。这样做可以解决任何漂浮的草地问题,并使随机缩放更容易。
这是它将输出的内容:
大家好,我正在尝试创建一个草生成器,使用 Maya python。
我是初学者,我只接触到复制部分,如何将刀片(草)铺在表面上???,谢谢!
import maya.cmds as MC
from random import uniform as RN
blade = MC.polyCone( sx=3, sy=5, sz=0, r=0.04, h=1, ax=(0, 1, 0))
MC.setAttr (blade[0] + '.translateY', (.5))
MC.delete(ch=True)
MC.polySoftEdge ( a=0 )
#adding deformer
bend = MC.nonLinear( type='bend', lowBound=0, highBound = 2,
curvature=30)
vtxPos = MC.xform(blade[0]+'.vtx[0]', q=True, t=True, ws=True)
MC.xform(bend, t=(0,vtxPos[1], 0))
MC.select (blade[0])
MC.delete(ch=True)
MC.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
groupblades = MC.group(empty=True, name=blade[0] + '_grp#')
for i in range(200):
obj = MC.instance(blade[0])
MC.move(RN(1,-1), 0, RN(1,-1), blade[0])
MC.rotate(0, RN(0,360),0, blade[0])
MC.parent(obj, groupblades)
肯定有很多不同的方法可以用来在表面上散布物体。您可以使用几何和法线约束。有 MASH 的框架。但我认为使用毛囊很容易,而且需要的开销更少。唯一的问题是表面必须有紫外线,因为这是我们移动毛囊的方式。
所以这就是我的建议:
- 创建毛囊,并将其连接到要散布的表面。
- 创建你的草对象。
- 每次实例化一个新草时,为毛囊选择一个随机 uv 弹出。这将使它与脸部的位置和法线对齐。
- 使用毛囊的变换move/rotate你的草。
- 冲洗并重复步骤 3 和 4。
这是一个完整的例子:
import maya.cmds as cmds
import random
surface = "pPlane1" # Specify the object you want to scatter on.
fol_shape = cmds.createNode("follicle") # Create a new follicle object.
fol = cmds.listRelatives(fol_shape, f=True, parent=True)[0] # Get the follicle's transform.
# Below we are connecting the follicle to the surface so we can control it with its uvs.
cmds.connectAttr("{}.outMesh".format(surface), "{}.inputMesh".format(fol))
cmds.connectAttr("{}.worldMatrix[0]".format(surface), "{}.inputWorldMatrix".format(fol))
cmds.connectAttr("{}.outTranslate".format(fol_shape), "{}.translate".format(fol))
cmds.connectAttr("{}.outRotate".format(fol_shape), "{}.rotate".format(fol))
cmds.setAttr("{}.simulationMethod".format(fol_shape), 0)
# Begin creating blade of grass.
blade = cmds.polyCone(sx=3, sy=5, sz=0, r=0.04, h=1, ax=(0, 1, 0))
cmds.setAttr(blade[0] + '.translateY', .5)
cmds.delete(ch=True)
cmds.polySoftEdge(a=0)
# Setting wirecolor to green so it's easier to see.
cmds.setAttr(blade[0] + ".overrideEnabled", True)
cmds.setAttr(blade[0] + ".overrideRGBColors", True)
cmds.setAttr(blade[0] + ".overrideColorRGB", 0.06, 0.2, 0.06)
bend = cmds.nonLinear(type='bend', lowBound=0, highBound=2, curvature=30)
vtxPos = cmds.xform(blade[0] + '.vtx[0]', q=True, t=True, ws=True)
cmds.xform(bend, t=(0, vtxPos[1], 0))
cmds.select(blade[0])
cmds.delete(ch=True)
cmds.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
groupblades = cmds.group(empty=True, name=blade[0] + '_grp#')
for i in range(400):
obj = cmds.instance(blade[0])
# Randomly set the follicle's uv so it's jumps somewhere else on the surface. Value needs to be between 0.0-1.0
cmds.setAttr("{}.parameterU".format(fol_shape), random.random())
cmds.setAttr("{}.parameterV".format(fol_shape), random.random())
cmds.matchTransform(obj, fol) # Make grass match follicle's position/rotation/scale.
cmds.rotate(-90, random.random() * 360, 0, obj, r=True) # Rotate it upright and give it random twist.
cmds.move(0, 0.4, 0, obj, r=True) # The grass's pivot is in the center, so need to offset translation a bit up.
cmds.parent(obj, groupblades)
cmds.delete(fol) # Follicle no longer needed, so delete it.
有一部分我需要将草向上移动一点,因为轴心点位于草的中心。这是一个小 hack,真正的解决方案是在其底部构建具有枢轴点的草。这样做可以解决任何漂浮的草地问题,并使随机缩放更容易。
这是它将输出的内容: