C4D UV脚本 Python : ProjUV(原版+汉化版) 

2014-11-21 22:33 发布 | 作品版权归原作者所有,仅供参考学习,禁止商业使用!

插件脚本 /[UV/贴图]
594 1 0
C4D UV脚本 Python : ProjUV
C4D UV脚本 Python : ProjUV(原版+汉化版) - C4D之家 - image_01.png C4D UV脚本 Python : ProjUV(原版+汉化版) - C4D之家 - image_02.png C4D UV脚本 Python : ProjUV(原版+汉化版) - C4D之家 - image_03.png

Fichier d'exemple : Télécharger

Projette le dépliage UV d'un objet sur un autre.

L'outil se présente sous la forme d'une propriété à appliquer sur l'objet à texturer.

L'utilisation d'une même projection sur deux modèles différents permet de les texturer de la même façon malgré leur différence de maillage.

v 1.2 :
- Correction de l'absence de rafraîchissement sous l'OpenGL.

v 1.15 :
- Modèle de projection d'exemple modifié.

v 1.1 :
- Les polygones affectés par la projection sont enregistrés.
- Correction d'une faute de frappe dans le menu.


Copier le dossier « vonc_projuv » dans le dossier « plugins » du répertoire de Cinéma 4D.
Fichier vonc_projuv.PYP :
  1. # ProjUV - v 1.2 - vonc.fr

  2. import os
  3. import c4d
  4. from c4d import bitmaps, plugins, utils, Vector
  5. from c4d.utils import GeRayCollider, Neighbor

  6. MODULE_ID = 1029370
  7. VONCPROJUV_MAJAUTO = 1000
  8. VONCPROJUV_OBJ = 1001
  9. VONCPROJUV_UVW = 1002
  10. VONCPROJUV_SEL = 1003
  11. VONCPROJUV_ACTU = 1004
  12. VONCPROJUV_PLAN = 1005
  13. VONCPROJUV_INVNOR = 1006
  14. VONCPROJUV_INFO = 1007
  15. VONCPROJUV_AFF = 1008

  16. class ProjUV(plugins.TagData):
  17.         lienObj = None
  18.         lienUVW = None
  19.         lienSel = None
  20.         lienAff = None
  21.         majauto = False
  22.         actuprio = False
  23.         creerplan = False
  24.         invnor = False
  25.         
  26.         def InitDonneeAff(self, donnees, obj) :
  27.                 if self.lienAff : return
  28.                 propAff = c4d.BaseTag(c4d.Tpolygonselection)
  29.                 propAff[c4d.ID_BASELIST_NAME] = "ProjUV Sel"
  30.                 obj.InsertTag(propAff)
  31.                 donnees.SetLink(VONCPROJUV_AFF, propAff)
  32.                 self.lienAff = propAff
  33.         
  34.         def InitDonneeUVW(self, donnees, obj) :
  35.                 if self.lienUVW : return
  36.                 propUVW = obj.GetTag(c4d.Tuvw)
  37.                 if propUVW :
  38.                         donnees.SetLink(VONCPROJUV_UVW, propUVW)
  39.                         self.lienUVW = propUVW
  40.         
  41.         def RecupDonnees(self, donnees, doc) :
  42.                 self.lienObj = donnees.GetLink(VONCPROJUV_OBJ, doc)
  43.                 self.lienUVW = donnees.GetLink(VONCPROJUV_UVW, doc)
  44.                 self.lienSel = donnees.GetLink(VONCPROJUV_SEL, doc)
  45.                 self.lienAff = donnees.GetLink(VONCPROJUV_AFF, doc)
  46.                 self.majauto = donnees.GetBool(VONCPROJUV_MAJAUTO)
  47.                 self.invnor = donnees.GetBool(VONCPROJUV_INVNOR)
  48.         
  49.         def Verifieur(self, obj, uvw, tex) :
  50.                 if self.actuprio :
  51.                         self.actuprio = False
  52.                         return True
  53.                 if not self.majauto : return False
  54.                 if not uvw or not tex : return False
  55.                 if not obj.CheckType(c4d.Opolygon) : return False
  56.                 if obj.IsDirty(c4d.DIRTY_MATRIX) : return True
  57.                 if obj.IsDirty(c4d.DIRTY_DATA) : return True
  58.                 texEtGosses = [tex]
  59.                 for o in texEtGosses :
  60.                         if o.IsDirty(c4d.DIRTY_DATA) or o.IsDirty(c4d.DIRTY_MATRIX) :
  61.                                 return True
  62.                         texEtGosses.extend(o.GetChildren())
  63.                
  64.                 return False
  65.         
  66.         def Applatisseur(self, o, doc) :
  67.                 if not o : return
  68.                 obj = utils.SendModelingCommand(command=c4d.MCOMMAND_CURRENTSTATETOOBJECT, list=[o], doc=doc)
  69.                 if not obj : return
  70.                 neutre = c4d.BaseObject(c4d.Onull)
  71.                 obj[0].InsertUnder(neutre)
  72.                 obj = utils.SendModelingCommand(command=c4d.MCOMMAND_JOIN, list=[neutre], doc=doc)
  73.                 if not obj : return
  74.                 utils.SendModelingCommand(command=c4d.MCOMMAND_TRIANGULATE, list=obj, doc=doc)
  75.                 if self.invnor :
  76.                         utils.SendModelingCommand(command=c4d.MCOMMAND_REVERSENORMALS, list=obj, doc=doc)
  77.                 return obj[0]
  78.         
  79.         def CalculNormaleEtUVW(self, o) :
  80.                 uvw = o.GetTag(c4d.Tuvw)
  81.                 if not uvw : return None, None
  82.                 polygones = o.GetAllPolygons()
  83.                 nbPoints = o.GetPointCount()
  84.                 normales = [Vector()] * nbPoints
  85.                 uvws = [Vector()] * nbPoints
  86.                 i = 0
  87.                
  88.                 for poly in polygones :
  89.                         a = poly.a
  90.                         b = poly.b
  91.                         c = poly.c
  92.                         d = poly.d
  93.                         ap = o.GetPoint(a)
  94.                         bp = o.GetPoint(b)
  95.                         cp = o.GetPoint(c)
  96.                         dp = o.GetPoint(d)
  97.                         n = (ap - cp).Cross(bp - dp)
  98.                         normales[a] += n
  99.                         normales[b] += n
  100.                         normales[c] += n
  101.                         normales[d] += n
  102.                         uvwdict = uvw.GetSlow(i)
  103.                         uvws[a] = uvwdict["a"]
  104.                         uvws[b] = uvwdict["b"]
  105.                         uvws[c] = uvwdict["c"]
  106.                         uvws[d] = uvwdict["d"]
  107.                         
  108.                         i += 1
  109.                         
  110.                 return normales, uvws
  111.         
  112.         def CompleteNormales(self, n, obj, objNbPts, objNor, objNorBol) :
  113.                 for ip in xrange(objNbPts) :
  114.                         ipolys = n.GetPointPolys(ip)
  115.                         if not objNorBol[ip] : continue
  116.                         direc = objNor[ip]
  117.                         for ipoly in ipolys :
  118.                                 poly = obj.GetPolygon(ipoly)
  119.                                 a = poly.a
  120.                                 b = poly.b
  121.                                 c = poly.c
  122.                                 d = poly.d
  123.                                 if objNor[a] == Vector() : objNor[a] = direc
  124.                                 if objNor[b] == Vector() : objNor[b] = direc
  125.                                 if objNor[c] == Vector() : objNor[c] = direc
  126.                                 if objNor[d] == Vector() : objNor[d] = direc
  127.                                 objNorBol[a] = True
  128.                                 objNorBol[b] = True
  129.                                 objNorBol[c] = True
  130.                                 objNorBol[d] = True
  131.         
  132.         def CreerProjPlanaire(self, doc, donnees) :
  133.                 self.creerplan = False
  134.                 bd = doc.GetActiveBaseDraw()
  135.                 cam = bd.GetSceneCamera(doc)
  136.                 mg = cam.GetMg() * c4d.utils.MatrixMove(c4d.Vector(0,0,300))
  137.                
  138.                 hn = c4d.BaseObject(c4d.Osds)
  139.                 hn[c4d.SDSOBJECT_SUBDIVIDE_UV] = c4d.SDSOBJECT_SUBDIVIDE_UV_EDGE
  140.                 hn[c4d.SDSOBJECT_SUBEDITOR_CM] = 2
  141.                 hn[c4d.SDSOBJECT_SUBRAY_CM] = 2
  142.                 hn.SetMg(mg)
  143.                 plan = c4d.BaseObject(c4d.Oplane)
  144.                 plan[c4d.PRIM_PLANE_WIDTH] = 200
  145.                 plan[c4d.PRIM_PLANE_HEIGHT] = 200
  146.                 plan[c4d.PRIM_PLANE_SUBW] = 3
  147.                 plan[c4d.PRIM_PLANE_SUBH] = 3
  148.                 plan[c4d.PRIM_AXIS] = 4
  149.                 plan[c4d.ID_BASEOBJECT_USECOLOR] = 2
  150.                 plan[c4d.ID_BASEOBJECT_COLOR] = c4d.Vector(1.0, 0.8, 0.0)
  151.                 aff = c4d.BaseTag(c4d.Tdisplay)
  152.                 aff[c4d.DISPLAYTAG_AFFECT_DISPLAYMODE] = True
  153.                 aff[c4d.DISPLAYTAG_SDISPLAYMODE] = c4d.DISPLAYTAG_SDISPLAY_NOSHADING
  154.                 aff[c4d.DISPLAYTAG_WDISPLAYMODE] = c4d.DISPLAYTAG_WDISPLAY_ISOPARMS
  155.                 hn.InsertTag(aff)
  156.                 plan.InsertUnder(hn)
  157.                 res = utils.SendModelingCommand(command = c4d.MCOMMAND_MAKEEDITABLE, list = [plan], doc = doc)
  158.                 res[0].InsertUnder(hn)
  159.                 doc.InsertObject(hn)
  160.                 donnees.SetLink(VONCPROJUV_OBJ, hn)
  161.         
  162.         def Init(self, node):
  163.                 donnees = node.GetDataInstance()
  164.                 donnees.SetBool(VONCPROJUV_MAJAUTO, True)
  165.                 return True
  166.         
  167.         def Execute(self, tag, doc, op, bt, priority, flags):
  168.                 donnees = tag.GetDataInstance()
  169.                 if not donnees : return c4d.EXECUTIONRESULT_OK
  170.                
  171.                 if self.creerplan : self.CreerProjPlanaire(doc, donnees)
  172.                
  173.                 self.RecupDonnees(donnees, doc)
  174.                 self.InitDonneeUVW(donnees, op)
  175.                 self.InitDonneeAff(donnees, op)
  176.                
  177.                 obj = op
  178.                 objPropUVW = self.lienUVW
  179.                 tex = self.lienObj
  180.                 selPolys = self.lienSel
  181.                
  182.                 verif = self.Verifieur(obj, objPropUVW, tex)
  183.                 if not verif : return c4d.EXECUTIONRESULT_OK
  184.                
  185.                 tex = self.Applatisseur(tex, doc)
  186.                 if not tex : return c4d.EXECUTIONRESULT_OK
  187.                
  188.                 texNor, texUVW = self.CalculNormaleEtUVW(tex)
  189.                 if not texUVW : return c4d.EXECUTIONRESULT_OK
  190.                
  191.                 texPts = tex.GetAllPoints()
  192.                 texMg = tex.GetMg()
  193.                
  194.                 objNbPolys = obj.GetPolygonCount()
  195.                 objNbPts = obj.GetPointCount()
  196.                 objPts = obj.GetAllPoints()
  197.                 objMg = obj.GetMg()
  198.                 objIMg = ~objMg
  199.                 objUVW = [None] * objNbPts
  200.                 objNor = [Vector()] * objNbPts
  201.                 objNorBol = [False] * objNbPts
  202.                
  203.                 bs = None
  204.                 objPtsSel = [False] * objNbPts
  205.                 if selPolys :
  206.                         bs = selPolys.GetBaseSelect()
  207.                         for index, sele in enumerate(bs.GetAll(objNbPolys)) :
  208.                                 if not sele : continue
  209.                                 poly = obj.GetPolygon(index)
  210.                                 objPtsSel[poly.a] = True
  211.                                 objPtsSel[poly.b] = True
  212.                                 objPtsSel[poly.c] = True
  213.                                 objPtsSel[poly.d] = True
  214.                
  215.                 distance = (obj.GetRad() + obj.GetMp() + objMg.off  + (tex.GetRad() + tex.GetMp() + texMg.off)).GetLength()
  216.                 distance *= 2
  217.                
  218.                 rayon = GeRayCollider()
  219.                 rayon.Init(obj)
  220.                 i = 0
  221.                 for pt in texPts :
  222.                         depart = objIMg.Mul(pt) + 0.0001
  223.                         direc = objIMg.MulV(texNor[i])
  224.                         #ajVec(depart, direc)
  225.                         coll = rayon.Intersect(depart, direc, distance)
  226.                         inter = rayon.GetNearestIntersection()
  227.                         
  228.                         if inter :
  229.                                 pos = inter["hitpos"]
  230.                                 poly = inter["face_id"]
  231.                                 poly = obj.GetPolygon(poly)
  232.                                 nor = direc.GetNormalized() * -1
  233.                                 #ajVec(pos, direc)
  234.                                 objNor[poly.a] += nor
  235.                                 objNor[poly.b] += nor
  236.                                 objNor[poly.c] += nor
  237.                                 objNor[poly.d] += nor
  238.                                 objNorBol[poly.a] = True
  239.                                 objNorBol[poly.b] = True
  240.                                 objNorBol[poly.c] = True
  241.                                 objNorBol[poly.d] = True
  242.                         i += 1
  243.                
  244.                 n = Neighbor()
  245.                 n.Init(obj)
  246.                 somme = sum(objNorBol)
  247.                 while somme != objNbPts :
  248.                         sommeAns = somme
  249.                         self.CompleteNormales(n, obj, objNbPts, objNor, objNorBol)
  250.                         somme = sum(objNorBol)
  251.                         if sommeAns == somme : break
  252.                
  253.                 rayon.Init(tex)
  254.                 i = 0
  255.                 for pt in objPts :
  256.                         if bs :
  257.                                 if not objPtsSel[i] :
  258.                                         i += 1
  259.                                         continue
  260.                         depart = objMg.Mul(pt) + 0.0001
  261.                         direc = objMg.MulV(objNor[i].GetNormalized())
  262.                         #ajVec(depart, direc)
  263.                         coll = rayon.Intersect(depart, direc, distance)
  264.                         inter = rayon.GetNearestIntersection()
  265.                         
  266.                         if inter :
  267.                                 bc = inter['barrycoords']
  268.                                 poly = inter["face_id"]
  269.                                 poly = tex.GetPolygon(poly)
  270.                                 a = poly.a
  271.                                 b = poly.b
  272.                                 c = poly.c
  273.                                 objUVW[i] = texUVW[a]*bc.x + texUVW[b]*bc.y + texUVW[c]*bc.z
  274.                                 
  275.                         i += 1
  276.                
  277.                 #doc.AddUndo(c4d.UNDOTYPE_CHANGE, objPropUVW)
  278.                
  279.                 objPolAff = self.lienAff.GetBaseSelect()
  280.                 objPolAff.DeselectAll()
  281.                
  282.                 if bs :
  283.                         for index, sele in enumerate(bs.GetAll(objNbPolys)):
  284.                                 if not sele : continue
  285.                                 poly = obj.GetPolygon(index)
  286.                                 a = objUVW[poly.a]
  287.                                 b = objUVW[poly.b]
  288.                                 c = objUVW[poly.c]
  289.                                 d = objUVW[poly.d]
  290.                                 if not a or not b or not c or not d : continue
  291.                                 objPolAff.Select(index)
  292.                                 objPropUVW.SetSlow(index, a, b, c, d)
  293.                
  294.                 else :
  295.                         for j in xrange(obj.GetPolygonCount()) :
  296.                                 poly = obj.GetPolygon(j)
  297.                                 a = objUVW[poly.a]
  298.                                 b = objUVW[poly.b]
  299.                                 c = objUVW[poly.c]
  300.                                 d = objUVW[poly.d]
  301.                                 if not a or not b or not c or not d : continue
  302.                                 objPolAff.Select(j)
  303.                                 objPropUVW.SetSlow(j, a, b, c, d)
  304.                
  305.                 #doc.EndUndo()
  306.                 #c4d.EventAdd()
  307.                
  308.                 obj.Message(c4d.MSG_UPDATE)
  309.                
  310.                 return c4d.EXECUTIONRESULT_OK
  311.         
  312.         def Message(self, op, type, donnees) :
  313.                 if type == c4d.MSG_DESCRIPTION_COMMAND :
  314.                         id = donnees["id"][0].id
  315.                         if id == VONCPROJUV_ACTU :
  316.                                 self.actuprio = True
  317.                         elif id == VONCPROJUV_PLAN :
  318.                                 self.creerplan = True
  319.                 return True

  320. if __name__ == "__main__":
  321.         bmp = bitmaps.BaseBitmap()
  322.         dir, file = os.path.split(__file__)
  323.         fn = os.path.join(dir, "res", "vonc_projuv.tif")
  324.         bmp.InitWith(fn)
  325.         plugins.RegisterTagPlugin(id=MODULE_ID, str="ProjUV",
  326.                                                           info=c4d.TAG_EXPRESSION | c4d.TAG_VISIBLE, g=ProjUV,
  327.                                                           description="vonc_projuv", icon=bmp)
复制代码

Fichier vonc_projuv.H :
  1. #ifndef _vonc_projuv_H_
  2. #define _vonc_projuv_H_

  3. enum
  4. {
  5.         VONCPROJUV_MAJAUTO        = 1000,
  6.         VONCPROJUV_OBJ                = 1001,
  7.         VONCPROJUV_UVW                = 1002,
  8.         VONCPROJUV_SEL                = 1003,
  9.         VONCPROJUV_ACTU                = 1004,
  10.         VONCPROJUV_PLAN                = 1005,
  11.         VONCPROJUV_INVNOR        = 1006,
  12.         VONCPROJUV_INFO                = 1007,
  13.         VONCPROJUV_AFF                = 1008
  14. }

  15. #endif
复制代码
Fichier vonc_projuv.RES :
  1. CONTAINER vonc_projuv
  2. {
  3.         NAME VONCPROJUV_TITRE;
  4.         INCLUDE Texpression;
  5.         
  6.         GROUP ID_TAGPROPERTIES
  7.         {
  8.                 GROUP {
  9.                         BUTTON VONCPROJUV_PLAN { }
  10.                 }
  11.                 SEPARATOR { LINE; }
  12.                 GROUP {
  13.                         LINK VONCPROJUV_OBJ { ACCEPT { Obase; } }
  14.                         LINK VONCPROJUV_UVW { ACCEPT { Tuvw; } }
  15.                         LINK VONCPROJUV_AFF { ACCEPT { Tpolygonselection; } }
  16.                         LINK VONCPROJUV_SEL { ACCEPT { Tpolygonselection; } }
  17.                         BOOL VONCPROJUV_INVNOR { }
  18.                 }
  19.                 SEPARATOR { LINE; }
  20.                 GROUP {
  21.                         COLUMNS 2;
  22.                         BOOL VONCPROJUV_MAJAUTO { }
  23.                         BUTTON VONCPROJUV_ACTU { }
  24.                 }
  25.                 SEPARATOR { LINE; }
  26.                 GROUP {
  27.                         STATICTEXT VONCPROJUV_INFO { }
  28.                 }
  29.         }
  30. }
复制代码

Fichier vonc_projuv.STR :
  1. STRINGTABLE vonc_projuv
  2. {
  3.         VONCPROJUV_TITRE                "ProjUV";
  4.         VONCPROJUV_MAJAUTO                "Mise \u00E0 jour auto";
  5.         VONCPROJUV_ACTU                        "Actualiser";
  6.         VONCPROJUV_OBJ                        "Projection";
  7.         VONCPROJUV_UVW                        "UVW";
  8.         VONCPROJUV_SEL                        "Limiter \u00E0 la s\u00E9lection";
  9.         VONCPROJUV_AFF                        "Polygones affect\u00E9s";
  10.         VONCPROJUV_PLAN                        "Cr\u00E9er une projection planaire modifiable";
  11.         VONCPROJUV_INVNOR                "Inverser la direction";
  12.         VONCPROJUV_INFO                        "v 1.2 - C\u00E9sar Vonc - http://code.vonc.fr";
  13. }
复制代码

点击下载图标
下载等级
C8D
登录后下载




Comment :1

插件脚本
软件性质:  
适用版本: C4D R15 - C4D R16 - C4D R17 - C4D R18 - C4D R19 - C4D R20 - C4D R21 - C4D S22 - C4D R23 - C4D S24 - C4D R25 - C4D S26 - C4D 2023 - C4D 2024 - C4D 2025
软件版本: ProjUV - v 1.2
系统平台: Win MAC 
软件语言: 汉化 
插件来源: https://www.c4d.cn/c4dsoft.html

相关推荐

百套精美Kitbash3D模型专题合集下载
时尚卡通办公室人物C4D立体图标工程下载Cinema 4D Fashion Cartoon Office Character 3D Icon Project Download
C4D科技新闻片头电视栏目频道包装动画工程下载Cinema 4D Technology News Headline TV Program Channel Packaging Animation Project Download
关闭

C4D精选推荐 上一条 /10 下一条

智能
客服
快速回复 返回顶部 返回列表