首页 > 其他分享 >plasticity 导入 blender

plasticity 导入 blender

时间:2023-10-27 10:35:26浏览次数:44  
标签:index obj plasticity mesh face 导入 bpy group blender

准备

  1. plasticity-blender-addon-Feature-Enhancements
  2. Quad Remesher for blender , https://www.exoside.com/quadremesherdata/QuadRemesher_1.2_UserDoc.pdf

步骤

  1. 连接刷新→三角refacet
  2. quad Remesher重拓扑

顶点颜色批量转材质

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

bl_info = {
    "name" : "VertexColor2Materials",
    "author" : "Nolca",
    "description" : "",
    "blender" : (2, 80, 0),
    "version" : (0, 0, 1),
    "location" : "Properties > Render > My Awesome Panel",
    "warning" : "",
    "category" : "Mesh"
}

import bpy
import random

class PLASTICITY_PT_Panel_VC2M(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Materials"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Plasticity'

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        row = layout.row()
        row.operator("materials.convert_to_materials")

class ConvertMaterialsOperator(bpy.types.Operator):
    bl_idname = "materials.convert_to_materials"
    bl_label = "顶点颜色转换为材质"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        return any("plasticity_id" in obj.keys() and obj.type == 'MESH' for obj in context.selected_objects)

    def execute(self, context):
        prev_obj_mode = bpy.context.mode

        for obj in context.selected_objects:
            if obj.type != 'MESH':
                continue
            if not "plasticity_id" in obj.keys():
                continue
            mesh = obj.data

            if "plasticity_id" not in obj.keys():
                self.report(
                    {'ERROR'}, "Object doesn't have a plasticity_id attribute.")
                return {'CANCELLED'}

            bpy.context.view_layer.objects.active = obj
            bpy.ops.object.mode_set(mode='OBJECT')

            self.colorize_mesh(obj, mesh)
            # vertex colors to mesh's material

            
        return {'FINISHED'}

    def colorize_mesh(self, obj, mesh):
        groups = mesh["groups"]
        face_ids = mesh["face_ids"]

        if len(groups) == 0:
            return
        if len(face_ids) * 2 != len(groups):
            return

        if not mesh.vertex_colors:
            mesh.vertex_colors.new()
        color_layer = mesh.vertex_colors.active

        group_idx = 0
        group_start = groups[group_idx * 2 + 0]
        group_count = groups[group_idx * 2 + 1]
        face_id = face_ids[group_idx]
        print(face_id,*face_ids)
        color = generate_color_by_density(face_id)

        for poly in mesh.polygons:
            loop_start = poly.loop_start
            if loop_start >= group_start + group_count:
                group_idx += 1
                group_start = groups[group_idx * 2 + 0]
                group_count = groups[group_idx * 2 + 1]
                face_id = face_ids[group_idx]
                color = generate_color_by_density(face_id)

                # Create a new material
                mat = bpy.data.materials.new(name="M."+str(face_id))
                mat.use_nodes = True
                bsdf = mat.node_tree.nodes["Principled BSDF"]
                bsdf.inputs[0].default_value = color
                obj.data.materials.append(mat)

            for loop_index in range(loop_start, loop_start + poly.loop_total):
                color_layer.data[loop_index].color = color
                # Apply the material to faces specified by loop_index
                mesh.polygons[poly.index].material_index = len(obj.data.materials) - 1

# TODO
def generate_color_by_density(face_id):
    return (random.random(), random.random(), random.random(), 1.0)

classes=(
    PLASTICITY_PT_Panel_VC2M,
    ConvertMaterialsOperator,
)

def register():
    for i in classes:
        bpy.utils.register_class(i)

def unregister():
    for i in classes:
        bpy.utils.unregister_class(i)

if __name__ == "__main__":
    register()


def main():
    # get the active object
    obj = bpy.context.active_object

    # get the vertex colors
    vcol_layer = obj.data.vertex_colors.active

    # get the mesh
    mesh = bpy.context.object.data

    print()

调试代码:了解polygons[index]的顺序

import bpy

def view_select():
    for area in bpy.context.screen.areas:
        if area.type == 'VIEW_3D':
            break

    # Set the context
    override = bpy.context.copy()
    override['area'] = area
    override['region'] = area.regions[-1]
    bpy.ops.view3d.view_selected(override,use_all_regions=False)


def select_face_by_index(obj, index):
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.object.mode_set(mode='OBJECT')
    obj.data.polygons[index].select = True
    bpy.ops.object.mode_set(mode='EDIT')
    
index=0
obj=bpy.context.active_object
try:
    with open("index.txt", "r") as f:
        index=int(f.read())
        select_face_by_index(obj, index)
        index+=1
    with open("index.txt", "w") as f:
        f.write(str(index))
    view_select()
except:
    with open("index.txt", "w") as f:
        f.write('0')

标签:index,obj,plasticity,mesh,face,导入,bpy,group,blender
From: https://www.cnblogs.com/nolca/p/17790167.html

相关文章

  • PostGIS安装及空间数据库的创建与shp数据导入
    PostGISisaspatialdatabaseextenderforPostgreSQLobject-relationaldatabase.ItaddssupportforgeographicobjectsallowinglocationqueriestoberuninSQL.PostGIS是空间数据库,是PostgreSQL的一个扩展,PostGIS提供如下空间信息服务功能:空间对象、空间索引、......
  • 在Houdini中创建布料,并导入到Unity中
    在Houdini中创建一个具有物理效果和贴图的布料,导入到Unity中,实现一个效果良好的、可以与模型互动、有贴图的静态布料模型。参考视频:Houdini+Unity2021制作布料全流程!_哔哩哔哩_bilibili1、创建节点首先创建一个obj文件:随后右键这个节点,创建一个DigitalAssret。进入Typ......
  • 如何用MySQL快速导入sql数据?
     在MySQL中,可以使用多种方法来快速导入SQL数据。以下是一些常用的方法和技巧,以帮助你在MySQL中快速导入大量的SQL数据。1.使用mysql命令行工具  -将SQL文件保存到本地计算机上。  -打开终端或命令提示符窗口,并导航到mysql命令行工具所在的路径。 ......
  • 导入失败!检索 COM 类工厂中 CLSID 为 {36D27C48-A1E8-11D3-BA55-00C04F72F325} 的组件
    出现以上错误,需要进行如下配置:一、配置project的DCOM权限1:在服务器上安装office的Project软件.2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务"3:依次双击"组件服务"->"计算机"->"我的电脑"->"DCOM配置"4:在"DCOM配置"中找到"Micro......
  • jdk导入安全证书
    jdk导入安全证书 SSLHandshakeExceptionExceptioninthread"main"javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException:unabletofind......
  • python模块导入规则(相对导入和绝对导入)
    python模块可以相对导入和绝对导入,但这两者是不能替换使用的。本文主要讨论工作目录下模块之间的导入规则。其中相对导入前面有一个'.',表示从该脚本所在目录开始索引,而绝对导入前面没有'.',表示从根目录开始索引。首先明确一点,python认为的根目录为当前运行的脚本所在的目录,而......
  • 导入模块、打开工程
    导入:一、先把要导入的模块复制过去,然后再导入黑点。比如把E盘文件复制到D盘,然后复制D盘路径再从idea导入 二、或者直接建一个新模块,然后直接复制进去  打开工程   ......
  • 延迟导入Python模块的几种方法
    延迟导入Python模块的几种方法-知乎(zhihu.com)#__init__.pyimportimportlib__all__=['complicated']def__getattr__(name):ifnamein__all__:returnimportlib.import_module("."+name,__name__)else:raiseAttributeError(f&qu......
  • zookeeper源码(02)源码编译启动及idea导入
    本文介绍一下zookeeper-3.9.0源码下载、编译及本地启动。下载源码gitclonehttps://gitee.com/apache/zookeeper.gitcdzookeepergitcheckoutrelease-3.9.0gitcheckout-brelease-3.9.0源码编译README_packaging.md文件该文件介绍了编译zookeeper需要的环境和命令......
  • mysql导入.cvs
    workbench新建1张表,没有import按钮,原因是没有设置主键将一个字段设置为主键后,即可导入数据将要导入的数据文件改为utf-8的格式,使用记事本打开查看选择文件选择数据库表查看字段与数据是否对应开始导入......