Skip to content

Instantly share code, notes, and snippets.

@gr4ph0s
Last active August 19, 2017 17:00
Show Gist options
  • Save gr4ph0s/ebecaabcf90bbdd73a54124544e0b532 to your computer and use it in GitHub Desktop.
Save gr4ph0s/ebecaabcf90bbdd73a54124544e0b532 to your computer and use it in GitHub Desktop.
[C4D] Set uv coordinate from pt_id or from pt_id+poly_id
import c4d
def get_points_id_selected(obj):
buffer_list = list()
pts_count = obj.GetPointCount()
sel_pts = obj.GetPointS()
list_pts = sel_pts.GetAll(pts_count)
for i in xrange(pts_count):
if list_pts[i]:
buffer_list.append(i)
return buffer_list
def get_linked_continuous(obj, pt_id):
nbr = c4d.utils.Neighbor()
nbr.Init(obj)
poly_info = nbr.GetPointPolys(pt_id)
polys_data = obj.GetAllPolygons()
list_id = list()
for poly_id in poly_info:
poly = polys_data[poly_id]
found_id = poly.Find(pt_id)
if found_id != c4d.NOTOK:
buffer_data = dict()
buffer_data["poly_id"] = poly_id
buffer_data["pt_num"] = found_id
list_id.append(buffer_data)
return list_id
def get_linked_discontinuous(obj, pt_id, poly_id):
list_id = list()
poly = obj.GetPolygon(poly_id)
found_id = poly.Find(pt_id)
if found_id != c4d.NOTOK:
buffer_data = dict()
buffer_data["poly_id"] = poly_id
buffer_data["pt_num"] = found_id
list_id.append(buffer_data)
return list_id
def set_uv(tag, pt_id, uv_coord, poly_id=None):
if not tag:
return
obj = tag.GetObject()
linked_data = list()
if not poly_id:
linked_data = get_linked_continuous(obj, pt_id)
else:
linked_data = get_linked_discontinuous(obj, pt_id, poly_id)
doc.AddUndo(c4d.UNDOTYPE_CHANGE, tag)
for data in linked_data:
buffer_uv = tag.GetSlow(data["poly_id"])
list_uv = [buffer_uv["a"],
buffer_uv["b"],
buffer_uv["c"],
buffer_uv["d"]]
list_uv[data["pt_num"]] = uv_coord
tag.SetSlow(data["poly_id"],
list_uv[0],
list_uv[1],
list_uv[2],
list_uv[3]
)
def main():
obj = op
tag = obj.GetTag(c4d.Tuvw)
pts_id_selected = get_points_id_selected(obj)
doc.StartUndo()
for pt_id in pts_id_selected:
poly_id = None
uv_coord = c4d.Vector()
set_uv(tag, pt_id, uv_coord, poly_id)
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment