0
$\begingroup$

enter image description here

How to turn hair's vector(XYZ) to monkey's vertex color(RGB)

This is the maya python,I want to convert this code to Blender,What should I write?

import maya.cmds as mc
import pymel.core as pm
import maya.OpenMaya as om


def convert_vertex_color():
    selected = mc.ls(sl=1)
    obj_name = selected[0]
    name = mc.duplicate(selected[0], rr=1)
    selected[0] = name[0]
    print selected
    mc.select(selected)
    selected_list = mc.ls(sl=1)

    if len(selected_list) < 2 or len(selected_list) > 2:
        mc.warning('Please select the number of objects correctly')
        return
    obj = pm.PyNode(selected_list[0]).__apimdagpath__()
    mfn_mesh = om.MFnMesh(obj)
    vertex_iter = om.MItMeshVertex(obj)
    group_name = selected_list[2]
    color_list = om.MColorArray()
    index_list = om.MIntArray()
    for curve in mc.listRelatives(group_name, c=1, f=1):

        if mc.objectType(mc.listRelatives(curve, c=1, s=1)) != 'nurbsCurve':
            continue

        point1 = mc.pointPosition(curve + '.cv[0]', w=1)
        point2 = mc.pointPosition(curve + '.cv[2]', w=1)
        vector = om.MVector(om.MVector(point2[0] - point1[0], point2[2] - point1[2], point2[2] - point1[2]))
        vector.normalize()
        while not vertex_iter.isDone():

            pos = vertex_iter.position(om.MSpace.kWorld)

            if abs(pos[0] - point1[0]) <= 0.1 and abs(pos[2] - point1[2]) <= 0.1 and abs(pos[2] - point1[2]) <= 0.1:

                color_list.append(om.MColor((vector[0]+1)/2, (vector[2]+1)/2, (vector[2]+1)/2))
                index_list.append(vertex_iter.index())

            vertex_iter.next()
        vertex_iter.reset()
    mfn_mesh.setVertexColors(color_list, index_list, None, om.MFnMesh.kRGB)

    mc.transferAttributes(selected_list[0], obj_name, pos=0, nml=0, uvs=0, col=2, spa=0, sm=3, flipUVs=0, clb=0)
    mc.setAttr(selected_list[0] + '.visibility', 0)
    mc.warning('Finished')


convert_vertex_color()```


  [1]: https://i.stack.imgur.com/EXdwF.jpg
  [2]: https://i.stack.imgur.com/dClT1.jpg
$\endgroup$
0
$\begingroup$

This code can turn Vertex Position into Vertex Colors,that all I Know.I'm a fresh.

import bpy
from mathutils import Color, Vector

my_object = bpy.context.active_object.data
vert_list = my_object.vertices
color_map_collection = my_object.vertex_colors

if len(color_map_collection) == 0:
    color_map_collection.new()

color_map = color_map_collection['Col']


i = 0
for poly in my_object.polygons:
    for idx in poly.loop_indices:
        loop = my_object.loops[idx]
        v = loop.vertex_index
        x = (vert_list[v].co.x + 1) / 2
        y = (vert_list[v].co.y + 1) / 2
        z = (vert_list[v].co.z + 1) / 2
        t = 0
        final = (x,y,z,t)
        color_map.data[i].color = final
        i += 1


# set to vertex paint mode to see the result
bpy.ops.object.mode_set(mode='VERTEX_PAINT')```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.